-
Notifications
You must be signed in to change notification settings - Fork 4
/
MintyBatteryMonitor.py
172 lines (147 loc) · 5.07 KB
/
MintyBatteryMonitor.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python2.7
'''
Date: 10/08/17
Author: HoolyHoo
Version: 3.0
Name: Battery Monitor Script - Utility for the MintyPi project.
Description: Monitors analog input for voltage and displays battery icon related to the input. Displays low warning battery video at 25% battery level and displays shutdown video at critical battery level followed by a gracefull safe shutdown.
Usage: Battery icons can be toggled and state remembered by using shortcut combo, "Mode + A".
'''
import time
import os
import signal
from subprocess import check_output
import Adafruit_ADS1x15
import pickle
# Config
warning = 0
status = 0
debug = 0
iconState = ""
toggleFile = "/home/pi/MintyComboScript/Toggle.txt"
PNGVIEWPATH = "/home/pi/MintyComboScript/Pngview/"
ICONPATH = "/home/pi/MintyComboScript/icons"
statePath = "/home/pi/MintyComboScript/combo.dat"
CLIPS = 1
REFRESH_RATE = 2
VOLT100 = 4.09 # 4.09
VOLT75 = 3.68 # 3.76
VOLT50 = 3.46 # 3.63
VOLT25 = 3.35 # 3.5
VOLT0 = 3.25 # 3.2
adc = Adafruit_ADS1x15.ADS1015()
GAIN = 1
def readData(filepath):
with open(filepath, 'rb') as file:
return pickle.load(file)
def changeicon(percent):
global iconState
if iconState != percent:
iconState = percent
i = 0
killid = 0
os.system(PNGVIEWPATH + "/pngview -b 0 -l 3000" + percent + " -x 650 -y 10 " + ICONPATH + "/battery" + percent + ".png &")
out = check_output("ps aux | grep pngview | awk '{ print $2 }'", shell=True)
nums = out.split('\n')
for num in nums:
i += 1
if i == 1:
killid = num
os.system("sudo kill " + killid)
def endProcess(signalnum=None, handler=None):
os.system("sudo killall pngview")
exit(0)
def readVoltage():
value = adc.read_adc(3, gain=GAIN)
return value
def convertVoltage(sensorValue):
voltage = float(sensorValue) * (4.09 / 2047.0)
return voltage
# Initial Setup
signal.signal(signal.SIGTERM, endProcess)
signal.signal(signal.SIGINT, endProcess)
comboStates = readData(statePath)
# Begin Battery Monitoring
os.system(PNGVIEWPATH + "/pngview -b 0 -l 299999 -x 650 -y 10 " + ICONPATH + "/blank.png &")
if comboStates['battery'] == 1:
while True:
try:
ret1 = readVoltage()
time.sleep(.4)
ret2 = readVoltage()
time.sleep(.4)
ret3 = readVoltage()
time.sleep(.4)
ret4 = (ret1 + ret2 + ret3) / 3
ret = convertVoltage(ret4)
if debug == 1:
print(ret)
if ret < VOLT0:
if status != 0:
changeicon("0")
if CLIPS == 1:
os.system("/usr/bin/omxplayer --no-osd --layer 999999 " + ICONPATH + "/lowbattshutdown.mp4 --alpha 160;")
os.system("sudo shutdown -h now")
status = 0
elif ret < VOLT25:
if status != 25:
changeicon("25")
if warning != 1:
if CLIPS == 1:
os.system("/usr/bin/omxplayer --no-osd --layer 999999 " + ICONPATH + "/lowbattalert.mp4 --alpha 160")
warning = 1
status = 25
elif ret < VOLT50:
if status != 50:
changeicon("50")
status = 50
elif ret < VOLT75:
if status != 75:
changeicon("75")
status = 75
else:
if status != 100:
changeicon("100")
status = 100
time.sleep(REFRESH_RATE)
except IOError:
print('No i2c Chip Found!')
exit(0)
elif comboStates['battery'] == 0:
while True:
try:
ret1 = readVoltage()
time.sleep(.4)
ret2 = readVoltage()
time.sleep(.4)
ret3 = readVoltage()
time.sleep(.4)
ret4 = (ret1 + ret2 + ret3) / 3
ret = convertVoltage(ret4)
if debug == 1:
print(ret)
if ret < VOLT0:
if status != 0:
changeicon("0")
if CLIPS == 1:
os.system("/usr/bin/omxplayer --no-osd --layer 999999 " + ICONPATH + "/lowbattshutdown.mp4 --alpha 160;")
os.system("sudo shutdown -h now")
status = 0
elif ret < VOLT25:
if status != 25:
changeicon("25")
if warning != 1:
if CLIPS == 1:
os.system("/usr/bin/omxplayer --no-osd --layer 999999 " + ICONPATH + "/lowbattalert.mp4 --alpha 160")
warning = 1
status = 25
elif ret < VOLT50:
status = 50
elif ret < VOLT75:
status = 75
else:
status = 100
time.sleep(REFRESH_RATE)
except IOError:
print('No i2c Chip Found!')
exit(0)