66from buttons import ButtonsInterface
77from buzzer import BuzzerInterface
88from rfid import RFIDInterface
9-
10-
11- class Track :
12- def __init__ (self , id : str , name : str ):
13- self .id = id
14- self .name = name
9+ from config import Config
1510
1611
1712class App :
1813 def __init__ (self ):
19- self .__display = DisplayInterface (sda_pin = 0 , scl_pin = 1 )
20- self .__api = ApiInterface ()
21- self .__buttons = ButtonsInterface (left_button_pin = 12 , right_button_pin = 13 )
22- self .__buzzer = BuzzerInterface (pin = 11 )
23- self .__rfid = RFIDInterface (sda_pin = 15 , sck_pin = 18 , mosi_pin = 19 , miso_pin = 16 , rst_pin = 14 )
24- self .__last_rfid_successful_read_time = 0
25- self .__rfid_read_delay = 2
14+ self .__display = DisplayInterface (
15+ sda_pin = Config .Display .SDA_PIN ,
16+ scl_pin = Config .Display .SCL_PIN
17+ )
18+ self .__buttons = ButtonsInterface (
19+ left_button_pin = Config .Buttons .LEFT_PIN ,
20+ right_button_pin = Config .Buttons .RIGHT_PIN
21+ )
22+ self .__rfid = RFIDInterface (
23+ sda_pin = Config .RFID .SDA_PIN ,
24+ sck_pin = Config .RFID .SCK_PIN ,
25+ mosi_pin = Config .RFID .MOSI_PIN ,
26+ miso_pin = Config .RFID .MISO_PIN ,
27+ rst_pin = Config .RFID .RST_PIN
28+ )
29+ self .__buzzer = BuzzerInterface (pin = Config .BUZZER_PIN )
30+ self .__api = ApiInterface (base_url = Config .API_BASE_URL )
2631
27- self .__tracks = {
28- 4117885779 : Track (id = "3uMUdlo47oEes3kgL4T4EC" , name = "Nonstop" ),
29- 4233351011 : Track (id = "5UW6yvwo3nVA609NgprdhK" , name = "Supermarket" )
30- }
32+ self .__last_rfid_successful_read_time = 0
33+ self .__rfid_read_delay = Config .RFID .READ_DELAY
3134
3235 def run (self ) -> None :
3336 self .__display .print_centered (f"{ chr (Symbols .NOTE .index )} Music Box { chr (Symbols .NOTE .index )} " , line = 1 )
@@ -36,49 +39,18 @@ def run(self) -> None:
3639 self .__connect_to_wifi ()
3740 if not self .__check_api_health ():
3841 return
42+
43+ self .__display .print ("Devices:" , line = 1 , clear_full = True )
3944 self .__display_current_device ()
4045
4146 while True :
42- if self .__buttons .was_left_button_pressed ():
43- self .__display .print ("Loading..." , line = 2 , clear_line = True )
44- current_device_data = self .__api .previous_device ()
45- self .__change_device (current_device_data )
46- self .__buttons .reset ()
47-
48- if self .__buttons .was_right_button_pressed ():
49- self .__display .print ("Loading..." , line = 2 , clear_line = True )
50- current_device_data = self .__api .next_device ()
51- self .__change_device (current_device_data )
52- self .__buttons .reset ()
53-
54- if time .time () - self .__last_rfid_successful_read_time > self .__rfid_read_delay :
55- card_id = self .__rfid .read_card_id ()
56-
57- if not card_id :
58- continue
59-
60- self .__display .print ("Reading card..." , line = 2 , clear_line = True )
61-
62- if card_id not in self .__tracks :
63- self .__display .print (f"{ chr (Symbols .CROSS .index )} Unknown card" , line = 2 , clear_line = True )
64- self .__buzzer .play_failure ()
65- time .sleep (1 )
66- self .__display_current_device ()
67- continue
68-
69- track = self .__tracks [card_id ]
70- self .__api .play (track .id )
71- self .__display .print (f"{ chr (Symbols .NOTE .index )} { track .name } " , line = 2 , clear_line = True )
72- self .__buzzer .play_success ()
73- time .sleep (1 )
74- self .__display_current_device ()
75-
76- self .__last_rfid_successful_read_time = time .time ()
47+ self .__check_buttons_press ()
48+ self .__check_rfid_read ()
7749
7850 def __connect_to_wifi (self ) -> None :
7951 self .__display .print_centered ("WiFi" , line = 2 )
8052
81- wlan .connect ()
53+ wlan .connect (Config . Wifi . SSID , Config . Wifi . PASSWORD )
8254
8355 self .__display .print_centered (f"{ chr (Symbols .TICK .index )} WiFi " , line = 2 )
8456 self .__buzzer .play_success ()
@@ -96,20 +68,60 @@ def __check_api_health(self) -> bool:
9668 return True
9769
9870 def __display_current_device (self ) -> None :
99- self .__display .clear ()
100- self .__display .print ("Devices:" , line = 1 )
101-
71+ self .__display .print ("Loading..." , line = 2 , clear_line = True )
10272 current_device_data = self .__api .get_current_device (reset = True )
103- self .__change_device (current_device_data )
73+ self .__display_device (current_device_data )
10474
105- def __change_device (self , device_data : dict ) -> None :
75+ def __display_device (self , device_data : dict ) -> None :
10676 if device_data == {}:
10777 self .__display .print ("No devices" , line = 2 )
10878 return
10979
11080 text = f"{ device_data ['index' ] + 1 } /{ device_data ['total' ]} { device_data ['device' ]['name' ]} "
11181 self .__display .print (text , line = 2 )
11282
83+ def __check_buttons_press (self ) -> None :
84+ was_left_button_pressed = self .__buttons .was_left_button_pressed ()
85+ was_right_button_pressed = self .__buttons .was_right_button_pressed ()
86+
87+ if not any ((was_left_button_pressed , was_right_button_pressed )):
88+ return
89+
90+ self .__display .print ("Loading..." , line = 2 , clear_line = True )
91+
92+ if was_left_button_pressed :
93+ current_device_data = self .__api .previous_device ()
94+ elif was_right_button_pressed :
95+ current_device_data = self .__api .next_device ()
96+
97+ self .__display_device (current_device_data )
98+ self .__buttons .reset ()
99+
100+ def __check_rfid_read (self ) -> None :
101+ if time .time () - self .__last_rfid_successful_read_time <= self .__rfid_read_delay :
102+ return
103+
104+ card_id = self .__rfid .read_card_id ()
105+
106+ if not card_id :
107+ return
108+
109+ self .__display .print ("Reading card..." , line = 2 , clear_line = True )
110+
111+ if card_id not in Config .TRACKS :
112+ self .__display .print (f"{ chr (Symbols .CROSS .index )} Unknown card" , line = 2 , clear_line = True )
113+ self .__buzzer .play_failure ()
114+ else :
115+ track = Config .TRACKS [card_id ]
116+ self .__api .play (track .id )
117+ self .__display .print (f"{ chr (Symbols .NOTE .index )} { track .name } " , line = 2 , clear_line = True )
118+ self .__buzzer .play_success ()
119+
120+ time .sleep (1 )
121+ self .__display_current_device ()
122+
123+ self .__last_rfid_successful_read_time = time .time ()
124+
113125
114126if __name__ == "__main__" :
115127 app = App ()
0 commit comments