Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

530 linhas
13 KiB

  1. import subprocess
  2. import sys, inspect, os
  3. import wiringpi
  4. import time
  5. from datetime import datetime
  6. from time import sleep
  7. import lirc
  8. import random
  9. import mpd
  10. from pyudmx import pyudmx
  11. import talkey
  12. # HTTP Server
  13. from twisted.web import server, resource
  14. from twisted.internet import reactor
  15. class Simple(resource.Resource):
  16. isLeaf = True
  17. def render_GET(self, request):
  18. statustext = ""
  19. if inUse:
  20. statustext = "Disco in use!"
  21. else:
  22. statustext = "Ready to Disco!"
  23. html = '<html><body style="min-height:100vh;display:flex;flex-grow:1;align-items:center;justify-content:center;"><div style="display:flex;font-size:10vw">%s</div></body></html>' % statustext
  24. return html.encode('utf-8')
  25. # GPIO
  26. pin_kugel = 2
  27. pin_sun = 4
  28. pin_pir = 0
  29. pin_door = 11
  30. # for system stuff
  31. dmxScenes = {
  32. "fadecolors":[255,255,255,255,255,192],
  33. "plain-red":[255,255,0,0,0,0],
  34. "strobe":[190,255,255,255,0,0],
  35. "nini":[120,0,255,255,0,224],
  36. "black":[0,0,0,0,0,0]
  37. }
  38. bye_sayings = [
  39. "Goodbye!",
  40. "Bye!",
  41. "Bye bye!",
  42. "See you!",
  43. #"Ciao!",
  44. "Have a nice day!",
  45. "Thank's for using!",
  46. #"I'm off!",
  47. "Take it easy!",
  48. "I look forward to our next meeting!",
  49. "Take care!",
  50. "See you later!",
  51. "This was nice. See you!",
  52. "Peace!"
  53. ]
  54. dmxUserScenes = [
  55. [255,255,255,255,255,192],
  56. [255,0,180,180,0,0],
  57. [255,255,0,0,0,0],
  58. [255,0,255,0,0,0],
  59. [255,0,0,255,0,0],
  60. [190,255,255,255,0,0],
  61. [120,0,255,255,0,224]
  62. ]
  63. # no strobe etc.
  64. dmxStartupScenes = [
  65. [255,255,255,255,255,192],
  66. [255,0,180,180,0,0],
  67. [255,255,0,0,0,0],
  68. [255,0,255,0,0,0],
  69. [255,0,0,255,0,0],
  70. [120,0,255,255,0,224]
  71. ]
  72. # Set a dmx scene by name
  73. def setDmxScene(scene):
  74. # a universe of zeros
  75. cv = [0 for v in range(0, 512)]
  76. errorcode = [240,255,0,0,0,0]
  77. for index, val in enumerate(dmxScenes.get(scene,errorcode)):
  78. cv[index] = val
  79. dev.send_multi_value(1, cv)
  80. # Set random startup dmx scene
  81. def setStartupDmxScene():
  82. # a universe of zeros
  83. cv = [0 for v in range(0, 512)]
  84. errorcode = [240,255,0,0,0,0]
  85. scene = random.choice(dmxStartupScenes)
  86. for index, val in enumerate(dmxUserScenes[scene]):
  87. cv[index] = val
  88. dev.send_multi_value(1, cv)
  89. # Switch betweeb user dmx scenes
  90. def setUserDmxScene():
  91. # loop through scenes
  92. global dmxScene
  93. if dmxScene < len(dmxUserScenes)-1:
  94. dmxScene += 1
  95. else:
  96. dmxScene = 0
  97. # setup the universe
  98. cv = [0 for v in range(0, 512)]
  99. for index, val in enumerate(dmxUserScenes[dmxScene]):
  100. cv[index] = val
  101. dev.send_multi_value(1, cv)
  102. def setKugel(state):
  103. if state == 'on':
  104. wiringpi.digitalWrite(pin_kugel, 0)
  105. if state == 'off':
  106. wiringpi.digitalWrite(pin_kugel, 1)
  107. def setSun(state):
  108. if state == 'off':
  109. wiringpi.digitalWrite(pin_sun, 0)
  110. if state == 'on':
  111. wiringpi.digitalWrite(pin_sun, 1)
  112. def startMusic(playlist, single=False, shuffle=True, repeat=True):
  113. try:
  114. client.clear() # clear playlist
  115. except Exception:
  116. client.connect("localhost", 6600)
  117. client.clear() # clear playlist
  118. client.add(playlist) # add file/directory to playlist
  119. if shuffle:
  120. client.shuffle() # shuffle playlist
  121. if repeat:
  122. client.repeat(1) # set playback mode repeat
  123. else:
  124. client.repeat(0) # set playback mode repeat
  125. if single:
  126. client.repeat(0) # set playback mode repeat
  127. client.single(1) # set playback mode single
  128. else:
  129. client.single(0) # set playback mode single
  130. client.setvol(80)# set volume
  131. client.play() # play
  132. def playMusic():
  133. try:
  134. client.play()
  135. except Exception:
  136. client.connect("localhost", 6600)
  137. client.play()
  138. def pauseMusic():
  139. try:
  140. client.pause()
  141. except Exception:
  142. client.connect("localhost", 6600)
  143. client.pause()
  144. def stopMusic():
  145. try:
  146. client.stop()
  147. except Exception:
  148. client.connect("localhost", 6600)
  149. client.stop()
  150. def nextSong():
  151. try:
  152. client.next()
  153. except Exception:
  154. client.connect("localhost", 6600)
  155. client.next()
  156. def previousSong():
  157. try:
  158. client.previous()
  159. except Exception:
  160. client.connect("localhost", 6600)
  161. client.previous()
  162. def muteMusic():
  163. global uservolume
  164. if getMpdVolume() != 0: # if not muted
  165. setMpdVolume(0)
  166. else:
  167. setMpdVolume(uservolume)
  168. def changeVolume(change=5):
  169. global uservolume
  170. global volume
  171. newvol = uservolume + change
  172. if newvol > 100:
  173. newvol = 100
  174. if newvol < 0:
  175. newvol = 0
  176. try:
  177. client.setvol(newvol)
  178. except Exception:
  179. client.connect("localhost", 6600)
  180. client.setvol(newvol)
  181. uservolume = newvol
  182. volume = newvol
  183. def setMode(string):
  184. global mode
  185. global inUse
  186. mode = string
  187. if mode == "off":
  188. inUse = False
  189. def setDiscoMode(startup=False):
  190. setKugel('on')
  191. if startup:
  192. setStartupDmxScene()
  193. else:
  194. setUserDmxScene()
  195. sleep(0.3)
  196. setSun('off')
  197. setMode('disco')
  198. def getMpdVolume():
  199. try:
  200. vol = int(client.status()['volume'])
  201. except Exception:
  202. client.connect("localhost", 6600)
  203. vol = int(client.status()['volume'])
  204. if vol != 0: #only if not muted
  205. global uservolume
  206. uservolume = vol
  207. return vol
  208. def setMpdVolume(vol):
  209. try:
  210. client.setvol(vol)
  211. except Exception:
  212. client.connect("localhost", 6600)
  213. client.setvol(vol)
  214. if vol != 0: #only if not muted
  215. global uservolume
  216. uservolume = vol
  217. return True
  218. def seek(secs):
  219. try:
  220. client.seekcur(secs)
  221. except Exception:
  222. client.connect("localhost", 6600)
  223. client.seekcur(secs)
  224. return True
  225. def getTrackInfo():
  226. try:
  227. currentsong = client.currentsong()
  228. except Exception:
  229. client.connect("localhost", 6600)
  230. currentsong = client.currentsong()
  231. print(currentsong)
  232. volume = getMpdVolume()
  233. setMpdVolume(10)
  234. try:
  235. tts.say(currentsong['artist'] + ', ' + currentsong['title'])
  236. except Exception:
  237. tts.say('Willkommen am Discoklo!', 'de')
  238. setMpdVolume(volume)
  239. def tour():
  240. tts.say("Hello, I'm Discobert!", "en")
  241. sleep(0.3)
  242. tts.say("Press 1 for nice electronic music", "en")
  243. sleep(0.3)
  244. tts.say("Press 2 for hard electronic music", "en")
  245. sleep(0.3)
  246. tts.say("Press 3 for HGichT", "en")
  247. sleep(0.3)
  248. tts.say("Press 4 for a good laugh", "en")
  249. sleep(0.3)
  250. tts.say("Press 5 for more music", "en")
  251. sleep(0.3)
  252. def setWorkingMode():
  253. setSun('on')
  254. sleep(0.3)
  255. setKugel('off')
  256. setDmxScene('black')
  257. setMode('work')
  258. def startTimeoutCountdown():
  259. global lastUsed
  260. global inUse
  261. tts.say('Timeout in')
  262. countdown = [5,4,3,2,1] #10,9,8,7,6,
  263. for sec in countdown:
  264. tts.say(str(sec))
  265. sleep(0.5)
  266. remotesignal = lirc.nextcode()
  267. if wiringpi.digitalRead(pin_pir) == 1 or remotesignal:
  268. lastUsed = time.time()
  269. tts.say('Timeout cancelled!')
  270. inUse = True
  271. toilet = lirc.nextcode()
  272. break
  273. if not inUse:
  274. tts.say('Shutting down now.', 'en')
  275. closeService()
  276. def closeService(sleepsecs=0):
  277. setSun('off')
  278. sleep(0.3)
  279. setKugel('off')
  280. setDmxScene('black')
  281. for x in range(0, 20):
  282. changeVolume(-5)
  283. sleep(0.1)
  284. stopMusic()
  285. inUseBefore = False # Pfusch pfusch!
  286. setMode('off')
  287. sleep(sleepsecs)
  288. def initService():
  289. startMusic('0', True) # start intro music
  290. setDiscoMode(True)
  291. global volume
  292. global defaultvolume
  293. global uservolume
  294. try:
  295. client.setvol(defaultvolume)
  296. except Exception:
  297. client.connect("localhost", 6600)
  298. client.setvol(defaultvolume)
  299. volume = defaultvolume
  300. uservolume = defaultvolume
  301. global starttime
  302. starttime = time.time()
  303. def say(text, lang="en"):
  304. originalvol = getMpdVolume()
  305. setMpdVolume(10)
  306. tts.say(text, lang)
  307. setMpdVolume(originalvol)
  308. def setOnOff():
  309. global mode
  310. stopMusic()
  311. if mode is not 'work':
  312. setWorkingMode()
  313. else:
  314. initService()
  315. def doorShutdown():
  316. tts.say(random.choice(bye_sayings), "en")
  317. # sleep to give user some time to close the door
  318. # (pir sensor also stays up for 2 sec)
  319. closeService(5)
  320. def bootstrap():
  321. wiringpi.wiringPiSetup()
  322. wiringpi.pinMode(pin_kugel, 1) # set Relay Disokugel mode to OUTPUT
  323. wiringpi.pinMode(pin_door, 0) # set Circuit Door mode to INPUT
  324. wiringpi.pinMode(pin_sun, 1) # set Relay Sun mode to OUTPUT # TODO: Set pin!
  325. wiringpi.pinMode(pin_pir, 0) # set PIR Sensor mode to INPUT
  326. def timestamp(stamp=time.time()):
  327. return datetime.fromtimestamp(stamp).strftime('%Y-%m-%d %H:%M:%S')
  328. dmxScene = 0
  329. bootstrap()
  330. dev = pyudmx.uDMXDevice()
  331. dev.open()
  332. client = mpd.MPDClient()
  333. client.connect("localhost", 6600)
  334. tts = talkey.Talkey(
  335. preferred_languages=['en'],
  336. engine_preference=['pico'],
  337. )
  338. site = server.Site(Simple())
  339. reactor.listenTCP(8080, site)
  340. reactor.startRunning(False)
  341. lirc.init("disco", "~/discobert/lircrc", blocking=False)
  342. starttime = time.time() # helper for doorshutdown
  343. lastUsed = time.time() # helper for timeout
  344. inUse = False # is toilet in use?
  345. inUseBefore = False # helper to check statechanges
  346. mode = "off" # can be: disco, work, off
  347. timeout = 2 * 60 - 10 # timeout since last user interaction
  348. defaultvolume = 90 # Volume when user enters the toilet
  349. volume = 90 # Global for actual volume
  350. uservolume = 90 # Global for user volume (ignores mute state)
  351. setSun('off')
  352. print(timestamp(), "Ready!")
  353. # Main event loop ...
  354. while True:
  355. sleep(0.25)
  356. pirstate = wiringpi.digitalRead(pin_pir)
  357. doorstate = wiringpi.digitalRead(pin_door)
  358. #print('pirstate: ', pirstate)
  359. # 0 => door closed
  360. # 1 => door open
  361. if doorstate == 1:
  362. if (time.time() > (starttime + 15) and inUse == True):
  363. doorShutdown()
  364. if pirstate == 1:
  365. lastUsed = time.time()
  366. inUse = True
  367. else:
  368. if(time.time() > lastUsed + timeout):
  369. inUse = False
  370. remotesignal = lirc.nextcode()
  371. if remotesignal:
  372. lastUsed = time.time() # user is active!
  373. inUse = True
  374. for code in remotesignal:
  375. print('received code:', code)
  376. if(code == "mode_disco"):
  377. setDiscoMode()
  378. if(code == "mode_work"):
  379. setWorkingMode()
  380. if(code == "mode_power"):
  381. setOnOff()
  382. #if(code == "mode_dmx_next"):
  383. if(code == "mode_music_play_1"):
  384. startMusic('1')
  385. if(code == "mode_music_play_2"):
  386. startMusic('2')
  387. if(code == "mode_music_play_3"):
  388. startMusic('3')
  389. if(code == "mode_music_play_4"):
  390. startMusic('4')
  391. if(code == "mode_music_play_5"):
  392. startMusic('5')
  393. if(code == "mode_play_fm4"):
  394. startMusic('http://185.85.29.141:8000')
  395. if(code == "mode_play_oe1"):
  396. startMusic('http://185.85.29.142:8000')
  397. if(code == "mode_music_previous"):
  398. previousSong()
  399. if(code == "mode_music_next"):
  400. nextSong()
  401. if(code == "mode_music_play"):
  402. playMusic()
  403. if(code == "mode_music_stop"):
  404. stopMusic()
  405. if(code == "mode_music_mute"):
  406. muteMusic()
  407. if(code == "mode_music_pause"):
  408. pauseMusic()
  409. if(code == "mode_music_rewind"):
  410. seek(-30)
  411. if(code == "mode_music_back"):
  412. seek(-5)
  413. if(code == "mode_music_forward"):
  414. seek(5)
  415. if(code == "mode_music_fastforward"):
  416. seek(30)
  417. if(code == "mode_volume_up"):
  418. changeVolume(5)
  419. if(code == "mode_volume_down"):
  420. changeVolume(-5)
  421. if(code == "mode_music_info"):
  422. getTrackInfo()
  423. if(code == "mode_record"):
  424. say("I'm sorry, I'm afraid I can't do that!")
  425. if(code == "mode_help"):
  426. tour()
  427. if(inUseBefore != inUse):
  428. print(timestamp(), "State change inUse:", inUseBefore, inUse)
  429. if inUse:
  430. initService()
  431. else:
  432. startTimeoutCountdown()
  433. inUseBefore = inUse
  434. # Webserver
  435. reactor.iterate()
  436. lirc.deinit() # Clean up lirc
  437. dev.close()