on Event

Run some code when a registered event happens.

control.onEvent(0, 0, () => {})

Instead of waiting inside of a loop that is part the main program, an event block is a good way to have some extra code respond to something that happens with the board or in your program.

It works like this: some code in another part of the program decides it wants to announce that something special or important just happened. It does this by raising an event. Some other code is placed inside an ||control:on event|| block. The ||control:on event|| block registers itself to run when a matching event is raised.

Your program decides what an event should be and makes it official by registering the event along with a source identification number (id), src. The id in src is a way to know what thing is causing an event, like a sensor, or some special time period that is now over. The value tells what the cause of the event is.

Why use events?

If you want to run some code when a status changes, you might do it this way in the main part of the program:

let nowStatus = false
control.runInParallel(() => {
    control.waitMicros(10000)
    nowStatus = true
})

let lastStatus = nowStatus;
while(lastStatus == nowStatus) {
    control.waitMicros(1000)
}

The ||loops:while|| loop checks for a change in nowStatus. The problem is that your program is stuck in the loop until nowStatus changes and it can’t do any more work until then.

But, we can change the program to use an event instead of waiting in a loop. Let’s register an event and give it a source identifier, or src, of 15 and a cause value of 1:

let because = 0;
control.runInParallel(() => {
    control.waitMicros(100000)
    control.raiseEvent(15, 1)
})

control.onEvent(15, 1, () => {
    because = 1;
})

So, you see that the program registered an event known as 15, 1. The source is known by 15 and the cause of the event is 1. Why use these numbers? Well, let’s say that you have a moisture sensor connected to your board. You could make it known to your program that any event that comes from a source of 15 is about your sensor. You can say that a cause of 1 means things are too dry.

Parameters

  • src: the identification number (the source) of this event, such as: 10.
  • value: a number that tells what the cause of the event is, like: 4.
  • handler: the code to run when the event happens.

Example

Register two events coming from source 22. Write to the console when the events of 0 and 1 are raised.

const myNotify = 22

control.runInParallel(function() {
    for (let i = 0; i < 2; i++) {
        pause(1000)
    control.raiseEvent(myNotify, i)
    }
})

control.onEvent(myNotify, 0, () => {
    console.logValue("myNotify", 0)
})

control.onEvent(myNotify, 1, () => {
    console.logValue("myNotify", 1)
})

See also #

raise event