action.py
in meinem mpd projekt habe ich diverse buttons zur steuerung meines mpd's zusammengelötet. was passiert aber, wenn einer dieser buttons
gedrückt wird ?
dieses skript nimmt den knopfdruck entgegen und führt dann eine aktion aus
'''
Created on 26.03.2015
@author: wkostorz
'''
import subprocess
import FileUtil
import MPDConstants
import RPi.GPIO as GPIO
import commands
import time
if __name__ == '__main__':
pass
def loop():
while (True):
pass
time.sleep(10)
# --- Die Anzeige aktualisieren durch Aufruf von Show.py
#def callShow():
# subprocess.call(['python','Show.py'])
def loadNextPlaylist():
#aktuelle playlist loeschen
subprocess.call(['mpc','clear'])
#naechste playlist ermitteln
actualPl = FileUtil.readString(MPDConstants.FILENAME_PLAYLIST)
#alle songs hinzufuegen
allPlaylists = commands.getoutput("mpc lsplaylists")
allPlaylists = allPlaylists.split('\n')
newPl = ""
for i in range (len(allPlaylists)):
print(str(i) + "/" + str(len(allPlaylists)) + "/" + allPlaylists[i])
if i == len(allPlaylists) - 1:
newPl = allPlaylists[0]
elif allPlaylists[i] == str(actualPl):
j = i + 1
newPl = allPlaylists[j]
#print(str(i) + "/" +str(j) + "/" + allPlaylists[i] + "/" + allPlaylists[j])
break
FileUtil.write(MPDConstants.FILENAME_PLAYLIST, newPl)
subprocess.call(['mpc','load', newPl])
startMPD()
def loadNextSong():
#print("loading next song")
subprocess.call(['mpc','next'])
# ----------------------------------------
# --- nextSong Button wurde gedrueckt
# ----------------------------------------
def nextSong(pin):
#mode auswerten
mode = FileUtil.read(MPDConstants.FILENAME_MODE)
if mode == MPDConstants.MODE_SHOW_VOLUME:
decreaseVolume()
else:
loadNextSong()
# ----------------------------------------
# --- nextPlaylist Button wurde gedrueckt
# ----------------------------------------
def nextPlaylist(pin):
#mode auswerten
mode = FileUtil.read(MPDConstants.FILENAME_MODE)
if mode == MPDConstants.MODE_SHOW_VOLUME:
increaseVolume()
elif mode == MPDConstants.MODE_SHOW_RADIO:
nextRadioStation()
else:
loadNextPlaylist()
def stopMPD():
#print("stopping")
subprocess.call(['mpc','pause'])
#subprocess.call('mpc stop', shell=False)
def startMPD():
#print("starting")
subprocess.call(['mpc','play'])
#subprocess.call('mpc play', shell=False)
def increaseVolume():
#print("increasing volume")
subprocess.call(['mpc','volume', '+1'])
#callShow()
def decreaseVolume():
#print("decreasing volume")
subprocess.call(['mpc','volume', '-1'])
def nextRadioStation():
actualStation = FileUtil.readString(MPDConstants.FILENAME_RADIO)
allStations = FileUtil.readString(MPDConstants.FILENAME_RADIO_STATIONS)
allStations=allStations.split('\n')
newStation = ""
for i in range (len(allStations)):
print(str(i) + "/" + str(len(allStations)) + "/" + allStations[i])
if i == len(allStations) - 1:
newStation = allStations[0]
elif allStations[i] == str(actualStation):
j = i + 1
newStation = allStations[j]
break
FileUtil.write(MPDConstants.FILENAME_RADIO, newStation)
print(newStation)
frequence = newStation.split("=")[1]
subprocess.call(['../wiringPi/radio', frequence])
# ----------------------------------------
# --- shutdown Button wurde gedrueckt
# ----------------------------------------
def shutdown():
FileUtil.write(MPDConstants.FILENAME_MODE, MPDConstants.MODE_SHOW_SHUTDOWN)
#subprocess.call('halt', shell=False)
# ----------------------------------------
# --- startStop Button wurde gedrueckt
# ----------------------------------------
def startStopMPD(pin):
#mode auswerten
mode = FileUtil.read(MPDConstants.FILENAME_MODE)
if mode == MPDConstants.MODE_SHOW_SHUTDOWN_CONFIRMATION:
shutdown()
else:
status = FileUtil.read(MPDConstants.FILENAME_STATUS)
if(status == 0):
stopMPD()
status = 1
else:
startMPD()
status = 0
FileUtil.write(MPDConstants.FILENAME_STATUS, status)
# ----------------------------------------
# --- nextMode Button wurde gedrueckt
# ----------------------------------------
def nextMode(pin):
#aktuellen mode ermitteln
mode = FileUtil.read(MPDConstants.FILENAME_MODE)
print(mode)
#naechsten mode ermitteln
if mode == MPDConstants.MODE_SHOW_CPU:
#print("was show cpu, now is show current song")
mode = MPDConstants.MODE_SHOW_CURRENT_SONG
elif mode == MPDConstants.MODE_SHOW_CURRENT_SONG:
#print("was show current song now is song details")
mode = MPDConstants.MODE_SHOW_SONG_DETAILS
elif mode == MPDConstants.MODE_SHOW_SONG_DETAILS:
#print("was song details now is show volume")
mode = MPDConstants.MODE_SHOW_VOLUME
elif mode == MPDConstants.MODE_SHOW_VOLUME:
#print("was show volume now is show cpu")
mode = MPDConstants.MODE_SHOW_PLAYLIST
elif mode == MPDConstants.MODE_SHOW_PLAYLIST:
#print("was show volume now is show cpu")
mode = MPDConstants.MODE_SHOW_RADIO
elif mode == MPDConstants.MODE_SHOW_RADIO:
#print("was show volume now is show cpu")
mode = MPDConstants.MODE_SHOW_SHUTDOWN_CONFIRMATION
elif mode == MPDConstants.MODE_SHOW_SHUTDOWN_CONFIRMATION:
#print("was shutdown now is show CPU")
mode = MPDConstants.MODE_SHOW_CPU
#neuen mode wegschreiben
FileUtil.write(MPDConstants.FILENAME_MODE, mode)
#callShow()
# --- Registrierung der Pins
GPIO.setmode(GPIO.BOARD)
#GPIO.setup(MPDConstants.PIN_BUTTON_SHUTDOWN, GPIO.IN)
GPIO.setup(MPDConstants.PIN_BUTTON_START_STOP, GPIO.IN)
GPIO.setup(MPDConstants.PIN_BUTTON_NEXT_SONG, GPIO.IN)
GPIO.setup(MPDConstants.PIN_BUTTON_NEXT_PLAYLIST, GPIO.IN)
GPIO.setup(MPDConstants.PIN_BUTTON_MODE, GPIO.IN)
# --- Events definieren
GPIO.add_event_detect(MPDConstants.PIN_BUTTON_MODE, GPIO.RISING, callback=nextMode, bouncetime=MPDConstants.BOUNCETIME)
GPIO.add_event_detect(MPDConstants.PIN_BUTTON_START_STOP, GPIO.RISING, callback=startStopMPD, bouncetime=MPDConstants.BOUNCETIME)
GPIO.add_event_detect(MPDConstants.PIN_BUTTON_NEXT_SONG, GPIO.RISING, callback=nextSong, bouncetime=MPDConstants.BOUNCETIME)
GPIO.add_event_detect(MPDConstants.PIN_BUTTON_NEXT_PLAYLIST, GPIO.RISING, callback=nextPlaylist, bouncetime=MPDConstants.BOUNCETIME)
#GPIO.add_event_detect(MPDConstants.PIN_BUTTON_SHUTDOWN, GPIO.RISING, callback=shutdown, bouncetime=MPDConstants.BOUNCETIME)
# --- Endlosschleife starten und warten, dass ein Knopf gedrueckt wird
loop()