-
Notifications
You must be signed in to change notification settings - Fork 0
/
buzzer.py
66 lines (58 loc) · 2.22 KB
/
buzzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python3
from aiy.board import Board, Led
#from aiy.voice.audio import aplay
from signal import signal, SIGINT
from pynput.keyboard import Key, Controller
import sys
import pychrome
import subprocess
import io
keyboard = Controller()
url = "https://buzzin.live/play"
browserbin = "chromium-browser"
def run_js(tab, code):
return tab.call_method("Runtime.evaluate", expression=code)
def stop_browser():
print("Closing the browser...")
subprocess.Popen(['pkill', '--oldest', 'chromium*'], stderr=subprocess.STDOUT)
def main():
print("Trivia Buzzer")
# stop_browser() # close Chromium if it's already open
print("Opening Buzzin.live...")
cp = subprocess.Popen([browserbin, '--remote-debugging-port=9222', '--app=' + url], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in io.TextIOWrapper(cp.stdout, encoding="utf-8"): # wait for Chromium to open
if ("DevTools listening on ws://127.0.0.1" in line):
break
print("Browser ready")
browser = pychrome.Browser(url="http://127.0.0.1:9222")
tab = browser.list_tab()[0]
tab.start()
subprocess.Popen(["mplayer", "start.mp3"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
with Board() as board:
print('Press the button when you have joined the game')
print('LED is ON while button is pressed (Ctrl-C for exit).')
board.button.wait_for_press()
run_js(tab, "document.getElementById('mute').checked = true") # turn buzzin sound on
board.led.state = Led.PULSE_SLOW
while True:
board.button.wait_for_press()
board.led.state = Led.ON
print('Pressed')
keyboard.press('b') # alternative: run_js(tab, 'document.getElementById("buzzer").click()')
#board.led.state = Led.DECAY
board.button.wait_for_release()
keyboard.release('b')
board.led.state = Led.PULSE_SLOW
print('Released')
def stop_program():
print('stopping program...')
stop_browser()
with Board() as board:
board.led.state = Led.OFF
sys.exit(0)
if __name__ == '__main__':
try:
main()
except (KeyboardInterrupt, Exception) as err:
print(err)
stop_program()