From 1e394891a163b083789ce65e1e4674ad8aba5b69 Mon Sep 17 00:00:00 2001
From: moon2 <moon2protonmail@protonmail.com>
Date: Mon, 24 Apr 2023 18:39:19 +0200
Subject: [PATCH] micropython: captouch calib

---
 badge23/captouch.c                            |  22 ++--
 badge23/captouch.h                            |   1 +
 badge23/espan.c                               | 117 +-----------------
 micropython/ports/esp32/badge23_mp_hardware.c |   9 +-
 python_payload/boot.py                        |   2 +-
 python_payload/cap_touch_demo.py              |  19 ++-
 python_payload/main.py                        |   9 ++
 7 files changed, 47 insertions(+), 132 deletions(-)
 create mode 100644 python_payload/main.py

diff --git a/badge23/captouch.c b/badge23/captouch.c
index e9cb80d5b0..36f14c2c74 100644
--- a/badge23/captouch.c
+++ b/badge23/captouch.c
@@ -160,8 +160,6 @@ void manual_captouch_readout(uint8_t top)
     xQueueSend(gpio_evt_queue, &chip, NULL);
 }
 
-void espan_handle_captouch(uint16_t pressed_top, uint16_t pressed_bot);
-
 static uint16_t pressed_top, pressed_bot;
 void gpio_event_handler(void* arg)
 {
@@ -177,7 +175,6 @@ void gpio_event_handler(void* arg)
 
             if(chip == &chip_top) pressed_top = pressed;
             if(chip == &chip_bot) pressed_bot = pressed;
-            //espan_handle_captouch(pressed_top, pressed_bot);
         }
     }
 }
