Move C++ sim stuff to proper namespace
This commit is contained in:
parent
61dd0075b2
commit
b76b32a825
@ -6,6 +6,6 @@
|
||||
],
|
||||
"public": true,
|
||||
"dependencies": {
|
||||
"core": "file:../microbit"
|
||||
"microbit": "file:../microbit"
|
||||
}
|
||||
}
|
||||
|
151
sim/libmbit.ts
151
sim/libmbit.ts
@ -88,78 +88,6 @@ namespace ks.rt.micro_bit {
|
||||
throw new Error("PANIC " + code)
|
||||
}
|
||||
|
||||
/* basic */
|
||||
export function showDigit(v: number) {
|
||||
if (!quiet)
|
||||
console.log("DIGIT:", v)
|
||||
plotLeds(createImageFromString(v.toString()[0]));
|
||||
}
|
||||
|
||||
export function clearScreen() {
|
||||
board().image.clear();
|
||||
runtime.queueDisplayUpdate()
|
||||
}
|
||||
|
||||
export function showLeds(leds: micro_bit.Image, delay: number): void {
|
||||
showAnimation(leds, delay);
|
||||
}
|
||||
|
||||
function scrollImage(leds: micro_bit.Image, interval: number, stride: number): void {
|
||||
let cb = getResume()
|
||||
let off = stride > 0 ? 0 : leds.width - 1;
|
||||
let display = board().image;
|
||||
|
||||
board().animationQ.enqueue({
|
||||
interval: interval,
|
||||
frame: () => {
|
||||
if (off >= leds.width || off < 0) return false;
|
||||
stride > 0 ? display.shiftLeft(stride) : display.shiftRight(-stride);
|
||||
let c = Math.min(stride, leds.width - off);
|
||||
leds.copyTo(off, c, display, 5 - stride)
|
||||
off += stride;
|
||||
return true;
|
||||
},
|
||||
whenDone: cb
|
||||
})
|
||||
}
|
||||
|
||||
export function showAnimation(leds: micro_bit.Image, interval: number = 400): void {
|
||||
scrollImage(leds, interval, 5);
|
||||
}
|
||||
|
||||
export function scrollNumber(x: number, interval: number) {
|
||||
if (interval < 0) return;
|
||||
|
||||
let leds = createImageFromString(x.toString());
|
||||
if (x < 0 || x >= 10) scrollImage(leds, interval, 1);
|
||||
else showLeds(leds, interval * 5);
|
||||
}
|
||||
|
||||
export function scrollString(s: string, interval: number) {
|
||||
if (interval < 0) return;
|
||||
if (s.length == 0) {
|
||||
clearScreen();
|
||||
pause(interval * 5);
|
||||
} else {
|
||||
let leds = createImageFromString(s);
|
||||
if (s.length == 1) showLeds(leds, interval * 5)
|
||||
else scrollImage(leds, interval, 1);
|
||||
}
|
||||
}
|
||||
|
||||
export function forever(a: RefAction) {
|
||||
function loop() {
|
||||
runtime.runFiberAsync(a)
|
||||
.then(() => Promise.delay(20))
|
||||
.then(loop)
|
||||
.done()
|
||||
}
|
||||
incr(a)
|
||||
loop()
|
||||
}
|
||||
|
||||
export var pause = thread.pause;
|
||||
|
||||
/* leds */
|
||||
export function plot(x: number, y: number) {
|
||||
board().image.set(x, y, 255);
|
||||
@ -198,9 +126,6 @@ namespace ks.rt.micro_bit {
|
||||
runtime.queueDisplayUpdate()
|
||||
}
|
||||
|
||||
/* control */
|
||||
export var runInBackground = thread.runInBackground;
|
||||
|
||||
/* serial */
|
||||
export function serialSendString(s: string) {
|
||||
board().writeSerial(s);
|
||||
@ -491,3 +416,79 @@ namespace ks.rt.micro_bit {
|
||||
}
|
||||
}
|
||||
|
||||
namespace ks.rt.basic {
|
||||
var board = micro_bit.board;
|
||||
|
||||
export var pause = thread.pause;
|
||||
|
||||
export function showNumber(x: number, interval: number) {
|
||||
if (interval < 0) return;
|
||||
|
||||
let leds = micro_bit.createImageFromString(x.toString());
|
||||
if (x < 0 || x >= 10) scrollImage(leds, interval, 1);
|
||||
else showLeds(leds, interval * 5);
|
||||
}
|
||||
|
||||
export function showString(s: string, interval: number) {
|
||||
if (interval < 0) return;
|
||||
if (s.length == 0) {
|
||||
clearScreen();
|
||||
pause(interval * 5);
|
||||
} else {
|
||||
let leds = micro_bit.createImageFromString(s);
|
||||
if (s.length == 1) showLeds(leds, interval * 5)
|
||||
else scrollImage(leds, interval, 1);
|
||||
}
|
||||
}
|
||||
|
||||
export function showLeds(leds: micro_bit.Image, delay: number): void {
|
||||
showAnimation(leds, delay);
|
||||
}
|
||||
|
||||
export function clearScreen() {
|
||||
board().image.clear();
|
||||
runtime.queueDisplayUpdate()
|
||||
}
|
||||
|
||||
function scrollImage(leds: micro_bit.Image, interval: number, stride: number): void {
|
||||
let cb = getResume()
|
||||
let off = stride > 0 ? 0 : leds.width - 1;
|
||||
let display = board().image;
|
||||
|
||||
board().animationQ.enqueue({
|
||||
interval: interval,
|
||||
frame: () => {
|
||||
if (off >= leds.width || off < 0) return false;
|
||||
stride > 0 ? display.shiftLeft(stride) : display.shiftRight(-stride);
|
||||
let c = Math.min(stride, leds.width - off);
|
||||
leds.copyTo(off, c, display, 5 - stride)
|
||||
off += stride;
|
||||
return true;
|
||||
},
|
||||
whenDone: cb
|
||||
})
|
||||
}
|
||||
|
||||
export function showAnimation(leds: micro_bit.Image, interval: number = 400): void {
|
||||
scrollImage(leds, interval, 5);
|
||||
}
|
||||
|
||||
export function forever(a: RefAction) {
|
||||
function loop() {
|
||||
runtime.runFiberAsync(a)
|
||||
.then(() => Promise.delay(20))
|
||||
.then(loop)
|
||||
.done()
|
||||
}
|
||||
incr(a)
|
||||
loop()
|
||||
}
|
||||
}
|
||||
|
||||
namespace ks.rt.control {
|
||||
export var inBackground = thread.runInBackground;
|
||||
|
||||
export function reset() {
|
||||
U.userError("reset not implemented in simulator yet")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user