Move touch polling to FreeRTOS task (fix long press during playback)

Long-press detection was in loop() which blocks during TCP audio handling.
Moved to dedicated touchTask on Core 1 that polls every 20ms regardless
of what loop() is doing.
This commit is contained in:
justLV 2026-03-27 19:15:16 -07:00
parent 9dc9abf753
commit daeaba9bf8

View file

@ -326,6 +326,7 @@ void setup()
xTaskCreatePinnedToCore(micTask, "MicTask", 4096, NULL, 1, NULL, 1);
xTaskCreatePinnedToCore(updateLedTask, "updateLedTask", 2048, NULL, 2, NULL, 1);
xTaskCreatePinnedToCore(touchTask, "TouchTask", 2048, NULL, 2, NULL, 1);
}
void loop()
@ -408,23 +409,6 @@ void loop()
}
}
// Long-press detection: ISR records touch-start, we poll for release
if (centerTouchActive) {
uint16_t touchVal = touchRead(T_C);
unsigned long elapsed = millis() - touchStartTimeCenter;
if (touchVal > 1800) { // Finger released
centerTouchActive = false;
lastTouchTimeCenter = millis();
if (elapsed >= LONG_PRESS_MS) {
handleLongPress();
} else {
handleShortPress();
}
}
}
WiFiClient client = tcpServer.available();
if (client)
{
@ -1025,6 +1009,28 @@ void setLed(uint8_t r, uint8_t g, uint8_t b, uint8_t level, uint8_t fade)
}
// volume currently implemented as header from server
void touchTask(void *parameter)
{
while (1) {
if (centerTouchActive) {
uint16_t touchVal = touchRead(T_C);
unsigned long elapsed = millis() - touchStartTimeCenter;
if (touchVal > 1800) { // Finger released
centerTouchActive = false;
lastTouchTimeCenter = millis();
if (elapsed >= LONG_PRESS_MS) {
handleLongPress();
} else {
handleShortPress();
}
}
}
vTaskDelay(pdMS_TO_TICKS(20)); // Poll every 20ms
}
}
void gotTouch1()
{
unsigned long currentTime = millis();