You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Marc Poulhiès e84e47045a Allow for getting &mut for both underlying pins. 2 years ago
.github/workflows fixup! ci: add github actions for linters and crosstesting 2 years ago
src Allow for getting &mut for both underlying pins. 2 years ago
tests initial commit 3 years ago
.gitignore initial commit 3 years ago
Cargo.toml Version number cleanup 2 years ago initial commit 3 years ago Make tests pass 3 years ago


Crate API

A simple, platform agnostic rotary encoder library.


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};

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
        .into_pull_up_input(&mut gpiob.moder, &mut gpiob.pupdr);
    let pin_b = gpiob
        .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 => {}

Alternatively, you can call update from an ISR.