@@ -39,44 +39,13 @@ def run(self) -> None:
3939 self .__connect_to_wifi ()
4040 if not self .__check_api_health ():
4141 return
42+
43+ self .__display .print ("Devices:" , line = 1 , clear_full = True )
4244 self .__display_current_device ()
4345
4446 while True :
45- if self .__buttons .was_left_button_pressed ():
46- self .__display .print ("Loading..." , line = 2 , clear_line = True )
47- current_device_data = self .__api .previous_device ()
48- self .__change_device (current_device_data )
49- self .__buttons .reset ()
50-
51- if self .__buttons .was_right_button_pressed ():
52- self .__display .print ("Loading..." , line = 2 , clear_line = True )
53- current_device_data = self .__api .next_device ()
54- self .__change_device (current_device_data )
55- self .__buttons .reset ()
56-
57- if time .time () - self .__last_rfid_successful_read_time > self .__rfid_read_delay :
58- card_id = self .__rfid .read_card_id ()
59-
60- if not card_id :
61- continue
62-
63- self .__display .print ("Reading card..." , line = 2 , clear_line = True )
64-
65- if card_id not in Config .TRACKS :
66- self .__display .print (f"{ chr (Symbols .CROSS .index )} Unknown card" , line = 2 , clear_line = True )
67- self .__buzzer .play_failure ()
68- time .sleep (1 )
69- self .__display_current_device ()
70- continue
71-
72- track = Config .TRACKS [card_id ]
73- self .__api .play (track .id )
74- self .__display .print (f"{ chr (Symbols .NOTE .index )} { track .name } " , line = 2 , clear_line = True )
75- self .__buzzer .play_success ()
76- time .sleep (1 )
77- self .__display_current_device ()
78-
79- self .__last_rfid_successful_read_time = time .time ()
47+ self .__check_buttons_press ()
48+ self .__check_rfid_read ()
8049
8150 def __connect_to_wifi (self ) -> None :
8251 self .__display .print_centered ("WiFi" , line = 2 )
@@ -99,20 +68,60 @@ def __check_api_health(self) -> bool:
9968 return True
10069
10170 def __display_current_device (self ) -> None :
102- self .__display .clear ()
103- self .__display .print ("Devices:" , line = 1 )
104-
71+ self .__display .print ("Loading..." , line = 2 , clear_line = True )
10572 current_device_data = self .__api .get_current_device (reset = True )
106- self .__change_device (current_device_data )
73+ self .__display_device (current_device_data )
10774
108- def __change_device (self , device_data : dict ) -> None :
75+ def __display_device (self , device_data : dict ) -> None :
10976 if device_data == {}:
11077 self .__display .print ("No devices" , line = 2 )
11178 return
11279
11380 text = f"{ device_data ['index' ] + 1 } /{ device_data ['total' ]} { device_data ['device' ]['name' ]} "
11481 self .__display .print (text , line = 2 )
11582
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+
116125
117126if __name__ == "__main__" :
118127 app = App ()
0 commit comments