From 9f194a9a03eb3382c682c12f111fb3ac9edcd072 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 7 Jan 2016 20:52:33 +0100 Subject: [PATCH] Integrated user-indication via led --- Moped/SwitchMopidy.py | 75 +++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 21 deletions(-) diff --git a/Moped/SwitchMopidy.py b/Moped/SwitchMopidy.py index f94a5be..23684e7 100755 --- a/Moped/SwitchMopidy.py +++ b/Moped/SwitchMopidy.py @@ -1,14 +1,40 @@ -#!/usr/bin/python +#!/usr/bin/python import glob import os import subprocess +import ConfigParser import RPi.GPIO as GPIO +yellowLed = 15 +redLed = 17 +greenLed = 18 + mopidyConf = '/etc/mopidy/mopidy.conf' 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(): command = ['service', 'mopidy', 'stop'] subprocess.call(command, shell=False) @@ -42,26 +68,33 @@ def switchConfig(): setConfig(getNextConfig()) def buttonHandler(channel): - print 'Switching to ' + getNextConfig() - try: - stopMopidy() - switchConfig() - startMopidy() - except OSError as e: - print 'Error: ' + e.strerror - + print 'Switching to ' + getNextConfig() + try: + setLeds(0, 0, 0) + stopMopidy() + switchConfig() + startMopidy() + setConfiguredLedColor() + except OSError as e: + print 'Error: ' + e.strerror + if __name__ == '__main__': - buttonPin = 4 - 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) + buttonPin = 4 + dummyPin = 15 - GPIO.add_event_detect(buttonPin, GPIO.FALLING, callback=buttonHandler, bouncetime=1000) + GPIO.setmode(GPIO.BCM) - try: - while True: - GPIO.wait_for_edge(dummyPin, GPIO.RISING) - finally: - GPIO.cleanup() + GPIO.setup(yellowLed, GPIO.OUT) + GPIO.setup(redLed, GPIO.OUT) + GPIO.setup(greenLed, GPIO.OUT) + + 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()