m5stack_core/driver/
pcnt.rs1use esp_hal::{
10 gpio::Input,
11 pcnt::{Unit, channel},
12 peripherals::PCNT0_UNIT1,
13};
14
15pub struct PcntDriver {
16 pub pcnt_unit: Unit<'static>,
17}
18
19impl PcntDriver {
20 pub fn get_and_reset(&mut self) -> i16 {
21 let c = self.pcnt_unit.counter.get();
22 self.pcnt_unit.clear();
23 c
24 }
25}
26
27impl PcntDriver {
28 pub fn new(unit: PCNT0_UNIT1<'static>, rpm_pin: Input<'static>) -> Self {
29 let u0 = Unit::new(unit);
30 u0.clear();
31
32 let ch0 = &u0.channel0;
34 ch0.set_edge_signal(rpm_pin);
35 ch0.set_input_mode(channel::EdgeMode::Increment, channel::EdgeMode::Increment);
36
37 u0.listen();
39 u0.resume();
40 Self { pcnt_unit: u0 }
41 }
42}