rotary-encoder-embedded
A rotary encoder library for embedded rust applications
features
example
All examples are based on the stm32h7xx-hal
, but are compatible with any project using embedded-hal
.
Its highly recommended to use the GPIO Interrupt driven implementation. Interrupts should occur on rising and falling edges for both CLK
and DT
.
simple example
fn main() -> ! {
let mut rotary_encoder = RotaryEncoder::new(
rotary_dt,
rotary_clk,
);
rotary_encoder.set_sensitivity(Sensitivity::Low);
loop {
rotary_encoder.update();
let direction = rotary_encoder.direction();
if direction == Direction::Clockwise {
} else if direction == Direction::AntiClockwise {
}
}
}
interrupt driven example
static ROTARY_ENCODER: Mutex<RefCell<Option<RotaryEncoder>>> = Mutex::new(RefCell::new(None));
fn main() -> ! {
interrupt::free(|cs| {
ROTARY_ENCODER.borrow(cs).replace(Some(
RotaryEncoder::new(
rotary_dt,
rotary_clk,
)
));
});
loop {}
}
fn handle_rotary(rotary_encoder: &mut RotaryEncoder) {
rotary_encoder.update();
let direction = rotary_encoder.direction();
if direction == Direction::Clockwise {
} else if direction == Direction::AntiClockwise {
}
}
#[interrupt]
fn EXTI1() {
interrupt::free(|cs| {
if let Some(ref mut rotary_encoder) = ROTARY_ENCODER.borrow(cs).borrow_mut().deref_mut() {
rotary_encoder
.borrow_pins()
.0
.clear_interrupt_pending_bit();
handle_rotary(rotary_encoder);
}
});
}
#[interrupt]
fn EXTI2() {
interrupt::free(|cs| {
if let Some(ref mut rotary_encoder) = ROTARY_ENCODER.borrow(cs).borrow_mut().deref_mut() {
rotary_encoder
.borrow_pins()
.1
.clear_interrupt_pending_bit();
handle_rotary(rotary_encoder);
}
});
}
angular velocity example
If angular velocity is required, then the following example could be used:
fn main() -> ! {
let mut rotary_encoder = RotaryEncoderWithVelocity::new(
rotary_dt,
rotary_clk,
Option::None,
Option::None,
Option::None,
);
rotary_encoder.borrow_inner().set_sensitivity(Sensitivity::Low);
loop {
rotary_encoder.update(current_time);
let direction = rotary_encoder.direction();
let velocity = rotary_encoder.velocity();
if direction == Direction::Clockwise {
} else if direction == Direction::AntiClockwise {
}
rotary_encoder.decay_velocity();
}
}