Skip to content

Commit 89fa5b8

Browse files
committed
Add buzzer reset and integrate into resetbot
Add a reset_buzzer method to Buzzer to immediately stop PWM output, deinit timers, and clear song state so the buzzer is silenced and reset. Shorten the missing-pin exception message. Add a reset_buzzer helper in resetbot.py and wire it into reset_hard and module-level cleanup (with try/except guards) so the buzzer is turned off during resets.
1 parent 8ba6e9f commit 89fa5b8

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

XRPLib/buzzer.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __init__(self, buzzer_pin: int|str = None):
5959
if hasattr(machine.Pin.board, "BOARD_BUZZER"):
6060
pin = "BOARD_BUZZER"
6161
else:
62-
raise Exception("No buzzer pin defined, are you sure this board has a buzzer?")
62+
raise Exception("No buzzer pin defined")
6363

6464
self._pin = machine.Pin(pin)
6565
self._pwm = machine.PWM(self._pin)
@@ -306,6 +306,23 @@ def play_song(self, song: list, tempo: int = None, blocking: bool = True):
306306
self._timer.init(freq=100, callback=lambda t: self._update(t))
307307
self._timer_in_use = True
308308

309+
def reset_buzzer(self):
310+
"""
311+
Stops all sounds and timers immediately.
312+
Resets the buzzer to a silent state.
313+
"""
314+
# Stop PWM output
315+
self._pwm.duty_u16(0)
316+
317+
# Stop background timers
318+
if self._timer_in_use:
319+
self._timer.deinit()
320+
self._timer_in_use = False
321+
322+
# Reset song state
323+
self._song = []
324+
self._song_index = 0
325+
309326
def play_move_it(self, blocking: bool = True):
310327
"""
311328
Play "I Like to Move It" song.

XRPLib/resetbot.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ def reset_led():
2828
except:
2929
pass
3030

31+
def reset_buzzer():
32+
from XRPLib.buzzer import Buzzer
33+
# Turn off the buzzer
34+
try:
35+
# Turn off the Buzzer if the board has one
36+
Buzzer.reset_buzzer()
37+
except:
38+
pass
39+
3140
def reset_servos():
3241
from XRPLib.servo import Servo
3342
# Turn off both Servos
@@ -48,6 +57,7 @@ def reset_hard():
4857
reset_gamepad()
4958
reset_motors()
5059
reset_led()
60+
reset_buzzer()
5161
reset_servos()
5262
reset_webserver()
5363

@@ -60,6 +70,9 @@ def reset_hard():
6070
if "XRPLib.board" in sys.modules:
6171
reset_led()
6272

73+
if "XRPLib.buzzer" in sys.modules:
74+
reset_buzzer()
75+
6376
if "XRPLib.servo" in sys.modules:
6477
reset_servos()
6578

0 commit comments

Comments
 (0)