Integrated user-indication via led

This commit is contained in:
mandlm 2016-01-07 20:52:33 +01:00
parent fdf61c5d3a
commit 9f194a9a03

View File

@ -1,14 +1,40 @@
#!/usr/bin/python #!/usr/bin/python
import glob import glob
import os import os
import subprocess import subprocess
import ConfigParser
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
yellowLed = 15
redLed = 17
greenLed = 18
mopidyConf = '/etc/mopidy/mopidy.conf' mopidyConf = '/etc/mopidy/mopidy.conf'
userDir = '/etc/mopidy/conf.user/' userDir = '/etc/mopidy/conf.user/'
def setLeds(yellow, red, green):
GPIO.output(yellowLed, yellow)
GPIO.output(redLed, red)
GPIO.output(greenLed, green)
def getConfiguredLedColor():
config = ConfigParser.ConfigParser()
config.read(mopidyConf)
return config.get('moped-switcher', 'led')
def setConfiguredLedColor():
ledColor = getConfiguredLedColor()
if ledColor == 'yellow':
setLeds(1, 0, 0)
elif ledColor == 'red':
setLeds(0, 1, 0)
elif ledColor == 'green':
setLeds(0, 0, 1)
def stopMopidy(): def stopMopidy():
command = ['service', 'mopidy', 'stop'] command = ['service', 'mopidy', 'stop']
subprocess.call(command, shell=False) subprocess.call(command, shell=False)
@ -42,26 +68,33 @@ def switchConfig():
setConfig(getNextConfig()) setConfig(getNextConfig())
def buttonHandler(channel): def buttonHandler(channel):
print 'Switching to ' + getNextConfig() print 'Switching to ' + getNextConfig()
try: try:
stopMopidy() setLeds(0, 0, 0)
switchConfig() stopMopidy()
startMopidy() switchConfig()
except OSError as e: startMopidy()
print 'Error: ' + e.strerror setConfiguredLedColor()
except OSError as e:
print 'Error: ' + e.strerror
if __name__ == '__main__': if __name__ == '__main__':
buttonPin = 4 buttonPin = 4
dummyPin = 15 dummyPin = 15
GPIO.setmode(GPIO.BCM)
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(dummyPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(buttonPin, GPIO.FALLING, callback=buttonHandler, bouncetime=1000) GPIO.setmode(GPIO.BCM)
try: GPIO.setup(yellowLed, GPIO.OUT)
while True: GPIO.setup(redLed, GPIO.OUT)
GPIO.wait_for_edge(dummyPin, GPIO.RISING) GPIO.setup(greenLed, GPIO.OUT)
finally:
GPIO.cleanup() GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(dummyPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(buttonPin, GPIO.FALLING, callback=buttonHandler, bouncetime=1000)
try:
while True:
GPIO.wait_for_edge(dummyPin, GPIO.RISING)
finally:
GPIO.cleanup()