Skip to content

beeper

Beeper utilities.

Make sure to connect the jumpers for J4 on the board!

Classes:

Name Description
Beeper

Beeper.

Functions:

Name Description
test_beeper

Test for the beeper class.

Beeper

Beeper(pin: int)

Beeper.

Parameters:

Name Type Description Default
pin int

The pin the beeper is connected to.

required

Methods:

Name Description
play

Play a tone.

Source code in src/aalec/beeper.py
20
21
def __init__(self, pin: int):
    self._pin = machine.Pin(pin, machine.Pin.OUT)

play

play(freq: int, dur: int | None = None) -> None

Play a tone.

Parameters:

Name Type Description Default
freq int

The frequency of the tone. If the frequency is <=0, no tone will be played.

required
dur int | None

Duration of the tone in ms. If set to None the tone will keep playing. Defaults to None.

None
Source code in src/aalec/beeper.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def play(self, freq: int, dur: int | None = None) -> None:
    """Play a tone.

    Args:
        freq (int): The frequency of the tone. If the frequency is <=0, no tone will be played.
        dur (int | None, optional): Duration of the tone in ms.
            If set to `None` the tone will keep playing. Defaults to None.
    """
    pwm = machine.PWM(self._pin, freq=freq, duty_u16=constants.DUTY50)
    if freq <= 0:
        pwm.deinit()
        return
    if dur is not None:
        time.sleep_ms(dur)  # type: ignore
        pwm.deinit()

test_beeper

test_beeper() -> None

Test for the beeper class.

Source code in src/aalec/beeper.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def test_beeper() -> None:
    """Test for the beeper class."""

    beeper = Beeper(constants.PIN_BEEPER)
    for tone in [
        constants.t_c_1,
        constants.t_d_1,
        constants.t_e_1,
        constants.t_f_1,
        constants.t_g_1,
        constants.t_a_1,
        constants.t_h_1,
        constants.t_off,
    ]:
        beeper.play(tone, 250)