From 9cb2ec827eaf1179900fdd3764322fcfebb537c3 Mon Sep 17 00:00:00 2001 From: petrucci4prez Date: Mon, 17 Jul 2017 03:03:01 -0400 Subject: [PATCH] clean up Power.lua --- module/Power.lua | 141 +++++++++++++++++++++++++---------------------- 1 file changed, 76 insertions(+), 65 deletions(-) diff --git a/module/Power.lua b/module/Power.lua index c105d27..eb262ad 100644 --- a/module/Power.lua +++ b/module/Power.lua @@ -6,45 +6,43 @@ local ScalePlot = require 'ScalePlot' local util = require 'util' local schema = require 'default_patterns' -local _STRING_MATCH = string.match -local _MATH_RAD = math.rad +local _MODULE_Y_ = 328 +local _SEPARATOR_SPACING_ = 20 +local _TEXT_SPACING_ = 20 +local _PLOT_SEC_BREAK_ = 20 +local _PLOT_HEIGHT_ = 56 ---construction params -local MODULE_Y = 328 -local SEPARATOR_SPACING = 20 -local TEXT_SPACING = 20 -local PLOT_SEC_BREAK = 20 -local PLOT_HEIGHT = 56 +local power_label_function = function(watts) return watts..' W' end -local __power_label_function = function(watts) - return watts..' W' -end - -local __calculate_power = function(cr, prev_cnt, cnt, update_frequency) - return cnt > prev_cnt and (cnt - prev_cnt) * update_frequency * 0.000001 or 0 +local calculate_power = function(cr, prev_cnt, cnt, update_frequency) + if cnt > prev_cnt then + return (cnt - prev_cnt) * update_frequency * 0.000001 + else + return 0 + end end local header = Widget.Header{ x = _G_INIT_DATA_.RIGHT_X, - y = MODULE_Y, + y = _MODULE_Y_, width = _G_INIT_DATA_.SECTION_WIDTH, header = 'POWER' } -local RIGHT_X = _G_INIT_DATA_.RIGHT_X + _G_INIT_DATA_.SECTION_WIDTH +local _RIGHT_X_ = _G_INIT_DATA_.RIGHT_X + _G_INIT_DATA_.SECTION_WIDTH local pp01 = { labels = Widget.TextColumn{ x = _G_INIT_DATA_.RIGHT_X, y = header.bottom_y, - spacing = TEXT_SPACING, + spacing = _TEXT_SPACING_, 'Core', 'iGPU' }, values = Widget.TextColumn{ - x = RIGHT_X, + x = _RIGHT_X_, y = header.bottom_y, - spacing = TEXT_SPACING, + spacing = _TEXT_SPACING_, x_align = 'right', text_color = schema.blue, append_end = ' W', @@ -52,24 +50,24 @@ local pp01 = { } } -local SEP_Y = header.bottom_y + TEXT_SPACING + SEPARATOR_SPACING +local _SEP_Y_ = header.bottom_y + _TEXT_SPACING_ + _SEPARATOR_SPACING_ local separator = Widget.Line{ - p1 = {x = _G_INIT_DATA_.RIGHT_X, y = SEP_Y}, - p2 = {x = RIGHT_X, y = SEP_Y} + p1 = {x = _G_INIT_DATA_.RIGHT_X, y = _SEP_Y_}, + p2 = {x = _RIGHT_X_, y = _SEP_Y_} } -local PKG0_Y = SEP_Y + SEPARATOR_SPACING +local _PKG0_Y_ = _SEP_Y_ + _SEPARATOR_SPACING_ local pkg0 = { label = Widget.Text{ x = _G_INIT_DATA_.RIGHT_X, - y = PKG0_Y, + y = _PKG0_Y_, text = 'PKG 0' }, value = Widget.Text{ - x = RIGHT_X, - y = PKG0_Y, + x = _RIGHT_X_, + y = _PKG0_Y_, x_align = 'right', text_color = schema.blue, text = '', @@ -77,24 +75,24 @@ local pkg0 = { }, plot = Widget.ScalePlot{ x = _G_INIT_DATA_.RIGHT_X, - y = PKG0_Y + PLOT_SEC_BREAK, + y = _PKG0_Y_ + _PLOT_SEC_BREAK_, width = _G_INIT_DATA_.SECTION_WIDTH, - height = PLOT_HEIGHT, - y_label_func = __power_label_function, + height = _PLOT_HEIGHT_, + y_label_func = power_label_function, } } -local DRAM_Y = PKG0_Y + PLOT_SEC_BREAK * 2 + PLOT_HEIGHT +local _DRAM_Y_ = _PKG0_Y_ + _PLOT_SEC_BREAK_ * 2 + _PLOT_HEIGHT_ local dram = { label = Widget.Text{ x = _G_INIT_DATA_.RIGHT_X, - y = DRAM_Y, + y = _DRAM_Y_, text = 'DRAM' }, value = Widget.Text{ - x = RIGHT_X, - y = DRAM_Y, + x = _RIGHT_X_, + y = _DRAM_Y_, x_align = 'right', text_color = schema.blue, text = '', @@ -102,33 +100,33 @@ local dram = { }, plot = Widget.ScalePlot{ x = _G_INIT_DATA_.RIGHT_X, - y = DRAM_Y + PLOT_SEC_BREAK, + y = _DRAM_Y_ + _PLOT_SEC_BREAK_, width = _G_INIT_DATA_.SECTION_WIDTH, - height = PLOT_HEIGHT, - y_label_func = __power_label_function, + height = _PLOT_HEIGHT_, + y_label_func = power_label_function, } } -local BATTERY_DRAW_Y = DRAM_Y + PLOT_SEC_BREAK * 2 + PLOT_HEIGHT +local _BATTERY_DRAW_Y_ = _DRAM_Y_ + _PLOT_SEC_BREAK_ * 2 + _PLOT_HEIGHT_ local battery_draw = { label = Widget.Text{ x = _G_INIT_DATA_.RIGHT_X, - y = BATTERY_DRAW_Y, - spacing = TEXT_SPACING, + y = _BATTERY_DRAW_Y_, + spacing = _TEXT_SPACING_, text = 'Battery Draw' }, value = Widget.CriticalText{ - x = RIGHT_X, - y = BATTERY_DRAW_Y, + x = _RIGHT_X_, + y = _BATTERY_DRAW_Y_, x_align = 'right', }, plot = Widget.ScalePlot{ x = _G_INIT_DATA_.RIGHT_X, - y = BATTERY_DRAW_Y + PLOT_SEC_BREAK, + y = _BATTERY_DRAW_Y_ + _PLOT_SEC_BREAK_, width = _G_INIT_DATA_.SECTION_WIDTH, - height = PLOT_HEIGHT, - y_label_func = __power_label_function, + height = _PLOT_HEIGHT_, + y_label_func = power_label_function, } } @@ -137,25 +135,25 @@ local CORE_PATH = '/sys/class/powercap/intel-rapl:0:0/energy_uj' local IGPU_PATH = '/sys/class/powercap/intel-rapl:0:1/energy_uj' local DRAM_PATH = '/sys/class/powercap/intel-rapl:0:2/energy_uj' -local PKG0_PREV_CNT = util.read_file(PKG0_PATH, nil, '*n') -local CORE_PREV_CNT = util.read_file(CORE_PATH, nil, '*n') -local IGPU_PREV_CNT = util.read_file(IGPU_PATH, nil, '*n') -local DRAM_PREV_CNT = util.read_file(DRAM_PATH, nil, '*n') +local prev_pkg0_uj_cnt = util.read_file(PKG0_PATH, nil, '*n') +local prev_core_uj_cnt = util.read_file(CORE_PATH, nil, '*n') +local prev_igpu_uj_cnt = util.read_file(IGPU_PATH, nil, '*n') +local prev_dram_uj_cnt = util.read_file(DRAM_PATH, nil, '*n') -local __update = function(cr, update_frequency, ac_active) - local pkg0_cnt = util.read_file(PKG0_PATH, nil, '*n') - local core_cnt = util.read_file(CORE_PATH, nil, '*n') - local igpu_cnt = util.read_file(IGPU_PATH, nil, '*n') - local dram_cnt = util.read_file(DRAM_PATH, nil, '*n') +local update = function(cr, update_frequency, is_using_ac) + local pkg0_uj_cnt = util.read_file(PKG0_PATH, nil, '*n') + local core_uj_cnt = util.read_file(CORE_PATH, nil, '*n') + local igpu_uj_cnt = util.read_file(IGPU_PATH, nil, '*n') + local dram_uj_cnt = util.read_file(DRAM_PATH, nil, '*n') TextColumn.set(pp01.values, cr, 1, util.precision_round_to_string( - __calculate_power(cr, CORE_PREV_CNT, core_cnt, update_frequency), 3)) + calculate_power(cr, prev_core_uj_cnt, core_uj_cnt, update_frequency), 3)) TextColumn.set(pp01.values, cr, 2, util.precision_round_to_string( - __calculate_power(cr, IGPU_PREV_CNT, igpu_cnt, update_frequency), 3)) + calculate_power(cr, prev_igpu_uj_cnt, igpu_uj_cnt, update_frequency), 3)) - local pkg0_power = __calculate_power(cr, PKG0_PREV_CNT, pkg0_cnt, update_frequency) - local dram_power = __calculate_power(cr, DRAM_PREV_CNT, dram_cnt, update_frequency) + local pkg0_power = calculate_power(cr, prev_pkg0_uj_cnt, pkg0_uj_cnt, update_frequency) + local dram_power = calculate_power(cr, prev_dram_uj_cnt, dram_uj_cnt, update_frequency) Text.set(pkg0.value, cr, util.precision_round_to_string(pkg0_power, 3)) ScalePlot.update(pkg0.plot, cr, pkg0_power) @@ -163,13 +161,13 @@ local __update = function(cr, update_frequency, ac_active) Text.set(dram.value, cr, util.precision_round_to_string(dram_power, 3)) ScalePlot.update(dram.plot, cr, dram_power) - PKG0_PREV_CNT = pkg0_cnt - CORE_PREV_CNT = core_cnt - IGPU_PREV_CNT = igpu_cnt - DRAM_PREV_CNT = dram_cnt + prev_pkg0_uj_cnt = pkg0_uj_cnt + prev_core_uj_cnt = core_uj_cnt + prev_igpu_uj_cnt = igpu_uj_cnt + prev_dram_uj_cnt = dram_uj_cnt - if ac_active then - Text.set(battery_draw.value, cr, 'A / C') + if is_using_ac then + Text.set(battery_draw.value, cr, 'A/C') ScalePlot.update(battery_draw.plot, cr, 0) else local current = util.read_file('/sys/class/power_supply/BAT0/current_now', nil, '*n') @@ -181,8 +179,21 @@ local __update = function(cr, update_frequency, ac_active) end end -local draw = function(cr, current_interface, update_frequency, ac_active) - __update(cr, update_frequency, ac_active) +Widget = nil +schema = nil +_MODULE_Y_ = nil +_SEPARATOR_SPACING_ = nil +_TEXT_SPACING_ = nil +_PLOT_SEC_BREAK_ = nil +_PLOT_HEIGHT_ = nil +_RIGHT_X_ = nil +_SEP_Y_ = nil +_PKG0_Y_ = nil +_DRAM_Y_ = nil +_BATTERY_DRAW_Y_ = nil + +local draw = function(cr, current_interface, update_frequency, is_using_ac) + update(cr, update_frequency, is_using_ac) if current_interface == 0 then Text.draw(header.text, cr)