From 88934881f9846d268378b95ba916f0713082c754 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Fri, 15 Dec 2017 14:45:38 -0800 Subject: [PATCH] upgrading various simulator parts --- sim/dalboard.ts | 15 +++++++++--- sim/state/ledmatrix.ts | 52 ++++++++++++++++++++++++++++++++---------- sim/state/serial.ts | 28 ++++++++++++----------- 3 files changed, 67 insertions(+), 28 deletions(-) diff --git a/sim/dalboard.ts b/sim/dalboard.ts index 5ac586ec..d123b64a 100644 --- a/sim/dalboard.ts +++ b/sim/dalboard.ts @@ -18,6 +18,9 @@ namespace pxsim { speakerState: SpeakerState; fileSystem: FileSystemState; + // visual + view: SVGElement; + constructor() { super() @@ -94,7 +97,7 @@ namespace pxsim { break; case "serial": let data = (msg).data || ""; - this.serialState.recieveData(data); + this.serialState.receiveData(data); break; case "radiopacket": let packet = msg; @@ -121,16 +124,22 @@ namespace pxsim { fnArgs: fnArgs, maxWidth: "100%", maxHeight: "100%", + highContrast: msg.highContrast }; const viewHost = new visuals.BoardHost(pxsim.visuals.mkBoardView({ - visual: boardDef.visual + visual: boardDef.visual, + highContrast: msg.highContrast }), opts); document.body.innerHTML = ""; // clear children - document.body.appendChild(viewHost.getView()); + document.body.appendChild(this.view = viewHost.getView()); return Promise.resolve(); } + + screenshot(): string { + return svg.toDataUri(new XMLSerializer().serializeToString(this.view)); + } } export function initRuntimeWithDalBoard() { diff --git a/sim/state/ledmatrix.ts b/sim/state/ledmatrix.ts index f8d32ab8..7c62600d 100644 --- a/sim/state/ledmatrix.ts +++ b/sim/state/ledmatrix.ts @@ -28,7 +28,7 @@ namespace pxsim { this.data = data; } public print() { - console.log(`Image id:${this.id} refs:${this.refcnt} size:${this.width}x${Image.height}`) + // console.debug(`Image id:${this.id} refs:${this.refcnt} size:${this.width}x${Image.height}`) } public get(x: number, y: number): number { if (x < 0 || x >= this.width || y < 0 || y >= 5) return 0; @@ -131,15 +131,33 @@ namespace pxsim.images { namespace pxsim.ImageMethods { export function showImage(leds: Image, offset: number, interval: number) { pxtrt.nullCheck(leds) - leds.copyTo(offset, 5, board().ledMatrixState.image, 0) - runtime.queueDisplayUpdate() - basic.pause(interval); + let cb = getResume(); + let first = true; + + board().ledMatrixState.animationQ.enqueue({ + interval, + frame: () => { + if (first) { + leds.copyTo(offset, 5, board().ledMatrixState.image, 0) + first = false; + return true; + } + return false; + }, + whenDone: cb + }) } export function plotImage(leds: Image, offset: number): void { pxtrt.nullCheck(leds) - leds.copyTo(offset, 5, board().ledMatrixState.image, 0) - runtime.queueDisplayUpdate() + + board().ledMatrixState.animationQ.enqueue({ + interval: 0, + frame: () => { + leds.copyTo(offset, 5, board().ledMatrixState.image, 0) + return false; + } + }) } export function height(leds: Image): number { @@ -216,15 +234,16 @@ namespace pxsim.ImageMethods { namespace pxsim.basic { export function showNumber(x: number, interval: number) { - if (interval < 0) return; - + if (interval <= 0) + interval = 1; let leds = createImageFromString(x.toString()); if (x < 0 || x >= 10) ImageMethods.scrollImage(leds, 1, interval); else showLeds(leds, interval * 5); } export function showString(s: string, interval: number) { - if (interval < 0) return; + if (interval <= 0) + interval = 1; if (s.length == 0) { clearScreen(); pause(interval * 5); @@ -254,7 +273,16 @@ namespace pxsim.basic { namespace pxsim.led { export function plot(x: number, y: number) { - board().ledMatrixState.image.set(x, y, 255); + board().ledMatrixState.image.set(x, y, 0xff); + runtime.queueDisplayUpdate() + } + + export function plotBrightness(x: number, y: number, brightness: number) { + const state = board().ledMatrixState; + brightness = Math.max(0, Math.min(0xff, brightness)); + if (brightness != 0 && brightness != 0xff && state.displayMode != DisplayMode.greyscale) + state.displayMode = DisplayMode.greyscale; + state.image.set(x, y, brightness); runtime.queueDisplayUpdate() } @@ -272,7 +300,7 @@ namespace pxsim.led { } export function setBrightness(value: number): void { - board().ledMatrixState.brigthness = value; + board().ledMatrixState.brigthness = Math.max(0, Math.min(255, value)); runtime.queueDisplayUpdate() } @@ -286,7 +314,7 @@ namespace pxsim.led { runtime.queueDisplayUpdate() } - export function displayMode() : DisplayMode { + export function displayMode(): DisplayMode { return board().ledMatrixState.displayMode; } diff --git a/sim/state/serial.ts b/sim/state/serial.ts index 518d067e..de7cdf6b 100644 --- a/sim/state/serial.ts +++ b/sim/state/serial.ts @@ -1,8 +1,9 @@ namespace pxsim { + const SERIAL_BUFFER_LENGTH = 16; export class SerialState { serialIn: string[] = []; - public recieveData(data: string) { + public receiveData(data: string) { this.serialIn.push(); } @@ -13,18 +14,15 @@ namespace pxsim { serialOutBuffer: string = ""; writeSerial(s: string) { - for (let i = 0; i < s.length; ++i) { - let c = s[i]; - this.serialOutBuffer += c; - if (c == "\n") { - Runtime.postMessage({ - type: "serial", - data: this.serialOutBuffer, - id: runtime.id - }) - this.serialOutBuffer = "" - break; - } + this.serialOutBuffer += s; + if (/\n/.test(this.serialOutBuffer) || this.serialOutBuffer.length > SERIAL_BUFFER_LENGTH) { + Runtime.postMessage({ + type: 'serial', + data: this.serialOutBuffer, + id: runtime.id, + sim: true + }) + this.serialOutBuffer = ''; } } } @@ -51,4 +49,8 @@ namespace pxsim.serial { export function redirect(tx: number, rx: number, rate: number) { // TODO? } + + export function redirectToUSB() { + // TODO + } } \ No newline at end of file