Skip to main content

Knob

Struct Knob 

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

Driver for the Modulino Knob module (rotary encoder).

§Example

use modulino::Knob;

let mut knob = Knob::new(i2c)?;

// Set a range for the encoder value
knob.set_range(-100, 100);

loop {
    knob.update()?;
     
    if knob.pressed() {
        println!("Button pressed!");
    }
     
    println!("Value: {}", knob.value());
}

Implementations§

Source§

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

Source

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

Create a new Knob instance with the default address.

Source

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

Create a new Knob instance with a custom address.

Source

pub fn address(&self) -> u8

Get the I2C address.

Source

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

Update the encoder state.

This should be called periodically to read the latest values. Returns true if the state has changed.

Source

pub fn value(&self) -> i16

Get the current encoder value.

Source

pub fn set_value(&mut self, value: i16) -> Result<(), E>

Set the encoder value.

Source

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

Reset the encoder value to 0.

Source

pub fn pressed(&self) -> bool

Check if the button is currently pressed.

Source

pub fn set_range(&mut self, min: i16, max: i16)

Set the value range for the encoder.

The encoder value will be constrained to this range. Pass None to remove the range constraint.

Source

pub fn clear_range(&mut self)

Clear the range constraint.

Source

pub fn range(&self) -> Option<(i16, i16)>

Get the current range, if set.

Source

pub fn rotation_delta(&self, previous_value: i16) -> i16

Get the rotation direction since the last update.

Returns:

  • Positive value for clockwise rotation
  • Negative value for counter-clockwise rotation
  • 0 for no rotation
Source

pub fn release(self) -> I2C

Release the I2C bus.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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