-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbutton.py
More file actions
59 lines (55 loc) · 2.2 KB
/
button.py
File metadata and controls
59 lines (55 loc) · 2.2 KB
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
# Modules
import asyncio
from gpiozero import Button
import logging
from time import sleep
# Functions and variables
from display import initialize
from payments import payment
from screens import make_press_overlay, make_description, make_idlescreen, make_press_overlay
from var import pin_in, button_delay, label, start_time
# Set up GPIO
buttons = [Button(btn, pull_up=False, hold_time=2) for btn in pin_in]
inventory_label = ["full", "empty"]
# A function that will check for each incoming pin whether there is a signal.
# A signal means that the tray is empty.
def get_inventory():
logging.info(f"Obtaining inventory for pins {pin_in}")
init_inventory = []
for btn in buttons:
global inventory
inventory = init_inventory.append(btn.value)
tray = buttons.index(btn)
item = btn
if btn.value ==1:
logging.info(f"Tray {tray} at pin {pin_in[tray]} is {inventory_label[btn.value]}. Item: {label[tray]}")
else:
logging.debug(f"Tray {tray} at pin {pin_in[tray]} is {inventory_label[btn.value]}. Item: {label[tray]}")
inventory = list(init_inventory)
logging.debug(f"Inventory: {inventory}")
async def listener():
logging.info(f"Listening on pins {pin_in}")
while True:
detected = False
for btn in buttons:
if btn.is_pressed and inventory[buttons.index(btn)] == 0:
logging.debug(f"{btn} HERE {btn.held_time}")
detected = True
global tray
tray = buttons.index(btn)
item = btn
await asyncio.sleep(button_delay)
if detected:
logging.info(f"Pin {pin_in[tray]} pressed. Fetching payment for tray {tray} ({label[tray]})")
logging.debug(item)
initialize()
make_press_overlay()
make_description()
await payment(tray)
make_idlescreen()
get_inventory()
logging.info(f"listening on pins {pin_in}")
# if an event remains high for more than 0.5 sec it might
# be counted again on the next loop. Likewise if an event
# comes and goes before the next loop it will be missed.
sleep(button_delay)