8
8
from rfid import RFIDInterface
9
9
10
10
11
+ class Track :
12
+ def __init__ (self , id : str , name : str ):
13
+ self .id = id
14
+ self .name = name
15
+
16
+
11
17
class App :
12
18
def __init__ (self ):
13
19
self .__display = DisplayInterface (sda_pin = 0 , scl_pin = 1 )
14
20
self .__api = ApiInterface ()
15
21
self .__buttons = ButtonsInterface (left_button_pin = 12 , right_button_pin = 13 )
16
22
self .__buzzer = BuzzerInterface (pin = 11 )
17
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
18
26
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" )
22
30
}
23
31
24
32
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 )
26
34
self .__display .print_centered ("Initializing..." , line = 2 )
27
35
28
36
self .__connect_to_wifi ()
@@ -31,24 +39,41 @@ def run(self) -> None:
31
39
self .__display_current_device ()
32
40
33
41
while True :
34
- self .__buttons .update ()
35
-
36
- if self .__buttons .is_left_button_pressed ():
42
+ if self .__buttons .was_left_button_pressed ():
37
43
self .__display .print ("Loading..." , line = 2 , clear_line = True )
38
44
current_device_data = self .__api .previous_device ()
39
45
self .__change_device (current_device_data )
46
+ self .__buttons .reset ()
40
47
41
- if self .__buttons .is_right_button_pressed ():
48
+ if self .__buttons .was_right_button_pressed ():
42
49
self .__display .print ("Loading..." , line = 2 , clear_line = True )
43
50
current_device_data = self .__api .next_device ()
44
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 ()
45
75
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 ()
52
77
53
78
def __connect_to_wifi (self ) -> None :
54
79
self .__display .print_centered ("WiFi" , line = 2 )
0 commit comments