diff --git a/discobert.py b/discobert.py index 40e0add..1c5311a 100644 --- a/discobert.py +++ b/discobert.py @@ -11,11 +11,15 @@ import mpd from pyudmx import pyudmx +import talkey + # GPIO pin_kugel = 2 pin_sun = 4 pin_pir = 0 + + dmxScenes = { "fadecolors":[255,255,255,255,255,192], "plain-red":[255,255,0,0,0,0], @@ -26,10 +30,10 @@ dmxScenes = { dmxUserScenes = [ [255,255,255,255,255,192], + [255,0,180,180,0,0], [255,255,0,0,0,0], [255,0,255,0,0,0], [255,0,0,255,0,0], - [255,0,180,180,0,0], [190,255,255,255,0,0], [120,0,255,255,0,224] ] @@ -42,9 +46,14 @@ def setDmxScene(scene): dev.send_multi_value(1, cv) -def setUserDmxScene(scene): +def setUserDmxScene(): + global dmxScene + if dmxScene < len(dmxUserScenes)-1: + dmxScene += 1 + else: + dmxScene = 0 cv = [0 for v in range(0, 512)] - for index, val in enumerate(dmxUserScenes[scene]): + for index, val in enumerate(dmxUserScenes[dmxScene]): cv[index] = val dev.send_multi_value(1, cv) @@ -64,37 +73,142 @@ def setSun(state): -def startMusic(playlist): - client.clear() # clear playlist +def startMusic(playlist, single=False, shuffle=True, repeat=True): + try: + client.clear() # clear playlist + except Exception: + client.connect("localhost", 6600) + client.clear() # clear playlist + client.add(playlist) # add file/directory to playlist - client.shuffle() # shuffle playlist - client.repeat(1) # set playback mode repeat + if shuffle: + client.shuffle() # shuffle playlist + if repeat: + client.repeat(1) # set playback mode repeat + else: + client.repeat(0) # set playback mode repeat + if single: + client.repeat(0) # set playback mode repeat + client.single(1) # set playback mode single + else: + client.single(0) # set playback mode single client.setvol(80)# set volume client.play() # play def stopMusic(): - client.stop() - + try: + client.stop() + except Exception: + client.connect("localhost", 6600) + client.stop() + +def muteMusic(): + global volume + global isMuted + + try: + currentvol = int(client.status()['volume']) + except Exception: + client.connect("localhost", 6600) + currentvol = int(client.status()['volume']) + + if currentvol != 0: + volume = currentvol + + if isMuted: + try: + client.setvol(volume) + except Exception: + client.connect("localhost", 6600) + client.setvol(volume) + else: + try: + client.setvol(0) + except Exception: + client.connect("localhost", 6600) + client.setvol(0) + isMuted = not isMuted + +def changeVolume(change=5): + global volume + global isMuted + isMuted = False + newvol = volume + change + if newvol > 100: + newvol = 100 + if newvol < 0: + newvol = 0 + + try: + client.setvol(newvol) + except Exception: + client.connect("localhost", 6600) + client.setvol(newvol) + volume = newvol + +def setMode(string): + global mode + mode = string def setDiscoMode(): - setSun('off') - sleep(0.3) setKugel('on') setDmxScene('fadecolors') + setUserDmxScene() + sleep(0.3) + setSun('off') + setMode('disco') + +def getTrackInfo(): + try: + currentsong = client.currentsong() + except Exception: + client.connect("localhost", 6600) + currentsong = client.currentsong() + print(currentsong) + + global volume + try: + currentvol = int(client.status()['volume']) + except Exception: + client.connect("localhost", 6600) + currentvol = int(client.status()['volume']) + + changeVolume(-30) + try: + tts.say(currentsong['artist'] + ', ' + currentsong['title']) + except Exception: + tts.say('Willkommen am Discoklo!') + client.setvol(currentvol) + volume = currentvol + + def setWorkingMode(): setSun('on') sleep(0.3) setKugel('off') setDmxScene('black') + setMode('work') -def setOff(): +def closeService(): setSun('off') sleep(0.3) setKugel('off') setDmxScene('black') - - + stopMusic() + setMode('off') + +def initService(): + startMusic('0', True) + setDiscoMode() + +def setOnOff(): + global mode + stopMusic() + if mode is not 'work': + setWorkingMode() + else: + initService() def bootstrap(): wiringpi.wiringPiSetup() @@ -106,29 +220,33 @@ def bootstrap(): +dmxScene = 0 bootstrap() dev = pyudmx.uDMXDevice() dev.open() -#connect to MPD client = mpd.MPDClient() client.connect("localhost", 6600) - +tts = talkey.Talkey( + preferred_languages=['en', 'de'], + engine_preference=['pico'], +) pirstate = wiringpi.digitalRead(6) -defaultscene = 1 +lirc.init("disco", "~/discobert/lircrc", blocking=False) lastUsed = time.time() inUse = True -inUseBefore = False +mode = "none" +inUseBefore = True timeout = 2 * 60 +volume = 80 +isMuted = False -dmxScene = 0 - +initService() -lirc.init("disco", "./lircrc", blocking=False) # Main event loop ... @@ -146,21 +264,17 @@ while True: remotesignal = lirc.nextcode() if remotesignal: lastUsed = time.time() # user is active! + inUse = True for code in remotesignal: print('received code:', code) if(code == "mode_disco"): setDiscoMode() if(code == "mode_work"): setWorkingMode() - if(code == "mode_off"): - setOff() - if(code == "mode_dmx_next"): - print("dmxScene", dmxScene) - setUserDmxScene(dmxScene) - if dmxScene < len(dmxUserScenes)-1: - dmxScene += 1 - else: - dmxScene = 0 + if(code == "mode_power"): + setOnOff() + #if(code == "mode_dmx_next"): + if(code == "mode_music_play_1"): startMusic('1') if(code == "mode_music_play_2"): @@ -172,20 +286,29 @@ while True: if(code == "mode_music_play_5"): startMusic('5') if(code == "mode_play_fm4"): - startMusic('http://185.85.28.144:8000/') + startMusic('http://185.85.29.141:8000') + if(code == "mode_play_oe1"): + startMusic('http://185.85.29.142:8000') if(code == "mode_music_stop"): stopMusic() + if(code == "mode_music_mute"): + muteMusic() + if(code == "mode_volume_up"): + changeVolume(5) + if(code == "mode_volume_down"): + changeVolume(-5) + if(code == "mode_music_info"): + getTrackInfo() + if(inUseBefore != inUse): print("State change inUse:", inUseBefore, inUse) - if (inUse): - setDiscoMode() + if inUse: + initService() else: - setOff() - - - inUseBefore = inUse + closeService() + inUseBefore = inUse lirc.deinit() # Clean up lirc