Skip to content

Commit 38578c4

Browse files
committed
feat(sec-touch): prioritize SET tasks over pending GET tasks in the queue
Previously, user-triggered SET tasks (e.g. fan speed changes) were appended to the back of the task queue, forcing them to wait behind all pending periodic GET polls before being dispatched to the hardware. add_set_task() now inserts incoming SET tasks after any already-queued SET tasks and before the first GET task. This means a user action is picked up as soon as the current in-flight task completes, regardless of how many GET polls are waiting. When multiple SET tasks are submitted in quick succession they all land at the front in submission order and are dispatched before any GET resumes. No changes to the dispatch loop, watchdog, or cleanup logic — the in-flight task is already removed from the deque when dispatched, so the priority insert naturally lands at position 0 while waiting for a response.
1 parent c8269dd commit 38578c4

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

components/sec_touch/sec_touch.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ void SECTouchComponent::loop() {
7171
(uint8_t) this->incoming_message.buffer[i]);
7272
}
7373
ESP_LOGW(TAG, "[watchdog] Task of type %s for property_id %d timed out — partial buffer (%d bytes: %s)",
74-
EnumToString::TaskType(this->current_running_task_type), this->current_running_task_property_id_,
75-
plen, hex_buf);
74+
EnumToString::TaskType(this->current_running_task_type), this->current_running_task_property_id_, plen,
75+
hex_buf);
7676
} else {
7777
ESP_LOGW(TAG, "[watchdog] Task of type %s for property_id %d timed out — no response received",
7878
EnumToString::TaskType(this->current_running_task_type), this->current_running_task_property_id_);
@@ -126,8 +126,7 @@ void SECTouchComponent::loop() {
126126
}
127127

128128
if (!got_etx) {
129-
ESP_LOGD(TAG, " Partial message in buffer (%d bytes), waiting for more",
130-
this->incoming_message.buffer_index + 1);
129+
ESP_LOGD(TAG, " Partial message in buffer (%d bytes), waiting for more", this->incoming_message.buffer_index + 1);
131130
return;
132131
}
133132

@@ -298,8 +297,14 @@ void SECTouchComponent::exit_scan_mode() {
298297
}
299298

300299
void SECTouchComponent::add_set_task(std::unique_ptr<SetDataTask> task) {
301-
ESP_LOGD(TAG, "add_set_task");
302-
this->data_task_queue.push_back(std::move(task));
300+
auto it = this->data_task_queue.begin();
301+
while (it != this->data_task_queue.end() && (*it)->get_task_type() == TaskType::SET_DATA) {
302+
++it;
303+
}
304+
ESP_LOGD(TAG, "add_set_task: inserting at priority position %d of %d", (int) (it - this->data_task_queue.begin()),
305+
(int) this->data_task_queue.size());
306+
307+
this->data_task_queue.insert(it, std::move(task));
303308

304309
// WARNING: Do not add get tasks to update here. For now, we will just wait for the usual update cycle
305310
// if you add a get task here a recursive loop will be created (TODO?)
@@ -392,11 +397,14 @@ Now, we need to extract the parts of the message. It can be either:
392397
// Defensive: ensure message starts with STX and ends with ETX
393398
if (len < 7 || static_cast<uint8_t>(buf[0]) != STX || static_cast<uint8_t>(buf[len - 1]) != ETX) {
394399
char hex_buf[128];
400+
hex_buf[0] = '\0';
395401
int hex_pos = 0;
396402
for (int i = 0; i < len && hex_pos < (int) sizeof(hex_buf) - 4; i++) {
397403
hex_pos += snprintf(hex_buf + hex_pos, sizeof(hex_buf) - hex_pos, "%02X ", (uint8_t) buf[i]);
398404
}
399-
ESP_LOGE(TAG_UART, " [process_data] Invalid message format (len=%d hex: %s). Task Failed", len, hex_buf);
405+
ESP_LOGE(TAG_UART, " [process_data] Invalid message format (task=%s property_id=%d len=%d hex: %s). Task Failed",
406+
EnumToString::TaskType(this->current_running_task_type), this->current_running_task_property_id_, len,
407+
hex_buf);
400408
this->cleanup_after_task_complete(true);
401409
return;
402410
}
@@ -416,13 +424,14 @@ Now, we need to extract the parts of the message. It can be either:
416424
}
417425
if (tab1 == -1 || tab2 == -1 || tab3 == -1) {
418426
char hex_buf[128];
427+
hex_buf[0] = '\0';
419428
int hex_pos = 0;
420429
for (int i = 0; i < len && hex_pos < (int) sizeof(hex_buf) - 4; i++) {
421430
hex_pos += snprintf(hex_buf + hex_pos, sizeof(hex_buf) - hex_pos, "%02X ", (uint8_t) buf[i]);
422431
}
423-
ESP_LOGE(TAG_UART, " [process_data] Not enough TABs in message (task=%s property_id=%d len=%d hex: %s). Task Failed",
424-
EnumToString::TaskType(this->current_running_task_type), this->current_running_task_property_id_, len,
425-
hex_buf);
432+
ESP_LOGE(
433+
TAG_UART, " [process_data] Not enough TABs in message (task=%s property_id=%d len=%d hex: %s). Task Failed",
434+
EnumToString::TaskType(this->current_running_task_type), this->current_running_task_property_id_, len, hex_buf);
426435
this->cleanup_after_task_complete(true);
427436
return;
428437
}

0 commit comments

Comments
 (0)