forked from JakubFojtik/linux-fake-battery-module
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbattery_update.py
executable file
·72 lines (57 loc) · 2.5 KB
/
battery_update.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
#!/usr/bin/env python3
import os
import time
import board
import adafruit_max1704x
import subprocess as sp
import sys
MODULE = '/home/dan/git/rpi-integrated-battery-module/integrated_battery.ko'
STATUS_FILE = '/home/dan/git/rpi-integrated-battery-module/status.log'
if __name__ == '__main__':
sp.call(['/sbin/insmod', MODULE])
i2c = board.I2C()
max17 = adafruit_max1704x.MAX17048(i2c)
print(dir(max17))
max17.wake()
max17.voltage_alert_min = 3.5
max17.voltage_alert_max = 4.1
print("Voltage alert minimum = %0.2f V" % max17.voltage_alert_min)
print("Voltage alert maximum = %0.2f V" % max17.voltage_alert_max)
while True:
time.sleep(15)
with open(STATUS_FILE, 'w') as sys.stdout:
print(f'Cell Voltage: {max17.cell_voltage:.2f} Volts')
print(f'Cell Percent: {max17.cell_percent:.1f} %')
print('Charge Rate:', max17.charge_rate)
if os.path.exists('/dev/integrated_battery'):
print('updating battery')
charging_status = 1 if max17.charge_rate > 0 else 0
try:
with open('/dev/integrated_battery', 'w') as f:
f.write(f'capacity0 = {int(max17.cell_percent)}\n')
with open('/dev/integrated_battery', 'w') as f:
f.write(f'charging = {charging_status}\n')
except Exception as e:
print("exceptin", e)
# if max17.hibernating:
# print("Hibernating!")
# if max17.active_alert:
# print("Alert!")
# if max17.reset_alert:
# print(" Reset indicator")
# max17.reset_alert = False # clear the alert
# if max17.voltage_high_alert:
# print(" Voltage high")
# max17.voltage_high_alert = False # clear the alert
# if max17.voltage_low_alert:
# print(" Voltage low")
# max17.voltage_low_alert = False # clear the alert
# if max17.voltage_reset_alert:
# print(" Voltage reset")
# max17.voltage_reset_alert = False # clear the alert
# if max17.SOC_low_alert:
# print(" Charge low")
# max17.SOC_low_alert = False # clear the alert
# if max17.SOC_change_alert:
# print(" Charge changed")
# max17.SOC_change_alert = False # clear the alert