quadrature_encoder/mode/
linear.rs

1//! A linear 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 linear quadrature encoder.
10#[repr(i8)]
11#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
12pub enum LinearMovement {
13    /// Forward movement.
14    Forward = 1,
15    /// Backward movement.
16    Backward = -1,
17}
18
19impl LinearMovement {
20    /// Returns the direction of `self`, flipped.
21    pub fn flipped(self) -> Self {
22        match self {
23            Self::Forward => Self::Backward,
24            Self::Backward => Self::Forward,
25        }
26    }
27}
28
29impl From<Change> for LinearMovement {
30    /// Interprets quadrature movement as a linear movement with the following mapping:
31    ///
32    /// - `Change::AB => LinearMovement::Forward`
33    /// - `Change::BA => LinearMovement::Backward`
34    fn from(change: Change) -> Self {
35        match change {
36            Change::Positive => Self::Forward,
37            Change::Negative => Self::Backward,
38        }
39    }
40}
41
42impl Movement for LinearMovement {
43    fn flipped(self) -> Self {
44        match self {
45            Self::Forward => Self::Backward,
46            Self::Backward => Self::Forward,
47        }
48    }
49}
50
51/// The mode of a linear quadrature encoder.
52pub struct Linear;
53
54impl OperationMode for Linear {
55    type Movement = LinearMovement;
56}