BuzzerController¶
- class bbl.buzzer.BuzzerController(buzzer_channel, freq=10, duty=0)[source]¶
Bases:
objectA singleton class to control a buzzer (PWM-controlled) connected to a specified GPIO pin.
This class allows setting the frequency, duty cycle, and volume of the buzzer. It ensures that only one instance of the controller exists per buzzer channel (BUZZER1 or BUZZER2).
Example
>>> buzzer = BuzzerController('BUZZER1', freq=1000, duty=512) >>> buzzer.set_freq(1500) >>> buzzer.set_duty(1023) >>> buzzer.set_volume(50) >>> buzzer.stop()
- __init__(buzzer_channel, freq=10, duty=0)[source]¶
Initializes the BuzzerController instance for controlling a buzzer
Sets the initial frequency and duty cycle for the buzzer
- Parameters:
- Raises:
ValueError – If the provided buzzer_channel is not valid.
Example
>>> buzzer = BuzzerController('BUZZER1')
- deinit()[source]¶
Deinitializes the PWM instance controlling the buzzer, effectively turning it off. .. rubric:: Example
>>> buzzer.deinit() # Deinitializes the buzzer PWM
- reinit(freq=5, duty=0)[source]¶
Reinitializes the buzzer by deinitializing and recreating the PWM instance with the specified frequency and duty cycle.
- Parameters:
Example
>>> # Reinitializes buzzer with new frequency and duty >>> buzzer.reinit(freq=1000, duty=512)
- set_duty(duty)[source]¶
Sets the duty cycle for the buzzer.
- Parameters:
duty (int) – The duty cycle value (0 to 1023).
Example
>>> buzzer.set_duty(512) # Sets the duty cycle to 50%
- set_freq(freq=10)[source]¶
Sets the frequency of the buzzer.
- Parameters:
freq (int) – The frequency to set for the buzzer.
Example
>>> buzzer.set_freq(1500) # Sets the buzzer frequency to 1500 Hz
- set_volume(volume=0)[source]¶
Sets the volume of the buzzer by adjusting the duty cycle.
The volume is represented as a percentage from 0 to 100, where 0 is silent and 100 is the maximum volume.
- Parameters:
volume (int) – The volume level, ranging from 0 to 100.
Example
>>> buzzer.set_volume(50) # Sets the volume to 50%
- class bbl.buzzer.MusicController(buzzer_ch, volume=0)[source]¶
Bases:
objectA singleton class to manage and play music through a buzzer using RTTTL (Ring Tone Text Transfer Language).
This class parses RTTTL formatted strings and plays the notes on the buzzer with a specified volume. The controller ensures that only one instance exists for the given buzzer channel. .. rubric:: Example
>>> music = MusicController('BUZZER1', volume=50) >>> music.play('Entertainer:d=4,o=5,b=140:8d,8d#,8e,c6,8e', volume=80)
- __init__(buzzer_ch, volume=0)[source]¶
Initializes the MusicController instance, setting up the buzzer and preparing the necessary attributes.
- Parameters:
Example
>>> # Initializes the MusicController >>> music = MusicController('BUZZER1', volume=50)
- play(tune, volume=50, block=True, loop=False)[source]¶
Plays a tune by parsing the RTTTL string and sending the corresponding frequencies to the buzzer.
- Parameters:
Example
>>> music.play('Entertainer:d=4,o=5,b=140:8d,8d#,8e,c6', volume=80)
- reinit()[source]¶
Reinitializes the music controller, stopping any music, resetting volume, and reinitializing the buzzer. .. rubric:: Example
>>> music.reinit() # Reinitializes the music controller
- set_volume(volume=0)[source]¶
Sets the volume for the music playback by adjusting the buzzer’s duty cycle.
- Parameters:
volume (int) – The volume level, ranging from 0 to 100.
Example
>>> # Sets the volume for music playback to 70% >>> music.set_volume(70)
- stop()[source]¶
Stops the music playback by setting the buzzer’s duty cycle to 0 and halting any ongoing tune. .. rubric:: Example
>>> music.stop() # Stops the current music playback
- timing_proc()[source]¶
A callback method to periodically check and play the next note in the tune.
This method is called in the event loop and ensures the correct timing for each note based on its duration. .. rubric:: Example
>>> # Call timing_proc in the main loop to play the tune >>> music.timing_proc()