Custom Timer#
Introduction#
The timer class is used to keep time and interact with a timer independent of the Brain’s timer.
Class Constructor#
timer();
Class Destructor#
Destroys the timer object and releases associated resources.
~timer();
Parameters#
This constructor does not accept any parameters.
Examples#
// Create a timer instance
timer MyTimer = timer();
Member Functions#
The timer class includes the following member functions:
clear— Resets the timer to zero.time— Returns the elapsed time since the timer started.event— Calls a function after a specified number of milliseconds, with optional arguments.
Before calling any timer member functions, a timer instance must be created, as shown below:
// Create a timer instance
timer MyTimer = timer();
clear#
Sets the timer to zero.
Available Functionsvoid clear();
This function does not accept any parameters.
Return ValuesThis function does not return a value.
Exampleswhile (true) {
// Clear the timer every time the screen is pressed
if (Brain.Screen.pressing()) {
MyTimer.clear();
}
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Time: %.2f", MyTimer.time(msec));
wait(50, msec);
}
time#
Returns the current value of the timer.
Available Functions1 — Returns the elapsed time in milliseconds.
uint32_t time() const;
Parameters2 — Returns the elapsed time in the specified time units.
double time( timeUnits units ) const;
Parameter |
Type |
Description |
|---|---|---|
|
|
The units used to return the elapsed time:
|
The parameterless function (1) returns a
uint32_trepresenting the elapsed time in milliseconds.The function with a
timeUnitsparameter (2) returns adoublerepresenting the elapsed time in the specified units.
The timer begins counting when the custom timer is constructed.
Calling
clearresets the timer value to zero.
// Display the timer as it ticks up
while (true) {
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Time: %.2f", MyTimer.time(sec));
wait(50, msec);
}
event#
Registers a callback function to be executed after a specified delay.
Available Functions1 — Registers a callback to run after a specified delay and passes a user-defined value to the callback.
static void event( void (*callback)(void *), uint32_t value );
Parameters2 — Registers a callback to run after a specified delay.
static void event( void (*callback)(void), uint32_t value );
Parameter |
Type |
Description |
|---|---|---|
|
|
A function that will be called after the specified delay and receives a user-defined argument. |
|
|
A function that will be called after the specified delay. |
|
|
The delay, in milliseconds, before the callback function is executed. |
This function does not return a value.
NotesThis function schedules a one-time callback; it does not repeat automatically.
The callback function must return
void.Multiple events may be scheduled at the same time.
// Create a function to turn the screen orange
void turnOrange() {
Brain.Screen.clearScreen(orange);
}
// Call turnOrange after 2 seconds
MyTimer.event(turnOrange, 2000);