From 92534f861a3b411a5c022c5fac66ff736a590c86 Mon Sep 17 00:00:00 2001 From: MKesenheimer Date: Thu, 6 Apr 2023 23:52:43 +0200 Subject: [PATCH 1/2] Display is now correctly updated with fixed fps. --- pwnagotchi/ui/view.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pwnagotchi/ui/view.py b/pwnagotchi/ui/view.py index 9ecc857f9..ba6ab036f 100644 --- a/pwnagotchi/ui/view.py +++ b/pwnagotchi/ui/view.py @@ -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 @@ -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) @@ -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() From 1cae35bf24c5549d27caab060d110bae39e2be42 Mon Sep 17 00:00:00 2001 From: MKesenheimer Date: Mon, 8 May 2023 22:22:38 +0200 Subject: [PATCH 2/2] UPS-Lite gave back wrong values. Fixed it by sending QuickStart- and PowerOnReset-commands to the UPS-Lite controller. --- pwnagotchi/plugins/default/ups_lite.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pwnagotchi/plugins/default/ups_lite.py b/pwnagotchi/plugins/default/ups_lite.py index c439636dd..b6a6b2bc5 100644 --- a/pwnagotchi/plugins/default/ups_lite.py +++ b/pwnagotchi/plugins/default/ups_lite.py @@ -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: @@ -43,6 +56,7 @@ def capacity(self): address = 0x36 read = self._bus.read_word_data(address, 4) swapped = struct.unpack("H", read))[0] + # may be 512 for some models, too: return swapped / 256 except: return 0.0 @@ -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'])