quadrature_encoder/mode/
rotary.rs

1//! A rotary quadrature encoder's movement.
2
3use quadrature_decoder::Change;
4
5use crate::mode::OperationMode;
6
7use super::Movement;
8
9/// The movement detected by a rotary quadrature encoder.
10#[repr(i8)]
11#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
12pub enum RotaryMovement {
13    /// Clockwise movement.
14    Clockwise = 1,
15    /// Counter-clockwise movement.
16    CounterClockwise = -1,
17}
18
19impl From<Change> for RotaryMovement {
20    /// Interprets quadrature movement as a rotary movement with the following mapping:
21    ///
22    /// - `Change::AB => RotaryMovement::Clockwise`
23    /// - `Change::BA => RotaryMovement::CounterClockwise`
24    fn from(change: Change) -> Self {
25        match change {
26            Change::Positive => Self::Clockwise,
27            Change::Negative => Self::CounterClockwise,
28        }
29    }
30}
31
32impl Movement for RotaryMovement {
33    fn flipped(self) -> Self {
34        match self {
35            Self::Clockwise => Self::CounterClockwise,
36            Self::CounterClockwise => Self::Clockwise,
37        }
38    }
39}
40
41/// The mode of a rotary quadrature encoder.
42pub struct Rotary;
43
44impl OperationMode for Rotary {
45    type Movement = RotaryMovement;
46}