From 96fc0a64de7322d42dbedf49c1710e551057fca8 Mon Sep 17 00:00:00 2001
From: Samuel Tardieu <sam@rfc1149.net>
Date: Sat, 27 Jul 2024 09:40:56 +0200
Subject: [PATCH] Separate relative tick count from encoder

---
 controller/src/encoders.rs  | 28 ++++++++++++++++++----------
 controller/src/logic/mod.rs | 10 ++++++++--
 2 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/controller/src/encoders.rs b/controller/src/encoders.rs
index c07b58a..0b14f59 100644
--- a/controller/src/encoders.rs
+++ b/controller/src/encoders.rs
@@ -7,8 +7,6 @@ use embassy_stm32::{
 pub struct Encoders<'a> {
     left: Qei<'a, TIM3>,
     right: Qei<'a, TIM2>,
-    count_left: u16,
-    count_right: u16,
 }
 
 pub struct EncoderValue {
@@ -16,6 +14,11 @@ pub struct EncoderValue {
     direction: Direction,
 }
 
+pub struct Relative {
+    count_left: u16,
+    count_right: u16,
+}
+
 impl core::fmt::Debug for EncoderValue {
     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
         write!(
@@ -36,12 +39,7 @@ impl Encoders<'_> {
         let left = Qei::new(tim3, QeiPin::new_ch1(pa6), QeiPin::new_ch2(pa7));
         let right = Qei::new(tim2, QeiPin::new_ch1(pa0), QeiPin::new_ch2(pa1));
         pac::TIM2.ccer().modify(|w| w.set_ccp(1, true));
-        Self {
-            left,
-            right,
-            count_left: 0,
-            count_right: 0,
-        }
+        Self { left, right }
     }
 
     pub fn read(&self) -> (EncoderValue, EncoderValue) {
@@ -56,9 +54,19 @@ impl Encoders<'_> {
             },
         )
     }
+}
+
+impl Relative {
+    pub fn new(encoders: &Encoders<'_>) -> Self {
+        let (count_left, count_right) = (encoders.left.count(), encoders.right.count());
+        Self {
+            count_left,
+            count_right,
+        }
+    }
 
-    pub fn ticks(&mut self) -> (i16, i16) {
-        let (left, right) = (self.left.count(), self.right.count());
+    pub fn ticks(&mut self, encoders: &Encoders<'_>) -> (i16, i16) {
+        let (left, right) = (encoders.left.count(), encoders.right.count());
         (
             ticks_since(&mut self.count_left, left),
             ticks_since(&mut self.count_right, right),
diff --git a/controller/src/logic/mod.rs b/controller/src/logic/mod.rs
index 5c6d2fb..7d544e4 100644
--- a/controller/src/logic/mod.rs
+++ b/controller/src/logic/mod.rs
@@ -1,4 +1,7 @@
-use crate::{encoders::Encoders, tb6612fng::Tb6612fng};
+use crate::{
+    encoders::{Encoders, Relative},
+    tb6612fng::Tb6612fng,
+};
 use embassy_executor::Spawner;
 use embassy_stm32::{peripherals::WWDG, time::hz};
 use futures::FutureExt;
@@ -14,13 +17,16 @@ pub const I2C_ADDRESS: u8 = 0x57;
 struct State {
     _i2c: I2C2,
     encoders: Encoders<'static>,
+    tick_count: Relative,
 }
 
 impl State {
     fn new(i2c2: I2C2, encoders: Encoders<'static>) -> Self {
+        let tick_count = Relative::new(&encoders);
         Self {
             _i2c: i2c2,
             encoders,
+            tick_count,
         }
     }
 }
@@ -144,7 +150,7 @@ fn process_command(
             response.push(watchdog::get_shutdown_timeout()).unwrap();
         }
         [CMD_ENCODER_TICKS] => {
-            let (left, right) = state.encoders.ticks();
+            let (left, right) = state.tick_count.ticks(&state.encoders);
             let (left, right) = (left.to_le_bytes(), right.to_le_bytes());
             response.push(left[0]).unwrap();
             response.push(left[1]).unwrap();
-- 
GitLab