-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
291 lines (254 loc) · 7.45 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import sys, os
sys.path.append(os.getcwd())
import machine_email as email
from settings import *
import transactions
import animatedGIF as ag
import time
from threading import Thread
from PIL import Image, ImageDraw, ImageFont
import io
INSTRUCTIONS_TIME = 5
NUM_PIXELS = 16
FPS = 7
FRAMETIME = 1 / FPS
idle = True # volatile
image = None
draw = None
FONTSIZE = 20
valve = None
emulate = False
font1 = ImageFont.truetype("fonts/Daydream.ttf", FONTSIZE-8)
font2 = ImageFont.truetype("fonts/Mario-Kart-DS.ttf", FONTSIZE)
font3 = ImageFont.truetype("fonts/Stencilia-A.ttf", FONTSIZE + 10)
settings = Settings()
PRICE = settings.price
VALVE_OPEN_TIME = settings.valve_open_time
VENMO_USER = settings.venmo_user
def is_raspberrypi():
try:
with io.open('/sys/firmware/devicetree/base/model', 'r') as m:
if 'raspberry pi' in m.read().lower(): return True
except Exception: pass
return False
def open_valve():
print("Valve open")
global valve
if valve is not None:
valve.value = True
return
def close_valve():
print("Valve closed")
global valve
if valve is not None:
valve.value = False
return
if is_raspberrypi():
print("Initializing Raspberry Pi")
import board
import neopixel
import digitalio
from adafruit_rgb_display.rgb import color565
from adafruit_rgb_display import st7789
pixels = neopixel.NeoPixel(board.D21, NUM_PIXELS, brightness = settings.led_brightness, auto_write = False)
# Configuration for CS and DC pins for Raspberry Pi
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = None
BAUDRATE = 64000000 # The pi can be very fast!
# Create the ST7789 display:
display = st7789.ST7789(
board.SPI(),
cs=cs_pin,
dc=dc_pin,
rst=reset_pin,
baudrate=BAUDRATE,
width=135,
height=240,
x_offset=53,
y_offset=40,
)
backlight = digitalio.DigitalInOut(board.D22)
backlight.switch_to_output()
backlight.value = True
buttonA = digitalio.DigitalInOut(board.D23)
buttonB = digitalio.DigitalInOut(board.D24)
buttonA.switch_to_input()
buttonB.switch_to_input()
valve = digitalio.DigitalInOut(board.D20)
valve.switch_to_output()
close_valve()
rotation = 90
WIDTH = display.height
HEIGHT = display.width
else:
print("Initializing Emulation")
import neopixelEmulator as ne
pixels = ne.Neopixel_Emulator(NUM_PIXELS)
import displayEmulator as de
display = de.DisplayEmulator()
display.begin()
pixels.begin()
emulate = True
rotation = 0
WIDTH = display.width
HEIGHT = display.height
logo = ag.AnimatedGif(display, rotation)
def show_purchase(venmo):
draw.rectangle((0, 0, WIDTH, HEIGHT), outline=0, fill=(0, 0, 0))
y = 2
x = 2
msg = '@'+venmo["Actor"]
draw.text((x, y), "paid", font=font2, fill="#15a815")
bbox = font1.getbbox(msg)
x = WIDTH - bbox[2] - 2
draw.text((x, y), msg, font=font1, fill="#00FF00")
y += bbox[3] + 4
x = 2
msg = '${:,.2f}'.format(venmo["Amount"])
draw.text((x, y), "amount", font=font2, fill="#1586a8")
bbox = font1.getbbox(msg)
x = WIDTH - bbox[2] - 2
draw.text((x, y), msg, font=font1, fill="#05c2fc")
y += bbox[3] + 4
x = 2
draw.text((x, y), "LET THE", font=font2, fill="#914401")
y += FONTSIZE + 4
draw.text((x, y), "ROOT BEER FLOW!", font=font2, fill="#914401")
y += FONTSIZE + 4
draw.text((x, y), "make sure to close", font=font2, fill="#3802fc")
y += FONTSIZE + 4
draw.text((x, y), "the tap completely!", font=font2, fill="#FF00FF")
display.image(image, rotation)
def show_instructions():
draw.rectangle((0, 0, WIDTH, HEIGHT), outline=0, fill=(0, 0, 0))
y = 2
x = 2
msg = 'Send me ${:,.2f}'.format(PRICE)
bbox = font1.getbbox(msg)
draw.text((x, y), msg, font=font3, fill="#00FF00")
y += FONTSIZE * 2 + 4
x = 2
msg = VENMO_USER
draw.text((x, y), "Venmo:", font=font3, fill="#1586a8")
bbox = font1.getbbox(msg)
x = WIDTH - bbox[2] * 2
y += FONTSIZE * 2 + 4
draw.text((x, y), msg, font=font3, fill="#05c2fc")
y += bbox[3] + 4
display.image(image, rotation)
def start_idle():
global idle, t
idle = True
t = Thread(target=idle_animation)
t.start()
return
def stop_idle():
global idle
idle = False
t.join()
return
def idle_animation():
def rainbow(j):
for i in range(NUM_PIXELS):
pixel_index = (i * 256 // NUM_PIXELS) + j
if emulate:
pixels.setPixelColor(i, wheel(pixel_index & 255))
else:
pixels[i] = wheel(pixel_index & 255)
pixels.show()
j = 0
frame_loop = 0
instructions_delta_t = 0
instructions_start = 0
while idle:
start = time.monotonic()
rainbow(j)
j += 1
rainbow(j)
j += 1
rainbow(j)
j += 1
rainbow(j)
j += 1
if j > 255:
j = 0
if frame_loop:
if instructions_delta_t == 0:
show_instructions()
instructions_start = time.monotonic()
instructions_delta_t = time.monotonic() - instructions_start
if instructions_delta_t > INSTRUCTIONS_TIME:
frame_loop = False
instructions_delta_t = 0
else:
frame_loop = logo.postFrame()
delta_t = time.monotonic() - start
if delta_t < FRAMETIME:
time.sleep(FRAMETIME - delta_t)
else:
print("Frame time overloaded! Took:" + str(delta_t) + " sec")
def dispense_drink():
for i in range(2):
pixels.fill((255, 255, 255))
pixels.show()
time.sleep(0.5)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.5)
open_valve()
for i in range(256):
# animation: fill in the ring less and less as time runs out and fade from green to red
pixels.fill((i, 255 - i, 0)) # r, g, b
for i in range(int((1-i/255)*NUM_PIXELS)+1, NUM_PIXELS):
if emulate:
pixels.setPixelColor(i, (0, 0, 0))
else:
pixels[i] = (0, 0, 0)
pixels.show()
time.sleep(VALVE_OPEN_TIME / 255)
close_valve()
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.5)
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos * 3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos * 3)
g = 0
b = int(pos * 3)
else:
pos -= 170
r = 0
g = int(pos * 3)
b = int(255 - pos * 3)
return (r, g, b)
def main():
global image
image = Image.new("RGB", (WIDTH, HEIGHT))
global draw
draw = ImageDraw.Draw(image)
logo.preload("out.gif")
start_idle()
while True:
venmos = email.check_venmos()
if len(venmos) > 0:
stop_idle()
for venmo in venmos:
if venmo["Amount"] == PRICE:
show_purchase(venmo)
transactions.add_transaction(venmo)
dispense_drink()
time.sleep(1)
start_idle()
time.sleep(1)
if __name__ == '__main__':
main()