forked from nikivanov/watney
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerplant.py
32 lines (29 loc) · 1.24 KB
/
powerplant.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
import smbus
DEVICE_BUS = 1
DEVICE_ADDR = 0x17
class PowerPlant:
def __init__(self, config):
self.bus = smbus.SMBus(DEVICE_BUS)
powerplantConfig = config["POWERPLANT"]
cutoffVoltage = int(powerplantConfig['CutoffVoltage'])
self.bus.write_byte_data(DEVICE_ADDR, 17, cutoffVoltage & 0xFF)
self.bus.write_byte_data(DEVICE_ADDR, 18, (cutoffVoltage >> 8)& 0xFF)
print('Set powerplant cutoff voltage to {} mv'.format(cutoffVoltage))
self.lastIsError = False
def getBatteryInfo(self):
try:
aReceiveBuf = []
aReceiveBuf.append(0x00) # Placeholder
for i in range(1,255):
aReceiveBuf.append(self.bus.read_byte_data(DEVICE_ADDR, i))
percentage = aReceiveBuf[20] << 8 | aReceiveBuf[19]
charging = (aReceiveBuf[10] << 8 | aReceiveBuf[9]) > 4000
if self.lastIsError:
print('Powerplant communication restored')
self.lastIsError = False
return [percentage, charging]
except Exception as e:
if not self.lastIsError:
print('Error obtaining powerplant info: ' + str(e))
self.lastIsError = True
return [0, False]