-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpisugar3.py
102 lines (84 loc) · 3.41 KB
/
pisugar3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import logging
import struct
import time
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
class UPS:
def __init__(self):
import smbus
self._bus = smbus.SMBus(1)
self.sample_size = 25
self.battery_readings = []
def capacity(self):
battery_level = 0
try:
battery_level = self._bus.read_byte_data(0x57, 0x2a)
except:
pass
return battery_level
def status(self):
# Try to read the bus, if it fails, return None.
# This is to prevent the plugin unnneded error logs when the PiSugar3 is not connected; Like when it is connected to a computer.
try:
stat02 = self._bus.read_byte_data(0x57, 0x02)
stat03 = self._bus.read_byte_data(0x57, 0x03)
stat04 = self._bus.read_byte_data(0x57, 0x04)
except:
return None, None, None
return stat02, stat03, stat04
def smoothed_capacity(self):
if len(self.battery_readings) < self.sample_size:
self.battery_readings.append(self.capacity())
else:
self.battery_readings.pop(0)
self.battery_readings.append(self.capacity())
return int(sum(self.battery_readings) / len(self.battery_readings))
class PiSugar3(plugins.Plugin):
__author__ = 'nullm0ose'
__version__ = '1.0.0'
__license__ = 'MIT'
__description__ = 'A plugin that shows charging status and battery percentage for the PiSugar3'
def __init__(self):
self.ups = None
self.lasttemp = 69
self.drot = 0
self.nextDChg = 0
def on_loaded(self):
self.ups = UPS()
logging.info("[pisugar3] plugin loaded.")
def on_ui_setup(self, ui):
try:
ui.add_element('bat', LabeledValue(color=BLACK, label='BAT:', value='0%',
position=(ui.width() / 2 + 10, 0),
label_font=fonts.Bold, text_font=fonts.Medium))
except Exception as err:
logging.warning("pisugar3 setup err: %s" % repr(err))
def on_unload(self, ui):
try:
with ui._lock:
ui.remove_element('bat')
except Exception as err:
logging.warning("pisugar3 unload err: %s" % repr(err))
def on_ui_update(self, ui):
capacity = self.ups.smoothed_capacity()
status = self.ups.status()
# If the battery is turned off, the status will display "NF", as in "Not Found".
if status[0] == None:
ui._state._state['bat'].label = "BAT:"
ui._state._state['bat'].value = "NF"
# Write the status to the log, so we can see if the battery is turned off.
# Using only a debug log, so it doesn't spam the log file.
logging.debug('[pisugar3] No battery found')
return
if status[0] & 0x80:
ui._state._state['bat'].label = "CHG:"
else:
ui._state._state['bat'].label = "BAT:"
if capacity <= self.options['shutdown']:
logging.info('[pisugar3] Empty battery (<= %s%%): shutting down' % self.options['shutdown'])
ui.update(force=True, new_data={'status': 'Battery exhausted, bye ...'})
pwnagotchi.shutdown()
ui.set('bat', "%2i%%" % capacity)