From d543144d2798016da9777cd34dd71097199d123c Mon Sep 17 00:00:00 2001 From: Jeroen Oudshoorn Date: Tue, 17 Dec 2024 18:56:37 +0100 Subject: [PATCH] Added optional rotation/fixed display setting Added optional fixed display parameter Signed-off-by: Jeroen Oudshoorn --- pwnagotchi/plugins/default/pisugarx.py | 69 ++++++++++++++++++-------- 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/pwnagotchi/plugins/default/pisugarx.py b/pwnagotchi/plugins/default/pisugarx.py index 65ea7bee..2f82b734 100644 --- a/pwnagotchi/plugins/default/pisugarx.py +++ b/pwnagotchi/plugins/default/pisugarx.py @@ -2,7 +2,6 @@ # wget https://cdn.pisugar.com/release/pisugar-power-manager.sh # bash pisugar-power-manager.sh -c release - # https://www.tindie.com/stores/pisugar/ import logging @@ -18,10 +17,13 @@ from flask import render_template_string class PiSugar(plugins.Plugin): __author__ = "jayofelony" - __version__ = "1.0" + __version__ = "1.2" __license__ = "GPL3" - __description__ = ("A plugin that will add a voltage indicator for the PiSugar batteries." - "This plugin will also have a web configuration in the future, just like the Power Manager.") + __description__ = ( + "A plugin that will add a voltage indicator for the PiSugar batteries. " + "Rotation of battery status can be enabled or disabled via configuration. " + "Additionally, when rotation is disabled, you can choose which metric to display." + ) def __init__(self): self._agent = None @@ -32,10 +34,25 @@ class PiSugar(plugins.Plugin): self.ready = False self.lasttemp = 69 self.drot = 0 # display rotation - self.nextDChg = 0 # last time display changed, rotate on updates after 5 seconds + self.nextDChg = 0 # last time display changed + self.rotation_enabled = True # default rotation enabled + self.default_display = "voltage" # default display option def on_loaded(self): logging.info("[PiSugarX] plugin loaded.") + # Read rotation option from configuration + cfg = pwnagotchi.config['main']['plugins']['pisugarx'] + self.rotation_enabled = cfg.get('rotation', True) + self.default_display = cfg.get('default_display', 'voltage').lower() + + # Validate default_display + valid_displays = ['voltage', 'percentage', 'temp'] + if self.default_display not in valid_displays: + logging.warning(f"[PiSugarX] Invalid default_display '{self.default_display}'. Using 'voltage'.") + self.default_display = 'voltage' + + logging.info(f"[PiSugarX] Rotation is {'enabled' if self.rotation_enabled else 'disabled'}.") + logging.info(f"[PiSugarX] Default display (when rotation disabled): {self.default_display}") def on_ready(self, agent): self.ready = True @@ -48,8 +65,7 @@ class PiSugar(plugins.Plugin): def on_internet_available(self, agent): self._agent = agent self.ps.rtc_web() - """ - WORK IN PROGRESS + def on_webhook(self, path, request): if not self.ready: ret = "PiSugarX not ready

PiSugarX not ready

" @@ -169,7 +185,7 @@ class PiSugar(plugins.Plugin): ret += "

%s

" % repr(e) logging.error("[PiSugarX] error: %s" % repr(e)) return render_template_string(ret), 404 - """ + def on_ui_setup(self, ui): ui.add_element( "bat", @@ -191,29 +207,42 @@ class PiSugar(plugins.Plugin): capacity = int(self.ps.get_battery_level()) voltage = self.ps.get_battery_voltage() temp = self.ps.get_temperature() + if temp != self.lasttemp: - logging.debug(f"[PiSugar3] ({capacity}%, {voltage}V, {temp}°C)") + logging.debug(f"[PiSugar3] ({capacity}%, {voltage:.2f}V, {temp}°C)") self.lasttemp = temp - # new model use battery_power_plugged & battery_allow_charging to detect real charging status + + # If it's a new model and charging is detected if self.is_new_model: if self.ps.get_battery_power_plugged() and self.ps.get_battery_allow_charging(): ui._state._state['bat'].label = "CHG" ui.update(force=True, new_data={"status": "Power!! I can feel it!"}) ui._state._state['bat'].label = "BAT" - if time.time() > self.nextDChg: - self.drot = (self.drot + 1) % 3 - self.nextDChg = time.time() + 5 + # If rotation is enabled, cycle through voltage, percentage, and temp + if self.rotation_enabled: + if time.time() > self.nextDChg: + self.drot = (self.drot + 1) % 3 + self.nextDChg = time.time() + 5 + + if self.drot == 0: # show battery voltage + ui.set('bat', f"{voltage:.2f}V") + elif self.drot == 1: # show battery capacity + ui.set('bat', f"{capacity}%") + else: # show battery temperature + ui.set('bat', f"{temp}°C") + else: + # Rotation disabled, show only the selected default display + if self.default_display == 'voltage': + ui.set('bat', f"{voltage:.2f}V") + elif self.default_display in ['percentage', 'percent']: + ui.set('bat', f"{capacity}%") + elif self.default_display == 'temp': + ui.set('bat', f"{temp}°C") - if self.drot == 0: # show battery voltage - ui.set('bat', f"{voltage:.2f}V") - elif self.drot == 1: # show battery capacity - ui.set('bat', f"{capacity}%") - else: # show battery temperature - ui.set('bat', f"{temp}°C") if self.ps.get_battery_charging() is not None: if capacity <= self.ps.get_battery_safe_shutdown_level(): logging.info( f"[PiSugarX] Empty battery (<= {self.ps.get_battery_safe_shutdown_level()}%): shutting down" ) - ui.update(force=True, new_data={"status": "Battery exhausted, bye ..."}) + ui.update(force=True, new_data={"status": "Battery exhausted, bye ..."}) \ No newline at end of file