From 8267491c3d8a0a78495d3a5f48c2537f0971cb51 Mon Sep 17 00:00:00 2001
From: Serge Bazanski <q3k@q3k.org>
Date: Wed, 28 Jun 2023 00:32:21 +0200
Subject: [PATCH] components/st3m: run jacksense in dedicated task

We already moved LEDs to their own task. Let's try doing that for
jacksense too. As long as we don't run out of RAM this should be better
than having larger tasks, as things won't block eachother that much..?
---
 components/badge23/espan.c   | 25 ++-----------------------
 components/st3m/st3m_audio.c | 26 ++++++++++++++++++++------
 components/st3m/st3m_audio.h |  9 ---------
 3 files changed, 22 insertions(+), 38 deletions(-)

diff --git a/components/badge23/espan.c b/components/badge23/espan.c
index 21fbaa98ef..2c76126584 100644
--- a/components/badge23/espan.c
+++ b/components/badge23/espan.c
@@ -1,23 +1,12 @@
 #include "badge23/captouch.h"
 #include "badge23/spio.h"
-
 #include "flow3r_bsp.h"
-#include "st3m_gfx.h"
-#include "st3m_fs.h"
 #include "st3m_audio.h"
-#include "st3m_leds.h"
-
 #include "bl00mbox.h"
 
 #include "esp_log.h"
-#include "driver/i2c.h"
-#include "driver/spi_master.h"
-#include "freertos/timers.h"
-
-#include <stdio.h>
-#include <string.h>
-#include <math.h>
-#include <stdint.h>
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
 
 static const char *TAG = "espan";
 
@@ -32,15 +21,6 @@ static void io_fast_task(void * data){
     }
 }
 
-// read out stuff like jack detection, battery status, usb connection etc.
-static void io_slow_task(void * data){
-    TickType_t last_wake = xTaskGetTickCount();
-    while(1) {
-        vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(100)); // 10 Hz
-        st3m_audio_update_jacksense();
-    }
-}
-
 void badge23_main(void)
 {
     ESP_LOGI(TAG, "Starting on %s...", flow3r_bsp_hw_name);
@@ -53,7 +33,6 @@ void badge23_main(void)
     captouch_force_calibration();
 
     xTaskCreatePinnedToCore(&io_fast_task, "iofast", 4096, NULL, configMAX_PRIORITIES-1, NULL, 0);
-    xTaskCreatePinnedToCore(&io_slow_task, "ioslow", 4096, NULL, configMAX_PRIORITIES-2, NULL, 0);
 
     hw_init_done = 1;
 }
diff --git a/components/st3m/st3m_audio.c b/components/st3m/st3m_audio.c
index 8818a70ebd..3abb26f9ce 100644
--- a/components/st3m/st3m_audio.c
+++ b/components/st3m/st3m_audio.c
@@ -15,7 +15,8 @@ static const char *TAG = "st3m-audio";
 
 #define TIMEOUT_MS 1000
 
-static void _audio_player_task(void* arg);
+static void _audio_player_task(void *data);
+static void _jacksense_update_task(void *data);
 static bool _headphones_connected(void);
 
 // used for exp(vol_dB * NAT_LOG_DB)
@@ -230,7 +231,7 @@ static bool _headphones_connected(void) {
     return state.jacksense.headphones || state.headphones_detection_override;
 }
 
-void st3m_audio_update_jacksense() {
+static void _update_jacksense() {
     flow3r_bsp_audio_jacksense_state_t st;
     flow3r_bsp_audio_read_jacksense(&st);
 
@@ -259,16 +260,18 @@ void st3m_audio_init(void) {
     flow3r_bsp_audio_init();
 
     st3m_audio_input_thru_set_volume_dB(-20);
-    st3m_audio_update_jacksense();
+    _update_jacksense();
     _output_apply(&state.speaker);
     _output_apply(&state.headphones);
 
-    TaskHandle_t handle;
-    xTaskCreate(&_audio_player_task, "audio", 3000, NULL, configMAX_PRIORITIES - 1, &handle);
+    xTaskCreate(&_audio_player_task, "audio", 3000, NULL, configMAX_PRIORITIES - 1, NULL);
+    xTaskCreate(&_jacksense_update_task, "jacksense", 2048, NULL, configMAX_PRIORITIES - 2, NULL);
     ESP_LOGI(TAG, "Audio task started");
 }
 
-static void _audio_player_task(void *arg) {
+static void _audio_player_task(void *data) {
+    (void)data;
+
     int16_t buffer_tx[FLOW3R_BSP_AUDIO_DMA_BUFFER_SIZE * 2];
     int16_t buffer_rx[FLOW3R_BSP_AUDIO_DMA_BUFFER_SIZE * 2];
     memset(buffer_tx, 0, sizeof(buffer_tx));
@@ -323,6 +326,17 @@ static void _audio_player_task(void *arg) {
     }
 }
 
+static void _jacksense_update_task(void * data) {
+    (void)data;
+
+    TickType_t last_wake = xTaskGetTickCount();
+    while(1) {
+        vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(100)); // 10 Hz
+        _update_jacksense();
+    }
+}
+
+
 // BSP wrappers that don't need locking.
 
 void st3m_audio_headphones_line_in_set_hardware_thru(bool enable) {
diff --git a/components/st3m/st3m_audio.h b/components/st3m/st3m_audio.h
index b3216391c5..76fdcb5e37 100644
--- a/components/st3m/st3m_audio.h
+++ b/components/st3m/st3m_audio.h
@@ -32,15 +32,6 @@ void st3m_audio_player_function_dummy(int16_t * rx, int16_t * tx, uint16_t len);
  */
 void st3m_audio_init(void);
 
-/* Polls hardware to check if headphones, headset or line in are plugged
- * into the 3.5mm jacks. If it detects a plug in the headphone jack, speakers
- * are automatically muted. There is no override for this at this moment.
- * 
- * Should be called periodically (100ms ish?) by a low priority task. Requires
- * the I2C lock.
- */
-void st3m_audio_update_jacksense(void);
-
 /* Returns true if headphones with or without microphone were connected to the
  * headphone jack at the last call of st3m_audio_update_jacksense.
  */
-- 
GitLab