Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import sys
- import subprocess
- import threading
- import evdev
- import os
- class Keys:
- KEY_MAXI = 44
- KEY_CLOSE = 2
- KEY_1 = 13
- KEY_2 = 14
- KEY_3 = 15
- KEY_4 = 16
- KEY_5 = 17
- KEY_6 = 18
- KEY_7 = 19
- KEY_8 = 20
- KEY_9 = 21
- KEY_BACK = 22
- KEY_0 = 23
- KEY_ENTER = 24
- KEY_VOL_UP = 9
- KEY_VOL_DOWN = 8
- KEY_MUTE = 10
- KEY_CH_UP = 11
- KEY_CH_DOWN = 12
- KEY_INFO = 46
- KEY_VENDOR = 0
- KEY_OPTION = 47
- KEY_UP = 26
- KEY_LEFT = 29
- KEY_OK = 30
- KEY_RIGHT = 31
- KEY_DOWN = 34
- KEY_MENU = 28
- KEY_EXIT = 32
- KEY_REW = 36
- KEY_PLAY = 37
- KEY_FWD = 38
- KEY_REC = 39
- KEY_STOP = 40
- KEY_PAUSE = 41
- KEY_NEXT = 42
- KEY_PREV = 43
- KEY_TV = 3
- KEY_DVD = 4
- KEY_PHOTOS = 5
- KEY_MUSIC = 6
- KEY_VIDEO = 7
- KEY_HELP = 1
- KEY_MOUSE = 45
- KEY_A = 25
- KEY_B = 27
- KEY_C = 33
- KEY_D = 35
- # The map of files to play when something is pressed
- map = {}
- map[Keys.KEY_OK] = '/home/pi/torture.mp3'
- map[Keys.KEY_LEFT] = '/home/pi/torture.mp3'
- map[Keys.KEY_RIGHT] = '/home/pi/torture.mp3'
- map[Keys.KEY_UP] = '/home/pi/torture.mp3'
- map[Keys.KEY_DOWN] = '/home/pi/torture.mp3'
- FNULL = open(os.devnull, 'w')
- def play(path):
- # -o local causes the output to play through the audio jack (to pipe to the amp) rather than the HDMI cable
- subprocess.Popen(['omxplayer', '-o', 'local', '--no-osd', '--no-keys', path], stdout=FNULL, stderr=subprocess.STDOUT)
- # The raspberry pi has an issue with playing sounds described here:
- # http://raspberrypi.stackexchange.com/a/3633
- # A workaround is to have an audio file playing in a loop. Here we play silence repeatedly
- # Not sure how to end this, though. i.e. when terminating the script we should end the looped playback.
- def loop_silence():
- # no-keys prevents interpretation of keys as commands to modify volume and such
- # no-osd prevents the on screed display showing seeking to the beginning of the file as it looops
- # loop causes the audio to loop forever
- subprocess.Popen(['omxplayer', '--loop', '--no-osd', '--no-keys', 'silence.wav'], stdout=FNULL, stderr=subprocess.STDOUT)
- # Main worker loop
- t = threading.Thread(target=loop_silence)
- t.start()
- devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
- firefly = None
- for device in devices:
- if device.name == "X10 WTI RF receiver":
- firefly = device
- break
- if not firefly:
- sys.exit()
- for event in firefly.read_loop():
- if event.type == evdev.ecodes.EV_MSC:
- press = event.value
- if press == 2:
- print("You pressed <close>, exiting...")
- break
- if press in map:
- print("Key " + str(press) + ": Playing " + map[press] + " ...")
- play(map[press])
- else:
- print("Nothing to do for [" + str(press) + "]")
Add Comment
Please, Sign In to add comment