活动#

介绍#

事件允许你异步启动函数。当一个事件被广播时,该事件的回调函数会在它自己的任务(线程)中启动,因此你的主代码可以同时继续运行。

Important: An event object stores one callback. If you register another function with the same event object, it replaces the previous callback.

如果你希望一个触发器触发多个事件,那么:

  • 从单个回调函数调用多个辅助函数,或者

  • 使用多个事件对象并分别广播它们。

  • 使用单独的线程独立运行任务。

要创建和使用事件,请按以下步骤操作:

  1. Create the event with event.

  2. Set the event’s callback using the event name (ex: myEvent(myCallback);).

  3. Broadcast an event:

    • broadcast — Triggers all registered functions in an event object to run in parallel.

    • broadcastAndWait — Triggers all registered functions in an event object and waits for them to finish before continuing.

创建活动#

The event constructor creates an event object. You can create an event with or without a callback.

Default Usage:
event myEvent;

Overload Usages:
event myEvent(callback);

范围

描述

myEvent

活动名称。

callback

预先定义好在事件广播时执行的函数。

// Create a callback function
void onMyEvent() {
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Event fired!");
}

int main() {
  vexcodeInit();

  Brain.Screen.print("Broadcasting in 2 seconds...");

  // Create an event called myEvent
  // Set onMyEvent as the callback function
  event myEvent(onMyEvent);

  wait(2, seconds);

  // Trigger the event
  myEvent.broadcast();
}

设置事件回调#

设置回调函数意味着当事件被广播时,该函数将运行。

**注意:**再次设置回调函数会覆盖之前的回调函数。

Usage:
eventName(callback);

范围

描述

eventName

先前创建的事件对象的名称。

callback

预先定义的回调函数,会在事件广播时自动调用。该函数必须符合所需的回调函数签名。更多信息请参见回调函数

void drawSquare() {
  Brain.Screen.clearScreen();
  Brain.Screen.drawRectangle(40, 40, 120, 120);
}

void drawCircle() {
  Brain.Screen.clearScreen();
  Brain.Screen.drawCircle(100, 100, 60);
}

int main() {
  vexcodeInit();

  // Create the event drawEvent
  event drawEvent;

  Brain.Screen.print("Drawing a square...");

  // Set the event to drawSquare
  drawEvent(drawSquare);

  wait(1, seconds);

  // Trigger the event
  drawEvent.broadcast();

  wait(1, seconds);

  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Drawing a circle...");

  // Replace drawSquare with drawCircle
  drawEvent(drawCircle);

  wait(1, seconds);

  // Trigger the event a second time
  drawEvent.broadcast();
}

直播活动#

broadcast#

broadcast starts the event’s callback and continues immediately.

Usage:
myEvent.broadcast();

范围

描述

myEvent

先前创建的事件对象的名称。

// Create a callback function
void onMyEvent() {
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1)
  Brain.Screen.print("Event fired!");
}

int main() {
  vexcodeInit();

  Brain.Screen.print("Broadcasting in 2 seconds...");

  // Create an event called myEvent
  // Set onMyEvent as the callback function
  event myEvent(onMyEvent);

  wait(2, seconds);

  // Trigger the event
  myEvent.broadcast();
}

broadcastAndWait#

broadcastAndWait starts the callback and waits until it finishes (or times out).

注意: 仅可用于会结束的回调函数(不能用于无限循环)。

Usage:
myEvent.broadcastAndWait(timeout);

范围

描述

myEvent

先前创建的事件对象的名称。

timeout

等待的毫秒数(以双精度浮点数表示),之后代码才会再次开始执行。

void startDriving() {
  Drivetrain.drive(forward);
  wait(2, seconds);
}

int main() {
  vexcodeInit();

  Brain.Screen.print("Starting drive...");

  event driveEvent(startDriving);
  // Give the event time to be set
  wait(0.25, seconds);

  // Run the event and wait until it finishes
  driveEvent.broadcastAndWait();

  // Stop driving after 2 seconds
  Drivetrain.stop();
  Brain.Screen.clearScreen();
  Brain.Screen.print("Drive complete!");
}