-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmonitor.py
315 lines (282 loc) · 8.64 KB
/
monitor.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python
#sudo apt-get install python-serial
#
# This file originates from Vascofazza's Retropie open OSD project.
# Author: Federico Scozzafava
#
# THIS HEADER MUST REMAIN WITH THIS FILE AT ALL TIMES
#
# This firmware is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This firmware is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this repo. If not, see <http://www.gnu.org/licenses/>.
#
import RPi.GPIO as GPIO
import time
import os,signal,sys
import serial
from subprocess import Popen, PIPE, check_output, check_call
import re
import logging
import logging.handlers
import thread
import threading
import signal
# Config variables
bin_dir = '/home/pi/Retropie-open-OSD/'
osd_path = bin_dir + 'osd/osd'
rfkill_path = bin_dir + 'rfkill/rfkill'
# Hardware variables
pi_charging = 26
pi_charged = 25
pi_shdn = 27
serport = '/dev/ttyACM0'
# Init GPIO pins
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(pi_charging, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(pi_charged, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(pi_shdn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Batt variables
voltscale = 118.0 #ADJUST THIS
currscale = 640.0
resdivmul = 4.0
resdivval = 1000.0
dacres = 33.0
dacmax = 1023.0
batt_threshold = 4
batt_full = 420
batt_low = 330
batt_shdn = 320
temperature_max = 70.0
temperature_threshold = 5.0
# Wifi variables
wifi_state = 'UNKNOWN'
wif = 0
wifi_off = 0
wifi_warning = 1
wifi_error = 2
wifi_1bar = 3
wifi_2bar = 4
wifi_3bar = 5
# Set up a port
try:
ser = serial.Serial(
port=serport,
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1)
except Exception as e:
logging.exception("ERROR: Failed to open serial port");
sys.exit(1);
# Set up OSD service
try:
osd_proc = Popen(osd_path, shell=False, stdin=PIPE, stdout=None, stderr=None)
osd_in = osd_proc.stdin
time.sleep(1)
osd_poll = osd_proc.poll()
if (osd_poll):
logging.error("ERROR: Failed to start OSD, got return code [" + str(osd_poll) + "]\n")
sys.exit(1)
except Exception as e:
logging.exception("ERROR: Failed start OSD binary");
sys.exit(1);
# Check for charge state
def checkCharge():
return not GPIO.input(pi_charging) and GPIO.input(pi_charged)
# Check for shutdown state
def checkShdn():
state = GPIO.input(pi_shdn)
if (state):
logging.info("SHUTDOWN")
doShutdown()
# Read brightness
def getBrightness():
ser.write('l')
ser.flush()
# Read brightness
def getVoltage():
ser.write('b')
ser.flush()
# Read voltage
def readVoltage(voltVal):
#volt = int(500.0/1023.0*voltVal)
volt = int((( voltVal * voltscale * dacres + ( dacmax * 5 ) ) / (( dacres * resdivval ) / resdivmul)))
logging.info("VoltVal [" + str(voltVal) + "]")
logging.info("Volt [" + str(volt) + "]V")
return volt
# Get voltage percent
def getVoltagepercent(volt):
return clamp(int( float(volt - batt_shdn)/float(batt_full - batt_shdn)*100 ), 0, 100)
# Read wifi (Credits: kite's SAIO project)
def readModeWifi(toggle = False):
global wif
ret = wif
wifiVal = not os.path.exists(osd_path+'wifi')# int(ser.readline().rstrip('\r\n'))
if toggle:
wifiVal = not wifiVal
global wifi_state
if (wifiVal):
if os.path.exists(osd_path+'wifi'):
os.remove(osd_path+'wifi')
if (wifi_state != 'ON'):
wifi_state = 'ON'
logging.info("Wifi [ENABLING]")
try:
out = check_output([ 'sudo', rfkill_path, 'unblock', 'wifi' ])
logging.info("Wifi [" + str(out) + "]")
out = check_output([ 'sudo', rfkill_path, 'unblock', 'bluetooth' ])
logging.info("BT [" + str(out) + "]")
except Exception, e:
logging.info("Wifi : " + str(e))
ret = wifi_warning # Get signal strength
else:
with open(osd_path+'wifi', 'a'):
n = 1
if (wifi_state != 'OFF'):
wifi_state = 'OFF'
logging.info("Wifi [DISABLING]")
try:
out = check_output([ 'sudo', rfkill_path, 'block', 'wifi' ])
logging.info("Wifi [" + str(out) + "]")
out = check_output([ 'sudo', rfkill_path, 'block', 'bluetooth' ])
logging.info("BT [" + str(out) + "]")
except Exception, e:
logging.info("Wifi : " + str(e))
ret = wifi_error
return ret
#check signal
raw = check_output([ 'cat', '/proc/net/wireless'] )
strengthObj = re.search( r'.wlan0: \d*\s*(\d*)\.\s*[-]?(\d*)\.', raw, re.I )
if strengthObj:
strength = 0
if (int(strengthObj.group(1)) > 0):
strength = int(strengthObj.group(1))
elif (int(strengthObj.group(2)) > 0):
strength = int(strengthObj.group(2))
logging.info("Wifi [" + str(strength) + "]strength")
if (strength > 55):
ret = wifi_3bar
elif (strength > 40):
ret = wifi_2bar
elif (strength > 5):
ret = wifi_1bar
else:
ret = wifi_warning
else:
logging.info("Wifi [---]strength")
ret = wifi_error
return ret
# Read CPU temp
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return float(res.replace("temp=","").replace("'C\n",""))
# Do a shutdown
def doShutdown(channel = None):
ser.write('s0')#shuts the screen off
ser.flush()
time.sleep(1)
check_call("sudo killall emulationstation", shell=True)
time.sleep(1)
check_call("sudo shutdown -h now", shell=True)
try:
sys.stdout.close()
except:
pass
try:
sys.stderr.close()
except:
pass
sys.exit(0)
# Signals the OSD binary
def updateOSD(volt = 0,bat = 0, temp = 0, wifi = 0, brightness = 0, info = False, charge = False):
commands = "v"+str(volt)+" b"+str(bat)+" t"+str(temp)+" w"+str(wifi)+" l"+str(brightness)+" "+("on " if info else "off ")+("charge" if charge else "ncharge")+"\n"
#print commands
osd_proc.send_signal(signal.SIGUSR1)
osd_in.write(commands)
osd_in.flush()
# Misc functions
def clamp(n, minn, maxn):
return max(min(maxn, n), minn)
brightness = -1
info = False
volt = -1
wifi = 2
charge = 0
bat = 100
condition = threading.Condition()
def reading():
global brightness
global volt
global info
global wifi
global charge
global bat
time.sleep(1)
while(1):
readval = ser.readline().strip('\n')
condition.acquire()
if len(readval) < 2:
condition.release()
continue
#print readval
if readval == 'c0':
wifi = readModeWifi(True)
elif readval[0] == 'l':
brightness = int(readval[1:])
elif readval == 'mod_on':
info = True
elif readval == 'mod_off':
info = False
elif readval[0] == 'b':
volt = readVoltage(int(readval[1:]))
if charge:
volt-=20
if info:
condition.notify()
bat = getVoltagepercent(volt)
updateOSD(volt, bat, temp, wifi, brightness, info, charge)
condition.release()
reading_thread = thread.start_new_thread(reading, ())
def lambdaCharge(channel):
condition.acquire()
condition.notify();
condition.release();
def exit_gracefully(signum=None,frame=None):
GPIO.cleanup
osd_proc.terminate()
sys.exit(0)
#interrupts
GPIO.add_event_detect(pi_shdn, GPIO.FALLING, callback=doShutdown, bouncetime=500)
GPIO.add_event_detect(pi_charging, GPIO.BOTH, callback=lambdaCharge, bouncetime=100)
GPIO.add_event_detect(pi_charged, GPIO.FALLING, callback=lambdaCharge, bouncetime=100)
signal.signal(signal.SIGINT, exit_gracefully)
signal.signal(signal.SIGTERM, exit_gracefully)
# Main loop
try:
print "STARTED!"
while 1:
#checkShdn()
charge = checkCharge()
condition.acquire()
getVoltage()
temp = getCPUtemperature()
wifi = readModeWifi()
if brightness < 0:
getBrightness()
condition.wait(4.5)
condition.release()
time.sleep(0.5)
#print 'WAKE'
except KeyboardInterrupt:
exit_gracefully()