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

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 is raised. Currently under testing since it’s raising the incorrect code.

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

blockControl.raiseEvent (control.raiseEvent)

This method raises an event to registered handlers with a component ID (src) and it’s specific event code

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.

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

blockControl.waitForEvent (control.waitForEvent)

Block calling thread until the specified event is run.

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

blockControl.runInBackground (control.runInBackground)

Run code in background.

// 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.

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

blockControl.createBuffer (control.createBuffer)

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

function createBuffer(size: number) => Buffer

blockControl.createBufferFromUTF8 (control.createBufferFromUTF8)

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

function createBufferFromUTF8(str: string) => Buffer

(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.

function dmesgValue(value: any) => void

blockControl.gc (control.gc)

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

function gc() => void

blockControl.dumpHeap (control.dumpHeap)

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

function dumpHeap() => void

blockControl.setDebugFlags (control.setDebugFlags)

Set flags for debugging.

function setDebugFlags(flags: number) => void

blockControl.heapSnapshot (control.heapSnapshot)

Record a heap snapshot for debugging memory leaks.

function heapSnapshot() => void

blockControl.benchmark (control.benchmark)

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

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

image

(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.

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

blockControl.deviceSerialNumber (control.deviceSerialNumber)

This method returns a unique serial number of the device.

function deviceSerialNumber() => number

blockControl.deviceLongSerialNumber (control.deviceLongSerialNumber)

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

function deviceLongSerialNumber() => Buffer

blockControl.deviceDalVersion (control.deviceDalVersion)

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

function deviceDalVersion() => string

blockControl.profilingEnabled (control.profilingEnabled)

Return true if profiling is enabled.

function profilingEnabled() => boolean

blockControl.programHash (control.programHash)

Return the program hash.

function programHash() => number

blockControl.programName (control.programName)

Return the program name.

function programName() => string

blockControl.ramSize (control.ramSize)

Return estimated RAM size in bytes.

function ramSize() => number

image

(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)

11 Likes

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

3 Likes

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

function inSimulator() => boolean

Thank you for using my extension!

3 Likes

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

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

2 Likes

rip mods :skull:

3 Likes

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.

1 Like

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)

2 Likes

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, 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

2 Likes