Sound

The robot has a buzzer. You can play its built-in melody or compose your own tune from PetoiBittle.Tones.

Use case: play a short tune

using PetoiBittle

connection = PetoiBittle.connect(PetoiBittle.find_bittle_port())

# the built-in melody
PetoiBittle.send_command(connection, PetoiBittle.PlayMelody())

# a custom three-note phrase (a rest is pitch -1)
PetoiBittle.send_command(connection, PetoiBittle.PlayMusic(
    (pitch = 20, duration = 4),
    (pitch = 22, duration = 4),
    (pitch = -1, duration = 2),
    (pitch = 24, duration = 8)
))

PetoiBittle.disconnect(connection)
Note

PetoiBittle.PlayMusic sends raw signed bytes (binary, '~'-terminated), so every pitch and duration must fit in -128:127.

Reference

PetoiBittle.PlayMusic — Type
PlayMusic(tones_as_named_tuples...)
PlayMusic(single_tuple_of_tones)

A PetoiBittle.Command that plays a custom sequence of PetoiBittle.Tones on the buzzer. This is the binary form of the protocol: it serializes to the token "B" followed by raw pitch/duration byte pairs and is terminated by '~' (see PetoiBittle.command_terminator).

julia> PetoiBittle.PlayMusic((pitch = 20, duration = 4), (pitch = -1, duration = 2))
PetoiBittle.PlayMusic{2}((PetoiBittle.Tone(20, 4), PetoiBittle.Tone(-1, 2)))

See also: PetoiBittle.PlayMelody (the built-in melody).

source
PetoiBittle.Tone — Type
Tone(pitch::Int, duration::Int)

A single note used by PetoiBittle.PlayMusic: pitch is the tone (a rest is encoded as -1) and duration its length. Both are sent as raw signed bytes, so each must fit in -128:127. Construct directly or via a named tuple (pitch = ..., duration = ...).

source