A rotary encoder library for embedded rust applications
Add the package via Cargo: rotary-encoder-embedded = "0.0.1"
Note: Quick example based on the stm32h7xx-hal
.
```
static ROTARY_ENCODER: Mutex
fn main() -> ! { // ... Initialize DT and CLK pins as desired. Typically PullUp Push-Pull. // ... Initialize interrupt on rising and falling edge // ... Initialize a timer to periodically update rotary-encoder and other control systems
interrupt::free(|cs| {
ROTARY_ENCODER.borrow(cs).replace(Some(
RotaryEncoder::new(
rotary_dt,
rotary_clk,
Option::None, // optional velocity_inc_factor
Option::None, // optional velocity_dec_factor
Option::None, // optional velocity_action_ms
)
));
});
loop {}
}
fn TIM1() {
// Periodic timer update interrupt vector
interrupt::free(|cs| {
if let Some(ref mut rotaryencoder) = ROTARYENCODER.borrow(cs).borrowmut().derefmut() {
// Note: This could also be run inside the main loop.
// The rotaryencoders internal velocity is decremented by velocity_dec_factor
when
// this function is called
rotaryencoder.tick();
}
});
}
fn handlerotary(rotaryencoder: &mut RotaryEncoder) { let current_time = ... // Get this NaiveDateTime based from your RTC or SysTick handler
// Update the state of the rotary-encoder, computing its current direction and angular velocity
rotary_encoder.update(current_time);
// Get the rotary values
let direction = rotary_encoder.direction();
let velocity = rotary_encoder.velocity();
if direction == Direction::Clockwise {
// Increment some value
} else if direction == Direction::AntiClockwise {
// Decrement some value
}
}
fn EXTI1() { // DT rising or falling edge interrupt interrupt::free(|cs| { if let Some(ref mut rotaryencoder) = ROTARYENCODER.borrow(cs).borrowmut().derefmut() { // Clear DT GPIO EXTI interrupt rotaryencoder .borrowpins() .0 .clearinterruptpending_bit();
handle_rotary(rotary_encoder);
}
});
}
fn EXTI2() { // CLK rising or falling edge interrupt interrupt::free(|cs| { if let Some(ref mut rotaryencoder) = ROTARYENCODER.borrow(cs).borrowmut().derefmut() { // Clear CLK GPIO EXTI interrupt rotaryencoder .borrowpins() .1 .clearinterruptpending_bit();
handle_rotary(rotary_encoder);
}
});
}
```