Skip to content

Commit 4259e0d

Browse files
committed
feat: add main script
1 parent c2c7357 commit 4259e0d

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

src/main.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import time
2+
3+
import wlan
4+
from display import DisplayInterface, Symbols
5+
from api import ApiInterface
6+
from buttons import ButtonsInterface
7+
from buzzer import BuzzerInterface
8+
from rfid import RFIDInterface
9+
10+
11+
class App:
12+
def __init__(self):
13+
self.__display = DisplayInterface(sda_pin=0, scl_pin=1)
14+
self.__api = ApiInterface()
15+
self.__buttons = ButtonsInterface(left_button_pin=12, right_button_pin=13)
16+
self.__buzzer = BuzzerInterface(pin=11)
17+
self.__rfid = RFIDInterface(sda_pin=15, sck_pin=18, mosi_pin=19, miso_pin=16, rst_pin=14)
18+
19+
self.__music = {
20+
4117885779: "3uMUdlo47oEes3kgL4T4EC",
21+
4233351011: "5UW6yvwo3nVA609NgprdhK"
22+
}
23+
24+
def run(self) -> None:
25+
self.__display.print_centered("Music Box", line=1)
26+
self.__display.print_centered("Initializing...", line=2)
27+
28+
self.__connect_to_wifi()
29+
if not self.__check_api_health():
30+
return
31+
self.__display_current_device()
32+
33+
while True:
34+
self.__buttons.update()
35+
36+
if self.__buttons.is_left_button_pressed():
37+
self.__display.print("Loading...", line=2, clear_line=True)
38+
current_device_data = self.__api.previous_device()
39+
self.__change_device(current_device_data)
40+
41+
if self.__buttons.is_right_button_pressed():
42+
self.__display.print("Loading...", line=2, clear_line=True)
43+
current_device_data = self.__api.next_device()
44+
self.__change_device(current_device_data)
45+
46+
card_id = self.__rfid.read_card_id()
47+
if card_id:
48+
print(self.__music[card_id])
49+
self.__api.play(self.__music[card_id])
50+
print("Playing")
51+
time.sleep(5)
52+
53+
def __connect_to_wifi(self) -> None:
54+
self.__display.print_centered("WiFi", line=2)
55+
56+
wlan.connect()
57+
58+
self.__display.print_centered(f"{chr(Symbols.TICK.index)} WiFi ", line=2)
59+
self.__buzzer.play_success()
60+
61+
def __check_api_health(self) -> bool:
62+
self.__display.print_centered("API", line=2)
63+
64+
if not self.__api.check_health():
65+
self.__display.print_centered(f"{chr(Symbols.CROSS.index)} API ", line=2)
66+
self.__buzzer.play_failure()
67+
return False
68+
69+
self.__display.print_centered(f"{chr(Symbols.TICK.index)} API ", line=2)
70+
self.__buzzer.play_success()
71+
return True
72+
73+
def __display_current_device(self) -> None:
74+
self.__display.clear()
75+
self.__display.print("Devices:", line=1)
76+
77+
current_device_data = self.__api.get_current_device()
78+
self.__change_device(current_device_data)
79+
80+
def __change_device(self, device_data: dict) -> None:
81+
text = f"{device_data['index'] + 1}/{device_data['total']} {device_data['device']['name']}"
82+
self.__display.print(text, line=2)
83+
84+
85+
if __name__ == "__main__":
86+
app = App()
87+
app.run()

0 commit comments

Comments
 (0)