Compare commits

...

1 Commits

Author SHA1 Message Date
Evan Cameron 42cfe51d1a Add example 3 years ago
  1. 6
      Cargo.toml
  2. 40
      examples/encoder.rs

6
Cargo.toml

@ -22,5 +22,7 @@ cortex-m-rt = "0.6.10"
version = "0.3.0"
features = ["rt"]
[badges]
travis-ci = { repository = "leshow/rotary-encoder-hal", branch = "master", service = "github" }
[badges.travis-ci]
repository = "leshow/rotary-encoder-hal"
branch = "master"
service = "github"

40
examples/encoder.rs

@ -0,0 +1,40 @@
#![no_std]
#![no_main]
extern crate panic_itm;
use cortex_m_rt::entry;
use hal::{delay::Delay, prelude::*, stm32};
use rotary_encoder_hal::{Direction, Rotary};
use stm32f3xx_hal as hal;
#[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 => {}
}
}
}
Loading…
Cancel
Save