Timer#

介绍#

The timer class is used to keep time and interact with a timer independent of the Brain’s timer.

类构造函数#

timer();

类析构函数#

Destroys the timer object and releases associated resources.

~timer();

参数#

此构造函数不接受任何参数。

示例#

// Create a timer instance
timer myTimer = timer();

成员功能#

The timer class includes the following member functions:

  • reset — Resets the timer to zero.

  • value — 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:

/* This constructor is required to use a 
custom timer. Replace the values
as needed. */

// Create a timer instance
timer myTimer = timer();

/* This constructor is required when using VS Code.
A Brain is generated automatically at the start of
VEXcode projects. */

// Create the Brain to use Brain.Timer
brain Brain = brain();

reset#

将计时器设置为零。

Available Functions
void reset();

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

Examples
while (true) {
  // Clear the timer every time the screen is pressed
  if (Brain.Screen.pressing()) {
    myTimer.reset();
  }
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Time: %.2f", myTimer.value());
  wait(50, msec);
}

value#

返回计时器的当前值。

Available Functions
double value();

Parameters

此函数不接受任何参数。

Return Values

A double representing the amount of time passed in seconds.

Notes
  • The timer begins counting when the timer is constructed.

  • Calling reset resets the timer value to zero.

Examples
// Display the timer as it ticks up
while (true) {
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Time: %.2f", myTimer.value());

  wait(50, msec);
}

event#

注册一个回调函数,该函数将在指定的延迟后执行。

Available Functions

1 — 注册一个回调函数,使其在指定的延迟后运行,并将用户定义的值传递给该回调函数。

static void event(
    void (*callback)(void *),
    uint32_t value );

2 — 注册一个回调函数,使其在指定的延迟后运行。

static void event(
    void (*callback)(void),
    uint32_t value );

Parameters

范围

类型

描述

callback

void (*)(void *)

一个将在指定延迟后调用并接收用户定义参数的函数。

callback

void (*)(void)

将在指定延迟时间后调用的函数。

value

uint32_t

回调函数执行前的延迟时间(以毫秒为单位)。

Return Values

此函数不返回值。

Notes
  • 此函数会安排一次性回调;它不会自动重复执行。

  • The callback function must return void.

  • 可以同时安排多个活动。

Examples

Define the function (outside of int main())

// Create a function to turn the screen orange
void turnOrange() {
  Brain.Screen.clearScreen(orange);
}

Register the function inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  timer myTimer = timer();
  // Call turnOrange after 2 seconds
  myTimer.event(turnOrange, 2000);
}