Skip to main content

Buzzer

Struct Buzzer 

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

Driver for the Modulino Buzzer module.

§Example

use modulino::{Buzzer, Note};

let mut buzzer = Buzzer::new(i2c)?;

// Play a tone at 440 Hz for 500ms
buzzer.tone(440, 500)?;

// Play a note
buzzer.play_note(Note::C5, 1000)?;

// Stop the tone
buzzer.no_tone()?;

Implementations§

Source§

impl<I2C, E> Buzzer<I2C>
where I2C: I2c<Error = E>,

Source

pub const MIN_FREQUENCY: u16 = 180

Minimum supported frequency in Hz.

Source

pub fn new(i2c: I2C) -> Result<Self, E>

Create a new Buzzer instance with the default address.

Source

pub fn new_with_address(i2c: I2C, address: u8) -> Result<Self, E>

Create a new Buzzer instance with a custom address.

Source

pub fn address(&self) -> u8

Get the I2C address.

Source

pub fn tone(&mut self, frequency: u16, duration_ms: u16) -> Result<(), E>

Play a tone at the specified frequency.

§Arguments
  • frequency - The frequency in Hz (minimum 180 Hz, or 0 for silence)
  • duration_ms - The duration in milliseconds (0xFFFF for indefinite)
§Note

Frequencies below 180 Hz (except 0) are not supported by the hardware.

Source

pub fn tone_continuous(&mut self, frequency: u16) -> Result<(), E>

Play a tone indefinitely until stopped.

Source

pub fn play_note(&mut self, note: Note, duration_ms: u16) -> Result<(), E>

Play a musical note.

§Arguments
  • note - The note to play
  • duration_ms - The duration in milliseconds
Source

pub fn no_tone(&mut self) -> Result<(), E>

Stop playing any tone.

Source

pub fn stop(&mut self) -> Result<(), E>

Alias for no_tone().

Source

pub fn release(self) -> I2C

Release the I2C bus.

Auto Trait Implementations§

§

impl<I2C> Freeze for Buzzer<I2C>
where I2C: Freeze,

§

impl<I2C> RefUnwindSafe for Buzzer<I2C>
where I2C: RefUnwindSafe,

§

impl<I2C> Send for Buzzer<I2C>
where I2C: Send,

§

impl<I2C> Sync for Buzzer<I2C>
where I2C: Sync,

§

impl<I2C> Unpin for Buzzer<I2C>
where I2C: Unpin,

§

impl<I2C> UnwindSafe for Buzzer<I2C>
where I2C: UnwindSafe,

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, 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.