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: ButtonLedLED A state
led_b: ButtonLedLED B state
led_c: ButtonLedLED C state
Implementations§
Source§impl<I2C, E> Buttons<I2C>where
I2C: I2c<Error = E>,
impl<I2C, E> Buttons<I2C>where
I2C: I2c<Error = E>,
Sourcepub fn new(i2c: I2C) -> Result<Self, E>
pub fn new(i2c: I2C) -> Result<Self, E>
Create a new Buttons instance with the default address.
Sourcepub fn new_with_address(i2c: I2C, address: u8) -> Result<Self, E>
pub fn new_with_address(i2c: I2C, address: u8) -> Result<Self, E>
Create a new Buttons instance with a custom address.
Sourcepub fn read(&mut self) -> Result<ButtonState, E>
pub fn read(&mut self) -> Result<ButtonState, E>
Read the current button states.
Returns a ButtonState struct with the pressed state of each button.
Sourcepub fn state(&self) -> ButtonState
pub fn state(&self) -> ButtonState
Get the last read button state without performing I2C communication.
Check if button A is pressed (uses cached state).
Check if button B is pressed (uses cached state).
Check if button C is pressed (uses cached state).
Sourcepub fn update_leds(&mut self) -> Result<(), E>
pub fn update_leds(&mut self) -> Result<(), E>
Update the LED states on the device.
This writes the current LED states to the hardware.
Sourcepub fn set_leds(&mut self, a: bool, b: bool, c: bool) -> Result<(), E>
pub fn set_leds(&mut self, a: bool, b: bool, c: bool) -> Result<(), E>
Set all LED states at once and update the hardware.
Sourcepub fn all_leds_off(&mut self) -> Result<(), E>
pub fn all_leds_off(&mut self) -> Result<(), E>
Turn all LEDs off.
Sourcepub fn all_leds_on(&mut self) -> Result<(), E>
pub fn all_leds_on(&mut self) -> Result<(), E>
Turn all LEDs on.
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more