From 60bf3a17d3cf650a501965e87c1ae3916b0050da Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Mon, 18 Dec 2017 22:36:32 -0800 Subject: [PATCH] a mini-console support with scroll up / down (#112) * a mini-console support with scroll up / down * fix compile error --- libs/base/_locales/base-jsdoc-strings.json | 5 - libs/base/_locales/base-strings.json | 4 - libs/base/pxt.json | 3 +- libs/core/_locales/core-jsdoc-strings.json | 7 ++ libs/core/_locales/core-strings.json | 5 + libs/core/console.ts | 107 +++++++++++++++++++++ libs/core/pxt.json | 1 + 7 files changed, 121 insertions(+), 11 deletions(-) create mode 100644 libs/core/console.ts diff --git a/libs/base/_locales/base-jsdoc-strings.json b/libs/base/_locales/base-jsdoc-strings.json index c9a8d82c..3d209b44 100644 --- a/libs/base/_locales/base-jsdoc-strings.json +++ b/libs/base/_locales/base-jsdoc-strings.json @@ -123,11 +123,6 @@ "String.substr": "Return a substring of the current string.", "String.substr|param|length": "number of characters to extract", "String.substr|param|start": "first character index; can be negative from counting from the end, eg:0", - "console": "Reading and writing data to the console output.", - "console.log": "Write a line of text to the console output.", - "console.logValue": "Write a name:value pair as a line of text to the console output.", - "console.logValue|param|name": "name of the value stream, eg: \"x\"", - "console.logValue|param|value": "to write", "control": "Program controls and events.", "control.AnimationQueue.cancel": "Cancels the current running animation and clears the queue", "control.AnimationQueue.runUntilDone": "Runs 'render' in a loop until it returns false or the 'stop' function is called", diff --git a/libs/base/_locales/base-strings.json b/libs/base/_locales/base-strings.json index 487727cd..347db075 100644 --- a/libs/base/_locales/base-strings.json +++ b/libs/base/_locales/base-strings.json @@ -19,9 +19,6 @@ "String.length|block": "length of %VALUE", "String.substr|block": "substring of %this=text|from %start|of length %length", "String|block": "String", - "console.logValue|block": "console|log value %name|= %value", - "console.log|block": "console|log %text", - "console|block": "console", "control.assert|block": "assert %cond|with value %code", "control.deviceSerialNumber|block": "device serial number", "control.millis|block": "millis (ms)", @@ -46,7 +43,6 @@ "{id:category}Arrays": "Arrays", "{id:category}Boolean": "Boolean", "{id:category}Buffer": "Buffer", - "{id:category}Console": "Console", "{id:category}Control": "Control", "{id:category}Helpers": "Helpers", "{id:category}Loops": "Loops", diff --git a/libs/base/pxt.json b/libs/base/pxt.json index 6af58cb4..98322f56 100644 --- a/libs/base/pxt.json +++ b/libs/base/pxt.json @@ -17,8 +17,7 @@ "control.cpp", "control.ts", "serial.cpp", - "serial.ts", - "console.ts" + "serial.ts" ], "testFiles": [ "test.ts" diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 8b0bd60b..59602d72 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -43,6 +43,13 @@ "brick.setStatusLight|param|pattern": "the lights pattern to use.", "brick.showImage": "Shows an image on screen", "brick.showImage|param|image": "image to draw", + "console": "Reading and writing data to the console output.\r\n\r\nReading and writing data to the console output.", + "console.addListener": "Adds a listener for the log messages", + "console.log": "Write a line of text to the console output.", + "console.logValue": "Write a name:value pair as a line of text to the console output.", + "console.logValue|param|name": "name of the value stream, eg: \"x\"", + "console.logValue|param|value": "to write", + "console.sendToScreen": "Sends the log messages to the brick screen and uses the brick up and down buttons to scroll.", "control": "Program controls and events.", "control.allocateNotifyEvent": "Allocates the next user notification event", "control.deviceFirmwareVersion": "Determine the version of system software currently running.", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index 49726aed..6d8f7bdc 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -41,6 +41,10 @@ "brick.setStatusLight|block": "set `icons.brickButtons` to %pattern=led_pattern", "brick.showImage|block": "`icons.brickDisplay` show image %image=screen_image_picker", "brick|block": "brick", + "console.logValue|block": "console|log value %name|= %value", + "console.log|block": "console|log %text", + "console.sendToScreen|block": "send console to screen", + "console|block": "console", "control.raiseEvent|block": "raise event|from %src|with value %value", "control|block": "control", "motors.Motor.move|block": "move `icons.motorLarge` %motor|for %value|%unit|at %speed|%", @@ -71,6 +75,7 @@ "sensors|block": "sensors", "serial|block": "serial", "{id:category}Brick": "Brick", + "{id:category}Console": "Console", "{id:category}Control": "Control", "{id:category}Image": "Image", "{id:category}Images": "Images", diff --git a/libs/core/console.ts b/libs/core/console.ts new file mode 100644 index 00000000..58e9f809 --- /dev/null +++ b/libs/core/console.ts @@ -0,0 +1,107 @@ +/// + +/** + * Reading and writing data to the console output. + */ +//% weight=12 color=#002050 icon="\uf120" +//% advanced=true +namespace console { + type Listener = (text: string) => void; + + const listeners: Listener[] = [ + (text: string) => serial.writeLine(text) + ]; + + /** + * Write a line of text to the console output. + * @param value to send + */ + //% weight=90 + //% help=console/log blockGap=8 + //% blockId=console_log block="console|log %text" + export function log(text: string): void { + for (let i = 0; i < listeners.length; ++i) + listeners[i](text); + } + + /** + * 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 + //% blockId=console_log_value block="console|log value %name|= %value" + export function logValue(name: string, value: number): void { + log(`${name}: ${value}`) + } + + /** + * Adds a listener for the log messages + * @param listener + */ + //% + export function addListener(listener: (text: string) => void) { + if (!listener) return; + listeners.push(listener); + } + + /** + * Sends the log messages to the brick screen and uses the brick up and down buttons to scroll. + */ + //% blockId=logsendtostreen block="send console to screen" + //% weight=1 + export function sendToScreen(): void { + console.screen.attach(); + } +} + +namespace console.screen { + const maxLines = 100; + const screenLines = 8; + const lineHeight = 12; + let lines: string[]; + let scrollPosition = 0; + + export function attach() { + if (!lines) { + lines = []; + console.addListener(log); + brick.buttonUp.onEvent(ButtonEvent.Click, () => scroll(1)) + brick.buttonDown.onEvent(ButtonEvent.Click, () => scroll(-1)) + } + } + + function printLog() { + brick.clearScreen() + if (!lines) return; + for (let i = 0; i < screenLines; ++i) { + const line = lines[i + scrollPosition]; + if (line) + brick.print(line, 0, 4 + i * lineHeight) + } + } + + function scroll(pos: number) { + if (!pos) return; + + scrollPosition += pos >> 0; + if (scrollPosition >= lines.length) scrollPosition = lines.length - 1; + if (scrollPosition < 0) scrollPosition = 0; + printLog(); + } + + function log(msg: string): void { + lines.push(msg); + if (lines.length + 5 > maxLines) { + lines.splice(0, maxLines - lines.length); + scrollPosition = Math.min(scrollPosition, lines.length - 1) + } + // move down scroll once it gets large than the screen + if (lines.length > screenLines + && lines.length >= scrollPosition + screenLines) { + scrollPosition++; + } + printLog(); + } +} \ No newline at end of file diff --git a/libs/core/pxt.json b/libs/core/pxt.json index e2f9c559..30ec9d16 100644 --- a/libs/core/pxt.json +++ b/libs/core/pxt.json @@ -10,6 +10,7 @@ "linux.cpp", "mmap.cpp", "control.cpp", + "console.ts", "serialnumber.cpp", "buttons.ts", "png.cpp",