Skip to content

Instantly share code, notes, and snippets.

@alex-pat
Last active October 27, 2016 14:30
Show Gist options
  • Save alex-pat/dad6f4cab087667db8a13130031c7ac1 to your computer and use it in GitHub Desktop.
Save alex-pat/dad6f4cab087667db8a13130031c7ac1 to your computer and use it in GitHub Desktop.
IAPD battery lab
#!/usr/bin/env python
""" Usage
Run once
$ sed "s/postskript/$USER/g" battery.py
$ sed "s/postskript/$USER/g" "$HOME/.config/awesome/battery_magic.py"
Apped the lua code to rc.lua and then
$ sed "s/postskript/$USER/g" "$HOME/.config/awesome/rc.lua"
$ echo 600 > $HOME/.config/awesome/display_brightness_delay
$ echo 10 > $HOME/.config/awesome/.true
So, reload awesome (modkey + Control + R) and run battery_magic.py.
Near the teacher run:
$ ./battery.py 10 # for 10 sec. delay
"""
import time, os, sys
SUPPLY = "/sys/class/power_supply/"
BRIGHTNESS_DELAY_PATH = "/home/postskript/.config/awesome/display_brightness_delay"
old_brightness = open(BRIGHTNESS_DELAY_PATH).read()[:-1]
def set_brightness_delay(brightness):
os.system("echo {} > {}".format(brightness, BRIGHTNESS_DELAY_PATH) )
if len(sys.argv) < 2:
print("Delay expected")
sys.exit(1)
try:
set_brightness_delay(sys.argv[1])
while True:
time.sleep(1)
os.system("clear")
ac_on = open(SUPPLY + "AC0/online").read()
if ac_on.startswith("0"):
print("Battery", end=', ')
else:
print("AC", end=', ')
print("{}%, ".format(open(SUPPLY + "BAT0/capacity").read()[:-1]), end='')
present_rate = int(open(SUPPLY + "BAT0/power_now").read()) / 1000.0
remaining_energy = int(open(SUPPLY + "BAT0/energy_now").read()) / 1000.0
last_capacity = int(open(SUPPLY + "BAT0/energy_full").read()) / 1000.0
if present_rate < 0.1:
print("will never fully charge")
continue
if ac_on.startswith("0"):
seconds = remaining_energy / present_rate * 3600
else:
seconds = (last_capacity - remaining_energy) / present_rate * 3600
hours = seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
print("%02d:%02d:%02d" % (hours, minutes, seconds))
except KeyboardInterrupt:
set_brightness_delay(old_brightness)
#!/usr/bin/env python
import os
import sys
from evdev import InputDevice
from select import select
from subprocess import check_output
CONF_PATH = "/home/postskript/.config/awesome/display_brightness_delay"
TRUE_PATH = "/home/postskript/.config/awesome/.true"
def update_timer():
if open("/sys/class/power_supply/AC0/online").read().startswith("0"):
seconds = open(CONF_PATH).read()[:-1]
os.system("echo {} > {}".format(seconds, TRUE_PATH))
if __name__ == "__main__":
if os.getuid():
print("You must be root")
sys.exit(1)
update_timer()
devices = map(InputDevice, ("/dev/input/event0", "/dev/input/event16")) # or what you have
devices = {dev.fd: dev for dev in devices}
while True:
r, w, x = select(devices, [], [])
for fd in r:
for event in devices[fd].read():
update_timer()
--- Append this code to your rc.lua
--- {{{ BATTERY HACK
was_ac = false
power_timer = timer({timeout = 1})
power_timer:connect_signal("timeout", function()
local pow_sec = tonumber(awful.util.pread("cat /home/postskript/.config/awesome/.true"))
local pow_full_sec = tonumber(awful.util.pread("cat /home/postskript/.config/awesome/display_brightness_delay"))
if pow_sec == pow_full_sec and was_ac == false then
awful.util.spawn_with_shell('ruby -e "l=`light`.to_i*2;`light -S #{l}`"' , false)
end
if string.sub(awful.util.pread("cat /sys/class/power_supply/AC0/online"),1,1) == "1" then
awful.util.spawn_with_shell("echo " .. pow_full_sec .. " > /home/postskript/.config/awesome/.true")
was_ac = true
return
end
was_ac = false
if pow_sec == 0 then
awful.util.spawn_with_shell('ruby -e "l=`light`.to_i/2;`light -S #{l}`"' , false)
end
pow_sec = pow_sec - 1
if pow_sec < 0 then
pow_sec = -1
end
awful.util.spawn_with_shell("echo " .. pow_sec .. " > /home/postskript/.config/awesome/.true")
end)
power_timer:start()
--- }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment