定时器#

介绍#

timer 类派生自 brain 基类,提供测量经过时间的功能,允许您在 VEX V5 程序中跟踪持续时间和安排基于时间的事件。

使用权#

Brain.Timer

笔记#

  • Brain.Timer 是 timer 类的一个实例,并提供相同的功能。

例子#

/* 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);
}

成员功能#

timer 类包含以下成员函数:

  • clear — 将计时器重置为零。

  • time — 返回自计时器开始以来经过的时间。

  • event — 在指定的毫秒数后调用函数,参数可选。

在调用 timer 成员函数之前,必须先创建一个 brain 实例,如下所示:

// Create the V5 Brain
brain Brain = brain();

clear#

将计时器的当前值重置为零。之后,计时器立即从零开始重新计数。

Available Functions
void clear();

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

Examples
while (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);
}

time#

返回计时器的当前值。

Available Functions

1 — 返回经过的时间(以毫秒为单位)。

uint32_t time() const;

2 — 返回以指定时间单位计算的经过时间。

double time(
    timeUnits units ) const;

Parameters

范围

类型

描述

units

timeUnits

The units used to return the elapsed time:

  • seconds / sec — seconds
  • msec — milliseconds

Return Values
  • 无参数函数 (1) 返回 uint32_t ,表示经过的时间(以毫秒为单位)。

  • 带有 timeUnits 参数 (2) 的函数返回一个 double 表示以指定单位表示的经过时间。

Notes
  • 项目开始时,计时器开始计时。

  • 调用 clear 将计时器值重置为零。

Examples
// 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);
}

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
  • 此函数会安排一次性回调;它不会自动重复执行。

  • 回调函数必须返回 void。

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

Examples

定义函数(在 int main()之外)

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

在 int main()

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

  // Call turnOrange after 2 seconds
  Brain.Timer.event(turnOrange, 2000);
}