# \[Extension\] Device control - control namespace ported to blocks!

**URL:** <https://forum.makecode.com/t/extension-device-control-control-namespace-ported-to-blocks/19190>\
**Category:** Show & Tell\
**Tags:** extension\
**Created:** [April 14, 2023, 9:34pm UTC](https://forum.makecode.com/t/extension-device-control-control-namespace-ported-to-blocks/19190 "2023-04-14T21:34:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [April 14, 2023, 9:34pm UTC](https://forum.makecode.com/t/extension-device-control-control-namespace-ported-to-blocks/19190/1 "2023-04-14T21:34:25Z")

</div>

Hey everyone, back with another extension. Today I was wondering why the `control` namespace hasn’t been ported to blocks like the `settings` namespace for example. That’s why I like usually, decided to do it myself. The namespace i’ve ported them to is named `blockControl`, alike `blockSettings`.

* * *

## The ‘control’ namespace

For anyone unfamiliar, the control namespace is a namespace that contains many methods used for managing the device and retrieving its information such as the ram size, serial number etc.

I have separated the blocks into 3 categories: Device control, Debugging and Device & session information.

P.S: If you’re unfamiliar with these functions and their use, don’t bother with this extension.  
One thing to add is that not all methods from the namspace are present, because i’m either unfamiliar with some of them, or they don’t serve a particular purpose. If you have any requests, let me know and I’ll be glad to add them to this extension.

* * *

## Device control

The device control sections consists of methods that manipulate system memory or the flow of the program running.

### blockControl.assert (control.assert)

This method asserts a condition. If the condition is not met, the program is halted and a [PANIC CODE](https://arcade.makecode.com/device/error-codes) is raised. Currently under testing since it’s raising the incorrect code.

```auto
function assert(condition: boolean, code: number) => void

```

### blockControl.raiseEvent (control.raiseEvent)

This method raises an event to registered handlers with a [component ID](https://github.com/microsoft/pxt-microbit/blob/master/libs/core/dal.d.ts) (`src`) and it’s specific event code

```auto
function raiseEvent(src: number, value: number) => void

```

### blockControl.onEvent (control.onEvent)

Execute code on registered event (with the `raiseEvent` method) with a component ID, code and optional flags.

```auto
function onEvent(src: number, code: number, flags?: number) => void

```

### blockControl.waitForEvent (control.waitForEvent)

Block calling thread until the specified event is run.

```auto
function waitForEvent(src: number, code: number) => void

```

### blockControl.runInBackground (control.runInBackground)

Run code in background.

```auto
// NOTICE: This method is deprecated! Use `control.runInParallel` instead.
function runInBackground(handler: () => void) => void

```

### blockControl.runInParallel (control.runInParallel)

Run code in parallel to main thread.

```auto
function runInParallel(handler: () => void) => void

```

### blockControl.createBuffer (control.createBuffer)

Create and return a zero-initiated buffer with a given size in bytes.

```auto
function createBuffer(size: number) => Buffer

```

### blockControl.createBufferFromUTF8 (control.createBufferFromUTF8)

Create and return a buffer from a UTF8-encoded string. (Unicode)

```auto
function createBufferFromUTF8(str: string) => Buffer

```

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/3/32162b6f7b457ad02ea9aa4a45f265ca07226397.png)

(available blocks, matching above methods)

* * *

## Debugging

The debugging section consists of various tools for debugging performance and functionality of the device.

### blockControl.dmesgValue (control.dmesgValue)

This method dumps internal information about any value or object.

```auto
function dmesgValue(value: any) => void

```

### blockControl.gc (control.gc)

This method forces garbage collection and dumps basic informations about the heap.

```auto
function gc() => void

```

### blockControl.dumpHeap (control.dumpHeap)

This method forces garbage collection and waits for a debugger to fully dump the heap.

```auto
function dumpHeap() => void

```

### blockControl.setDebugFlags (control.setDebugFlags)

Set flags for debugging.

```auto
function setDebugFlags(flags: number) => void

```

### blockControl.heapSnapshot (control.heapSnapshot)

Record a heap snapshot for debugging memory leaks.

```auto
function heapSnapshot() => void

```

### blockControl.benchmark (control.benchmark)

Run a block of code or method and return runtime in microseconds.

```auto
function benchmark(func: () => void) => void

```

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/2/2b88b41084c9a393460179462f4c9edf4bd7aa50.png)

(available blocks, matching above methods)

* * *

## Device & session information

The device and session information section includes method that retrieve information and data about the device and / or session.

### blockControl.timeSinceStart (control.millis + control.micros)

This method returns time since device start in micro/milliseconds.

```auto
// TimeFormat = Microseconds | Milliseconds
function timeSinceStart(format: TimeFormat) => number

```

### blockControl.deviceSerialNumber (control.deviceSerialNumber)

This method returns a unique serial number of the device.

```auto
function deviceSerialNumber() => number

```

### blockControl.deviceLongSerialNumber (control.deviceLongSerialNumber)

This method returns a 64-bit unique serial number of the device.

```auto
function deviceLongSerialNumber() => Buffer

```

### blockControl.deviceDalVersion (control.deviceDalVersion)

This method returns the system software version string of the device. (`sim` if running in the simulator)

```auto
function deviceDalVersion() => string

```

### blockControl.profilingEnabled (control.profilingEnabled)

Return true if profiling is enabled.

```auto
function profilingEnabled() => boolean

```

### blockControl.programHash (control.programHash)

Return the program hash.

```auto
function programHash() => number

```

### blockControl.programName (control.programName)

Return the program name.

```auto
function programName() => string

```

### blockControl.ramSize (control.ramSize)

Return estimated RAM size in bytes.

```auto
function ramSize() => number

```

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/b/b9a9d8540c5e8286ca5d287eeeb96e3a1734f66b.png)

