AxeOfMen

Play audio on Raspberry Pi with Snapstream Firefly RF remote

Jul 6th, 2016
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import subprocess
  5. import threading
  6. import evdev
  7. import os
  8.  
  9. class Keys:
  10.     KEY_MAXI = 44
  11.     KEY_CLOSE = 2
  12.  
  13.     KEY_1 = 13
  14.     KEY_2 = 14
  15.     KEY_3 = 15
  16.     KEY_4 = 16
  17.     KEY_5 = 17
  18.     KEY_6 = 18
  19.     KEY_7 = 19
  20.     KEY_8 = 20
  21.     KEY_9 = 21
  22.  
  23.     KEY_BACK = 22
  24.     KEY_0 = 23
  25.     KEY_ENTER = 24
  26.    
  27.     KEY_VOL_UP = 9
  28.     KEY_VOL_DOWN = 8
  29.     KEY_MUTE = 10
  30.     KEY_CH_UP = 11
  31.     KEY_CH_DOWN = 12
  32.  
  33.     KEY_INFO = 46
  34.     KEY_VENDOR = 0
  35.     KEY_OPTION = 47
  36.    
  37.     KEY_UP = 26
  38.     KEY_LEFT = 29
  39.     KEY_OK = 30
  40.     KEY_RIGHT = 31
  41.     KEY_DOWN = 34
  42.  
  43.     KEY_MENU = 28
  44.     KEY_EXIT = 32
  45.    
  46.     KEY_REW = 36
  47.     KEY_PLAY = 37
  48.     KEY_FWD = 38
  49.     KEY_REC = 39
  50.     KEY_STOP = 40
  51.     KEY_PAUSE = 41
  52.     KEY_NEXT = 42    
  53.     KEY_PREV = 43
  54.  
  55.     KEY_TV = 3
  56.     KEY_DVD = 4
  57.     KEY_PHOTOS = 5
  58.     KEY_MUSIC = 6
  59.     KEY_VIDEO = 7
  60.    
  61.     KEY_HELP = 1
  62.     KEY_MOUSE = 45
  63.  
  64.     KEY_A = 25
  65.     KEY_B = 27
  66.     KEY_C = 33
  67.     KEY_D = 35
  68.    
  69. # The map of files to play when something is pressed
  70. map = {}
  71. map[Keys.KEY_OK] = '/home/pi/torture.mp3'
  72. map[Keys.KEY_LEFT] = '/home/pi/torture.mp3'
  73. map[Keys.KEY_RIGHT] = '/home/pi/torture.mp3'
  74. map[Keys.KEY_UP] = '/home/pi/torture.mp3'
  75. map[Keys.KEY_DOWN] = '/home/pi/torture.mp3'
  76.  
  77. FNULL = open(os.devnull, 'w')
  78.  
  79. def play(path):
  80.     # -o local causes the output to play through the audio jack (to pipe to the amp) rather than the HDMI cable
  81.     subprocess.Popen(['omxplayer', '-o', 'local', '--no-osd', '--no-keys', path], stdout=FNULL, stderr=subprocess.STDOUT)
  82.  
  83. # The raspberry pi has an issue with playing sounds described here:
  84. # http://raspberrypi.stackexchange.com/a/3633
  85. # A workaround is to have an audio file playing in a loop. Here we play silence repeatedly
  86. # Not sure how to end this, though. i.e. when terminating the script we should end the looped playback.
  87. def loop_silence():
  88.     # no-keys prevents interpretation of keys as commands to modify volume and such
  89.     # no-osd prevents the on screed display showing seeking to the beginning of the file as it looops
  90.     # loop causes the audio to loop forever
  91.     subprocess.Popen(['omxplayer', '--loop', '--no-osd', '--no-keys', 'silence.wav'], stdout=FNULL, stderr=subprocess.STDOUT)
  92.  
  93. # Main worker loop
  94. t = threading.Thread(target=loop_silence)
  95. t.start()
  96. devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
  97. firefly = None
  98. for device in devices:
  99.     if device.name == "X10 WTI RF receiver":
  100.         firefly = device
  101.         break
  102.  
  103. if not firefly:
  104.     sys.exit()
  105.  
  106. for event in firefly.read_loop():
  107.     if event.type == evdev.ecodes.EV_MSC:
  108.         press = event.value
  109.         if press == 2:
  110.             print("You pressed <close>, exiting...")
  111.             break
  112.  
  113.         if press in map:
  114.             print("Key " + str(press) + ": Playing " + map[press] + " ...")
  115.             play(map[press])
  116.         else:
  117.             print("Nothing to do for [" + str(press) + "]")
Add Comment
Please, Sign In to add comment