事件#
初始化事件类#
使用以下构造函数创建事件:
event(callback)
构造函数使用两个参数:
范围 |
描述 |
---|---|
|
先前定义的函数,当事件广播时将作为线程被调用。 |
// Define the function print()
void print(){
brain.screen.print("Event was broadcast");
}
// Construct an Event "Event" with the
// event class.
event Event = event(print);
当引用事件类方法时,此“Event”对象将在整个 API 文档的所有后续示例中使用。
类方法#
放()#
set(callback)
命令为事件设置新的回调和参数。
参数 |
描述 |
---|---|
打回来 |
事件触发时调用的新回调函数。 |
**返回:**无。
播送()#
broadcast()
命令广播事件并导致所有已注册的回调函数运行。
**返回:**无。
void runOnBroadcast1() {
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Broadcast1 Running");
}
void runOnBroadcast2() {
Brain.Screen.setCursor(2, 1);
Brain.Screen.print("Broadcast2 Running");
}
int main() {
// Register callback functions to event.
Event(runOnBroadcast1);
Event(runOnBroadcast2);
// Brief wait to ensure event is registered
wait(15, msec);
// Broadcast the event.
Event.broadcast();
}
广播并等待()#
broadcastAndWait()
命令广播事件,导致所有已注册的回调函数运行,并等待所有回调函数完成。
**返回:**无。
void runOnBroadcast1() {
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Broadcast1 Running");
}
void runOnBroadcast2() {
Brain.Screen.setCursor(2, 1);
Brain.Screen.print("Broadcast2 Running");
}
int main() {
// Register callback functions to event.
Event(runOnBroadcast1);
Event(runOnBroadcast2);
// Brief wait to ensure event is registered
wait(15, msec);
// Broadcast the event and wait for completion.
Event.broadcastAndWait();
Brain.Screen.setCursor(3, 1);
Brain.Screen.print("Listeners finished running");
}