xjjak / LapCal

Building gloves that enable typing on a 34-key keyboard without an actual physical keyboard using IMUs and machine learning.
Other
2 stars 0 forks source link

[ERROR] Fix reset of task_reset_fifos_flag in the task callback #12

Closed xjjak closed 7 months ago

xjjak commented 7 months ago

Accessing the void* parameters in the task callback works:

void task_fifo_reset(void *flag) {
    Serial.print("Running on other core: ");
    Serial.println(xPortGetCoreID());
    for(;;){
        if (*((bool *) flag)){
            Serial.println("Calling buf reset");
            reset_all_bufs();
            *(bool*)flag = false;
        }
    }
}

Setting the flag back to false however doesn't work as expected as task_reset_fifos_flag remains true. *(bool*)flag = false; needs to be fixed.

xjjak commented 7 months ago

Look into EventGroups. This seems to be the proper way to handle events through flags.

xjjak commented 7 months ago

This could indeed be fixed by using an xEventGroup with the bit at index 0 indicating whether the buffers should be reset or not.

void task_fifo_reset(void *flag) {
    const TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;
    for(;;){
        if (xEventGroupWaitBits(FifoResetEventGroup, BIT_0, pdFALSE, pdTRUE, xTicksToWait) == pdPASS){
            reset_all_bufs();
            xEventGroupClearBits(FifoResetEventGroup, BIT_0);
        } else {
            Serial.println("Something went wrong waiting for events");
        }
    }
}

The task code now looks like this and utilizes xEventGroupWaitBits to wait for the 0-bit to change, with a timeout of 100ms, which may need more consideration in the future.