Skip to main content

Buttons

Struct Buttons 

Source
pub struct Buttons<I2C> {
    pub led_a: ButtonLed,
    pub led_b: ButtonLed,
    pub led_c: ButtonLed,
    /* private fields */
}
Expand description

Driver for the Modulino Buttons module.

§Example

use modulino::Buttons;

let mut buttons = Buttons::new(i2c)?;

// Read button states
let state = buttons.read()?;
if state.a {
    println!("Button A pressed!");
}

// Control LEDs
buttons.led_a.on();
buttons.led_b.off();
buttons.led_c.set(state.c);
buttons.update_leds()?;

Fields§

§led_a: ButtonLed

LED A state

§led_b: ButtonLed

LED B state

§led_c: ButtonLed

LED C state

Implementations§

Source§

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

Source

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

Create a new Buttons instance with the default address.

Source

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

Create a new Buttons instance with a custom address.

Source

pub fn address(&self) -> u8

Get the I2C address.

Source

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

Read the current button states.

Returns a ButtonState struct with the pressed state of each button.

Source

pub fn state(&self) -> ButtonState

Get the last read button state without performing I2C communication.

Source

pub fn button_a_pressed(&self) -> bool

Check if button A is pressed (uses cached state).

Source

pub fn button_b_pressed(&self) -> bool

Check if button B is pressed (uses cached state).

Source

pub fn button_c_pressed(&self) -> bool

Check if button C is pressed (uses cached state).

Source

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

Update the LED states on the device.

This writes the current LED states to the hardware.

Source

pub fn set_leds(&mut self, a: bool, b: bool, c: bool) -> Result<(), E>

Set all LED states at once and update the hardware.

Source

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

Turn all LEDs off.

Source

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

Turn all LEDs on.

Source

pub fn release(self) -> I2C

Release the I2C bus.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<I2C> UnwindSafe for Buttons<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.