diff --git a/pwnagotchi/defaults.toml b/pwnagotchi/defaults.toml index 8e8b45ed..abf99e44 100644 --- a/pwnagotchi/defaults.toml +++ b/pwnagotchi/defaults.toml @@ -76,6 +76,9 @@ main.plugins.onlinehashcrack.email = "" main.plugins.onlinehashcrack.dashboard = "" main.plugins.onlinehashcrack.single_files = false +main.plugins.pisugar2.enabled = false +main.plugins.pisugar2.sync_rtc_on_boot = false + main.plugins.pisugar3.enabled = false main.plugins.pisugar3.shutdown = 5 diff --git a/pwnagotchi/plugins/default/pisugar2.py b/pwnagotchi/plugins/default/pisugar2.py new file mode 100644 index 00000000..f4d5cb76 --- /dev/null +++ b/pwnagotchi/plugins/default/pisugar2.py @@ -0,0 +1,95 @@ +# Gets status of Pisugar2 - requires installing the PiSugar-Power-Manager +# curl http://cdn.pisugar.com/release/Pisugar-power-manager.sh | sudo bash +# +# based on https://github.com/evilsocket/pwnagotchi/blob/master/pwnagotchi/plugins/default/ups_lite.py +# https://www.tindie.com/products/pisugar/pisugar2-battery-for-raspberry-pi-zero/ +import logging + +from pwnagotchi.ui.components import LabeledValue +from pwnagotchi.ui.view import BLACK +import pwnagotchi.ui.fonts as fonts +import pwnagotchi.plugins as plugins +import pwnagotchi +import time +from pisugar import * + +class PiSugar(plugins.Plugin): + __author__ = "10230718+tisboyo@users.noreply.github.com" + __version__ = "0.0.1" + __license__ = "GPL3" + __description__ = "A plugin that will add a voltage indicator for the PiSugar 2" + + def __init__(self): + self.is_charging = False + self.is_new_model = False + self.options = dict() + conn, event_conn = connect_tcp() + self.ps = PiSugarServer(conn, event_conn) + + def on_loaded(self): + # Load here so it doesn't attempt to load if the plugin is not enabled + + logging.info("[pisugar2] plugin loaded.") + + if self.ps.get_battery_led_amount.value == 2: + self.is_new_model = True + else: + self.is_new_model = False + + if self.options["sync_rtc_on_boot"]: + self.ps.rtc_rtc2pi() + + def on_ui_setup(self, ui): + ui.add_element( + "bat", + LabeledValue( + color=BLACK, + label="BAT", + value="0%", + position=(ui.width() / 2 + 15, 0), + label_font=fonts.Bold, + text_font=fonts.Medium, + ), + ) + # display charging status + if self.is_new_model: + ui.add_element( + "chg", + LabeledValue( + color=BLACK, + label="", + value="", + position=(ui.width() / 2 - 12, 0), + label_font=fonts.Bold, + text_font=fonts.Bold, + ), + ) + + def on_unload(self, ui): + with ui._lock: + ui.remove_element("bat") + ui.remove_element("chg") + + def on_ui_update(self, ui): + capacity = int(self.ps.get_battery_level()) + logging.info("BATT GET VALUE IS : " + str(capacity)) + # new model use battery_power_plugged & battery_allow_charging to detect real charging status + if self.is_new_model: + if self.ps.get_battery_power_plugged().value and self.ps.get_battery_allow_charging().value: + ui.set("chg", "CHG") + if not self.is_charging: + ui.update(force=True, new_data={"status": "Power!! I can feel it!"}) + self.is_charging = True + else: + ui.set("chg", "") + self.is_charging = False + + ui.set("bat", str(capacity) + "%") + + if capacity <= self.options["shutdown"]: + logging.info( + f"[pisugar2] Empty battery (<= {self.options['shutdown']}): shutting down" + ) + ui.update(force=True, new_data={"status": "Battery exhausted, bye ..."}) + time.sleep(3) + pwnagotchi.shutdown() \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 11865033..eafe25f1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ dependencies = [ "PyYAML", "dbus-python", "file-read-backwards", "flask", "flask-cors", "flask-wtf", "gast", "gpiozero", "inky", "numpy", "pycryptodome", "pydrive2", "python-dateutil", "requests", "rpi-lgpio", "rpi_hardware_pwm", "scapy", "setuptools", "shimmy", "smbus", "smbus2", - "spidev", "toml", "tweepy", "websockets", + "spidev", "toml", "tweepy", "websockets", "pisugar", ] requires-python = ">=3.11" @@ -42,4 +42,4 @@ Issues = "https://github.com/jayofelony/pwnagotchi/issues" Download = "https://github.com/jayofelony/pwnagotchi/releases/latest" [project.scripts] -pwnagotchi = "pwnagotchi.cli:pwnagotchi_cli" +pwnagotchi = "pwnagotchi.cli:pwnagotchi_cli" \ No newline at end of file