-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
145 lines (110 loc) · 4.8 KB
/
main.py
File metadata and controls
145 lines (110 loc) · 4.8 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
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
import time
import wlan
from display import DisplayInterface, Symbols
from api import ApiInterface
from buttons import ButtonsInterface
from buzzer import BuzzerInterface
from rfid import RFIDInterface
from config import Config
class App:
def __init__(self):
self.__display = DisplayInterface(
sda_pin=Config.Display.SDA_PIN,
scl_pin=Config.Display.SCL_PIN
)
self.__buttons = ButtonsInterface(
left_button_pin=Config.Buttons.LEFT_PIN,
right_button_pin=Config.Buttons.RIGHT_PIN
)
self.__rfid = RFIDInterface(
sda_pin=Config.RFID.SDA_PIN,
sck_pin=Config.RFID.SCK_PIN,
mosi_pin=Config.RFID.MOSI_PIN,
miso_pin=Config.RFID.MISO_PIN,
rst_pin=Config.RFID.RST_PIN
)
self.__buzzer = BuzzerInterface(pin=Config.BUZZER_PIN)
self.__api = ApiInterface(base_url=Config.API_BASE_URL)
self.__last_rfid_successful_read_time = 0
self.__rfid_read_delay = Config.RFID.READ_DELAY
def handle_error(self, exception: Exception) -> None:
self.__display.clear()
self.__display.print_centered("Exception", line=1)
self.__display.print_centered("occurred", line=2)
self.__buzzer.play_failure()
time.sleep(2)
self.__display.print_scroll_text(f"{type(exception).__name__}:{str(exception)}", line=2)
def run(self) -> None:
self.__display.print_centered(f"{chr(Symbols.NOTE.index)} Music Box {chr(Symbols.NOTE.index)}", line=1)
self.__display.print_centered("Initializing...", line=2)
self.__connect_to_wifi()
if not self.__check_api_health():
return
self.__display.print("Devices:", line=1, clear_full=True)
self.__display_current_device(reset=True)
while True:
self.__check_buttons_press()
self.__check_rfid_read()
def __connect_to_wifi(self) -> None:
self.__display.print_centered("WiFi", line=2)
wlan.connect(Config.Wifi.SSID, Config.Wifi.PASSWORD)
self.__display.print_centered(f"{chr(Symbols.TICK.index)} WiFi ", line=2)
self.__buzzer.play_success()
def __check_api_health(self) -> bool:
self.__display.print_centered("API", line=2)
if not self.__api.check_health():
self.__display.print_centered(f"{chr(Symbols.CROSS.index)} API ", line=2)
self.__buzzer.play_failure()
return False
self.__display.print_centered(f"{chr(Symbols.TICK.index)} API ", line=2)
self.__buzzer.play_success()
return True
def __display_current_device(self, reset: bool = False) -> None:
self.__display.print("Loading...", line=2, clear_line=True)
current_device_data = self.__api.get_current_device(reset=reset)
self.__display_device(current_device_data)
def __display_device(self, device_data: dict) -> None:
if device_data == {}:
self.__display.print("No devices", line=2)
return
text = f"{device_data['index'] + 1}/{device_data['total']} {device_data['device']['name']}"
self.__display.print(text, line=2)
def __check_buttons_press(self) -> None:
was_left_button_pressed = self.__buttons.was_left_button_pressed()
was_right_button_pressed = self.__buttons.was_right_button_pressed()
if not any((was_left_button_pressed, was_right_button_pressed)):
return
self.__buzzer.play_ok()
self.__display.print("Loading...", line=2, clear_line=True)
if was_left_button_pressed:
current_device_data = self.__api.previous_device()
elif was_right_button_pressed:
current_device_data = self.__api.next_device()
else:
return
self.__display_device(current_device_data)
self.__buttons.reset()
def __check_rfid_read(self) -> None:
if time.time() - self.__last_rfid_successful_read_time <= self.__rfid_read_delay:
return
card_id = self.__rfid.read_card_id()
if not card_id:
return
self.__display.print("Reading card...", line=2, clear_line=True)
if card_id not in Config.TRACKS:
self.__display.print(f"{chr(Symbols.CROSS.index)} Unknown card", line=2, clear_line=True)
self.__buzzer.play_failure()
else:
track = Config.TRACKS[card_id]
self.__api.play(track.id)
self.__display.print(f"{chr(Symbols.NOTE.index)} {track.name}", line=2, clear_line=True)
self.__buzzer.play_success()
time.sleep(1)
self.__display_current_device()
self.__last_rfid_successful_read_time = time.time()
if __name__ == "__main__":
app = App()
try:
app.run()
except Exception as e:
app.handle_error(e)