2016-03-11 01:24:11 +01:00
|
|
|
/// <reference path="../node_modules/kindscript/typings/bluebird/bluebird.d.ts"/>
|
|
|
|
/// <reference path="../node_modules/kindscript/built/kindsim.d.ts"/>
|
2016-04-02 00:44:04 +02:00
|
|
|
/// <reference path="../libs/microbit/dal.d.ts"/>
|
2016-03-11 01:24:11 +01:00
|
|
|
|
|
|
|
namespace ks.rt.micro_bit {
|
|
|
|
export function initCurrentRuntime() {
|
|
|
|
U.assert(!runtime.board)
|
|
|
|
runtime.board = new Board()
|
|
|
|
}
|
|
|
|
|
2016-03-11 07:09:04 +01:00
|
|
|
ks.rt.initCurrentRuntime = initCurrentRuntime;
|
|
|
|
|
2016-03-11 01:24:11 +01:00
|
|
|
export function board() {
|
|
|
|
return runtime.board as Board
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AnimationOptions {
|
|
|
|
interval: number;
|
|
|
|
// false means last frame
|
|
|
|
frame: () => boolean;
|
|
|
|
whenDone?: (cancelled: boolean) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class AnimationQueue {
|
|
|
|
private queue: AnimationOptions[] = [];
|
|
|
|
private process: () => void;
|
|
|
|
|
|
|
|
constructor(private runtime: Runtime) {
|
|
|
|
this.process = () => {
|
|
|
|
let top = this.queue[0]
|
|
|
|
if (!top) return
|
|
|
|
if (this.runtime.dead) return
|
|
|
|
runtime = this.runtime
|
|
|
|
let res = top.frame()
|
|
|
|
runtime.queueDisplayUpdate()
|
|
|
|
runtime.maybeUpdateDisplay()
|
|
|
|
if (res === false) {
|
|
|
|
this.queue.shift();
|
|
|
|
// if there is already something in the queue, start processing
|
|
|
|
if (this.queue[0])
|
|
|
|
setTimeout(this.process, this.queue[0].interval)
|
|
|
|
// this may push additional stuff
|
|
|
|
top.whenDone(false);
|
|
|
|
} else {
|
|
|
|
setTimeout(this.process, top.interval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public cancelAll() {
|
|
|
|
let q = this.queue
|
|
|
|
this.queue = []
|
|
|
|
for (let a of q) {
|
|
|
|
a.whenDone(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public cancelCurrent() {
|
|
|
|
let top = this.queue[0]
|
|
|
|
if (top) {
|
|
|
|
this.queue.shift();
|
|
|
|
top.whenDone(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public enqueue(anim: AnimationOptions) {
|
|
|
|
if (!anim.whenDone) anim.whenDone = () => { };
|
|
|
|
this.queue.push(anim)
|
|
|
|
// we start processing when the queue goes from 0 to 1
|
|
|
|
if (this.queue.length == 1)
|
|
|
|
this.process()
|
|
|
|
}
|
|
|
|
|
|
|
|
public executeAsync(anim: AnimationOptions) {
|
|
|
|
U.assert(!anim.whenDone)
|
|
|
|
return new Promise<boolean>((resolve, reject) => {
|
|
|
|
anim.whenDone = resolve
|
|
|
|
this.enqueue(anim)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function panic(code: number) {
|
|
|
|
console.log("PANIC:", code)
|
|
|
|
throw new Error("PANIC " + code)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* leds */
|
|
|
|
export function plot(x: number, y: number) {
|
|
|
|
board().image.set(x, y, 255);
|
|
|
|
runtime.queueDisplayUpdate()
|
|
|
|
}
|
|
|
|
|
|
|
|
export function unPlot(x: number, y: number) {
|
|
|
|
board().image.set(x, y, 0);
|
|
|
|
runtime.queueDisplayUpdate()
|
|
|
|
}
|
|
|
|
|
|
|
|
export function point(x: number, y: number): boolean {
|
|
|
|
return !!board().image.get(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function brightness(): number {
|
|
|
|
return board().brigthness;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setBrightness(value: number): void {
|
|
|
|
board().brigthness = value;
|
|
|
|
runtime.queueDisplayUpdate()
|
|
|
|
}
|
|
|
|
|
|
|
|
export function stopAnimation(): void {
|
|
|
|
board().animationQ.cancelAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function plotLeds(leds: micro_bit.Image): void {
|
|
|
|
leds.copyTo(0, 5, board().image, 0)
|
|
|
|
runtime.queueDisplayUpdate()
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setDisplayMode(mode: DisplayMode): void {
|
|
|
|
board().displayMode = mode;
|
|
|
|
runtime.queueDisplayUpdate()
|
|
|
|
}
|
|
|
|
|
|
|
|
/* serial */
|
|
|
|
export function serialSendString(s: string) {
|
|
|
|
board().writeSerial(s);
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function serialReadString(): string {
|
2016-03-11 01:24:11 +01:00
|
|
|
return board().readSerial();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* input */
|
2016-03-31 00:53:00 +02:00
|
|
|
export function onButtonPressed(button: number, handler: RefAction): void {
|
2016-03-18 08:10:54 +01:00
|
|
|
let b = board();
|
2016-04-02 00:44:04 +02:00
|
|
|
if (button == DAL.MICROBIT_ID_BUTTON_AB && !board().usesButtonAB) {
|
2016-03-18 08:10:54 +01:00
|
|
|
b.usesButtonAB = true;
|
2016-03-18 22:54:27 +01:00
|
|
|
runtime.queueDisplayUpdate();
|
2016-03-18 08:10:54 +01:00
|
|
|
}
|
2016-04-02 00:44:04 +02:00
|
|
|
b.bus.listen(button, DAL.MICROBIT_BUTTON_EVT_CLICK, handler);
|
2016-03-11 01:24:11 +01:00
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
2016-03-11 01:24:11 +01:00
|
|
|
export function isButtonPressed(button: number): boolean {
|
2016-03-18 08:10:54 +01:00
|
|
|
let b = board();
|
2016-04-02 00:44:04 +02:00
|
|
|
if (button == DAL.MICROBIT_ID_BUTTON_AB && !board().usesButtonAB) {
|
2016-03-18 08:10:54 +01:00
|
|
|
b.usesButtonAB = true;
|
2016-03-18 22:54:27 +01:00
|
|
|
runtime.queueDisplayUpdate();
|
2016-03-11 01:24:11 +01:00
|
|
|
}
|
2016-03-18 22:54:27 +01:00
|
|
|
let bts = b.buttons;
|
2016-04-02 00:44:04 +02:00
|
|
|
if (button == DAL.MICROBIT_ID_BUTTON_A) return bts[0].pressed;
|
|
|
|
if (button == DAL.MICROBIT_ID_BUTTON_B) return bts[1].pressed;
|
2016-03-11 01:24:11 +01:00
|
|
|
return bts[2].pressed || (bts[0].pressed && bts[1].pressed);
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
2016-03-18 22:54:27 +01:00
|
|
|
export function onGesture(gesture: number, handler: RefAction) {
|
|
|
|
let b = board();
|
2016-03-20 03:15:20 +01:00
|
|
|
b.accelerometer.activate();
|
2016-03-31 00:53:00 +02:00
|
|
|
|
2016-03-18 22:54:27 +01:00
|
|
|
if (gesture == 11 && !b.useShake) { // SAKE
|
|
|
|
b.useShake = true;
|
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
}
|
2016-04-02 00:44:04 +02:00
|
|
|
b.bus.listen(DAL.MICROBIT_ID_GESTURE, gesture, handler);
|
2016-03-18 22:54:27 +01:00
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
2016-03-11 01:24:11 +01:00
|
|
|
export function onPinPressed(pin: Pin, handler: RefAction) {
|
|
|
|
pin.isTouched();
|
|
|
|
onButtonPressed(pin.id, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ioP0() { return board().pins[0]; }
|
|
|
|
export function ioP1() { return board().pins[1]; }
|
|
|
|
export function ioP2() { return board().pins[2]; }
|
|
|
|
export function ioP3() { return board().pins[3]; }
|
|
|
|
export function ioP4() { return board().pins[4]; }
|
|
|
|
export function ioP5() { return board().pins[5]; }
|
|
|
|
export function ioP6() { return board().pins[6]; }
|
|
|
|
export function ioP7() { return board().pins[7]; }
|
|
|
|
export function ioP8() { return board().pins[8]; }
|
|
|
|
export function ioP9() { return board().pins[9]; }
|
|
|
|
export function ioP10() { return board().pins[10]; }
|
|
|
|
export function ioP11() { return board().pins[11]; }
|
|
|
|
export function ioP12() { return board().pins[12]; }
|
|
|
|
export function ioP13() { return board().pins[13]; }
|
|
|
|
export function ioP14() { return board().pins[14]; }
|
|
|
|
export function ioP15() { return board().pins[15]; }
|
|
|
|
export function ioP16() { return board().pins[16]; }
|
|
|
|
export function ioP19() { return board().pins[19]; }
|
|
|
|
export function ioP20() { return board().pins[20]; }
|
|
|
|
|
|
|
|
export function isPinTouched(pin: Pin): boolean {
|
|
|
|
return pin.isTouched();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function compassHeading(): number {
|
|
|
|
var b = board();
|
|
|
|
if (!b.usesHeading) {
|
|
|
|
b.usesHeading = true;
|
2016-03-18 22:54:27 +01:00
|
|
|
runtime.queueDisplayUpdate();
|
2016-03-11 01:24:11 +01:00
|
|
|
}
|
|
|
|
return b.heading;
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
2016-03-14 22:03:31 +01:00
|
|
|
export function temperature(): number {
|
|
|
|
var b = board();
|
|
|
|
if (!b.usesTemperature) {
|
|
|
|
b.usesTemperature = true;
|
2016-03-18 22:54:27 +01:00
|
|
|
runtime.queueDisplayUpdate();
|
2016-03-14 22:03:31 +01:00
|
|
|
}
|
|
|
|
return b.temperature;
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
2016-03-11 01:24:11 +01:00
|
|
|
export function getAcceleration(dimension: number): number {
|
2016-03-11 22:17:49 +01:00
|
|
|
let b = board();
|
2016-03-20 03:15:20 +01:00
|
|
|
let acc = b.accelerometer;
|
|
|
|
acc.activate();
|
2016-03-11 01:24:11 +01:00
|
|
|
switch (dimension) {
|
2016-03-20 03:15:20 +01:00
|
|
|
case 0: return acc.getX();
|
|
|
|
case 1: return acc.getY();
|
|
|
|
case 2: return acc.getZ();
|
|
|
|
default: return Math.floor(Math.sqrt(acc.instantaneousAccelerationSquared()));
|
2016-03-11 01:24:11 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function setAccelerometerRange(range: number) {
|
2016-03-16 22:56:50 +01:00
|
|
|
let b = board();
|
2016-03-20 03:15:20 +01:00
|
|
|
b.accelerometer.setSampleRange(range);
|
2016-03-16 22:56:50 +01:00
|
|
|
}
|
2016-03-11 01:24:11 +01:00
|
|
|
|
|
|
|
export function lightLevel(): number {
|
2016-03-11 22:17:49 +01:00
|
|
|
let b = board();
|
|
|
|
if (!b.usesLightLevel) {
|
|
|
|
b.usesLightLevel = true;
|
2016-03-18 22:54:27 +01:00
|
|
|
runtime.queueDisplayUpdate();
|
2016-03-11 22:17:49 +01:00
|
|
|
}
|
|
|
|
return b.lightLevel;
|
2016-03-11 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getMagneticForce(): number {
|
|
|
|
// TODO
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCurrentTime(): number {
|
|
|
|
return runtime.runningTime();
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
2016-03-11 01:24:11 +01:00
|
|
|
/* pins */
|
2016-03-31 00:53:00 +02:00
|
|
|
export function digitalReadPin(pin: Pin): number {
|
2016-03-11 01:24:11 +01:00
|
|
|
pin.mode = PinMode.Digital | PinMode.Input;
|
|
|
|
return pin.value > 100 ? 1 : 0;
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function digitalWritePin(pin: Pin, value: number) {
|
2016-03-11 01:24:11 +01:00
|
|
|
pin.mode = PinMode.Digital | PinMode.Output;
|
|
|
|
pin.value = value > 0 ? 1023 : 0;
|
2016-03-18 22:54:27 +01:00
|
|
|
runtime.queueDisplayUpdate();
|
2016-03-11 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2016-03-31 00:53:00 +02:00
|
|
|
export function analogReadPin(pin: Pin): number {
|
2016-03-11 01:24:11 +01:00
|
|
|
pin.mode = PinMode.Analog | PinMode.Input;
|
|
|
|
return pin.value || 0;
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function analogWritePin(pin: Pin, value: number) {
|
2016-03-11 01:24:11 +01:00
|
|
|
pin.mode = PinMode.Analog | PinMode.Output;
|
|
|
|
pin.value = value ? 1 : 0;
|
2016-03-18 22:54:27 +01:00
|
|
|
runtime.queueDisplayUpdate();
|
2016-03-11 01:24:11 +01:00
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function setAnalogPeriodUs(pin: Pin, micros: number) {
|
2016-03-11 01:24:11 +01:00
|
|
|
pin.mode = PinMode.Analog | PinMode.Output;
|
2016-03-14 16:32:02 +01:00
|
|
|
pin.period = micros;
|
2016-03-18 22:54:27 +01:00
|
|
|
runtime.queueDisplayUpdate();
|
2016-03-11 01:24:11 +01:00
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
2016-03-11 01:24:11 +01:00
|
|
|
export function servoWritePin(pin: Pin, value: number) {
|
|
|
|
setAnalogPeriodUs(pin, 20000);
|
|
|
|
// TODO
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function servoSetPulse(pin: Pin, micros: number) {
|
2016-03-11 01:24:11 +01:00
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
2016-03-14 05:24:11 +01:00
|
|
|
module AudioContextManager {
|
2016-03-31 00:53:00 +02:00
|
|
|
var _context: any; // AudioContext
|
|
|
|
var _vco: any; //OscillatorNode;
|
2016-03-14 05:24:11 +01:00
|
|
|
var _vca: any; // GainNode;
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
function context(): any {
|
2016-03-14 05:24:11 +01:00
|
|
|
if (!_context) _context = freshContext();
|
|
|
|
return _context;
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
function freshContext(): any {
|
2016-03-14 05:24:11 +01:00
|
|
|
(<any>window).AudioContext = (<any>window).AudioContext || (<any>window).webkitAudioContext;
|
|
|
|
if ((<any>window).AudioContext) {
|
|
|
|
try {
|
|
|
|
// this call my crash.
|
|
|
|
// SyntaxError: audio resources unavailable for AudioContext construction
|
2016-03-31 00:53:00 +02:00
|
|
|
return new (<any>window).AudioContext();
|
|
|
|
} catch (e) { }
|
|
|
|
}
|
2016-03-14 05:24:11 +01:00
|
|
|
return undefined;
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
2016-03-14 05:24:11 +01:00
|
|
|
export function stop() {
|
|
|
|
if (_vca) _vca.gain.value = 0;
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function tone(frequency: number, gain: number) {
|
|
|
|
if (frequency <= 0) return;
|
2016-03-14 05:24:11 +01:00
|
|
|
var ctx = context();
|
|
|
|
if (!ctx) return;
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
gain = Math.max(0, Math.min(1, gain));
|
2016-03-14 05:24:11 +01:00
|
|
|
if (!_vco) {
|
|
|
|
try {
|
|
|
|
_vco = ctx.createOscillator();
|
|
|
|
_vca = ctx.createGain();
|
|
|
|
_vco.connect(_vca);
|
|
|
|
_vca.connect(ctx.destination);
|
|
|
|
_vca.gain.value = gain;
|
|
|
|
_vco.start(0);
|
2016-03-31 00:53:00 +02:00
|
|
|
} catch (e) {
|
2016-03-14 05:24:11 +01:00
|
|
|
_vco = undefined;
|
|
|
|
_vca = undefined;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
2016-03-14 05:24:11 +01:00
|
|
|
_vco.frequency.value = frequency;
|
|
|
|
_vca.gain.value = gain;
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
2016-03-11 01:24:11 +01:00
|
|
|
export function enablePitch(pin: Pin) {
|
2016-03-14 17:10:13 +01:00
|
|
|
board().pins.filter(p => !!p).forEach(p => p.pitch = false);
|
|
|
|
pin.pitch = true;
|
2016-03-11 01:24:11 +01:00
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
2016-03-11 01:24:11 +01:00
|
|
|
export function pitch(frequency: number, ms: number) {
|
2016-03-14 16:32:02 +01:00
|
|
|
// update analog output
|
2016-03-14 17:10:13 +01:00
|
|
|
let pin = board().pins.filter(pin => !!pin && pin.pitch)[0] || board().pins[0];
|
|
|
|
pin.mode = PinMode.Analog | PinMode.Output;
|
2016-03-14 16:32:02 +01:00
|
|
|
if (frequency <= 0) {
|
2016-03-31 00:53:00 +02:00
|
|
|
pin.value = 0;
|
|
|
|
pin.period = 0;
|
2016-03-14 16:32:02 +01:00
|
|
|
} else {
|
|
|
|
pin.value = 512;
|
2016-03-31 00:53:00 +02:00
|
|
|
pin.period = 1000000 / frequency;
|
2016-03-14 16:32:02 +01:00
|
|
|
}
|
2016-03-18 22:54:27 +01:00
|
|
|
runtime.queueDisplayUpdate();
|
2016-03-31 00:53:00 +02:00
|
|
|
|
2016-03-11 01:24:11 +01:00
|
|
|
let cb = getResume();
|
2016-03-14 05:24:11 +01:00
|
|
|
AudioContextManager.tone(frequency, 1);
|
2016-03-14 16:32:02 +01:00
|
|
|
if (ms <= 0) cb();
|
|
|
|
else {
|
|
|
|
setTimeout(() => {
|
2016-03-31 00:53:00 +02:00
|
|
|
AudioContextManager.stop();
|
|
|
|
pin.value = 0;
|
|
|
|
pin.period = 0;
|
2016-03-14 17:10:13 +01:00
|
|
|
pin.mode = PinMode.Unused;
|
2016-03-18 22:54:27 +01:00
|
|
|
runtime.queueDisplayUpdate();
|
2016-03-31 00:53:00 +02:00
|
|
|
cb()
|
2016-03-14 16:32:02 +01:00
|
|
|
}, ms);
|
|
|
|
}
|
2016-03-11 01:24:11 +01:00
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
|
2016-03-11 01:24:11 +01:00
|
|
|
/* radio */
|
2016-03-31 00:53:00 +02:00
|
|
|
export function broadcastMessage(msg: number): void {
|
2016-03-11 01:24:11 +01:00
|
|
|
board().radio.broadcast(msg);
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function onBroadcastMessageReceived(msg: number, handler: RefAction): void {
|
2016-04-02 00:44:04 +02:00
|
|
|
board().bus.listen(DAL.MES_BROADCAST_GENERAL_ID, msg, handler);
|
2016-03-11 01:24:11 +01:00
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function setGroup(id: number): void {
|
2016-03-11 01:24:11 +01:00
|
|
|
board().radio.setGroup(id);
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function setTransmitPower(power: number): void {
|
|
|
|
board().radio.setTransmitPower(power);
|
2016-03-15 21:05:11 +01:00
|
|
|
}
|
2016-03-11 01:24:11 +01:00
|
|
|
|
2016-03-31 00:53:00 +02:00
|
|
|
export function datagramSendNumbers(value0: number, value1: number, value2: number, value3: number): void {
|
2016-03-11 01:24:11 +01:00
|
|
|
board().radio.datagram.send([value0, value1, value2, value3]);
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function datagramReceiveNumber(): number {
|
2016-03-11 01:24:11 +01:00
|
|
|
return board().radio.datagram.recv().data[0];
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function datagramGetNumber(index: number): number {
|
2016-03-11 01:24:11 +01:00
|
|
|
return board().radio.datagram.lastReceived.data[index] || 0;
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function datagramGetRSSI(): number {
|
2016-03-11 01:24:11 +01:00
|
|
|
return board().radio.datagram.lastReceived.rssi;
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function onDatagramReceived(handler: RefAction): void {
|
2016-04-02 00:44:04 +02:00
|
|
|
board().bus.listen(DAL.MICROBIT_ID_RADIO, DAL.MICROBIT_RADIO_EVT_DATAGRAM, handler);
|
2016-03-11 01:24:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 00:53:00 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|