定时器#
介绍#
The timer class is derived from the brain base class and provides functionality for measuring elapsed time, allowing you to track durations and schedule time-based events within a VEX V5 program.
使用权#
Brain.Timer
笔记#
Brain.Timeris an instance of thetimerclass and provides the same functionality.
例子#
/* This constructor is required when using VS Code.
A Brain is generated automatically at the start of
VEXcode projects. */
// Create the V5 Brain
brain Brain = brain();
// Display the timer as it ticks up
while (true) {
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Time: %.2f", Brain.Timer.time());
wait(50, msec);
}
成员功能#
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 sdcard member functions, a brain instance must be created, as shown below:
// Create the V5 Brain
brain Brain = brain();
清除#
将计时器的当前值重置为零。之后,计时器立即从零开始重新计数。
Available Functionsvoid clear();
此函数不接受任何参数。
Return Values此函数不返回值。
Exampleswhile (true) {
// Reset the timer every time the screen is pressed
if (Brain.Screen.pressing()) {
Brain.Timer.clear();
}
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Time: %.2f", Brain.Timer.value());
wait(50, msec);
}
时间#
返回计时器的当前值。
Available Functions1 — 返回经过的时间(以毫秒为单位)。
uint32_t time() const;
Parameters2 — 返回以指定时间单位计算的经过时间。
double time( timeUnits units ) const;
范围 |
类型 |
描述 |
|---|---|---|
|
|
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.
项目开始时,计时器开始计时。
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", Brain.Timer.time());
wait(50, msec);
}
事件#
注册一个回调函数,该函数将在指定的延迟后执行。
Available Functions1 — 注册一个回调函数,使其在指定的延迟后运行,并将用户定义的值传递给该回调函数。
static void event( void (*callback)(void*), uint32_t value );
Parameters2 — 注册一个回调函数,使其在指定的延迟后运行。
static void event( void (*callback)(void), uint32_t value );
范围 |
类型 |
描述 |
|---|---|---|
|
|
一个将在指定延迟后调用并接收用户定义参数的函数。 |
|
|
将在指定延迟时间后调用的函数。 |
|
|
回调函数执行前的延迟时间(以毫秒为单位)。 |
此函数不返回值。
Notes此函数会安排一次性回调;它不会自动重复执行。
The callback function must return
void.可以同时安排多个活动。
// Create a function to turn the screen orange
void turnOrange() {
Brain.Screen.clearScreen(orange);
}
// Call turnOrange after 2 seconds
Brain.Timer.event(turnOrange, 2000);