(available blocks, matching above methods)

* * *

If you’ve made it this far, thank you for reading! If you have any questions or suggestions, feel free to leave them in the replies!

By the way, I still haven’t made a release for this extension, so I hope I’ll get around to doing that in the next few days!

(extension / repo link)

> **[GitHub - sargedev/control: Control namespace turned into block form](https://github.com/sargedev/control)**
>
> Control namespace turned into block form

---

<div class="post-metadata">

**Author:** ![BoostedGames](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/boostedgames/32/29419_2.png) [@BoostedGames](https://forum.makecode.com/u/BoostedGames)\
**Post date:** [April 15, 2023, 1:56am UTC](https://forum.makecode.com/t/extension-device-control-control-namespace-ported-to-blocks/19190/2 "2023-04-15T01:56:34Z")

</div>

holy moly you are making a lot of extensions, but very nice!

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [July 3, 2023, 5:16pm UTC](https://forum.makecode.com/t/extension-device-control-control-namespace-ported-to-blocks/19190/3 "2023-07-03T17:16:11Z")

</div>

## Quick update

I had a thought for a while to add a new block for a while, to check whether the program is running within the simulator, or physical hardware.

### blockControl.inSimulator (no control namespace equivalent)

Returns `true` if the program is running in simulator

```auto
function inSimulator() => boolean

```

Thank you for using my extension!

> **[GitHub - sargedev/control: Control namespace turned into block form](https://github.com/sargedev/control)**
>
> Control namespace turned into block form

---

<div class="post-metadata">

**Author:** ![Matthew-diehl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/matthew-diehl/32/11947_2.png) [@Matthew-diehl](https://forum.makecode.com/u/Matthew-diehl)\
**Post date:** [November 27, 2023, 6:28pm UTC](https://forum.makecode.com/t/extension-device-control-control-namespace-ported-to-blocks/19190/4 "2023-11-27T18:28:56Z")

</div>

Hi, quick question if I had a raise event go off before the wait is reached would it still go through the wait? if not how can I get around this while still making sure the code always waits

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [November 27, 2023, 7:54pm UTC](https://forum.makecode.com/t/extension-device-control-control-namespace-ported-to-blocks/19190/5 "2023-11-27T19:54:12Z")

</div>

I’m not sure what you mean by this, can you elaborate a bit more please?

---

<div class="post-metadata">

**Author:** ![DangerNoodle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dangernoodle/32/14797_2.png) [@DangerNoodle](https://forum.makecode.com/u/DangerNoodle)\
**Post date:** [November 28, 2023, 6:13pm UTC](https://forum.makecode.com/t/extension-device-control-control-namespace-ported-to-blocks/19190/6 "2023-11-28T18:13:21Z")

</div>

rip mods 💀

---

<div class="post-metadata">

**Author:** ![Matthew-diehl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/matthew-diehl/32/11947_2.png) [@Matthew-diehl](https://forum.makecode.com/u/Matthew-diehl)\
**Post date:** [November 29, 2023, 12:48am UTC](https://forum.makecode.com/t/extension-device-control-control-namespace-ported-to-blocks/19190/7 "2023-11-29T00:48:36Z")

</div>

I figured it out, but basically I was trying to raise an event before the code even got to the wait, which obviously won’t work, but I fixed my code so it will always reach the wait before the event is raised. Thanks for help on the block Objects as well the other day, I will eventually link the Rogue-like game I’m making (should be done by this weekend) if you want to take a look at it lol.

---

<div class="post-metadata">

**Author:** ![Matthew-diehl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/matthew-diehl/32/11947_2.png) [@Matthew-diehl](https://forum.makecode.com/u/Matthew-diehl)\
**Post date:** [December 18, 2023, 10:39pm UTC](https://forum.makecode.com/t/extension-device-control-control-namespace-ported-to-blocks/19190/8 "2023-12-18T22:39:43Z")

</div>

Almost forgot (there is one bug where the game freezes on cutscenes sometimes and please don’t look too closely at the messy JS lol)

> **[Digestive Dungeon Beta Version 1.3](https://arcade.makecode.com/S43335-22706-82920-89779)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [May 14, 2024, 9:20pm UTC](https://forum.makecode.com/t/extension-device-control-control-namespace-ported-to-blocks/19190/9 "2024-05-14T21:20:24Z")

</div>

## Imporant announcement

I changed my GitHub username, so please remove this extension in any project you may have it imported, and instead import the new link under my new username (sargedev).

This is not necessary, as [the extension should still work](https://forum.makecode.com/t/how-does-makecode-handle-github-username-migrations/28716/11), but it would be the safest option. By importing the new link you will also get access to any new updates.

**WARNING: Do not import the new link before removing the old one, as this could cause conflicts**

> **[GitHub - sargedev/control: Control namespace turned into block form](https://github.com/sargedev/control)**
>
> Control namespace turned into block form
