88from rfid import RFIDInterface
99
1010
11+ class Track :
12+ def __init__ (self , id : str , name : str ):
13+ self .id = id
14+ self .name = name
15+
16+
1117class App :
1218 def __init__ (self ):
1319 self .__display = DisplayInterface (sda_pin = 0 , scl_pin = 1 )
1420 self .__api = ApiInterface ()
1521 self .__buttons = ButtonsInterface (left_button_pin = 12 , right_button_pin = 13 )
1622 self .__buzzer = BuzzerInterface (pin = 11 )
1723 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
1826
19- self .__music = {
20- 4117885779 : "3uMUdlo47oEes3kgL4T4EC" ,
21- 4233351011 : "5UW6yvwo3nVA609NgprdhK"
27+ self .__tracks = {
28+ 4117885779 : Track ( id = "3uMUdlo47oEes3kgL4T4EC" , name = "Nonstop" ) ,
29+ 4233351011 : Track ( id = "5UW6yvwo3nVA609NgprdhK" , name = "Supermarket" )
2230 }
2331
2432 def run (self ) -> None :
25- self .__display .print_centered (" Music Box" , line = 1 )
33+ self .__display .print_centered (f" { chr ( Symbols . NOTE . index ) } Music Box { chr ( Symbols . NOTE . index ) } " , line = 1 )
2634 self .__display .print_centered ("Initializing..." , line = 2 )
2735
2836 self .__connect_to_wifi ()
@@ -31,24 +39,41 @@ def run(self) -> None:
3139 self .__display_current_device ()
3240
3341 while True :
34- self .__buttons .update ()
35-
36- if self .__buttons .is_left_button_pressed ():
42+ if self .__buttons .was_left_button_pressed ():
3743 self .__display .print ("Loading..." , line = 2 , clear_line = True )
3844 current_device_data = self .__api .previous_device ()
3945 self .__change_device (current_device_data )
46+ self .__buttons .reset ()
4047
41- if self .__buttons .is_right_button_pressed ():
48+ if self .__buttons .was_right_button_pressed ():
4249 self .__display .print ("Loading..." , line = 2 , clear_line = True )
4350 current_device_data = self .__api .next_device ()
4451 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 ()
4575
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 )
76+ self .__last_rfid_successful_read_time = time .time ()
5277
5378 def __connect_to_wifi (self ) -> None :
5479 self .__display .print_centered ("WiFi" , line = 2 )
0 commit comments