upgrading various simulator parts

This commit is contained in:
Peli de Halleux 2017-12-15 14:45:38 -08:00
parent 433e8c8805
commit 88934881f9
3 changed files with 67 additions and 28 deletions

View File

@ -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 = (<SimulatorSerialMessage>msg).data || "";
this.serialState.recieveData(data);
this.serialState.receiveData(data);
break;
case "radiopacket":
let packet = <SimulatorRadioPacketMessage>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() {

View File

@ -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)
let cb = getResume();
let first = true;
board().ledMatrixState.animationQ.enqueue({
interval,
frame: () => {
if (first) {
leds.copyTo(offset, 5, board().ledMatrixState.image, 0)
runtime.queueDisplayUpdate()
basic.pause(interval);
first = false;
return true;
}
return false;
},
whenDone: cb
})
}
export function plotImage(leds: Image, offset: number): void {
pxtrt.nullCheck(leds)
board().ledMatrixState.animationQ.enqueue({
interval: 0,
frame: () => {
leds.copyTo(offset, 5, board().ledMatrixState.image, 0)
runtime.queueDisplayUpdate()
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()
}

View File

@ -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") {
this.serialOutBuffer += s;
if (/\n/.test(this.serialOutBuffer) || this.serialOutBuffer.length > SERIAL_BUFFER_LENGTH) {
Runtime.postMessage(<SimulatorSerialMessage>{
type: "serial",
type: 'serial',
data: this.serialOutBuffer,
id: runtime.id
id: runtime.id,
sim: true
})
this.serialOutBuffer = ""
break;
}
this.serialOutBuffer = '';
}
}
}
@ -51,4 +49,8 @@ namespace pxsim.serial {
export function redirect(tx: number, rx: number, rate: number) {
// TODO?
}
export function redirectToUSB() {
// TODO
}
}