# How to go about an Interval extension

**URL:** <https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812>\
**Category:** micro:bit\
**Created:** [November 23, 2019, 7:33am UTC](https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812 "2019-11-23T07:33:51Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![gbraad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gbraad/32/338_2.png) [@gbraad](https://forum.makecode.com/u/gbraad)\
**Post date:** [November 23, 2019, 7:33am UTC](https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812/1 "2019-11-23T07:33:52Z")

</div>

I wanted to use an interval based execution instead of relying on the forever loop with sleeps. Since I didn’t find a good solution so far from the pxt side of stuff, I looked at the actual uBit and mBed code, and it seems they have a Ticker that allows you to add an interval based execution. However, I wanted to use this from the Blocks interface and started with a pxt extension. However, I am a little bit confused about the way this would have to interact, as I woudl not attach to the message bus, and I would be using mbed directly without using the microbit-dal.

How to go about this?

```auto
namespace interval {
    /**
    * Trigger the execution
    */
    //% weight=30 blockId="interval_onInterval" block="Execute codeblock on interval"
    export function OnInterval(interval: number, cb: () => void) {
        onInterval(interval, cb)
    }

    //% shim=interval::onInterval
    function onInterval(interval: number, cb: () => void) {
        return;
    }
}

```

```auto
#include <stdlib.h>
#include "pxt.h"
#include "mbed.h"

using namespace std;

namespace interval {
    Ticker timer;

    //%
    void onInterval(uint8_t interval, void (*fptr)(void)) {
        timer.attach(fptr, interval)
    }
    
} // namespace interval

```

However this fails on `void (*fptr)(void)` not being a valid declaration.

```auto
Extension interval: interval/main.cpp(12): declaration not understood: void onInterval(uint8_t interval, void (*fptr)(void)) { 

```

---

<div class="post-metadata">

**Author:** ![gbraad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gbraad/32/338_2.png) [@gbraad](https://forum.makecode.com/u/gbraad)\
**Post date:** [November 23, 2019, 7:55am UTC](https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812/2 "2019-11-23T07:55:55Z")

</div>

Looking at [https://github.com/microsoft/pxt-common-packages/blob/b21afa452bc4c841f865d89587d06b2039d6aeb3/libs/base/docs/reference/control/wait-for-event.md](https://github.com/microsoft/pxt-common-packages/blob/b21afa452bc4c841f865d89587d06b2039d6aeb3/libs/base/docs/reference/control/wait-for-event.md)

Wait for Event, but this seems to run a piece of code in parallel with a pause statement, and the caller would also have to be a parallel, like a `while(1)` loop to accomplish having a `callback` being triggered after the event ‘raised’.

```auto
const myTimer = 6;
const timerTimeout = 1;

control.runInParallel(() => {
    while (true) {
        pause(2000);
        control.raiseEvent(myTimer, timerTimeout);
    }
})

control.runInParallel(() => {
    while (true) {
       control.waitForEvent(myTimer, timerTimeout);
       tick(); // or cb()
   }
}

```

I would hope something more low-level with a timer interrupt from hardware is possible.

Also, I know arcade has the following:

```auto
game.onUpdateInterval(1000, function () {
})

```

which as far as I remember uses the frame loop to determine if a certain time period has passed before invoking the call… but this is not available on a micro:bit (or maker?)

---

<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 23, 2019, 9:34pm UTC](https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812/3 "2019-11-23T21:34:41Z")

</div>

What precision are you looking for? If 6ms is enough you can just implement on interval with run in parallel, while and pause.

---

<div class="post-metadata">

**Author:** ![gbraad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gbraad/32/338_2.png) [@gbraad](https://forum.makecode.com/u/gbraad)\
**Post date:** [December 7, 2019, 6:25am UTC](https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812/4 "2019-12-07T06:25:12Z")

</div>

it is not the precision I am looking for, but the use and integration options between lower level code and extensions. For now I’ll try to go the route of using the aforementioned option. However, am I even allowed to use the `control` namespace within an extensions, and if so… how? Somehow the layering is not clear to me yet and would like to know what is available to me. I am looking at [https://makecode.microbit.org/reference](https://makecode.microbit.org/reference) and only `basic`, `serial`, `pins`, etc are available to me.

Correction: `control` IS available, but `inBackground` is exposed instead of `runInParallel`, and `onEvent`.

---

<div class="post-metadata">

**Author:** ![gbraad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gbraad/32/338_2.png) [@gbraad](https://forum.makecode.com/u/gbraad)\
**Post date:** [December 7, 2019, 11:48am UTC](https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812/5 "2019-12-07T11:48:31Z")

</div>

Test version is up: [https://github.com/gbraad/pxt-interval](https://github.com/gbraad/pxt-interval)

> **[interval test](https://makecode.microbit.org/79442-76045-17925-87824)**
>
> Made with ❤️ in Microsoft MakeCode for micro:bit.

Example with a metronome:

> **[metronome test](https://makecode.microbit.org/04573-65368-20165-93745)**
>
> Made with ❤️ in Microsoft MakeCode for micro:bit.

---

<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:** [December 7, 2019, 2:04pm UTC](https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812/6 "2019-12-07T14:04:02Z")

</div>

Have you considered using [https://github.com/microsoft/pxt-common-packages/blob/f460cc8ac025fc93ac82cd9d5dcb61b47e3a2dfb/libs/base/scheduling.ts#L7](https://github.com/microsoft/pxt-common-packages/blob/f460cc8ac025fc93ac82cd9d5dcb61b47e3a2dfb/libs/base/scheduling.ts#L7) ?

---

<div class="post-metadata">

**Author:** ![gbraad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gbraad/32/338_2.png) [@gbraad](https://forum.makecode.com/u/gbraad)\
**Post date:** [December 7, 2019, 2:39pm UTC](https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812/7 "2019-12-07T14:39:20Z")

</div>

> [@peli](#):
>
> Have you considered using `setInterval`

Nope, not sure how to use this (the function you refer to is not exported).

Note: as mentioned in the first message, on Acrade you can use:

```auto
game.onUpdateInterval(1000, function () {
})

```

to achieve what I want… but this is not exposed/available on Micro:bit

---

<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:** [December 7, 2019, 6:09pm UTC](https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812/8 "2019-12-07T18:09:34Z")

</div>

It doesn’t seem available in microbit.

---

<div class="post-metadata">

**Author:** ![gbraad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gbraad/32/338_2.png) [@gbraad](https://forum.makecode.com/u/gbraad)\
**Post date:** [December 7, 2019, 6:22pm UTC](https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812/9 "2019-12-07T18:22:04Z")

</div>

As I figured as it all referred to game logic (I had seen these functions before, just unsure if they were able to be used… and therefore the extension).

---

<div class="post-metadata">

**Author:** ![gbraad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gbraad/32/338_2.png) [@gbraad](https://forum.makecode.com/u/gbraad)\
**Post date:** [December 8, 2019, 4:00am UTC](https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812/10 "2019-12-08T04:00:26Z")

</div>

Just wondering: why is there a difference between the Microbit and Arcade target for the run in parallel function?

Micro:bit

```auto
control.inBackground

```

Arcade

```auto
control.runInParallel

```

---

<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:** [December 9, 2019, 5:44pm UTC](https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812/11 "2019-12-09T17:44:23Z")

</div>

We decided to rename it in our shared library (pxt-common-packages) because we thought the wording was more readable. micro:bit is our oldest editor and it still uses the old name because that’s what it originally shipped with; I think we planned to update it at some point but ultimately didn’t think it was worth upgrading existing scripts/documentation.

@peli the scheduling stuff isn’t in micro:bit.

---

<div class="post-metadata">

**Author:** ![gbraad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gbraad/32/338_2.png) [@gbraad](https://forum.makecode.com/u/gbraad)\
**Post date:** [December 10, 2019, 5:47pm UTC](https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812/12 "2019-12-10T17:47:03Z")

</div>

> [@richard](#):
>
> I think we planned to update it at some point

At the moment it causes issues with portability of extensions. Not many might be affected, but some workaround might be needed.

However, using a `=== func` does not work as `any` is assumed.

---

<div class="post-metadata">

**Author:** ![gbraad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gbraad/32/338_2.png) [@gbraad](https://forum.makecode.com/u/gbraad)\
**Post date:** [March 14, 2020, 2:06pm UTC](https://forum.makecode.com/t/how-to-go-about-an-interval-extension/812/13 "2020-03-14T14:06:19Z")

</div>

Forgot to post it here, but actually got a basic implementation of an interval extension: [https://github.com/gbraad/pxt-interval](https://github.com/gbraad/pxt-interval)
