commit 47d3bebbb5012e9d7b544fbf0a3f961ae530f549 Author: mandlm Date: Fri Jan 8 10:19:53 2016 +0100 renamed to mopidy-switcher diff --git a/Moped.pyproj b/Moped.pyproj new file mode 100644 index 0000000..1571b26 --- /dev/null +++ b/Moped.pyproj @@ -0,0 +1,33 @@ + + + + Debug + 2.0 + {34cbb86d-9088-41d8-b0b9-68a131d30188} + + moped.py + + . + . + {888888a0-9f3d-457c-b088-3a5042f75d52} + Standard Python launcher + {2af0f10d-7135-4994-9156-5d01c9c11b7e} + 2.7 + + + + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets + + + + + + + + + + + + \ No newline at end of file diff --git a/Moped.sln b/Moped.sln new file mode 100644 index 0000000..19644e0 --- /dev/null +++ b/Moped.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.24720.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "Moped", "Moped.pyproj", "{34CBB86D-9088-41D8-B0B9-68A131D30188}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {34CBB86D-9088-41D8-B0B9-68A131D30188}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {34CBB86D-9088-41D8-B0B9-68A131D30188}.Release|Any CPU.ActiveCfg = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/UserLed.py b/UserLed.py new file mode 100755 index 0000000..c9ae68b --- /dev/null +++ b/UserLed.py @@ -0,0 +1,40 @@ +#!/usr/bin/python + +import ConfigParser +import RPi.GPIO as GPIO + +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) + +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) + +GPIO.setmode(GPIO.BCM) +GPIO.setup(yellowLed, GPIO.OUT) +GPIO.setup(redLed, GPIO.OUT) +GPIO.setup(greenLed, GPIO.OUT) + +setConfiguredLedColor() + +GPIO.cleanup() diff --git a/moped.py b/moped.py new file mode 100755 index 0000000..43ec2a6 --- /dev/null +++ b/moped.py @@ -0,0 +1,45 @@ +#!/usr/bin/python + +from time import sleep + +import RPi.GPIO as GPIO + +yellowLed = 15 +redLed = 17 +greenLed = 18 + +buttonPin = 27 + +sleepTimes = [0.75, 0.5, 0.25, 0.125, 0.0625] +sleepTimeIndex = 0 + +def buttonHandler(channel): + global sleepTimeIndex + print 'button pushed' + sleepTimeIndex = (sleepTimeIndex + 1) % len(sleepTimes) + +def setLeds(yellow, red, green): + GPIO.output(yellowLed, yellow) + GPIO.output(redLed, red) + GPIO.output(greenLed, green) + +try: + GPIO.setmode(GPIO.BCM) + 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.add_event_detect(buttonPin, GPIO.FALLING, callback=buttonHandler, bouncetime=1000) + + while True: + setLeds(1, 0, 0) + sleep(sleepTimes[sleepTimeIndex]) + setLeds(0, 1, 0) + sleep(sleepTimes[sleepTimeIndex]) + setLeds(0, 0, 1) + sleep(sleepTimes[sleepTimeIndex]) +finally: + setLeds(0, 0, 0) + GPIO.cleanup() diff --git a/mopidy-switcher.py b/mopidy-switcher.py new file mode 100755 index 0000000..465ff73 --- /dev/null +++ b/mopidy-switcher.py @@ -0,0 +1,102 @@ +#!/usr/bin/python + +import glob +import os +import subprocess +import ConfigParser + +import RPi.GPIO as GPIO + +yellowLed = 15 +redLed = 17 +greenLed = 18 + +buttonPin = 27 +dummyPin = 1 + +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) + +def startMopidy(): + command = ['service', 'mopidy', 'start'] + subprocess.call(command, shell=False) + +def getConfigs(): + return sorted(glob.glob(os.path.join(userDir, '*.conf'))) + +def getCurrentConfig(): + if os.path.islink(mopidyConf): + return os.path.realpath(mopidyConf) + else: + return None + +def getNextConfig(): + currentConfig = getCurrentConfig() + availableConfigs = getConfigs() + currentIndex = availableConfigs.index(currentConfig) + nextIndex = (currentIndex + 1) % len(availableConfigs) + return availableConfigs[nextIndex] + +def setConfig(newConfig): + if os.path.islink(mopidyConf) and os.path.isfile(newConfig): + os.unlink(mopidyConf) + os.symlink(newConfig, mopidyConf) + +def switchConfig(): + setConfig(getNextConfig()) + +def buttonHandler(channel): + print 'Switching to ' + getNextConfig() + try: + setLeds(0, 0, 0) + stopMopidy() + switchConfig() + startMopidy() + setConfiguredLedColor() + except OSError as e: + print 'Error: ' + e.strerror + +if __name__ == '__main__': + GPIO.setmode(GPIO.BCM) + + 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) + + setConfiguredLedColor() + + try: + while True: + GPIO.wait_for_edge(dummyPin, GPIO.RISING) + finally: + GPIO.cleanup()