From fdf61c5d3a4889eeea818b7ce07b06c5437e6438 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 7 Jan 2016 20:51:49 +0100 Subject: [PATCH] Refactoring --- Moped/UserLed.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Moped/UserLed.py b/Moped/UserLed.py index 8c812a7..c9ae68b 100755 --- a/Moped/UserLed.py +++ b/Moped/UserLed.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python import ConfigParser import RPi.GPIO as GPIO @@ -7,27 +7,34 @@ yellowLed = 15 redLed = 17 greenLed = 18 +mopidyConf = '/etc/mopidy/mopidy.conf' + def setLeds(yellow, red, green): GPIO.output(yellowLed, yellow) GPIO.output(redLed, red) GPIO.output(greenLed, green) -config = ConfigParser.ConfigParser() -config.read('/etc/mopidy/mopidy.conf') +def getConfiguredLedColor(): + config = ConfigParser.ConfigParser() + config.read(mopidyConf) -ledColor = config.get('moped-switcher', 'led') + 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) GPIO.setmode(GPIO.BCM) GPIO.setup(yellowLed, GPIO.OUT) GPIO.setup(redLed, GPIO.OUT) GPIO.setup(greenLed, GPIO.OUT) -if ledColor == 'yellow': - print 'yellow' - setLeds(1, 0, 0) -elif ledColor == 'red': - setLeds(0, 1, 0) -elif ledColor == 'green': - setLeds(0, 0, 1) +setConfiguredLedColor() GPIO.cleanup()