-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
77 lines (57 loc) · 1.82 KB
/
app.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
67
68
69
70
71
72
73
74
75
76
77
import time
from pathlib import Path
from shlex import split
from subprocess import run
from sys import exit
from requests import get
from RPi import GPIO as GPIO
NUM_PIN = 37
GPIO.setmode(GPIO.BOARD)
GPIO.setup(NUM_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
DEFAULT_PORT = 5000
TICKER_HOST = "ticker"
SCREEN1_HOST = "mbp-van-lotte"
SCREEN2_HOST = "ilinx"
counter = 0
COUNTER_GOAL = 30
SLEEP_FOR_SCREEN_ANIM = 320
TIP_TINY = Path("assets/sounds/tip_tiny.mp3").absolute()
TIP_GOAL = Path("assets/sounds/tip_medium.mp3").absolute()
def play_sound(fpath):
run(split(f"play {fpath}"))
def fire_request(url):
try:
get(url)
return True
except Exception:
print(f"Failed to request {url}...")
return False
def go_test():
for host in [TICKER_HOST, SCREEN1_HOST, SCREEN2_HOST]:
success = fire_request(f"http://{host}:{DEFAULT_PORT}/ok")
if not success:
print(f"Unable to contact the '{host}' host, bailing out...")
exit(1)
print("Running start-up tests...")
go_test()
print("Success!")
try:
print("Running GPIO read loop...")
while 1:
if GPIO.input(NUM_PIN) == 1:
print("Coin detected...")
play_sound(TIP_TINY)
fire_request(f"http://{SCREEN1_HOST}:{DEFAULT_PORT}")
fire_request(f"http://{SCREEN2_HOST}:{DEFAULT_PORT}")
counter += 1
if counter == COUNTER_GOAL:
print("Goal reached...")
play_sound(TIP_GOAL)
fire_request(f"http://{TICKER_HOST}:{DEFAULT_PORT}")
counter = 0
print(f"Going to sleep for {SLEEP_FOR_SCREEN_ANIM} seconds...")
time.sleep(SLEEP_FOR_SCREEN_ANIM)
print("Waking up again...")
time.sleep(0.1)
except KeyboardInterrupt:
GPIO.cleanup()