https://github.com/leshow/rotary-encoder-hal
A simple platform agnostic rotary encoder library
https://github.com/leshow/rotary-encoder-hal
Last synced: about 1 year ago
JSON representation
A simple platform agnostic rotary encoder library
- Host: GitHub
- URL: https://github.com/leshow/rotary-encoder-hal
- Owner: leshow
- License: mit
- Created: 2019-10-15T23:03:02.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-02-11T04:57:59.000Z (over 1 year ago)
- Last Synced: 2025-04-11T11:14:34.404Z (about 1 year ago)
- Language: Rust
- Size: 56.6 KB
- Stars: 29
- Watchers: 0
- Forks: 15
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# rotary-encoder-hal
[](https://crates.io/crates/rotary-encoder-hal)
[](https://docs.rs/rotary-encoder-hal)
A simple, platform agnostic rotary encoder library.
An alternate decoder algorithm that is more tolerant of
noise is enabled by the Cargo feature `table-decoder`. It
follows the discussion of noisy decoding
[here](https://www.best-microcontroller-projects.com/rotary-encoder.html).
You can call `update` from an ISR. You may get many spurious
interrupts from switch bounce, though the algorithm handles
them appropriately. When polling `update`, a poll time of
about 1 ms seems to work well.
## Examples
```rust
#![no_std]
#![no_main]
extern crate panic_semihosting;
use cortex_m_rt::entry;
use hal::{delay::Delay, prelude::*, stm32};
use stm32f3xx_hal as hal;
use rotary_encoder_hal::{Direction, Rotary};
#[entry]
fn main() -> ! {
let cp = cortex_m::Peripherals::take().unwrap();
let peripherals = stm32::Peripherals::take().unwrap();
let mut flash = peripherals.FLASH.constrain();
let mut rcc = peripherals.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let mut delay = Delay::new(cp.SYST, clocks);
let mut gpiob = peripherals.GPIOB.split(&mut rcc.ahb);
let pin_a = gpiob
.pb10
.into_pull_up_input(&mut gpiob.moder, &mut gpiob.pupdr);
let pin_b = gpiob
.pb11
.into_pull_up_input(&mut gpiob.moder, &mut gpiob.pupdr);
let mut enc = Rotary::new(pin_a, pin_b);
let mut pos: isize = 0;
loop {
match enc.update().unwrap() {
Direction::Clockwise => {
pos += 1;
}
Direction::CounterClockwise => {
pos -= 1;
}
Direction::None => {}
}
}
}
```