25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

317 satır
7.0 KiB

  1. import subprocess
  2. import sys, inspect, os
  3. import wiringpi
  4. import time
  5. from time import sleep
  6. import lirc
  7. import mpd
  8. from pyudmx import pyudmx
  9. import talkey
  10. # GPIO
  11. pin_kugel = 2
  12. pin_sun = 4
  13. pin_pir = 0
  14. dmxScenes = {
  15. "fadecolors":[255,255,255,255,255,192],
  16. "plain-red":[255,255,0,0,0,0],
  17. "strobe":[190,255,255,255,0,0],
  18. "nini":[120,0,255,255,0,224],
  19. "black":[0,0,0,0,0,0]
  20. }
  21. dmxUserScenes = [
  22. [255,255,255,255,255,192],
  23. [255,0,180,180,0,0],
  24. [255,255,0,0,0,0],
  25. [255,0,255,0,0,0],
  26. [255,0,0,255,0,0],
  27. [190,255,255,255,0,0],
  28. [120,0,255,255,0,224]
  29. ]
  30. def setDmxScene(scene):
  31. cv = [0 for v in range(0, 512)]
  32. errorcode = [240,255,0,0,0,0]
  33. for index, val in enumerate(dmxScenes.get(scene,errorcode)):
  34. cv[index] = val
  35. dev.send_multi_value(1, cv)
  36. def setUserDmxScene():
  37. global dmxScene
  38. if dmxScene < len(dmxUserScenes)-1:
  39. dmxScene += 1
  40. else:
  41. dmxScene = 0
  42. cv = [0 for v in range(0, 512)]
  43. for index, val in enumerate(dmxUserScenes[dmxScene]):
  44. cv[index] = val
  45. dev.send_multi_value(1, cv)
  46. def setKugel(state):
  47. if state == 'on':
  48. wiringpi.digitalWrite(pin_kugel, 0)
  49. if state == 'off':
  50. wiringpi.digitalWrite(pin_kugel, 1)
  51. def setSun(state):
  52. if state == 'off':
  53. wiringpi.digitalWrite(pin_sun, 0)
  54. if state == 'on':
  55. wiringpi.digitalWrite(pin_sun, 1)
  56. def startMusic(playlist, single=False, shuffle=True, repeat=True):
  57. try:
  58. client.clear() # clear playlist
  59. except Exception:
  60. client.connect("localhost", 6600)
  61. client.clear() # clear playlist
  62. client.add(playlist) # add file/directory to playlist
  63. if shuffle:
  64. client.shuffle() # shuffle playlist
  65. if repeat:
  66. client.repeat(1) # set playback mode repeat
  67. else:
  68. client.repeat(0) # set playback mode repeat
  69. if single:
  70. client.repeat(0) # set playback mode repeat
  71. client.single(1) # set playback mode single
  72. else:
  73. client.single(0) # set playback mode single
  74. client.setvol(80)# set volume
  75. client.play() # play
  76. def stopMusic():
  77. try:
  78. client.stop()
  79. except Exception:
  80. client.connect("localhost", 6600)
  81. client.stop()
  82. def muteMusic():
  83. global volume
  84. global isMuted
  85. try:
  86. currentvol = int(client.status()['volume'])
  87. except Exception:
  88. client.connect("localhost", 6600)
  89. currentvol = int(client.status()['volume'])
  90. if currentvol != 0:
  91. volume = currentvol
  92. if isMuted:
  93. try:
  94. client.setvol(volume)
  95. except Exception:
  96. client.connect("localhost", 6600)
  97. client.setvol(volume)
  98. else:
  99. try:
  100. client.setvol(0)
  101. except Exception:
  102. client.connect("localhost", 6600)
  103. client.setvol(0)
  104. isMuted = not isMuted
  105. def changeVolume(change=5):
  106. global volume
  107. global isMuted
  108. isMuted = False
  109. newvol = volume + change
  110. if newvol > 100:
  111. newvol = 100
  112. if newvol < 0:
  113. newvol = 0
  114. try:
  115. client.setvol(newvol)
  116. except Exception:
  117. client.connect("localhost", 6600)
  118. client.setvol(newvol)
  119. volume = newvol
  120. def setMode(string):
  121. global mode
  122. mode = string
  123. def setDiscoMode():
  124. setKugel('on')
  125. setDmxScene('fadecolors')
  126. setUserDmxScene()
  127. sleep(0.3)
  128. setSun('off')
  129. setMode('disco')
  130. def getTrackInfo():
  131. try:
  132. currentsong = client.currentsong()
  133. except Exception:
  134. client.connect("localhost", 6600)
  135. currentsong = client.currentsong()
  136. print(currentsong)
  137. global volume
  138. try:
  139. currentvol = int(client.status()['volume'])
  140. except Exception:
  141. client.connect("localhost", 6600)
  142. currentvol = int(client.status()['volume'])
  143. changeVolume(-30)
  144. try:
  145. tts.say(currentsong['artist'] + ', ' + currentsong['title'])
  146. except Exception:
  147. tts.say('Willkommen am Discoklo!')
  148. client.setvol(currentvol)
  149. volume = currentvol
  150. def setWorkingMode():
  151. setSun('on')
  152. sleep(0.3)
  153. setKugel('off')
  154. setDmxScene('black')
  155. setMode('work')
  156. def closeService():
  157. setSun('off')
  158. sleep(0.3)
  159. setKugel('off')
  160. setDmxScene('black')
  161. stopMusic()
  162. setMode('off')
  163. def initService():
  164. startMusic('0', True)
  165. setDiscoMode()
  166. def setOnOff():
  167. global mode
  168. stopMusic()
  169. if mode is not 'work':
  170. setWorkingMode()
  171. else:
  172. initService()
  173. def bootstrap():
  174. wiringpi.wiringPiSetup()
  175. wiringpi.pinMode(pin_kugel, 1) # set Relay Disokugel mode to OUTPUT
  176. wiringpi.pinMode(pin_sun, 1) # set Relay Sun mode to OUTPUT # TODO: Set pin!
  177. wiringpi.pinMode(pin_pir, 0) # set PIR Sensor mode to INPUT
  178. dmxScene = 0
  179. bootstrap()
  180. dev = pyudmx.uDMXDevice()
  181. dev.open()
  182. client = mpd.MPDClient()
  183. client.connect("localhost", 6600)
  184. tts = talkey.Talkey(
  185. preferred_languages=['en', 'de'],
  186. engine_preference=['pico'],
  187. )
  188. pirstate = wiringpi.digitalRead(6)
  189. lirc.init("disco", "~/discobert/lircrc", blocking=False)
  190. lastUsed = time.time()
  191. inUse = True
  192. mode = "none"
  193. inUseBefore = True
  194. timeout = 2 * 60
  195. volume = 80
  196. isMuted = False
  197. initService()
  198. # Main event loop ...
  199. while True:
  200. sleep(0.2)
  201. pirstate = wiringpi.digitalRead(pin_pir)
  202. #print('pirstate: ', pirstate)
  203. if pirstate == 1:
  204. lastUsed = time.time()
  205. inUse = True
  206. else:
  207. if(time.time() > lastUsed + timeout):
  208. inUse = False
  209. remotesignal = lirc.nextcode()
  210. if remotesignal:
  211. lastUsed = time.time() # user is active!
  212. inUse = True
  213. for code in remotesignal:
  214. print('received code:', code)
  215. if(code == "mode_disco"):
  216. setDiscoMode()
  217. if(code == "mode_work"):
  218. setWorkingMode()
  219. if(code == "mode_power"):
  220. setOnOff()
  221. #if(code == "mode_dmx_next"):
  222. if(code == "mode_music_play_1"):
  223. startMusic('1')
  224. if(code == "mode_music_play_2"):
  225. startMusic('2')
  226. if(code == "mode_music_play_3"):
  227. startMusic('3')
  228. if(code == "mode_music_play_4"):
  229. startMusic('4')
  230. if(code == "mode_music_play_5"):
  231. startMusic('5')
  232. if(code == "mode_play_fm4"):
  233. startMusic('http://185.85.29.141:8000')
  234. if(code == "mode_play_oe1"):
  235. startMusic('http://185.85.29.142:8000')
  236. if(code == "mode_music_stop"):
  237. stopMusic()
  238. if(code == "mode_music_mute"):
  239. muteMusic()
  240. if(code == "mode_volume_up"):
  241. changeVolume(5)
  242. if(code == "mode_volume_down"):
  243. changeVolume(-5)
  244. if(code == "mode_music_info"):
  245. getTrackInfo()
  246. if(inUseBefore != inUse):
  247. print("State change inUse:", inUseBefore, inUse)
  248. if inUse:
  249. initService()
  250. else:
  251. closeService()
  252. inUseBefore = inUse
  253. lirc.deinit() # Clean up lirc
  254. dev.close()