From 334d9ce8ec92372ea6842545fe99c00aaf892394 Mon Sep 17 00:00:00 2001
From: moon2 <moon2protonmail@protonmail.com>
Date: Mon, 20 Nov 2023 03:36:24 +0100
Subject: [PATCH] captouch: disconnect idle bottom petals from bias.

mitigates floating flow3r captouch threshold issues.
side effects unknown but there probably are some.
it's nice not having to care about EMI n'est-ce pas?
---
 components/flow3r_bsp/flow3r_bsp_ad7147.c    | 3 +++
 components/flow3r_bsp/flow3r_bsp_ad7147.h    | 2 ++
 components/flow3r_bsp/flow3r_bsp_ad7147_hw.c | 5 +++--
 components/flow3r_bsp/flow3r_bsp_ad7147_hw.h | 4 +++-
 components/flow3r_bsp/flow3r_bsp_captouch.c  | 2 ++
 components/st3m/st3m_captouch.c              | 5 ++---
 6 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/components/flow3r_bsp/flow3r_bsp_ad7147.c b/components/flow3r_bsp/flow3r_bsp_ad7147.c
index b45118cc7a..8649ea54c4 100644
--- a/components/flow3r_bsp/flow3r_bsp_ad7147.c
+++ b/components/flow3r_bsp/flow3r_bsp_ad7147.c
@@ -43,6 +43,9 @@ static esp_err_t _sequence_request(ad7147_chip_t *chip, bool reprogram) {
         int8_t offset = chip->channels[channel].afe_offset;
         seq_out.channels[i] = channel;
         seq_out.pos_afe_offsets[i] = offset;
+        // seq_out.idle_to_bias[i] = !(chip->is_bot && (channel < 10)); // jumpy
+        // petal 2
+        seq_out.idle_to_bias[i] = !chip->is_bot;
     }
 
     esp_err_t ret;
diff --git a/components/flow3r_bsp/flow3r_bsp_ad7147.h b/components/flow3r_bsp/flow3r_bsp_ad7147.h
index 42d8d2cbc1..47f2889e7f 100644
--- a/components/flow3r_bsp/flow3r_bsp_ad7147.h
+++ b/components/flow3r_bsp/flow3r_bsp_ad7147.h
@@ -40,6 +40,8 @@ typedef struct {
 typedef struct {
     // Opaque name used to prefix log messages.
     const char *name;
+    // True for bottom chip, false for top
+    bool is_bot;
 
     // [0, n_channels) are the expected connected channels to the inputs of the
     // chip.
diff --git a/components/flow3r_bsp/flow3r_bsp_ad7147_hw.c b/components/flow3r_bsp/flow3r_bsp_ad7147_hw.c
index d3f10b731e..d33be94320 100644
--- a/components/flow3r_bsp/flow3r_bsp_ad7147_hw.c
+++ b/components/flow3r_bsp/flow3r_bsp_ad7147_hw.c
@@ -7,7 +7,7 @@
 
 #define TIMEOUT_MS 1000
 
-#define CIN CDC_NONE 0
+#define CIN_CDC_NONE 0
 #define CIN_CDC_NEG 1
 #define CIN_CDC_POS 2
 #define CIN_BIAS 3
@@ -281,8 +281,9 @@ esp_err_t ad7147_hw_configure_stages(ad7147_hw_t *device,
                                      bool reprogram) {
     // Reset all stage/channel configuration.
     for (size_t i = 0; i < 12; i++) {
+        uint8_t idle_con = seq->idle_to_bias[i] ? CIN_BIAS : CIN_CDC_NONE;
         for (int8_t j = 0; j < 13; j++) {
-            device->stage_config[i].cinX_connection_setup[j] = CIN_BIAS;
+            device->stage_config[i].cinX_connection_setup[j] = idle_con;
         }
         device->dev_config.stageX_complete_int_enable[i] = false;
     }
diff --git a/components/flow3r_bsp/flow3r_bsp_ad7147_hw.h b/components/flow3r_bsp/flow3r_bsp_ad7147_hw.h
index 5cdfd63b34..3eba6417c6 100644
--- a/components/flow3r_bsp/flow3r_bsp_ad7147_hw.h
+++ b/components/flow3r_bsp/flow3r_bsp_ad7147_hw.h
@@ -100,6 +100,8 @@ typedef struct {
     // For each sequencer stage, AFE offset that this stage should use when
     // sampling the configured channel.
     int8_t pos_afe_offsets[12];
+    // Whether idle pads are supposed to be connected to bias. Awful hack.
+    bool idle_to_bias[12];
 } ad7147_sequence_t;
 
 // Configure sequencer stages.
@@ -112,4 +114,4 @@ esp_err_t ad7147_hw_configure_stages(ad7147_hw_t *device,
 
 // Polls sequencer status from the chip and calls the user callback if new data
 // is available / the sequence finished.
-esp_err_t ad7147_hw_process(ad7147_hw_t *device);
\ No newline at end of file
+esp_err_t ad7147_hw_process(ad7147_hw_t *device);
diff --git a/components/flow3r_bsp/flow3r_bsp_captouch.c b/components/flow3r_bsp/flow3r_bsp_captouch.c
index 34b6977c5e..32bcbe16c7 100644
--- a/components/flow3r_bsp/flow3r_bsp_captouch.c
+++ b/components/flow3r_bsp/flow3r_bsp_captouch.c
@@ -106,6 +106,7 @@ static bool _interrupt_shared = false;
 
 static ad7147_chip_t _top = {
     .name = "top",
+    .is_bot = false,
     .nchannels = 12,
     .sequences = {
         {
@@ -120,6 +121,7 @@ static ad7147_chip_t _top = {
 
 static ad7147_chip_t _bot = {
     .name = "bot",
+    .is_bot = true,
     .nchannels = 13,
     .sequences = {
         /// This is the ideal sequence we want. First, all the bottom sensors.
diff --git a/components/st3m/st3m_captouch.c b/components/st3m/st3m_captouch.c
index fb90c0a53d..590f8ac9da 100644
--- a/components/st3m/st3m_captouch.c
+++ b/components/st3m/st3m_captouch.c
@@ -31,8 +31,7 @@ static bool _calibrating = false;
 static inline void _pad_feed(st3m_petal_pad_state_t *pad, uint16_t data,
                              uint8_t index) {
     bool top = (index % 2) == 0;
-    int32_t thres =
-        top ? (TOP_PETAL_THRESHOLD) : (BOTTOM_PETAL_THRESHOLD);
+    int32_t thres = top ? (TOP_PETAL_THRESHOLD) : (BOTTOM_PETAL_THRESHOLD);
     thres = pad->pressed_prev ? thres - (PETAL_HYSTERESIS)
                               : thres;  // some hysteresis
     pad->raw = data;
@@ -85,7 +84,7 @@ static inline void _petal_process(st3m_petal_state_t *petal, uint8_t index) {
     } else {
         petal->pressure = (petal->base.pressure + petal->tip.pressure) / 2;
         int32_t raw = petal->base.raw + petal->tip.raw;
-        if(index == 5) raw *= 2;
+        if (index == 5) raw *= 2;
         petal->pressed = raw > thres;
         int32_t base = petal->base.raw;
         int32_t tip = petal->tip.raw;
-- 
GitLab