About freeRtos

static void taskAuto(void *para)
{
    while (true)
    {
        uint32_t now = pros::millis();
/    /XXXX
        pros::Task::delay_until(&now, 10);
    }
}

That’s what I usually write.

static void taskAuto(void *para)
{
    uint32_t now = pros::millis();
    while (true)
    {
/  /XXXX
        pros::Task::delay_until(&now, 10);
    }
}

I see that often in okapi.
“Pros:: delay” pass variables. Not references. Why can it be written like this?

Looking at the official example, I think I know.

yes, as you can see in the official example, the first argument to task_delay_until (or vTaskDelayUntil) is a pointer to the “previous wake time.” in this case, the & (ampersand) operator is not giving a reference, but is instead passing the address of now in memory. it’s just quicker than having to do something like

uint32_t* now = malloc(sizeof(*now));
*now = millis();
// ...
task_delay_until(now, 10);
2 Likes

Now the students are too strong. I can’t answer all the questions . I don’t feel like a qualified teacher.:rofl:
Students prefer to use “okapi”
Students don’t like my “ncrapi” :sleepy:

Well, it is passing a reference. And you don’t need to allocate memory to pass a pointer instead. Good old API defined and used as:

void task_delay_until(uint32_t *nowPtr, uint32_t delay);

uint32_t now;
task_delay_until(&now, 10);

Is much more readable. Using non-const reference in APIs is generally quite a poor taste. It obfuscates the fact that the “now” is an “output argument”, something possibly written to by the function, while explicitly requiring a naked pointer makes that quite obvious.
The OP question just shows why…

1 Like

*Not a c++ reference

1 Like

Also, one thing to note is that you should keep the definition of now outside the loop.

From the documentation you linked:

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().

1 Like