| @@ -6,13 +6,16 @@ import time | |||||
| from datetime import datetime | from datetime import datetime | ||||
| from time import sleep | from time import sleep | ||||
| import lirc | |||||
| from evdev import InputDevice, categorize, ecodes | |||||
| import random | import random | ||||
| import mpd | import mpd | ||||
| import requests | import requests | ||||
| from xml.etree import ElementTree as ET | from xml.etree import ElementTree as ET | ||||
| import evdev | |||||
| from pyudmx import pyudmx | from pyudmx import pyudmx | ||||
| import talkey | import talkey | ||||
| @@ -33,6 +36,50 @@ class Simple(resource.Resource): | |||||
| html = '[{"inuse":"%s"}]' % statustext | html = '[{"inuse":"%s"}]' % statustext | ||||
| return html.encode('utf-8') | return html.encode('utf-8') | ||||
| # Preparing IR Device | |||||
| def get_ir_device(): | |||||
| devices = [evdev.InputDevice(path) for path in evdev.list_devices()] | |||||
| for device in devices: | |||||
| if (device.name == "gpio_ir_recv"): | |||||
| print("Using device", device.path, "\n") | |||||
| return device | |||||
| print("No device found!") | |||||
| ir_dev = get_ir_device() | |||||
| remote_keys = { | |||||
| 0x100028: 'mode_disco', # phono start | |||||
| 0x100029: 'mode_work', # phono stop | |||||
| 0x10000f: 'mode_power', # tuner band | |||||
| 0x100000: 'mode_music_play_1', # tuner 1 | |||||
| 0x100001: 'mode_music_play_2', # tuner 2 | |||||
| 0x100002: 'mode_music_play_3', # tuner 3 | |||||
| 0x100003: 'mode_music_play_4', # tuner 4 | |||||
| 0x100004: 'mode_music_play_5', # tuner 5 | |||||
| 0x100033: 'mode_music_rewind', # tape fast rewind | |||||
| 0x100037: 'mode_music_back', # tape slow rewind | |||||
| 0x100032: 'mode_music_forward', # tape slow forward | |||||
| 0x100034: 'mode_music_fastforward', # tape fast forward | |||||
| 0x100038: 'mode_music_stop', # tape stop | |||||
| 0x100039: 'mode_music_pause', # tape pause | |||||
| 0x10003f: 'mode_music_info', # tape stoprec | |||||
| 0x110032: 'mode_music_play', # cd play | |||||
| 0x110030: 'mode_music_previous', # cd previous | |||||
| 0x110031: 'mode_music_next', # cd next | |||||
| 0x110038: 'mode_music_stop', # cd stop | |||||
| 0x100025: 'mode_news', # source cd | |||||
| 0x10001e: 'mode_play_fm4', # source video 2 | |||||
| 0x100022: 'mode_play_oe1', # source video 1 | |||||
| 0x100012: 'mode_volume_up', # vol up | |||||
| 0x100013: 'mode_volume_down', # vol down | |||||
| 0x100014: 'mode_music_mute', # vol mute | |||||
| } | |||||
| 0x100000 | |||||
| # GPIO | # GPIO | ||||
| pin_kugel = 2 # Input: dico ball + DMX on/off | pin_kugel = 2 # Input: dico ball + DMX on/off | ||||
| pin_sun = 4 # Input: light bulb on/off | pin_sun = 4 # Input: light bulb on/off | ||||
| @@ -86,6 +133,90 @@ dmxStartupScenes = [ | |||||
| [120,0,255,255,0,224] | [120,0,255,255,0,224] | ||||
| ] | ] | ||||
| def interpretRemoteInputs(): | |||||
| events = ir_dev.read() | |||||
| try: | |||||
| ir_event_list = [event.value for event in events] | |||||
| print("Received commands:", ir_event_list) | |||||
| if ir_event_list and len(ir_event_list) > 0: | |||||
| remote_key = ir_event_list[0] | |||||
| print(remote_key) | |||||
| code = None | |||||
| try: | |||||
| code = remote_keys[remote_key] | |||||
| except KeyError: | |||||
| print('received invalid signal: ' + str(remote_key)) | |||||
| lastUsed = time.time() # user is active! | |||||
| hadUserInteraction = True | |||||
| inUse = True | |||||
| print('received code:', str(code)) | |||||
| if(code == "mode_disco"): | |||||
| setDiscoMode() | |||||
| if(code == "mode_work"): | |||||
| setWorkingMode() | |||||
| if(code == "mode_power"): | |||||
| setOnOff() | |||||
| if(code == "mode_music_play_1"): | |||||
| startMusic('1') | |||||
| if(code == "mode_music_play_2"): | |||||
| startMusic('2') | |||||
| if(code == "mode_music_play_3"): | |||||
| startMusic('3') | |||||
| if(code == "mode_music_play_4"): | |||||
| startMusic('4') | |||||
| if(code == "mode_music_play_5"): | |||||
| startMusic('5') | |||||
| if(code == "mode_play_fm4"): | |||||
| startMusic('https://orf-live.ors-shoutcast.at/fm4-q2a') | |||||
| if(code == "mode_play_oe1"): | |||||
| startMusic('https://orf-live.ors-shoutcast.at/oe1-q2a') | |||||
| if(code == "mode_music_previous"): | |||||
| previousSong() | |||||
| if(code == "mode_music_next"): | |||||
| nextSong() | |||||
| if(code == "mode_music_play"): | |||||
| playMusic() | |||||
| if(code == "mode_music_stop"): | |||||
| stopMusic() | |||||
| if(code == "mode_music_mute"): | |||||
| muteMusic() | |||||
| if(code == "mode_music_pause"): | |||||
| pauseMusic() | |||||
| if(code == "mode_music_rewind"): | |||||
| seek('-30') | |||||
| if(code == "mode_music_back"): | |||||
| seek('-5') | |||||
| if(code == "mode_music_forward"): | |||||
| seek('+5') | |||||
| if(code == "mode_music_fastforward"): | |||||
| seek('+30') | |||||
| if(code == "mode_volume_up"): | |||||
| changeVolume(5) | |||||
| if(code == "mode_volume_down"): | |||||
| changeVolume(-5) | |||||
| if(code == "mode_music_info"): | |||||
| getTrackInfo() | |||||
| if(code == "mode_record"): | |||||
| say("I'm sorry, I'm afraid I can't do that!") | |||||
| if(code == "mode_news"): | |||||
| oejournalUrl = getNewestPodcastUrl('https://files.orf.at/podcast/oe1/oe1_journale.xml') | |||||
| startMusic(oejournalUrl) | |||||
| except BlockingIOError: | |||||
| # ir_event_list = None | |||||
| print("No command received.\n") | |||||
| # Set a dmx scene by name | # Set a dmx scene by name | ||||
| def setDmxScene(scene): | def setDmxScene(scene): | ||||
| # a universe of zeros | # a universe of zeros | ||||
| @@ -349,7 +480,7 @@ def initService(): | |||||
| try: | try: | ||||
| client.setvol(defaultvolume) | client.setvol(defaultvolume) | ||||
| except Exception: | except Exception: | ||||
| client.connect("localhost", 6600) | |||||
| client.connect("0.0.0.0", 6600) | |||||
| client.setvol(defaultvolume) | client.setvol(defaultvolume) | ||||
| volume = defaultvolume | volume = defaultvolume | ||||
| uservolume = defaultvolume | uservolume = defaultvolume | ||||
| @@ -368,7 +499,7 @@ def say(text, lang="en"): | |||||
| def setOnOff(): | def setOnOff(): | ||||
| global mode | global mode | ||||
| stopMusic() | stopMusic() | ||||
| if mode is not 'work': | |||||
| if mode != 'work': | |||||
| setWorkingMode() | setWorkingMode() | ||||
| else: | else: | ||||
| initService() | initService() | ||||
| @@ -395,6 +526,7 @@ dmxScene = 0 | |||||
| bootstrap() | bootstrap() | ||||
| dev = pyudmx.uDMXDevice() | dev = pyudmx.uDMXDevice() | ||||
| dev.open() | dev.open() | ||||
| print('dmx device: ' + str(dev)) | |||||
| client = mpd.MPDClient() | client = mpd.MPDClient() | ||||
| client.connect("localhost", 6600) | client.connect("localhost", 6600) | ||||
| @@ -426,11 +558,12 @@ setSun('off') | |||||
| print(timestamp(), "Ready!") | print(timestamp(), "Ready!") | ||||
| lirc.init("disco", "~/discobert/lircrc", blocking=False) | |||||
| #lirc.init("disco", "~/discobert/lircrc", blocking=False) | |||||
| # Main event loop ... | # Main event loop ... | ||||
| while True: | while True: | ||||
| sleep(0.25) | |||||
| print("start new loop round") | |||||
| sleep(0.3) | |||||
| pirstate = wiringpi.digitalRead(pin_pir) | pirstate = wiringpi.digitalRead(pin_pir) | ||||
| doorstate = wiringpi.digitalRead(pin_door) | doorstate = wiringpi.digitalRead(pin_door) | ||||
| #print('pirstate: ', pirstate) | #print('pirstate: ', pirstate) | ||||
| @@ -454,74 +587,8 @@ while True: | |||||
| inUse = False | inUse = False | ||||
| remotesignal = lirc.nextcode() | |||||
| print('remotesignal: ', remotesignal) | |||||
| if remotesignal: | |||||
| lastUsed = time.time() # user is active! | |||||
| hadUserInteraction = True | |||||
| inUse = True | |||||
| for code in remotesignal: | |||||
| print('received code:', code) | |||||
| if(code == "mode_disco"): | |||||
| setDiscoMode() | |||||
| if(code == "mode_work"): | |||||
| setWorkingMode() | |||||
| if(code == "mode_power"): | |||||
| setOnOff() | |||||
| if(code == "mode_music_play_1"): | |||||
| startMusic('1') | |||||
| if(code == "mode_music_play_2"): | |||||
| startMusic('2') | |||||
| if(code == "mode_music_play_3"): | |||||
| startMusic('3') | |||||
| if(code == "mode_music_play_4"): | |||||
| startMusic('4') | |||||
| if(code == "mode_music_play_5"): | |||||
| startMusic('5') | |||||
| if(code == "mode_play_fm4"): | |||||
| startMusic('https://orf-live.ors-shoutcast.at/fm4-q2a') | |||||
| if(code == "mode_play_oe1"): | |||||
| startMusic('https://orf-live.ors-shoutcast.at/oe1-q2a') | |||||
| if(code == "mode_music_previous"): | |||||
| previousSong() | |||||
| if(code == "mode_music_next"): | |||||
| nextSong() | |||||
| if(code == "mode_music_play"): | |||||
| playMusic() | |||||
| if(code == "mode_music_stop"): | |||||
| stopMusic() | |||||
| if(code == "mode_music_mute"): | |||||
| muteMusic() | |||||
| if(code == "mode_music_pause"): | |||||
| pauseMusic() | |||||
| if(code == "mode_music_rewind"): | |||||
| seek('-30') | |||||
| if(code == "mode_music_back"): | |||||
| seek('-5') | |||||
| if(code == "mode_music_forward"): | |||||
| seek('+5') | |||||
| if(code == "mode_music_fastforward"): | |||||
| seek('+30') | |||||
| if(code == "mode_volume_up"): | |||||
| changeVolume(5) | |||||
| if(code == "mode_volume_down"): | |||||
| changeVolume(-5) | |||||
| if(code == "mode_music_info"): | |||||
| getTrackInfo() | |||||
| if(code == "mode_record"): | |||||
| say("I'm sorry, I'm afraid I can't do that!") | |||||
| if(code == "mode_news"): | |||||
| oejournalUrl = getNewestPodcastUrl('https://files.orf.at/podcast/oe1/oe1_journale.xml') | |||||
| startMusic(oejournalUrl) | |||||
| interpretRemoteInputs() | |||||
| if(inUseBefore != inUse): | if(inUseBefore != inUse): | ||||
| print(timestamp(), "State change inUse:", inUseBefore, inUse) | print(timestamp(), "State change inUse:", inUseBefore, inUse) | ||||
| if inUse: | if inUse: | ||||
| @@ -534,7 +601,7 @@ while True: | |||||
| # Webserver | # Webserver | ||||
| reactor.iterate() | reactor.iterate() | ||||
| lirc.deinit() # Clean up lirc | |||||
| #lirc.deinit() # Clean up lirc | |||||
| dev.close() | |||||
| # dev.close() | |||||