pub struct Buzzer<'a> { /* private fields */ }Expand description
A buzzer instance driven by Ledc
Implementations§
Source§impl<'a> Buzzer<'a>
impl<'a> Buzzer<'a>
Sourcepub fn new(
ledc: &'a Ledc<'_>,
timer_number: Number,
channel_number: Number,
output_pin: impl OutputPin + 'a,
) -> Self
pub fn new( ledc: &'a Ledc<'_>, timer_number: Number, channel_number: Number, output_pin: impl OutputPin + 'a, ) -> Self
Create a new buzzer for the given pin
Sourcepub fn with_volume(
self,
volume_pin: impl OutputPin + 'a,
volume_type: VolumeType,
) -> Self
pub fn with_volume( self, volume_pin: impl OutputPin + 'a, volume_type: VolumeType, ) -> Self
Add a volume control for the buzzer.
Sourcepub fn set_volume(&mut self, level: u8) -> Result<(), Error>
pub fn set_volume(&mut self, level: u8) -> Result<(), Error>
Set the volume of the buzzer
For VolumeType::Duty, the level should be between 0 and 100. For VolumeType::OnOff, it will only be mute on 0 and playing on 1 or more
Sourcepub fn play(&mut self, frequency: u32) -> Result<(), Error>
pub fn play(&mut self, frequency: u32) -> Result<(), Error>
Play a frequency through the buzzer
Sourcepub fn play_tones<const T: usize>(
&mut self,
sequence: [u32; T],
timings: [u32; T],
) -> Result<(), Error>
pub fn play_tones<const T: usize>( &mut self, sequence: [u32; T], timings: [u32; T], ) -> Result<(), Error>
Play a sound sequence through the buzzer
Uses a pair of frequencies and timings to play a sound sequence.
§Arguments
sequence- A list of frequencies to play through the buzzertimings- A list of timings in ms for each frequencies
§Examples
Play a single beep at 300Hz for 1 second
buzzer.play_tones([300], [1000]);Play a sequence of 3 beeps with a break inbetween
buzzer.play_tones([200, 0, 200, 0, 200], [200, 50, 200, 50, 200]);Play a sequence of 3 beeps with the same duration
buzzer.play_tones([100, 200, 300], [100; 3]);§Errors
This function returns an Error in case of an error. An error can occur when an invalid value is used as a tone
Sourcepub fn play_tones_from_slice(
&mut self,
sequence: &[u32],
timings: &[u32],
) -> Result<(), Error>
pub fn play_tones_from_slice( &mut self, sequence: &[u32], timings: &[u32], ) -> Result<(), Error>
Play a sound sequence through the buzzer
Uses a pair of frequency and duration slices to play a sound sequence.
Both slices must be of the same length, where each pair of (frequency, duration) defines one tone.
§Arguments
sequence- A slice of frequencies to play through the buzzertimings- A slice of durations in milliseconds for each frequency
§Examples
Play a single beep at 300Hz for 1 second
buzzer.play_tones_from_slice(&[300], &[1000]);Play a sequence of 3 beeps with a break in between
buzzer.play_tones_from_slice(&[200, 0, 200, 0, 200], &[200, 50, 200, 50, 200]);Play a sequence of 3 beeps with the same duration
buzzer.play_tones_from_slice(&[100, 200, 300], &[100; 3]);§Errors
This function returns an Error in the following cases:
- If the
sequenceandtimingsslices have different lengths (Error::LengthMismatch) - If playing a frequency results in an error
Sourcepub fn play_song(&mut self, tones: &[ToneValue]) -> Result<(), Error>
pub fn play_song(&mut self, tones: &[ToneValue]) -> Result<(), Error>
Play a tone sequence through the buzzer
Uses a pair of frequencies and timings to play a sound sequence.
§Arguments
tones- A list of type ToneValue to play through the buzzer
§Examples
Play a tone sequence
let song = [
ToneValue {
frequency: 100,
duration: 100,
},
ToneValue {
frequency: 200,
duration: 100,
},
ToneValue {
frequency: 300,
duration: 100,
},
];
buzzer.play_song(&song);§Errors
This function returns an Error in case of an error. An error can occur when an invalid value is used as a tone