Skip to content

Instantly share code, notes, and snippets.

@brantsch
Created October 24, 2013 14:40
Show Gist options
  • Select an option

  • Save brantsch/7138499 to your computer and use it in GitHub Desktop.

Select an option

Save brantsch/7138499 to your computer and use it in GitHub Desktop.
My battery widget for AwesomeWM on my Laptop. If you want this to work on your machine, you might need to make substantial changes. This widget targets Awesome 3.4.
-- Create a battery widget
mybatterywidget = widget({type="textbox"})
-- do not forget to add the widget to your panel ;-)
-- display the current charge (in %) and the charging/discharging rate (in W)
-- for the battery identified by <adapter>
function show_battery(adapter)
local fcur = io.open("/sys/class/power_supply/"..adapter.."/charge_now")
local fcap = io.open("/sys/class/power_supply/"..adapter.."/charge_full")
local fvolt = io.open("/sys/class/power_supply/"..adapter.."/voltage_now")
local famp = io.open("/sys/class/power_supply/"..adapter.."/current_now")
if fcur and fcap and fvolt and famp then
local cur = fcur:read()
local cap = fcap:read()
local battery = math.floor(cur * 100 / cap)
local volt = fvolt:read()
local amp = famp:read()
local watts = string.format("%.2f",(volt/1000000)*(amp/1000000))
text = battery.."% "..watts.."W"
fcur:close()
fcap:close()
fvolt:close()
famp:close()
else
text = "n.a."
end
mybatterywidget.text = text
end
-- create a timer to update the widget periodically
battery_timer = timer({timeout = 20})
battery_timer:add_signal("timeout", function() show_battery("BAT0") end)
battery_timer:start()
show_battery("BAT0") --run the function once on startup of the window manager so we don't have to wait for the timer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment