# Table of reserved event source IDs

**URL:** <https://forum.makecode.com/t/table-of-reserved-event-source-ids/768>\
**Category:** micro:bit\
**Created:** [November 12, 2019, 1:45pm UTC](https://forum.makecode.com/t/table-of-reserved-event-source-ids/768 "2019-11-12T13:45:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpwoos](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dpwoos](https://forum.makecode.com/u/dpwoos)\
**Post date:** [November 12, 2019, 1:45pm UTC](https://forum.makecode.com/t/table-of-reserved-event-source-ids/768/1 "2019-11-12T13:45:06Z")

</div>

I am experimenting with using custom events to control program flow, and am looking for a way to use “source IDs” that aren’t already in use. The doc provides a link to a page that is claimed to contain a table of reserved IDs, but I don’t see this info there or anyplace else. Is there such a table?

From the doc:

Creating an event is easy - just create a MicroBitEvent with the source and value you need, and the runtime takes care of the rest:

MicroBitEvent(MICROBIT\_ID\_BUTTON\_A, MICROBIT\_BUTTON\_EVT\_CLICK);  
Feel free to create your own events lke this too. Just try to avoid using any source ID that is already used by the runtime! 🙂 See the messageBus page for a complete table of the reserved source IDs.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [November 12, 2019, 7:04pm UTC](https://forum.makecode.com/t/table-of-reserved-event-source-ids/768/2 "2019-11-12T19:04:45Z")

</div>

Here’s where MakeCode pulls its values from: [https://github.com/microsoft/pxt-microbit/blob/master/libs/core/dal.d.ts](https://github.com/microsoft/pxt-microbit/blob/master/libs/core/dal.d.ts)

If you search for “ID” in that page you should find most of them. I think the only other one we use that isn’t listed there is `MICROBIT_MELODY_ID = 2000` which is used to produce events for the melody APIs in the music category.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [November 12, 2019, 7:07pm UTC](https://forum.makecode.com/t/table-of-reserved-event-source-ids/768/3 "2019-11-12T19:07:22Z")

</div>

And here is the actual source of truth: [https://github.com/lancaster-university/microbit-dal/blob/master/inc/core/MicroBitComponent.h#L32](https://github.com/lancaster-university/microbit-dal/blob/master/inc/core/MicroBitComponent.h#L32)

@jamesadevine correct me if I’m wrong

---

<div class="post-metadata">

**Author:** ![jamesadevine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jamesadevine/32/423_2.png) [@jamesadevine](https://forum.makecode.com/u/jamesadevine)\
**Post date:** [November 12, 2019, 8:05pm UTC](https://forum.makecode.com/t/table-of-reserved-event-source-ids/768/4 "2019-11-12T20:05:42Z")

</div>

Yes @richard, that is the definitive list of source values for events used in the microbit-dal. A similar list is available for CODAL: [https://github.com/lancaster-university/codal-core/blob/master/inc/core/CodalComponent.h](https://github.com/lancaster-university/codal-core/blob/master/inc/core/CodalComponent.h)

---

<div class="post-metadata">

**Author:** ![dpwoos](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dpwoos](https://forum.makecode.com/u/dpwoos)\
**Post date:** [November 12, 2019, 8:17pm UTC](https://forum.makecode.com/t/table-of-reserved-event-source-ids/768/5 "2019-11-12T20:17:16Z")

</div>

Excellent - thanks! I am thinking that kids might find events a good way to implement some more complicated logic, though I am a bit chagrined that there aren’t built in timer events of any kind?

---

<div class="post-metadata">

**Author:** ![jamesadevine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jamesadevine/32/423_2.png) [@jamesadevine](https://forum.makecode.com/u/jamesadevine)\
**Post date:** [November 12, 2019, 10:26pm UTC](https://forum.makecode.com/t/table-of-reserved-event-source-ids/768/6 "2019-11-12T22:26:55Z")

</div>

@dpwoos In a later evolution of the dal (CODAL) there are. In the microbit-dal, you can use classes from mbed to achieve such a thing… Ticker ([https://os.mbed.com/docs/mbed-os/v5.12/apis/ticker.html](https://os.mbed.com/docs/mbed-os/v5.12/apis/ticker.html)), regular interrupts, and Timeout ([https://os.mbed.com/docs/mbed-os/v5.12/apis/timeout.html](https://os.mbed.com/docs/mbed-os/v5.12/apis/timeout.html)), a single interrupt, are available… you are responsible for handling interrupts appropriately 🙂

There is also a regular callback that occurs every 6 milliseconds for components that you could use to create your own Timer class.

---

<div class="post-metadata">

**Author:** ![peli](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/peli/32/12_2.png) [@peli](https://forum.makecode.com/u/peli)\
**Post date:** [November 12, 2019, 10:43pm UTC](https://forum.makecode.com/t/table-of-reserved-event-source-ids/768/7 "2019-11-12T22:43:27Z")

</div>

would this work?

```auto
forever(function() {
   something
   pause(interval)
})

```

---

<div class="post-metadata">

**Author:** ![dpwoos](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dpwoos](https://forum.makecode.com/u/dpwoos)\
**Post date:** [November 13, 2019, 1:15am UTC](https://forum.makecode.com/t/table-of-reserved-event-source-ids/768/8 "2019-11-13T01:15:46Z")

</div>

I am concerned that relying on using “pause” in a background “while(true)” will result in considerable drift, but I need to do some tests on a micro:bit. For example, 25 background “while(true)” loops containing a “pause(100)” that each toggle one of the leds.

edit: I just tried this, and on a physical micro:bit I cannot create 25 background subprograms (X020 No Free Memory), but I can create 12. Also, the drift is a lot more than in the simulator.

let ledBlink = ((x: number, y: number, pauseMS: number) =\> {  
control.inBackground(() =\> {  
while (true) {  
led.toggle(x, y)  
basic.pause(pauseMS)  
}  
})  
})  
// 5x5 doesn’t work but this 4x3 does  
for (let ix = 0; ix \< 4; ix++) {  
for (let iy = 0; iy \< 3; iy++) {  
ledBlink(ix, iy, 100)  
}  
}

I think that I will look at creating an extension that will raise an event every x milliseconds.  
Also, I wonder if it might be fun for our students to use “music” events and “tempo” to get a nice pulse?

music.onEvent(MusicEvent.MelodyNotePlayed, function () {  
led.toggle(4, 4)  
})  
// tempo of 60 is 1 beat / sec  
music.setTempo(60)  
// play a rest for 1 sec  
music.beginMelody([“r1:4”], MelodyOptions.Forever)

---

<div class="post-metadata">

**Author:** ![mmoskal](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/mmoskal/32/20_2.png) [@mmoskal](https://forum.makecode.com/u/mmoskal)\
**Post date:** [November 13, 2019, 3:11am UTC](https://forum.makecode.com/t/table-of-reserved-event-source-ids/768/9 "2019-11-13T03:11:23Z")

</div>

You could hack something together by compensating for drift by looking at current time. Pause on microbit has i think 6ms resolution. It’s 4ms or so on codal targets.

---

<div class="post-metadata">

**Author:** ![dpwoos](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dpwoos](https://forum.makecode.com/u/dpwoos)\
**Post date:** [November 18, 2019, 6:59am UTC](https://forum.makecode.com/t/table-of-reserved-event-source-ids/768/10 "2019-11-18T06:59:21Z")

</div>

Music.Melody with beat=60 produces a pulse that runs 9ms/sec slow:

let t = 0  
let seconds = 0  
music.onEvent(MusicEvent.MelodyNotePlayed, function () {  
// init t  
if (t == 0) {  
t = input.runningTime()  
}  
led.toggle(0, 0)  
seconds += 1  
//if (seconds % 10 == 0) {  
serial.writeNumbers([seconds, input.runningTime() - t])  
serial.writeLine("")  
//}  
t += 1000  
})  
// tempo of 60 is 1 beat / sec  
music.setTempo(60)  
// play a rest for 1 sec  
music.beginMelody([“r:4”], MelodyOptions.Forever)
