incremental/
incremental.rs

1use quadrature_decoder::{FullStep, HalfStep, IncrementalDecoder, QuadStep, StepMode};
2
3fn decode<S, I>(decoder: &mut IncrementalDecoder<S>, pulse_trains: I)
4where
5    S: StepMode,
6    I: Iterator<Item = (bool, bool)>,
7{
8    println!("Decoder is at counter: {:?}.", decoder.counter());
9    println!();
10
11    for (a, b) in pulse_trains {
12        println!("Reading pulses: (a: {a:?}, b: {b:?})");
13        match decoder.update(a, b) {
14            Ok(Some(change)) => println!("Change detected: {:?}.", change),
15            Ok(None) => println!("No change detected."),
16            Err(error) => println!("Error detected: {:?}.", error),
17        }
18        println!("Decoder is at counter: {:?}.", decoder.counter());
19        println!();
20    }
21}
22
23fn main() {
24    let a: Vec<bool> = vec![false, false, true, true, false, false, true, true];
25    let b: Vec<bool> = vec![true, false, false, true, true, false, false, true];
26
27    println!("Full-step decoder:");
28    let mut full_step_decoder = IncrementalDecoder::<FullStep>::default();
29    decode(
30        &mut full_step_decoder,
31        a.iter().cloned().zip(b.iter().cloned()),
32    );
33    println!();
34
35    println!("Half-step decoder:");
36    let mut half_step_decoder = IncrementalDecoder::<HalfStep>::default();
37    decode(
38        &mut half_step_decoder,
39        a.iter().cloned().zip(b.iter().cloned()),
40    );
41    println!();
42
43    println!("Quad-step decoder:");
44    let mut quad_step_decoder = IncrementalDecoder::<QuadStep>::default();
45    decode(
46        &mut quad_step_decoder,
47        a.iter().cloned().zip(b.iter().cloned()),
48    );
49    println!();
50}