Buzzer

Struct Buzzer 

Source
pub struct Buzzer<'a> { /* private fields */ }
Expand description

A buzzer instance driven by Ledc

Implementations§

Source§

impl<'a> Buzzer<'a>

Source

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

Source

pub fn with_volume( self, volume_pin: impl OutputPin + 'a, volume_type: VolumeType, ) -> Self

Add a volume control for the buzzer.

Source

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

Source

pub fn mute(&self)

Mute the buzzer

The muting is done by simply setting the duty to 0

Source

pub fn play(&mut self, frequency: u32) -> Result<(), Error>

Play a frequency through the buzzer

Source

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 buzzer
  • timings - 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

Source

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 buzzer
  • timings - 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 sequence and timings slices have different lengths (Error::LengthMismatch)
  • If playing a frequency results in an error
Source

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

Auto Trait Implementations§

§

impl<'a> Freeze for Buzzer<'a>

§

impl<'a> !RefUnwindSafe for Buzzer<'a>

§

impl<'a> !Send for Buzzer<'a>

§

impl<'a> !Sync for Buzzer<'a>

§

impl<'a> Unpin for Buzzer<'a>

§

impl<'a> !UnwindSafe for Buzzer<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.