@@ -186,10 +183,10 @@ static uint8_t top_map[] = {2, 2, 2, 0, 0, 8, 8, 8, 6, 6, 4, 4};
 static uint8_t bot_map[] = {1, 1, 3, 3, 5, 5, 7, 7, 9, 9};
 
 uint16_t read_captouch(){
-
     uint16_t petals = 0;
     uint16_t top = pressed_top;
     uint16_t bot = pressed_bot;
+
     for(int i=0; i<12; i++) {
         if(top  & (1 << i)) {
             petals |= (1<<top_map[i]);
@@ -205,6 +202,11 @@ uint16_t read_captouch(){
     return petals;
 }
 
+void captouch_force_calibration(){
+    ad714x_i2c_write(&chip_top, 2, (1 << 14));
+    ad714x_i2c_write(&chip_bot, 2, (1 << 14));
+}
+
 static void captouch_init_chip(const struct ad714x_chip* chip, const struct ad7147_device_config device_config)
 {
     uint16_t data;
@@ -221,8 +223,7 @@ static void captouch_init_chip(const struct ad714x_chip* chip, const struct ad71
         ad714x_set_stage_config(chip, i, &stage_config);
     }
 
-    // Force calibration
-    ad714x_i2c_write(chip, 2, (1 << 14));
+    captouch_force_calibration();
 
     gpio_config_t io_conf = {};
     io_conf.intr_type = GPIO_INTR_NEGEDGE;
@@ -232,15 +233,13 @@ static void captouch_init_chip(const struct ad714x_chip* chip, const struct ad71
     io_conf.pull_down_en = 0;
     gpio_config(&io_conf);
 
-    gpio_evt_queue = xQueueCreate(10, sizeof(const struct ad714x_chip*));
-    xTaskCreate(gpio_event_handler, "gpio_event_handler", 2048 * 2, NULL, configMAX_PRIORITIES - 2, NULL);
-    gpio_isr_handler_add(chip->gpio, gpio_isr_handler, (void *)chip);
+    // gpio_isr_handler_add(chip->gpio, gpio_isr_handler, (void *)chip);
 
 }
 
 void captouch_init(void)
 {
-    gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
+    //gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
     captouch_init_chip(&chip_top, (struct ad7147_device_config){.sequence_stage_num = 11,
                                                  .decimation = 1,
                                                  .stage0_cal_en = 1,
@@ -294,6 +293,9 @@ void captouch_init(void)
                                                  .stage8_high_int_enable = 1,
                                                  .stage9_high_int_enable = 1,
                                                  });
+
+    gpio_evt_queue = xQueueCreate(10, sizeof(const struct ad714x_chip*));
+    xTaskCreate(gpio_event_handler, "gpio_event_handler", 2048 * 2, NULL, configMAX_PRIORITIES - 2, NULL);
 }
 
 static void captouch_print_debug_info_chip(const struct ad714x_chip* chip)
diff --git a/badge23/captouch.h b/badge23/captouch.h
index 46275f47bf..d5dfd9412a 100644
--- a/badge23/captouch.h
+++ b/badge23/captouch.h
@@ -6,4 +6,5 @@ void captouch_print_debug_info(void);
 void gpio_event_handler(void * arg);
 void manual_captouch_readout(uint8_t top);
 void captouch_get_cross(int paddle, int * x, int * y);
+void captouch_force_calibration();
 uint16_t read_captouch();
diff --git a/badge23/espan.c b/badge23/espan.c
index 647b47d4af..5aa2eb8ed4 100644
--- a/badge23/espan.c
+++ b/badge23/espan.c
@@ -23,7 +23,6 @@ static const char *TAG = "espan";
 #define CONFIG_I2C_MASTER_SDA 10
 #define CONFIG_I2C_MASTER_SCL 9
 
-
 static esp_err_t i2c_master_init(void)
 {
     int i2c_master_port = I2C_MASTER_NUM;
@@ -42,53 +41,7 @@ static esp_err_t i2c_master_init(void)
     return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
 }
 
-// channel -> paddle
-uint8_t top_map[] = {2, 2, 2, 0, 0, 8, 8, 8, 6, 6, 4, 4};
-uint8_t bot_map[] = {1, 1, 3, 3, 5, 5, 7, 7, 9, 9};
-
-static bool active_paddles[10];
-void espan_handle_captouch(uint16_t pressed_top, uint16_t pressed_bot)
-{
-
-    bool paddles[10] = {0,};
-    for(int i=0; i<12; i++) {
-        if(pressed_top  & (1 << i)) {
-            paddles[top_map[i]] = true;
-        }
-    }
-
-    for(int i=0; i<10; i++) {
-        if(pressed_bot  & (1 << i)) {
-            paddles[bot_map[i]] = true;
-        }
-    }
-
-    bool changed = false;
-    for(int i=0; i<10; i++) {
-        if(active_paddles[i] == false && paddles[i] == true) {
-            //if(!(i == 2 || i == 8)) synth_start(i);
-            //leds_animate(i);
-            active_paddles[i] = true;
-            changed = true;
-        } else if(active_paddles[i] == true && paddles[i] == false) {
-            active_paddles[i] = false;
-            changed = true;
-        //if(!(i == 2 || i == 8)) synth_stop(i);
-        }
-    }
-
-    if(changed) {
-        //display_show(active_paddles);
-    }
-}
-
-#define VIB_DEPTH 0.01
-#define VIB 0
-#define VIOLIN 1
-#define VIOLIN_DECAY 10
-#define VIOLIN_VOL_BOOST 0.004
-#define VIOLIN_SENS_POW 2
-#define CAPTOUCH_POLLING_PERIOD 3
+#define CAPTOUCH_POLLING_PERIOD 10
 
 void os_app_main(void)
 {
@@ -104,79 +57,11 @@ void os_app_main(void)
     captouch_init();
 
     mp_hal_stdout_tx_str("task inits done\n\r");
-    //not sure how slow captouch_get_cross is so duplicating edge detection here;
-    bool prev_petals[10] = {0};
-    //pitch bend as movement relative to inital touch pos to make intonation easier
-    int petal_refs[10] = {0};
-    int petal_tot_prev[10] = {0};
-    int petal_min[10] = {0};
-    int petal_max[10] = {0};
-    int petal_dir_prev[10] = {0};
-    int i = 0;
-    void * asdasd = &i;
     while(1) {
         manual_captouch_readout(1);
         vTaskDelay((CAPTOUCH_POLLING_PERIOD) / portTICK_PERIOD_MS);
         manual_captouch_readout(0);
         vTaskDelay((CAPTOUCH_POLLING_PERIOD) / portTICK_PERIOD_MS);
-
-        continue;
-        /*
-        i = (i + 1) % 10;
-        if(!(i == 2 || i == 8)) continue;
-        if(VIB){
-            if(active_paddles[i]){
-                int x = 0;
-                int y = 0;
-                captouch_get_cross(i, &x, &y);
-                int tot = (x >> 1) + (y >> 1);
-                if(!prev_petals[i]){
-                    prev_petals[i] = 1;
-                    petal_refs[i] = tot;
-                    synth_set_bend(i, 0);
-                } else {
-                    float bend = tot - petal_refs[i];
-                    bend *= VIB_DEPTH;
-                    synth_set_bend(i, bend);
-                }
-            } else {
-                prev_petals[i] = 0;
-            }
-        }
-        if(VIOLIN){
-            if(active_paddles[i]){
-                int x = 0;
-                int y = 0;
-                captouch_get_cross(i, &x, &y);
-                int tot = (x >> 1) + (y >> 1);
-
-                uint8_t dir = tot > petal_tot_prev[i];
-
-                if(dir != petal_dir_prev[i]){
-                    if(dir){
-                        petal_min[i] = petal_tot_prev[i];
-                    } else {
-                        petal_max[i] = petal_tot_prev[i];
-                    }
-                    petal_refs[i] = tot;
-                    float vol = (VIOLIN_VOL_BOOST) * (petal_max[i] - petal_min[i]);
-                    for(int i = 1; i < VIOLIN_SENS_POW; i++){
-                        vol *= vol;
-                    }
-                    vol += synth_get_env(i);
-                    if(vol > 1.) vol = 1;
-                    if(vol < 0.) vol = 0;
-                    synth_set_vol(i, vol);
-                    synth_start(i);
-                }
-                petal_tot_prev[i] = tot;
-                petal_dir_prev[i] = dir;
-            }
-        }
-
-        vTaskDelay(10 / portTICK_PERIOD_MS);
-        //captouch_print_debug_info();
-        */
     }
 
     ESP_ERROR_CHECK(i2c_driver_delete(I2C_MASTER_NUM));
diff --git a/micropython/ports/esp32/badge23_mp_hardware.c b/micropython/ports/esp32/badge23_mp_hardware.c
index 8e5bccbc4c..360f43ccb4 100644
--- a/micropython/ports/esp32/badge23_mp_hardware.c
+++ b/micropython/ports/esp32/badge23_mp_hardware.c
@@ -16,13 +16,19 @@
 
 STATIC mp_obj_t mp_get_captouch(size_t n_args, const mp_obj_t *args) {
     uint16_t captouch = read_captouch();
-    uint8_t pad = mp_obj_get_int(args[0]);
+    uint16_t pad = mp_obj_get_int(args[0]);
     uint8_t output = (captouch >> pad) & 1;
 
     return mp_obj_new_int(output);
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_get_captouch_obj, 1, 2, mp_get_captouch);
 
+STATIC mp_obj_t mp_captouch_autocalib(size_t n_args, const mp_obj_t *args) {
+    captouch_force_calibration();
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_captouch_autocalib_obj, 0, 2, mp_captouch_autocalib);
+
 STATIC mp_obj_t mp_set_global_volume_dB(size_t n_args, const mp_obj_t *args) {
     mp_float_t x = mp_obj_get_float(args[0]);
     int8_t d = x;
@@ -77,6 +83,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_update_leds_obj, 0, 2, mp_update_l
 STATIC const mp_rom_map_elem_t mp_module_hardware_globals_table[] = {
     { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_badge_audio) },
     { MP_ROM_QSTR(MP_QSTR_get_captouch), MP_ROM_PTR(&mp_get_captouch_obj) },
+    { MP_ROM_QSTR(MP_QSTR_captouch_autocalib), MP_ROM_PTR(&mp_captouch_autocalib_obj) },
     { MP_ROM_QSTR(MP_QSTR_set_global_volume_dB), MP_ROM_PTR(&mp_set_global_volume_dB_obj) },
     { MP_ROM_QSTR(MP_QSTR_count_sources), MP_ROM_PTR(&mp_count_sources_obj) },
     { MP_ROM_QSTR(MP_QSTR_dump_all_sources), MP_ROM_PTR(&mp_dump_all_sources_obj) },
diff --git a/python_payload/boot.py b/python_payload/boot.py
index 8d9d516c95..38a85e0666 100644
--- a/python_payload/boot.py
+++ b/python_payload/boot.py
@@ -3,4 +3,4 @@
 #esp.osdebug(None)
 #import webrepl
 #webrepl.start()
-import cap_touch_demo
+import main
diff --git a/python_payload/cap_touch_demo.py b/python_payload/cap_touch_demo.py
index 8933591beb..6a5f3ebcfe 100644
--- a/python_payload/cap_touch_demo.py
+++ b/python_payload/cap_touch_demo.py
@@ -1,6 +1,9 @@
 from synth import tinysynth
 from hardware import *
 import time
+from machine import Pin
+
+p = Pin(0, Pin.IN)
 
 set_global_volume_dB(0)
 synths = []
@@ -11,7 +14,13 @@ for synth in synths:
     synth.decay(100)
     synth.waveform(1)
 
-chords = [[0,3,7,10,12],[-2,2,5,8,10],[-2,3,7,10,14],[-4,0,3,8,12],[-1,2,5,7,11]]
+chords = [\
+[-4,0,3,8,10],\
+[-3,0,5,7,12],\
+[-1,2,5,7,11],\
+[0,3,7,12,14],\
+[3,7,10,14,15]\
+]
 
 chord = chords[3]
 chord_index = -1
@@ -29,11 +38,15 @@ def set_chord(i):
 
 set_chord(3)
 
-def cap_touch_demo_start(delay):
+def cap_touch_demo_start():
     global chord_index
     global chord
+    global p
     while True:
         update_leds()
+        if(p.value() == 0):
+            captouch_autocalib()
+            time.sleep_ms(100)
         for i in range(10):
             if(get_captouch(i)):
                 if(i%2):
@@ -44,5 +57,3 @@ def cap_touch_demo_start(delay):
                     synths[k].tone(chord[k])
                     synths[k].start()
                     print("synth " +str(k))
-            time.sleep_ms(delay)
-
diff --git a/python_payload/main.py b/python_payload/main.py
new file mode 100644
index 0000000000..43337f8097
--- /dev/null
+++ b/python_payload/main.py
@@ -0,0 +1,9 @@
+from machine import Pin
+
+p = Pin(0, Pin.IN)
+
+while (p.value() == 1):
+    pass
+
+import cap_touch_demo
+cap_touch_demo.cap_touch_demo_start()
-- 
GitLab