Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pwnagotchi/plugins/default/ups_lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,21 @@ class UPS:
def __init__(self):
# only import when the module is loaded and enabled
import smbus
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4, GPIO.IN)
# 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
self._bus = smbus.SMBus(1)
self.QuickStart()
self.PowerOnReset()

def QuickStart(self):
address = 0x36
self._bus.write_word_data(address, 0x06, 0x4000)

def PowerOnReset(self):
address = 0x36
self._bus.write_word_data(address, 0xfe, 0x0054)

def voltage(self):
try:
Expand All @@ -43,6 +56,7 @@ def capacity(self):
address = 0x36
read = self._bus.read_word_data(address, 4)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
# may be 512 for some models, too:
return swapped / 256
except:
return 0.0
Expand Down Expand Up @@ -79,6 +93,9 @@ def on_unload(self, ui):
def on_ui_update(self, ui):
capacity = self.ups.capacity()
charging = self.ups.charging()
# I don't know, why we have to do this regularly. But it only seems to give correct results with these two lines:
self.ups.QuickStart()
self.ups.PowerOnReset()
ui.set('ups', "%2i%s" % (capacity, charging))
if capacity <= self.options['shutdown']:
logging.info('[ups_lite] Empty battery (<= %s%%): shuting down' % self.options['shutdown'])
Expand Down
15 changes: 13 additions & 2 deletions pwnagotchi/ui/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, config, impl, state=None):
self._config = config
self._canvas = None
self._frozen = False
self._lasttime = time.time()
self._lock = Lock()
self._voice = Voice(lang=config['main']['lang'])
self._implementation = impl
Expand Down Expand Up @@ -360,6 +361,15 @@ def on_custom(self, text):
self.set('status', self._voice.custom(text))
self.update()

def check_display_update(self):
now = time.time()
timedif = now - self._lasttime
delay = 1.0 / self._config['ui']['fps']
if timedif < delay:
return False
self._lasttime = now
return True

def update(self, force=False, new_data={}):
for key, val in new_data.items():
self.set(key, val)
Expand All @@ -381,7 +391,8 @@ def update(self, force=False, new_data={}):

web.update_frame(self._canvas)

for cb in self._render_cbs:
cb(self._canvas)
if self.check_display_update():
for cb in self._render_cbs:
cb(self._canvas)

self._state.reset()