From 815fc367acaa04cd9642dc328969a39b1a876683 Mon Sep 17 00:00:00 2001
From: moon2 <moon2protonmail@protonmail.com>
Date: Mon, 29 May 2023 21:17:51 +0200
Subject: [PATCH] captouch: bugfix in afe saturation logic

---
 components/badge23/captouch.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/components/badge23/captouch.c b/components/badge23/captouch.c
index d2592faec8..4b7539954a 100644
--- a/components/badge23/captouch.c
+++ b/components/badge23/captouch.c
@@ -220,17 +220,13 @@ static int8_t captouch_configure_stage_afe_offset(uint8_t top, uint8_t stage, in
     int8_t sat = 0;
     struct ad714x_chip * chip = chip_bot;
     if(top) chip = chip_top;
-
     int8_t afe = chip->pos_afe_offsets[stage] - chip->neg_afe_offsets[stage];
+    if((afe >= 63) && (delta_afe > 0)) sat = 1;
+    if((afe <= 63) && (delta_afe < 0)) sat = -1;
     afe += delta_afe;
-    if(afe >= 63){
-        afe = 63;
-        sat = 1;
-    }
-    if(afe <= -63){
-        afe = -63;
-        sat = -1;
-    }
+    if(afe >= 63) afe = 63;
+    if(afe <= -63)afe = -63;
+
     if(afe>0){
         chip->pos_afe_offsets[stage] = afe;
         chip->neg_afe_offsets[stage] = 0;
-- 
GitLab