任务中的 ESP32 FreeRTOS 非阻塞信号量

ESP32 FreeRTOS non-blocking semaphore in task

我有一个等待信号量的任务 运行。然后它必须进入一个无限循环来控制电机位置。

我想在按下停止按钮时中断第二个循环。有没有办法调用xSemaphoreTake()跳出第二个循环又不阻塞控件的while循环?

让我尝试给出一些代码示例:

void startTaskFunction(void *params){ //this is the function
    while (true)
    {
        xSemaphoreTake(StartSema,portMAX_DELAY); //which in order to start waits this Semaphore to be Given
        while(true){

            //do some logic to controll a motor and keep it steady

            /////to stop this while on some stop button if i use the next i think it will 
            //stop and waits on the first run of the loop so there is no control
            xSemaphoreTake(StopSema,portMAX_DELAY);
            break;
            // if i wrap it in an if condition i think it is the same isn't it?

        }
    }
}

您可以立即尝试take the semaphore,如果成功,则跳出循环:

void startTaskFunction(void *params)
{
    while (true) {
        xSemaphoreTake(StartSema, portMAX_DELAY);
        while (true) {
            //do some logic to controll a motor and keep it steady

            if (xSemaphoreTake(StopSema, 0) == pdTRUE) {
                break;
            }
        }
    }
}