DavidAntliff / esp32-ds18b20-example

ESP32-compatible example for Maxim Integrated DS18B20 Programmable Resolution 1-Wire Digital Thermometer.
MIT License
108 stars 34 forks source link

Fix last_wake_time reset to current time on each loop iteration. #22

Closed hg closed 3 years ago

hg commented 3 years ago

Thank you for the example and this wonderful library, they've been very helpful.

There's a small issue with using vTaskDelayUntil, which (I think?) makes it perform the same as vTaskDelay: the last wake time is reset on each measurement loop iteration.

If we look at the FreeRTOS docs for this function:

@param pxPreviousWakeTime Pointer to a variable that holds the time at which the
task was last unblocked.  The variable must be initialised with the current time
prior to its first use (see the example below).  Following this the variable is
automatically updated within vTaskDelayUntil ().

and the accompanying example:

// Perform an action every 10 ticks.
void vTaskFunction( void * pvParameters )
{
    TickType_t xLastWakeTime;
    const TickType_t xFrequency = 10;

    // Initialise the xLastWakeTime variable with the current time.
    xLastWakeTime = xTaskGetTickCount ();
    for( ;; )
    {
           // Wait for the next cycle.
           vTaskDelayUntil( &xLastWakeTime, xFrequency );

           // Perform action here.
    }
}
DavidAntliff commented 3 years ago

Huh, funny, seems so obvious now you mention it! :)

Thank you for the PR. As far as I can tell your change is correct. I'll merge it soon, just checking my other projects in case I inadvertently copy/pasted it into those...

DavidAntliff commented 3 years ago

Thank you for this - seems I've been making this mistake for a long time, in a lot of projects. I appreciate the heads-up.