Skip to content

Instantly share code, notes, and snippets.

@lukestanley
Last active August 29, 2015 14:24
Show Gist options
  • Save lukestanley/6866cf075c0ab8076c2c to your computer and use it in GitHub Desktop.
Save lukestanley/6866cf075c0ab8076c2c to your computer and use it in GitHub Desktop.
Battery Alarm - run red alert sound if power is unplugged
#download mp3 from http://soundbible.com/81-Red-Alert.html
from time import sleep #load a delay function
from os import system #load the "system" call function
import subprocess #load subprocess module
maybeRunningAlertSound = False #store if we are playing sounds, we are not yet
#Because "True" is always true, this is a good way to run the program forever:
while True is True:
#run a system command called acpi and get it's output:
powerStatus = subprocess.check_output('acpi -a', shell=True)
if 'off-line' in powerStatus:
#run red alert mp3 with app called mpg123:
system('mpg123 /home/luke/bin/Red_Alert_sound.mp3 &')
maybeRunningAlertSound = True
else: #we have the power cable plugged in and working
if maybeRunningAlertSound:#so now we can disable any alerts
system('killall mpg123') #kill the sound program called mpg123
maybeRunningAlertSound = False #we know not to run killall again for now
sleep(1) #wait a second before we check again
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment