From 904354c7185bbc37a00478b4e3337bc24b173f80 Mon Sep 17 00:00:00 2001 From: a012 Date: Sat, 21 Mar 2026 22:34:49 -0400 Subject: [PATCH 01/19] Update control.ts --- libs/base/control.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libs/base/control.ts b/libs/base/control.ts index 9e087a5d9..2be88b305 100644 --- a/libs/base/control.ts +++ b/libs/base/control.ts @@ -64,14 +64,20 @@ namespace control { //% shim=pxtrt::panic export function panic(code: number) { } + //% shim=U::userError + function _throwValue(msg: string) { } + /** - * Display an error code and stop the program when the assertion is `false`. + * Display a message, an error code, and stop the program when the assertion is `false`. */ //% help=control/assert weight=30 //% blockId="control_assert" block="assert %cond|with value %code" export function assert(cond: boolean, code: number) { if (!cond) { - fail("Assertion failed, code=" + code) + console.log("Assertion failed") + console.log(code) + dmesg("Assertion failed: " + code) + _throwValue("Assertion failed: " + code) } } From 4c06385d0f23dc18d622da292725e49555e0dde7 Mon Sep 17 00:00:00 2001 From: a012 Date: Sat, 21 Mar 2026 23:20:36 -0400 Subject: [PATCH 02/19] Update assert.md --- libs/base/docs/reference/control/assert.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/base/docs/reference/control/assert.md b/libs/base/docs/reference/control/assert.md index 8c3c16fff..8dfb9fd21 100644 --- a/libs/base/docs/reference/control/assert.md +++ b/libs/base/docs/reference/control/assert.md @@ -1,6 +1,6 @@ # assert -Display an error number and stop the program if the assertion condition is false. +Display a message, error number, and stop the program if the assertion condition is false. ```sig control.assert(false, 0) @@ -30,4 +30,4 @@ forever(function () { ```package base -``` \ No newline at end of file +``` From 92f04c37ffdf60d1c44362f1bb86d7ed0631ac5c Mon Sep 17 00:00:00 2001 From: a012 Date: Sat, 21 Mar 2026 23:20:54 -0400 Subject: [PATCH 03/19] Update assert.md --- libs/base/docs/reference/control/assert.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/base/docs/reference/control/assert.md b/libs/base/docs/reference/control/assert.md index 8dfb9fd21..9032e362d 100644 --- a/libs/base/docs/reference/control/assert.md +++ b/libs/base/docs/reference/control/assert.md @@ -24,7 +24,7 @@ forever(function () { }) ``` -## See also #seealso +## See also [panic](/reference/control/panic) From 4a88612d6dafd7f735fb872f91fe68bb1a404d94 Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 00:20:13 -0400 Subject: [PATCH 04/19] Update control.ts --- libs/base/control.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/base/control.ts b/libs/base/control.ts index 2be88b305..32660195d 100644 --- a/libs/base/control.ts +++ b/libs/base/control.ts @@ -74,10 +74,10 @@ namespace control { //% blockId="control_assert" block="assert %cond|with value %code" export function assert(cond: boolean, code: number) { if (!cond) { - console.log("Assertion failed") + console.log("Assertion failed. CODE:") console.log(code) - dmesg("Assertion failed: " + code) - _throwValue("Assertion failed: " + code) + dmesg("Assertion failed. CODE: " + code) + _throwValue("Assertion failed. CODE: " + code) } } From af26d39c96eeea8597c8ce5c270d3f981b6ffd43 Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 15:47:20 -0400 Subject: [PATCH 05/19] Update control.ts --- libs/base/control.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/libs/base/control.ts b/libs/base/control.ts index 32660195d..3330dff8f 100644 --- a/libs/base/control.ts +++ b/libs/base/control.ts @@ -72,14 +72,19 @@ namespace control { */ //% help=control/assert weight=30 //% blockId="control_assert" block="assert %cond|with value %code" - export function assert(cond: boolean, code: number) { - if (!cond) { - console.log("Assertion failed. CODE:") - console.log(code) - dmesg("Assertion failed. CODE: " + code) - _throwValue("Assertion failed. CODE: " + code) - } + export function assert(cond: boolean, code?: number) { + if (!cond) { + const msg = code !== undefined + ? `Assertion failed (code ${code})` + : `Assertion failed` + + console.log("Assertion failed") + if (code !== undefined) console.log(code) + dmesg(msg) + _throwValue(msg) } + } + export function fail(message: string) { console.log("Fatal failure: ") From a78276c390caace13b839d784f81d3b21d10a986 Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 15:55:17 -0400 Subject: [PATCH 06/19] Update control.ts --- libs/base/control.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/base/control.ts b/libs/base/control.ts index 3330dff8f..492c3b4e9 100644 --- a/libs/base/control.ts +++ b/libs/base/control.ts @@ -55,6 +55,7 @@ namespace control { UNHANDLED_EXCEPTION = 999, } + /** * Display an error code and stop the program. * @param code an error number to display. eg: 5 @@ -85,7 +86,6 @@ namespace control { } } - export function fail(message: string) { console.log("Fatal failure: ") console.log(message) From 7107874ef719776ab493f5f917b7dd965f4e89f4 Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 16:15:56 -0400 Subject: [PATCH 07/19] Update control.ts --- libs/base/control.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libs/base/control.ts b/libs/base/control.ts index 492c3b4e9..df8fbf245 100644 --- a/libs/base/control.ts +++ b/libs/base/control.ts @@ -69,7 +69,8 @@ namespace control { function _throwValue(msg: string) { } /** - * Display a message, an error code, and stop the program when the assertion is `false`. + * Display an error message, an error code, and stop the program when the assertion is `false`. + @code (optional) an error number to display. eg: 5 */ //% help=control/assert weight=30 //% blockId="control_assert" block="assert %cond|with value %code" @@ -82,6 +83,7 @@ namespace control { console.log("Assertion failed") if (code !== undefined) console.log(code) dmesg(msg) + debugger _throwValue(msg) } } @@ -169,10 +171,8 @@ namespace control { export declare function programName(): string; //% shim=control::_ramSize - function _ramSize() { - return 32 * 1024 * 1024; - } - + declare function _ramSize(): number + /** Returns estimated size of memory in bytes. */ export function ramSize() { return getConfigValue(DAL.CFG_RAM_BYTES, 0) || _ramSize(); From 98c095168ac0cc10b4a1e9d5d867f9f3b21e2298 Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 16:16:58 -0400 Subject: [PATCH 08/19] Update assert.md --- libs/base/docs/reference/control/assert.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/base/docs/reference/control/assert.md b/libs/base/docs/reference/control/assert.md index 9032e362d..3e7b671ba 100644 --- a/libs/base/docs/reference/control/assert.md +++ b/libs/base/docs/reference/control/assert.md @@ -11,7 +11,7 @@ You can insist that your program will stop at an assert block if a certain condi ## Parameters * **cond**: a [boolean](/types/boolean) where true means everything is ok or false which means, stop the program! -* **code**: an error [number](/types/number) you match to an error situation in your program. +* **code**: an optional parameter for displaying an error [number](/types/number) you match to an error situation in your program. ## Example #example From 9bfa97b205c91c1233781c967dc2db278d127a49 Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 16:17:22 -0400 Subject: [PATCH 09/19] Update control.ts --- libs/base/control.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/base/control.ts b/libs/base/control.ts index df8fbf245..878ae4206 100644 --- a/libs/base/control.ts +++ b/libs/base/control.ts @@ -176,7 +176,7 @@ namespace control { /** Returns estimated size of memory in bytes. */ export function ramSize() { return getConfigValue(DAL.CFG_RAM_BYTES, 0) || _ramSize(); - } + } /** Runs the function and returns run time in microseconds. */ export function benchmark(f: () => void) { From 27c7d64166e0647ac81c213001910ca6197265af Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 16:26:44 -0400 Subject: [PATCH 10/19] Update control.ts --- libs/base/control.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/libs/base/control.ts b/libs/base/control.ts index 878ae4206..703e93be6 100644 --- a/libs/base/control.ts +++ b/libs/base/control.ts @@ -83,7 +83,6 @@ namespace control { console.log("Assertion failed") if (code !== undefined) console.log(code) dmesg(msg) - debugger _throwValue(msg) } } From 0a65bfb4d21981f705f7904cedac984106fe218f Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 16:41:52 -0400 Subject: [PATCH 11/19] Update control.ts --- libs/base/control.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libs/base/control.ts b/libs/base/control.ts index 703e93be6..477113da8 100644 --- a/libs/base/control.ts +++ b/libs/base/control.ts @@ -75,9 +75,10 @@ namespace control { //% help=control/assert weight=30 //% blockId="control_assert" block="assert %cond|with value %code" export function assert(cond: boolean, code?: number) { + // adapted this idea from how real browsers work and optionally kept the code parameter for convenience if (!cond) { const msg = code !== undefined - ? `Assertion failed (code ${code})` + ? `Assertion failed, code: ${code})` : `Assertion failed` console.log("Assertion failed") From 50c009abbe2c6e51aaf948ba465756a43edd0a63 Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 16:43:39 -0400 Subject: [PATCH 12/19] Update control.ts --- libs/base/control.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/base/control.ts b/libs/base/control.ts index 477113da8..8b2f474b4 100644 --- a/libs/base/control.ts +++ b/libs/base/control.ts @@ -171,7 +171,7 @@ namespace control { export declare function programName(): string; //% shim=control::_ramSize - declare function _ramSize(): number + declare function _ramSize(): number; /** Returns estimated size of memory in bytes. */ export function ramSize() { From 2efdecd71de1ec93c471777d7eaf938babe90c06 Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 16:58:35 -0400 Subject: [PATCH 13/19] Update control.ts --- libs/base/control.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/base/control.ts b/libs/base/control.ts index 8b2f474b4..22ae7b5ed 100644 --- a/libs/base/control.ts +++ b/libs/base/control.ts @@ -81,7 +81,7 @@ namespace control { ? `Assertion failed, code: ${code})` : `Assertion failed` - console.log("Assertion failed") + console.log(msg) if (code !== undefined) console.log(code) dmesg(msg) _throwValue(msg) From 3a8b17459a8d41528b3148d6f0d95cec18061141 Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 17:01:27 -0400 Subject: [PATCH 14/19] Update control.ts --- libs/base/control.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/libs/base/control.ts b/libs/base/control.ts index 22ae7b5ed..a9294eed8 100644 --- a/libs/base/control.ts +++ b/libs/base/control.ts @@ -80,9 +80,7 @@ namespace control { const msg = code !== undefined ? `Assertion failed, code: ${code})` : `Assertion failed` - console.log(msg) - if (code !== undefined) console.log(code) dmesg(msg) _throwValue(msg) } From 792bcc114442279af640f4b9eddf814dfecc3282 Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 17:03:35 -0400 Subject: [PATCH 15/19] Update control.ts --- libs/base/control.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/base/control.ts b/libs/base/control.ts index a9294eed8..dee62dd43 100644 --- a/libs/base/control.ts +++ b/libs/base/control.ts @@ -78,7 +78,7 @@ namespace control { // adapted this idea from how real browsers work and optionally kept the code parameter for convenience if (!cond) { const msg = code !== undefined - ? `Assertion failed, code: ${code})` + ? `Assertion failed, code: ${code}` : `Assertion failed` console.log(msg) dmesg(msg) From f6f3574e0d55ab9b19f9bf6b5e647990d6c25f1f Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 22:09:40 -0400 Subject: [PATCH 16/19] Update control.ts --- libs/base/control.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/libs/base/control.ts b/libs/base/control.ts index dee62dd43..680487f92 100644 --- a/libs/base/control.ts +++ b/libs/base/control.ts @@ -75,16 +75,17 @@ namespace control { //% help=control/assert weight=30 //% blockId="control_assert" block="assert %cond|with value %code" export function assert(cond: boolean, code?: number) { - // adapted this idea from how real browsers work and optionally kept the code parameter for convenience - if (!cond) { - const msg = code !== undefined + if (!cond) { + // adapted this idea from how real browsers work and optionally kept the code parameter for clearer diagnostics + const msg = code !== undefined ? `Assertion failed, code: ${code}` : `Assertion failed` - console.log(msg) - dmesg(msg) - _throwValue(msg) + + console.log(msg) + dmesg(msg) + _throwValue(msg) + } } - } export function fail(message: string) { console.log("Fatal failure: ") From bb86d6233984b5e097a21ac0ce92b701c32ab57c Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 22:42:40 -0400 Subject: [PATCH 17/19] Update control.ts --- libs/base/control.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libs/base/control.ts b/libs/base/control.ts index 680487f92..eeabdd724 100644 --- a/libs/base/control.ts +++ b/libs/base/control.ts @@ -77,6 +77,8 @@ namespace control { export function assert(cond: boolean, code?: number) { if (!cond) { // adapted this idea from how real browsers work and optionally kept the code parameter for clearer diagnostics + // added the message also because the original one for receiving the message is only logged + // in the console, not together in the panel const msg = code !== undefined ? `Assertion failed, code: ${code}` : `Assertion failed` From bf250c281210ab5399594481f066c94010904a30 Mon Sep 17 00:00:00 2001 From: a012 Date: Sun, 22 Mar 2026 23:59:11 -0400 Subject: [PATCH 18/19] Update assert.md --- libs/base/docs/reference/control/assert.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/base/docs/reference/control/assert.md b/libs/base/docs/reference/control/assert.md index 3e7b671ba..87ab1a792 100644 --- a/libs/base/docs/reference/control/assert.md +++ b/libs/base/docs/reference/control/assert.md @@ -13,7 +13,7 @@ You can insist that your program will stop at an assert block if a certain condi * **cond**: a [boolean](/types/boolean) where true means everything is ok or false which means, stop the program! * **code**: an optional parameter for displaying an error [number](/types/number) you match to an error situation in your program. -## Example #example +## Example Stop the program if a sensor connected to pin `A0` sends a low (`0`) signal. From 48fc1e08ec3034f78c195604fe45dc27c23abc85 Mon Sep 17 00:00:00 2001 From: a012 Date: Mon, 23 Mar 2026 13:05:45 -0400 Subject: [PATCH 19/19] Update assert.md --- libs/base/docs/reference/control/assert.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/base/docs/reference/control/assert.md b/libs/base/docs/reference/control/assert.md index 87ab1a792..5108525db 100644 --- a/libs/base/docs/reference/control/assert.md +++ b/libs/base/docs/reference/control/assert.md @@ -1,6 +1,6 @@ # assert -Display a message, error number, and stop the program if the assertion condition is false. +Display an error message, error number, and stop the program if the assertion condition is false. ```sig control.assert(false, 0)