def lowBattery(channel):
#Checking for LED bounce for the duration of the battery Timeout
for bounceSample in range(1, int(round(batteryTimeout / sampleRate))):
time.sleep(sampleRate)
if GPIO.input(batteryGPIO) is 1:
break
global playerFlag
while playerFlag is 1:
time.sleep(1)
#If the LED is a solid condition, there will be no bounce. Launch shutdown video and then gracefully shutdown
if bounceSample is int(round(batteryTimeout / sampleRate)) - 1:
playerFlag = 1
os.system("/usr/bin/omxplayer --no-osd --layer 999999 " + shutdownVideo + " --alpha 180;")
if GPIO.input(batteryGPIO) is 0:
os.system("sudo shutdown -h now")
playerFlag = 0
sys.exit(0)
#If the LED is a solid for more than 10% of the timeout, we know that the battery is getting low. Launch the Low Battery alert.
if bounceSample > int(round(batteryTimeout / sampleRate * 0.1)):
playerFlag = 1
os.system("/usr/bin/omxplayer --no-osd --layer 999999 " + lowalertVideo + " --alpha 160;")
playerFlag = 0
#Discovered a bug with the Python GPIO library and threaded events. Need to unbind and rebind after a System Call or the program will crash
GPIO.remove_event_detect(batteryGPIO)
GPIO.add_event_detect(batteryGPIO, GPIO.BOTH, callback=lowBattery, bouncetime=300)
#If we know the battery is low, we aggresively monitor the level to ensure we shutdown once the Power Timeout is exceeded.
lowBattery(batteryGPIO)
评论列表
文章目录