#![deny(unsafe_code)]
#![no_main]
#![no_std]
use panic_halt as _;
use cortex_m_rt::entry;
use embedded_hal::Direction as RotaryDirection;
use stm32f4xx_hal::{pac, prelude::*, qei::Qei};
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().expect("Failed to get stm32 peripherals");
let cp = cortex_m::peripheral::Peripherals::take().expect("Failed to get cortex_m peripherals");
let gpioc = dp.GPIOC.split();
let mut led = gpioc.pc13.into_push_pull_output();
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze();
let mut delay = cp.SYST.delay(&clocks);
let gpioa = dp.GPIOA.split();
let rotary_encoder_pins = (gpioa.pa0.into_alternate(), gpioa.pa1.into_alternate());
let rotary_encoder_timer = dp.TIM2;
let rotary_encoder = Qei::new(rotary_encoder_timer, rotary_encoder_pins);
let mut current_count = rotary_encoder.count();
loop {
let new_count = rotary_encoder.count();
if new_count != current_count {
let _diff = new_count.wrapping_sub(current_count) as i16;
match rotary_encoder.direction() {
RotaryDirection::Upcounting => led.set_low(),
RotaryDirection::Downcounting => led.set_high(),
}
current_count = new_count;
}
delay.delay_ms(10_u32);
}
}