pxt-calliope/libs/core/console.ts
Amerlander 918af4f3ac
Bump V3.0.22 (#110)
* change simulator svg

* change radio image

* Remove google fonts cdn

* change color of 'advanced' button

* font fix

* font fix 2

* display fix

* change fullsceen simulator bg

* Continuous servo

* handle continuous state

* adding shims

* update rendering for continuous servos

* fixing sim

* fix sig

* typo

* fix sim

* bump pxt

* bump pxt

* rerun travis

* Input blocks revision

- add Button and Pin event types
- merge onPinPressed & onPinReleased in new onPinEvent function
- create new onButtonEvent function

* update input blocks in docs and tests

* remove device_pin_release block

* Hide DAL.x behind Enum

* bring back deprecated blocks, but hide them

* shims and locales files

* fix input.input. typing

* remove buildpr

* bump V3

* update simulator aspect ratio

* add Loudness Block

* revoke loudness block

* Adds soundLevel

To be replaced by pxt-common-packages when DAL is updated.

* Remove P0 & P3 from AnalogPin

Co-authored-by: Juri <gitkraken@juriwolf.de>
2020-09-08 02:04:25 -07:00

94 lines
2.8 KiB
TypeScript

/// <reference no-default-lib="true"/>
/**
* Reading and writing data to the console output.
*/
//% weight=12 color=#002050 icon="\uf120"
//% advanced=true
namespace console {
type Listener = (text: string) => void;
//% whenUsed
let listeners: Listener[] = undefined;
/**
* Write a line of text to the console output.
* @param value to send
*/
//% weight=90
//% help=console/log blockGap=8
export function log(text: any): void {
let stringified = inspect(text);
// pad text on the 32byte boundar
stringified += "\r\n";
control.__log(stringified);
// send to listeners
if (listeners)
for (let i = 0; i < listeners.length; ++i)
listeners[i](stringified);
}
/**
* Write a name:value pair as a line of text to the console output.
* @param name name of the value stream, eg: "x"
* @param value to write
*/
//% weight=88 blockGap=8
//% help=console/log-value
export function logValue(name: any, value: number): void {
const nameText = inspect(name);
log(nameText ? `${nameText}: ${value}` : `${value}`)
}
/**
* Adds a listener for the log messages
* @param listener
*/
//%
export function addListener(listener: (text: string) => void) {
if (!listener) return;
if (!listeners)
listeners = [];
listeners.push(listener);
}
/**
* Convert any object or value to a string representation
* @param obj value to be converted to a string
* @param maxElements [optional] max number values in an object to include in output
*/
export function inspect(obj: any, maxElements = 20): string {
if (typeof obj == "string") {
return obj;
} else if (typeof obj == "number") {
return "" + obj;
} else if (Array.isArray(obj)) {
const asArr = (obj as Array<string>);
if (asArr.length <= maxElements) {
return asArr.join(",");
} else {
return `${asArr.slice(0, maxElements).join(",")}...`;
}
} else {
const asString = obj + "";
if (asString != "[object Object]"
&& asString != "[Object]") { // on arcade at least, default toString is [Object] on hardware instead of standard
return asString;
}
let keys = Object.keys(obj);
const snipped = keys.length > maxElements;
if (snipped) {
keys = keys.slice(0, maxElements);
}
return `{${
keys.reduce(
(prev, currKey) => prev + `\n ${currKey}: ${obj[currKey]}`,
""
) + (snipped ? "\n ..." : "")
}
}`;
}
}
}