Warning:

To protect the digital assets of creators and enhance system security and stability, the CyberBrick Multi-Function Core Board does not support firmware flashing with any third-party tools! If you need to restore the Multi-Function Core Board to its factory state, please wait for the official recovery tool to be released by CyberBrick. If you proceed to flash the firmware using third-party tools, the firmware of the Multi-Function Core Board will be permanently damaged and cannot be recovered. CyberBrick will not be responsible for any consequences resulting from such operations.

This documentation is adapted from the official MicroPython project. The CyberBrick team has extended the source code with custom interfaces and features to our hardware and application needs.

In addition to these enhancements, certain built-in MicroPython interfaces have been intentionally disabled to protect the system's integrity, and ensure the overall security and reliability of the device. This also facilitates content protection for creators' intellectual property, where applicable.

Portions of the content are derived from the official MicroPython documentation and have been included here under its open-source license to provide users with a consistent and enriched development experience tailored to the CyberBrick platform.

BuzzerController

class bbl.buzzer.BuzzerController(buzzer_channel, freq=10, duty=0)[source]

Bases: object

A 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).

buzzer

PWM instance for controlling the buzzer.

Type:

PWM

ch

The buzzer channel, either ‘BUZZER1’ or ‘BUZZER2’.

Type:

str

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:
  • buzzer_channel (str) – The buzzer channel, either ‘BUZZER1’ or ‘BUZZER2’.

  • freq (int) – Frequency for the buzzer (default: 10).

  • duty (int) – Duty cycle for the buzzer (default: 0).

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:
  • freq (int) – The frequency to set after reinitialization (default: 5).

  • duty (int) – The duty cycle to set after reinitialization (default: 0).

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%
stop()[source]

Stops the buzzer by setting its duty cycle to 0, turning it off. .. rubric:: Example

>>> buzzer.stop()  # Stops the buzzer
class bbl.buzzer.MusicController(buzzer_ch, volume=0)[source]

Bases: object

A 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:
  • buzzer_ch (str) – The buzzer channel to control (BUZZER1 or BUZZER2).

  • volume (int) – The initial volume level for the buzzer (0 to 100).

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:
  • tune (str) – The RTTTL formatted string representing the melody to play.

  • volume (int) – The volume level for playback (0 to 100).

  • block (bool) – If True, plays the tune synchronously, blocking further code execution.

  • loop (bool) – If True, the tune will repeat indefinitely after it finishes.

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()