-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.py
97 lines (72 loc) · 2.44 KB
/
controller.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
import subprocess
from signal import pause
from gpiozero import Button, LED
from time import sleep, time
import RPi.GPIO as GPIO
import threading
import os
START_BUTTON_PIN = 17
STOP_BUTTON_PIN = 27
REDEEM_BUTTON_PIN = 22
START_LED_PIN = 6
COFFEE_AVAILABLE_LED_PIN = 13
start_button = None
stop_button = None
redeem_button = None
def start():
print("Starting stream")
GPIO.output(START_LED_PIN, True)
subprocess.Popen("/home/pi/Documents/stream.sh")
def stop():
print("Stopping stream")
GPIO.output(START_LED_PIN, False)
subprocess.Popen("/home/pi/Documents/stop_stream.sh")
def redeem():
print("Redeeming cofee")
redeem_file = open("/home/pi/Documents/redemptions2.txt", "r")
redemptions = redeem_file.readline()
redemptions = int(redemptions) + 1
redeem_file.close()
redeem_file2 = open("redemptions2.txt", "w")
redeem_file2.write(str(redemptions))
redeem_file2.close()
redeem_file_tmp = open("redemptions.txt.tmp", "w")
redeem_file_tmp.write(str(redemptions))
redeem_file_tmp.close()
os.rename('redemptions.txt.tmp', 'redemptions.txt')
def coffee_available():
threading.Timer(5.0, coffee_available).start()
GPIO.setup(COFFEE_AVAILABLE_LED_PIN, GPIO.OUT)
redeem_file = open("/home/pi/Documents/redemptions2.txt", "r")
redemptions = int(redeem_file.readline())
redeem_file.close()
coffee_file = open("/home/pi/Documents/coffees_donated2.txt", "r")
coffees = int(coffee_file.readline())
coffee_file.close()
if coffees > redemptions:
GPIO.output(COFFEE_AVAILABLE_LED_PIN, True)
else:
GPIO.output(COFFEE_AVAILABLE_LED_PIN, False)
def check_wallet():
threading.Timer(30.0, check_wallet).start()
subprocess.Popen("/home/pi/Documents/query_wallet.sh")
def setup_buttons():
global start_button, stop_button, redeem_button
print("Initializing hardware ...")
start_button = Button(START_BUTTON_PIN)
start_button.when_pressed = start
stop_button = Button(STOP_BUTTON_PIN)
stop_button.when_pressed = stop
redeem_button = Button(REDEEM_BUTTON_PIN)
redeem_button.when_pressed = redeem
GPIO.setup(START_LED_PIN, GPIO.OUT)
GPIO.output(START_LED_PIN, False)
GPIO.setup(COFFEE_AVAILABLE_LED_PIN, GPIO.OUT)
GPIO.output(COFFEE_AVAILABLE_LED_PIN, False)
check_wallet()
coffee_available()
print("Ready")
if __name__ == '__main__':
setup_buttons()
pause()