Skip to content

Commit 699d6cf

Browse files
authored
Merge pull request #4 from MusicBoxRaspberryPi/develop
Add Buttons Interrupts and RFID Debouncing
2 parents dae4415 + a8e5a7c commit 699d6cf

File tree

5 files changed

+70
-67
lines changed

5 files changed

+70
-67
lines changed

src/buttons.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
from lib.button import Button
1+
from machine import Pin
22

33

44
class ButtonsInterface:
55
def __init__(self, left_button_pin: int, right_button_pin: int):
6-
self.__left_button = Button(left_button_pin, internal_pullup=True)
7-
self.__right_button = Button(right_button_pin, internal_pullup=True)
6+
self.__left_button = Pin(left_button_pin, Pin.IN, Pin.PULL_UP)
7+
self.__right_button = Pin(right_button_pin, Pin.IN, Pin.PULL_UP)
88

9-
def update(self) -> None:
10-
self.__left_button.update()
11-
self.__right_button.update()
9+
self.__left_button.irq(trigger=Pin.IRQ_FALLING, handler=self.__set_left_button_pressed)
10+
self.__right_button.irq(trigger=Pin.IRQ_FALLING, handler=self.__set_right_button_pressed)
1211

13-
def is_left_button_pressed(self) -> bool:
14-
return self.__left_button.active
12+
self.__left_button_pressed = False
13+
self.__right_button_pressed = False
1514

16-
def is_right_button_pressed(self) -> bool:
17-
return self.__right_button.active
15+
def __set_left_button_pressed(self, pin):
16+
self.__left_button_pressed = True
17+
18+
def __set_right_button_pressed(self, pin):
19+
self.__right_button_pressed = True
20+
21+
def was_left_button_pressed(self):
22+
return self.__left_button_pressed
23+
24+
def was_right_button_pressed(self):
25+
return self.__right_button_pressed
26+
27+
def reset(self):
28+
self.__left_button_pressed = False
29+
self.__right_button_pressed = False

src/display.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def __init__(self, index: int, data: bytearray):
1111
class Symbols:
1212
TICK = Symbol(0, bytearray([0x00, 0x00, 0x01, 0x02, 0x14, 0x08, 0x00, 0x00]))
1313
CROSS = Symbol(1, bytearray([0x00, 0x00, 0x11, 0x0A, 0x04, 0x0A, 0x11, 0x00]))
14+
NOTE = Symbol(2, bytearray([0x00, 0x0F, 0x09, 0x09, 0x19, 0x1B, 0x03, 0x00]))
1415

1516

1617
class DisplayInterface:

src/lib/button.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/main.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,29 @@
88
from 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+
1117
class 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)

src/rfid.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ def __init__(self, sda_pin: int, sck_pin: int, mosi_pin: int, miso_pin: int, rst
88
def read_card_id(self):
99
self.__rfid.init()
1010
(stat, tag_type) = self.__rfid.request(self.__rfid.REQIDL)
11-
if stat == self.__rfid.OK:
12-
(stat, uid) = self.__rfid.SelectTagSN()
13-
if stat == self.__rfid.OK:
14-
return int.from_bytes(bytes(uid), "little", False)
11+
if stat != self.__rfid.OK:
12+
return None
13+
14+
(stat, uid) = self.__rfid.SelectTagSN()
15+
if stat != self.__rfid.OK:
16+
return None
17+
18+
return int.from_bytes(bytes(uid), "little", False)

0 commit comments

Comments
 (0)