conky-config/drawing/power.lua

119 lines
3.0 KiB
Lua
Raw Normal View History

2021-08-08 15:58:53 -04:00
local format = require 'format'
2022-07-13 01:38:18 -04:00
local pure = require 'pure'
2021-08-08 19:12:31 -04:00
local sys = require 'sys'
2017-07-19 00:36:15 -04:00
return function(update_freq, config, common, width, point)
2021-07-18 00:21:20 -04:00
local TEXT_SPACING = 20
local PLOT_SEC_BREAK = 20
local PLOT_HEIGHT = 56
2021-07-18 19:18:14 -04:00
local power_label_function = function(plot_max)
local fmt = common.y_label_format_string(plot_max, 'W')
2021-07-18 19:18:14 -04:00
return function(watts) return string.format(fmt, watts) end
end
2021-07-18 00:21:20 -04:00
2021-07-19 23:58:58 -04:00
local format_rapl = function(watts)
2021-08-08 15:58:53 -04:00
return format.precision_round_to_string(watts, 3)..' W'
end
2022-07-13 01:38:18 -04:00
local mk_static = function(obj)
return pure.partial(common.tagged_scaled_timeseries_draw_static, obj)
end
local mk_dynamic = function(obj)
return pure.partial(common.tagged_scaled_timeseries_draw_dynamic, obj)
end
local mk_rate_plot = function(label, address, y)
local read_joules = sys.intel_powercap_reader(address)
2022-07-13 01:38:18 -04:00
local obj = common.make_rate_timeseries(
point.x,
2021-07-18 00:21:20 -04:00
y,
width,
2021-07-18 00:21:20 -04:00
PLOT_HEIGHT,
2021-07-19 23:58:58 -04:00
format_rapl,
2021-07-18 00:21:20 -04:00
power_label_function,
PLOT_SEC_BREAK,
label,
0,
2021-07-19 23:58:58 -04:00
update_freq,
read_joules()
2022-07-13 01:38:18 -04:00
)
2022-07-17 18:54:23 -04:00
return common.mk_acc(
width,
2022-07-13 01:38:18 -04:00
PLOT_HEIGHT + PLOT_SEC_BREAK,
function(_) common.update_rate_timeseries(obj, read_joules()) end,
2022-07-13 01:38:18 -04:00
mk_static(obj),
mk_dynamic(obj)
2021-07-18 00:21:20 -04:00
)
end
local mk_rate_blockspec = function(spec)
2022-07-17 01:25:53 -04:00
local f = pure.partial(mk_rate_plot, spec.name, spec.address)
return {f, true, TEXT_SPACING}
end
2021-07-18 00:21:20 -04:00
-----------------------------------------------------------------------------
-- battery power plot
local _read_battery_power = sys.battery_power_reader(config.battery)
local read_battery_power = function(is_using_ac)
if is_using_ac then
return 0
else
return _read_battery_power()
end
end
2021-07-19 23:58:58 -04:00
local format_ac = function(watts)
2021-07-18 00:21:20 -04:00
if watts == 0 then
return "A/C"
else
2021-07-19 23:58:58 -04:00
return format_rapl(watts)
2021-07-18 00:21:20 -04:00
end
end
2022-07-13 01:38:18 -04:00
local mk_bat = function(y)
local read_bat_status = sys.battery_status_reader(config.battery)
2022-07-13 01:38:18 -04:00
local obj = common.make_tagged_scaled_timeseries(
point.x,
2022-07-13 01:38:18 -04:00
y,
width,
2022-07-13 01:38:18 -04:00
PLOT_HEIGHT,
format_ac,
power_label_function,
PLOT_SEC_BREAK,
'Battery Draw',
0,
update_freq
)
2022-07-17 18:54:23 -04:00
return common.mk_acc(
width,
2022-07-13 01:38:18 -04:00
PLOT_HEIGHT + PLOT_SEC_BREAK,
function()
common.tagged_scaled_timeseries_set(
obj,
read_battery_power(read_bat_status())
)
2022-07-13 01:38:18 -04:00
end,
mk_static(obj),
mk_dynamic(obj)
)
end
2017-07-19 00:36:15 -04:00
2021-07-18 00:21:20 -04:00
-----------------------------------------------------------------------------
2021-07-19 23:58:58 -04:00
-- main functions
2021-07-18 00:21:20 -04:00
return {
header = 'POWER',
point = point,
width = width,
2022-07-17 22:30:38 -04:00
set_state = nil,
top = pure.concat(
pure.map(mk_rate_blockspec, config.rapl_specs),
{{mk_bat, config.battery ~= '', 0}}
)
}
end