2016-04-06 01:54:09 +02:00
|
|
|
/**
|
|
|
|
* Runtime and event utilities.
|
|
|
|
*/
|
2017-01-20 05:55:31 +01:00
|
|
|
//% weight=1 color="#42495F" icon="\uf233"
|
2016-10-11 22:48:25 +02:00
|
|
|
//% advanced=true
|
2016-03-10 23:01:04 +01:00
|
|
|
namespace control {
|
2020-09-08 11:04:25 +02:00
|
|
|
/**
|
|
|
|
* Run other code in the parallel.
|
|
|
|
*/
|
|
|
|
//% hidden=1
|
|
|
|
export function runInParallel(a: () => void) {
|
|
|
|
control.inBackground(a);
|
|
|
|
}
|
2019-12-02 05:58:26 +01:00
|
|
|
|
2022-03-22 17:36:19 +01:00
|
|
|
//% hidden=1 deprecated=1
|
|
|
|
export function runInBackground(a: () => void) {
|
|
|
|
control.inBackground(a);
|
|
|
|
}
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
/**
|
|
|
|
* Returns the value of a C++ runtime constant
|
|
|
|
*/
|
|
|
|
//% weight=2 weight=19 blockId="control_event_source_id" block="%id" blockGap=8
|
2020-09-08 11:04:25 +02:00
|
|
|
//% help=control/event-source-id
|
2019-12-02 05:58:26 +01:00
|
|
|
//% shim=TD_ID advanced=true
|
|
|
|
export function eventSourceId(id: EventBusSource): number {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns the value of a C++ runtime constant
|
|
|
|
*/
|
|
|
|
//% weight=1 weight=19 blockId="control_event_value_id" block="%id"
|
2020-09-08 11:04:25 +02:00
|
|
|
//% help=control/event-value-id
|
2019-12-02 05:58:26 +01:00
|
|
|
//% shim=TD_ID advanced=true
|
|
|
|
export function eventValueId(id: EventBusValue): number {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2022-03-22 17:36:19 +01:00
|
|
|
export const enum PXT_PANIC {
|
|
|
|
CODAL_OOM = 20,
|
|
|
|
GC_OOM = 21,
|
|
|
|
GC_TOO_BIG_ALLOCATION = 22,
|
|
|
|
CODAL_HEAP_ERROR = 30,
|
|
|
|
CODAL_NULL_DEREFERENCE = 40,
|
|
|
|
CODAL_USB_ERROR = 50,
|
|
|
|
CODAL_HARDWARE_CONFIGURATION_ERROR = 90,
|
|
|
|
|
|
|
|
INVALID_BINARY_HEADER = 901,
|
|
|
|
OUT_OF_BOUNDS = 902,
|
|
|
|
REF_DELETED = 903,
|
|
|
|
SIZE = 904,
|
|
|
|
INVALID_VTABLE = 905,
|
|
|
|
INTERNAL_ERROR = 906,
|
|
|
|
NO_SUCH_CONFIG = 907,
|
|
|
|
NO_SUCH_PIN = 908,
|
|
|
|
INVALID_ARGUMENT = 909,
|
|
|
|
MEMORY_LIMIT_EXCEEDED = 910,
|
|
|
|
SCREEN_ERROR = 911,
|
|
|
|
MISSING_PROPERTY = 912,
|
|
|
|
INVALID_IMAGE = 913,
|
|
|
|
CALLED_FROM_ISR = 914,
|
|
|
|
HEAP_DUMPED = 915,
|
|
|
|
STACK_OVERFLOW = 916,
|
|
|
|
BLOCKING_TO_STRING = 917,
|
|
|
|
VM_ERROR = 918,
|
|
|
|
SETTINGS_CLEARED = 920,
|
|
|
|
SETTINGS_OVERLOAD = 921,
|
|
|
|
SETTINGS_SECRET_MISSING = 922,
|
|
|
|
DELETE_ON_CLASS = 923,
|
|
|
|
|
|
|
|
CAST_FIRST = 980,
|
|
|
|
CAST_FROM_UNDEFINED = 980,
|
|
|
|
CAST_FROM_BOOLEAN = 981,
|
|
|
|
CAST_FROM_NUMBER = 982,
|
|
|
|
CAST_FROM_STRING = 983,
|
|
|
|
CAST_FROM_OBJECT = 984,
|
|
|
|
CAST_FROM_FUNCTION = 985,
|
|
|
|
CAST_FROM_NULL = 989,
|
|
|
|
|
|
|
|
UNHANDLED_EXCEPTION = 999,
|
|
|
|
}
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
/**
|
|
|
|
* Display specified error code and stop the program.
|
|
|
|
*/
|
|
|
|
//% shim=pxtrt::panic
|
|
|
|
export function panic(code: number) { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the condition is false, display msg on serial console, and panic with code 098.
|
|
|
|
*/
|
|
|
|
export function assert(condition: boolean, msg?: string) {
|
|
|
|
if (!condition) {
|
|
|
|
console.log("ASSERTION FAILED")
|
|
|
|
if (msg != null) {
|
|
|
|
console.log(msg)
|
2016-07-18 11:12:00 +02:00
|
|
|
}
|
2019-12-02 05:58:26 +01:00
|
|
|
panic(98)
|
2016-04-12 04:44:49 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-02 05:58:26 +01:00
|
|
|
|
|
|
|
export function fail(message: string) {
|
|
|
|
console.log("Fatal failure: ")
|
|
|
|
console.log(message)
|
|
|
|
panic(108)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display warning in the simulator.
|
|
|
|
*/
|
|
|
|
//% shim=pxtrt::runtimeWarning
|
|
|
|
export function runtimeWarning(message: string) { }
|
2022-03-22 17:36:19 +01:00
|
|
|
|
|
|
|
//% shim=pxt::programHash
|
|
|
|
export declare function programHash(): number;
|
|
|
|
|
|
|
|
//% shim=pxt::programName
|
|
|
|
export declare function programName(): string;
|
|
|
|
|
|
|
|
/** Returns estimated size of memory in bytes. */
|
|
|
|
//% shim=control::_ramSize
|
|
|
|
export function ramSize() {
|
|
|
|
return 32 * 1024 * 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Runs the function and returns run time in microseconds. */
|
|
|
|
export function benchmark(f: () => void) {
|
|
|
|
const t0 = micros()
|
|
|
|
f()
|
|
|
|
let t = micros() - t0
|
|
|
|
if (t < 0)
|
|
|
|
t += 0x3fffffff
|
|
|
|
return t
|
|
|
|
}
|
2019-12-02 05:58:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert any value to text
|
|
|
|
* @param value value to be converted to text
|
|
|
|
*/
|
|
|
|
//% help=text/convert-to-text weight=1
|
|
|
|
//% block="convert $value=math_number to text"
|
|
|
|
//% blockId=variable_to_text blockNamespace="text"
|
|
|
|
function convertToText(value: any): string {
|
|
|
|
return "" + value;
|
2020-09-08 11:04:25 +02:00
|
|
|
}
|