#![deny(missing_docs)]
#![deny(warnings)]
#![no_std]
use embedded_hal::digital::v2::InputPin;
pub mod angular_velocity;
pub mod standard;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Direction {
None,
Clockwise,
Anticlockwise,
}
pub struct RotaryEncoder<MODE, DT, CLK> {
mode: MODE,
pin_dt: DT,
pin_clk: CLK,
}
impl<MODE, DT, CLK> RotaryEncoder<MODE, DT, CLK>
where
DT: InputPin,
CLK: InputPin,
{
pub fn pins_mut(&mut self) -> (&mut DT, &mut CLK) {
(&mut self.pin_dt, &mut self.pin_clk)
}
pub fn release(self) -> (DT, CLK) {
(self.pin_dt, self.pin_clk)
}
pub fn mode(&mut self) -> &mut MODE {
&mut self.mode
}
}
pub struct InitalizeMode;
impl<DT, CLK> RotaryEncoder<InitalizeMode, DT, CLK>
where
DT: InputPin,
CLK: InputPin,
{
pub fn new(pin_dt: DT, pin_clk: CLK) -> Self {
RotaryEncoder {
pin_dt,
pin_clk,
mode: InitalizeMode {},
}
}
}
#[cfg(test)]
mod test {
use crate::{
angular_velocity::AngularVelocityMode, standard::StandardMode, Direction, RotaryEncoder,
};
use embedded_hal_mock::eh0::pin::{Mock, State, Transaction};
#[test]
fn standard_mode_api() {
let expectations = [Transaction::get(State::High)];
let dt = Mock::new(&expectations);
let clk = Mock::new(&expectations);
let mut encoder = RotaryEncoder::new(dt, clk).into_standard_mode();
let _dir = encoder.update();
let mut raw_encoder = StandardMode::new();
let _dir = raw_encoder.update(true, false);
let (mut dt, mut clk) = encoder.release();
dt.done();
clk.done();
}
#[test]
fn angular_velocity_mode_api() {
let expectations = [Transaction::get(State::High)];
let dt = Mock::new(&expectations);
let clk = Mock::new(&expectations);
let mut encoder = RotaryEncoder::new(dt, clk).into_angular_velocity_mode();
let dir = encoder.update(2);
assert_eq!(dir, Direction::None);
let mut raw_encoder = AngularVelocityMode::new();
let _dir = raw_encoder.update(false, false, 100);
assert_eq!(dir, Direction::None);
let (mut dt, mut clk) = encoder.release();
dt.done();
clk.done();
}
}