2016-04-07 20:30:22 +02:00
|
|
|
/// <reference path="../node_modules/pxt-core/typings/bluebird/bluebird.d.ts"/>
|
|
|
|
/// <reference path="../node_modules/pxt-core/built/pxtsim.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
|
|
|
|
2016-04-07 21:52:02 +02:00
|
|
|
namespace pxsim {
|
|
|
|
pxsim.initCurrentRuntime = () => {
|
2016-03-11 01:24:11 +01:00
|
|
|
U.assert(!runtime.board)
|
|
|
|
runtime.board = new Board()
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-04-14 16:39:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Error codes used in the micro:bit runtime.
|
|
|
|
*/
|
|
|
|
export enum PanicCode {
|
|
|
|
// PANIC Codes. These are not return codes, but are terminal conditions.
|
|
|
|
// These induce a panic operation, where all code stops executing, and a panic state is
|
|
|
|
// entered where the panic code is diplayed.
|
|
|
|
|
|
|
|
// Out out memory error. Heap storage was requested, but is not available.
|
|
|
|
MICROBIT_OOM = 20,
|
|
|
|
|
|
|
|
// Corruption detected in the micro:bit heap space
|
|
|
|
MICROBIT_HEAP_ERROR = 30,
|
|
|
|
|
|
|
|
// Dereference of a NULL pointer through the ManagedType class,
|
|
|
|
MICROBIT_NULL_DEREFERENCE = 40,
|
|
|
|
};
|
2016-03-11 01:24:11 +01:00
|
|
|
|
|
|
|
export function panic(code: number) {
|
|
|
|
console.log("PANIC:", code)
|
2016-04-14 16:39:24 +02:00
|
|
|
led.setBrightness(255);
|
|
|
|
let img = board().image;
|
|
|
|
img.clear();
|
|
|
|
img.set(0, 4, 255);
|
|
|
|
img.set(1, 3, 255);
|
|
|
|
img.set(2, 3, 255);
|
|
|
|
img.set(3, 3, 255);
|
|
|
|
img.set(4, 4, 255);
|
|
|
|
img.set(0, 0, 255);
|
|
|
|
img.set(1, 0, 255);
|
|
|
|
img.set(0, 1, 255);
|
|
|
|
img.set(1, 1, 255);
|
|
|
|
img.set(3, 0, 255);
|
|
|
|
img.set(4, 0, 255);
|
|
|
|
img.set(3, 1, 255);
|
|
|
|
img.set(4, 1, 255);
|
|
|
|
runtime.updateDisplay();
|
|
|
|
|
2016-03-11 01:24:11 +01:00
|
|
|
throw new Error("PANIC " + code)
|
|
|
|
}
|
|
|
|
|
2016-04-03 05:44:29 +02:00
|
|
|
export function getPin(id: number) {
|
|
|
|
return board().pins.filter(p => p && p.id == id)[0] || null
|
2016-03-11 01:24:11 +01:00
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
|
2016-04-03 05:44:29 +02:00
|
|
|
export namespace 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
|
|
|
}
|
|
|
|
|
2016-04-07 21:52:02 +02:00
|
|
|
namespace pxsim.basic {
|
2016-03-31 00:53:00 +02:00
|
|
|
export var pause = thread.pause;
|
2016-04-05 06:52:03 +02:00
|
|
|
export var forever = thread.forever;
|
2016-03-31 00:53:00 +02:00
|
|
|
|
|
|
|
export function showNumber(x: number, interval: number) {
|
|
|
|
if (interval < 0) return;
|
|
|
|
|
2016-04-03 05:35:38 +02:00
|
|
|
let leds = createImageFromString(x.toString());
|
2016-04-12 17:55:20 +02:00
|
|
|
if (x < 0 || x >= 10) ImageMethods.scrollImage(leds, interval, 1);
|
2016-03-31 00:53:00 +02:00
|
|
|
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 {
|
2016-04-15 06:42:02 +02:00
|
|
|
if (s.length == 1) showLeds(createImageFromString(s), interval * 5)
|
|
|
|
else ImageMethods.scrollImage(createImageFromString(s + ' '), interval, 1);
|
2016-03-31 00:53:00 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-18 18:47:27 +02:00
|
|
|
|
2016-04-03 05:35:38 +02:00
|
|
|
export function showLeds(leds: Image, delay: number): void {
|
2016-03-31 00:53:00 +02:00
|
|
|
showAnimation(leds, delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function clearScreen() {
|
|
|
|
board().image.clear();
|
|
|
|
runtime.queueDisplayUpdate()
|
|
|
|
}
|
|
|
|
|
2016-04-03 05:35:38 +02:00
|
|
|
export function showAnimation(leds: Image, interval: number = 400): void {
|
2016-04-12 17:55:20 +02:00
|
|
|
ImageMethods.scrollImage(leds, interval, 5);
|
2016-03-31 00:53:00 +02:00
|
|
|
}
|
|
|
|
|
2016-04-03 05:35:38 +02:00
|
|
|
export function plotLeds(leds: Image): void {
|
2016-04-12 17:55:20 +02:00
|
|
|
ImageMethods.plotImage(leds, 0);
|
2016-04-03 05:35:38 +02:00
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
}
|
|
|
|
|
2016-04-07 21:52:02 +02:00
|
|
|
namespace pxsim.control {
|
2016-03-31 00:53:00 +02:00
|
|
|
export var inBackground = thread.runInBackground;
|
|
|
|
|
|
|
|
export function reset() {
|
|
|
|
U.userError("reset not implemented in simulator yet")
|
|
|
|
}
|
2016-04-26 22:43:02 +02:00
|
|
|
|
|
|
|
export function deviceName() : string {
|
|
|
|
let b = board();
|
|
|
|
return b && b.id
|
|
|
|
? b.id.slice(0, 4)
|
|
|
|
: 'abcd';
|
|
|
|
}
|
2016-04-03 05:47:27 +02:00
|
|
|
|
|
|
|
export function onEvent(id: number, evid: number, handler: RefAction) {
|
2016-04-07 21:52:02 +02:00
|
|
|
pxt.registerWithDal(id, evid, handler)
|
2016-04-03 05:47:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function raiseEvent(id: number, evid: number, mode: number) {
|
|
|
|
// TODO mode?
|
|
|
|
board().bus.queue(id, evid)
|
|
|
|
}
|
2016-03-31 00:53:00 +02:00
|
|
|
}
|
2016-04-03 05:35:38 +02:00
|
|
|
|
2016-04-07 21:52:02 +02:00
|
|
|
namespace pxsim.pxt {
|
2016-04-03 05:35:38 +02:00
|
|
|
export function registerWithDal(id: number, evid: number, handler: RefAction) {
|
|
|
|
board().bus.listen(id, evid, handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 21:52:02 +02:00
|
|
|
namespace pxsim.input {
|
2016-04-03 05:35:38 +02:00
|
|
|
export function onButtonPressed(button: number, handler: RefAction): void {
|
|
|
|
let b = board();
|
|
|
|
if (button == DAL.MICROBIT_ID_BUTTON_AB && !board().usesButtonAB) {
|
|
|
|
b.usesButtonAB = true;
|
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
}
|
2016-04-18 19:46:44 +02:00
|
|
|
pxt.registerWithDal(button, DAL.MICROBIT_BUTTON_EVT_CLICK, handler);
|
2016-04-03 05:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function buttonIsPressed(button: number): boolean {
|
|
|
|
let b = board();
|
|
|
|
if (button == DAL.MICROBIT_ID_BUTTON_AB && !board().usesButtonAB) {
|
|
|
|
b.usesButtonAB = true;
|
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
}
|
|
|
|
let bts = b.buttons;
|
|
|
|
if (button == DAL.MICROBIT_ID_BUTTON_A) return bts[0].pressed;
|
|
|
|
if (button == DAL.MICROBIT_ID_BUTTON_B) return bts[1].pressed;
|
|
|
|
return bts[2].pressed || (bts[0].pressed && bts[1].pressed);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function onGesture(gesture: number, handler: RefAction) {
|
|
|
|
let b = board();
|
|
|
|
b.accelerometer.activate();
|
|
|
|
|
|
|
|
if (gesture == 11 && !b.useShake) { // SAKE
|
|
|
|
b.useShake = true;
|
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
}
|
2016-04-18 19:46:44 +02:00
|
|
|
pxt.registerWithDal(DAL.MICROBIT_ID_GESTURE, gesture, handler);
|
2016-04-03 05:35:38 +02:00
|
|
|
}
|
|
|
|
|
2016-04-03 05:44:29 +02:00
|
|
|
export function onPinPressed(pinId: number, handler: RefAction) {
|
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
|
|
|
pin.isTouched();
|
|
|
|
input.onButtonPressed(pin.id, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pinIsPressed(pinId: number): boolean {
|
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return false;
|
|
|
|
return pin.isTouched();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-03 05:35:38 +02:00
|
|
|
export function compassHeading(): number {
|
|
|
|
var b = board();
|
|
|
|
if (!b.usesHeading) {
|
|
|
|
b.usesHeading = true;
|
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
}
|
|
|
|
return b.heading;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function temperature(): number {
|
|
|
|
var b = board();
|
|
|
|
if (!b.usesTemperature) {
|
|
|
|
b.usesTemperature = true;
|
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
}
|
|
|
|
return b.temperature;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function acceleration(dimension: number): number {
|
|
|
|
let b = board();
|
|
|
|
let acc = b.accelerometer;
|
|
|
|
acc.activate();
|
|
|
|
switch (dimension) {
|
|
|
|
case 0: return acc.getX();
|
|
|
|
case 1: return acc.getY();
|
|
|
|
case 2: return acc.getZ();
|
|
|
|
default: return Math.floor(Math.sqrt(acc.instantaneousAccelerationSquared()));
|
|
|
|
}
|
|
|
|
}
|
2016-04-14 16:39:24 +02:00
|
|
|
|
|
|
|
export function rotation(kind: number): number {
|
2016-04-12 17:38:48 +02:00
|
|
|
let b = board();
|
|
|
|
let acc = b.accelerometer;
|
|
|
|
acc.activate();
|
|
|
|
let x = acc.getX(MicroBitCoordinateSystem.NORTH_EAST_DOWN);
|
|
|
|
let y = acc.getX(MicroBitCoordinateSystem.NORTH_EAST_DOWN);
|
|
|
|
let z = acc.getX(MicroBitCoordinateSystem.NORTH_EAST_DOWN);
|
|
|
|
|
2016-04-14 16:39:24 +02:00
|
|
|
let roll = Math.atan2(y, z);
|
|
|
|
let pitch = Math.atan(-x / (y * Math.sin(roll) + z * Math.cos(roll)));
|
|
|
|
|
2016-04-12 17:38:48 +02:00
|
|
|
let r = 0;
|
2016-04-14 16:39:24 +02:00
|
|
|
switch (kind) {
|
2016-04-12 17:38:48 +02:00
|
|
|
case 0: r = pitch; break;
|
|
|
|
case 1: r = roll; break;
|
|
|
|
}
|
|
|
|
return Math.floor(r / Math.PI * 180);
|
|
|
|
}
|
2016-04-03 05:35:38 +02:00
|
|
|
|
|
|
|
export function setAccelerometerRange(range: number) {
|
|
|
|
let b = board();
|
|
|
|
b.accelerometer.setSampleRange(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function lightLevel(): number {
|
|
|
|
let b = board();
|
|
|
|
if (!b.usesLightLevel) {
|
|
|
|
b.usesLightLevel = true;
|
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
}
|
|
|
|
return b.lightLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function magneticForce(): number {
|
|
|
|
// TODO
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function runningTime(): number {
|
|
|
|
return runtime.runningTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function calibrate() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 21:52:02 +02:00
|
|
|
namespace pxsim.led {
|
2016-04-03 05:35:38 +02:00
|
|
|
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 setDisplayMode(mode: DisplayMode): void {
|
|
|
|
board().displayMode = mode;
|
|
|
|
runtime.queueDisplayUpdate()
|
|
|
|
}
|
2016-04-14 16:39:24 +02:00
|
|
|
|
|
|
|
export function screenshot(): Image {
|
2016-04-12 17:55:20 +02:00
|
|
|
let img = createImage(5)
|
|
|
|
board().image.copyTo(0, 5, img, 0);
|
|
|
|
return img;
|
|
|
|
}
|
2016-04-03 05:35:38 +02:00
|
|
|
}
|
|
|
|
|
2016-04-07 21:52:02 +02:00
|
|
|
namespace pxsim.serial {
|
2016-04-03 05:35:38 +02:00
|
|
|
export function writeString(s: string) {
|
|
|
|
board().writeSerial(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function readString(): string {
|
|
|
|
return board().readSerial();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-07 21:52:02 +02:00
|
|
|
namespace pxsim.radio {
|
2016-04-03 05:35:38 +02:00
|
|
|
export function broadcastMessage(msg: number): void {
|
|
|
|
board().radio.broadcast(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function onBroadcastMessageReceived(msg: number, handler: RefAction): void {
|
2016-04-18 19:46:44 +02:00
|
|
|
pxt.registerWithDal(DAL.MES_BROADCAST_GENERAL_ID, msg, handler);
|
2016-04-03 05:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setGroup(id: number): void {
|
|
|
|
board().radio.setGroup(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setTransmitPower(power: number): void {
|
|
|
|
board().radio.setTransmitPower(power);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function sendNumbers(value0: number, value1: number, value2: number, value3: number): void {
|
|
|
|
board().radio.datagram.send([value0, value1, value2, value3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function receiveNumber(): number {
|
|
|
|
return board().radio.datagram.recv().data[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function receivedNumberAt(index: number): number {
|
|
|
|
return board().radio.datagram.lastReceived.data[index] || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function receivedSignalStrength(): number {
|
|
|
|
return board().radio.datagram.lastReceived.rssi;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function onDataReceived(handler: RefAction): void {
|
2016-04-18 19:46:44 +02:00
|
|
|
pxt.registerWithDal(DAL.MICROBIT_ID_RADIO, DAL.MICROBIT_RADIO_EVT_DATAGRAM, handler);
|
2016-04-03 05:35:38 +02:00
|
|
|
}
|
2016-04-03 05:44:29 +02:00
|
|
|
}
|
|
|
|
|
2016-04-07 21:52:02 +02:00
|
|
|
namespace pxsim.pins {
|
2016-04-03 05:44:29 +02:00
|
|
|
export function digitalReadPin(pinId: number): number {
|
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
|
|
|
pin.mode = PinMode.Digital | PinMode.Input;
|
|
|
|
return pin.value > 100 ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function digitalWritePin(pinId: number, value: number) {
|
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
|
|
|
pin.mode = PinMode.Digital | PinMode.Output;
|
|
|
|
pin.value = value > 0 ? 1023 : 0;
|
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function analogReadPin(pinId: number): number {
|
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
|
|
|
pin.mode = PinMode.Analog | PinMode.Input;
|
|
|
|
return pin.value || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function analogWritePin(pinId: number, value: number) {
|
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
|
|
|
pin.mode = PinMode.Analog | PinMode.Output;
|
|
|
|
pin.value = value ? 1 : 0;
|
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function analogSetPeriod(pinId: number, micros: number) {
|
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
|
|
|
pin.mode = PinMode.Analog | PinMode.Output;
|
|
|
|
pin.period = micros;
|
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function servoWritePin(pinId: number, value: number) {
|
|
|
|
analogSetPeriod(pinId, 20000);
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
export function servoSetPulse(pinId: number, micros: number) {
|
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
export function analogSetPitchPin(pinId: number) {
|
|
|
|
let pin = getPin(pinId);
|
2016-04-03 05:47:27 +02:00
|
|
|
if (!pin) return;
|
2016-04-03 05:44:29 +02:00
|
|
|
board().pins.filter(p => !!p).forEach(p => p.pitch = false);
|
|
|
|
pin.pitch = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function analogPitch(frequency: number, ms: number) {
|
|
|
|
// update analog output
|
|
|
|
let pin = board().pins.filter(pin => !!pin && pin.pitch)[0] || board().pins[0];
|
|
|
|
pin.mode = PinMode.Analog | PinMode.Output;
|
|
|
|
if (frequency <= 0) {
|
|
|
|
pin.value = 0;
|
|
|
|
pin.period = 0;
|
|
|
|
} else {
|
|
|
|
pin.value = 512;
|
|
|
|
pin.period = 1000000 / frequency;
|
|
|
|
}
|
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
|
|
|
|
let cb = getResume();
|
|
|
|
AudioContextManager.tone(frequency, 1);
|
|
|
|
if (ms <= 0) cb();
|
|
|
|
else {
|
|
|
|
setTimeout(() => {
|
|
|
|
AudioContextManager.stop();
|
|
|
|
pin.value = 0;
|
|
|
|
pin.period = 0;
|
|
|
|
pin.mode = PinMode.Unused;
|
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
cb()
|
|
|
|
}, ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-07 21:52:02 +02:00
|
|
|
namespace pxsim.images {
|
2016-04-03 05:53:51 +02:00
|
|
|
export function createImage(img: Image) { return img }
|
|
|
|
export function createBigImage(img: Image) { return img }
|
|
|
|
}
|
2016-04-03 05:44:29 +02:00
|
|
|
|
2016-04-07 21:52:02 +02:00
|
|
|
namespace pxsim.ImageMethods {
|
2016-04-14 16:39:24 +02:00
|
|
|
export function showImage(leds: Image, offset: number) {
|
|
|
|
if (!leds) panic(PanicCode.MICROBIT_NULL_DEREFERENCE);
|
|
|
|
|
|
|
|
leds.copyTo(offset, 5, board().image, 0)
|
2016-04-03 05:53:51 +02:00
|
|
|
runtime.queueDisplayUpdate()
|
|
|
|
}
|
2016-04-14 16:39:24 +02:00
|
|
|
|
|
|
|
export function plotImage(leds: Image, offset: number): void {
|
|
|
|
if (!leds) panic(PanicCode.MICROBIT_NULL_DEREFERENCE);
|
|
|
|
|
2016-04-12 17:55:20 +02:00
|
|
|
leds.copyTo(offset, 5, board().image, 0)
|
|
|
|
runtime.queueDisplayUpdate()
|
|
|
|
}
|
2016-04-14 16:39:24 +02:00
|
|
|
|
2016-04-18 18:47:27 +02:00
|
|
|
export function height(leds: Image) : number {
|
|
|
|
if (!leds) panic(PanicCode.MICROBIT_NULL_DEREFERENCE);
|
|
|
|
return Image.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function width(leds: Image) : number {
|
|
|
|
if (!leds) panic(PanicCode.MICROBIT_NULL_DEREFERENCE);
|
|
|
|
return leds.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function plotFrame(leds: Image, frame: number) {
|
|
|
|
ImageMethods.plotImage(leds, frame * Image.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function showFrame(leds: Image, frame: number) {
|
|
|
|
ImageMethods.showImage(leds, frame * Image.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pixel(leds: Image, x: number, y: number) : number {
|
|
|
|
if (!leds) panic(PanicCode.MICROBIT_NULL_DEREFERENCE);
|
|
|
|
return leds.get(x,y);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setPixel(leds: Image, x: number, y: number, v:number) {
|
|
|
|
if (!leds) panic(PanicCode.MICROBIT_NULL_DEREFERENCE);
|
|
|
|
leds.set(x,y,v);
|
|
|
|
}
|
|
|
|
|
2016-04-14 16:39:24 +02:00
|
|
|
export function clear(leds: Image) {
|
|
|
|
if (!leds) panic(PanicCode.MICROBIT_NULL_DEREFERENCE);
|
|
|
|
|
|
|
|
leds.clear();
|
2016-04-12 17:55:20 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 16:39:24 +02:00
|
|
|
export function setPixelBrightness(i: Image, x: number, y: number, b: number) {
|
|
|
|
if (!i) panic(PanicCode.MICROBIT_NULL_DEREFERENCE);
|
|
|
|
|
|
|
|
i.set(x, y, b);
|
2016-04-12 17:55:20 +02:00
|
|
|
}
|
2016-04-14 16:39:24 +02:00
|
|
|
|
|
|
|
export function pixelBrightness(i: Image, x: number, y: number): number {
|
|
|
|
if (!i) panic(PanicCode.MICROBIT_NULL_DEREFERENCE);
|
|
|
|
|
|
|
|
return i.get(x, y);
|
|
|
|
}
|
|
|
|
|
2016-04-12 17:55:20 +02:00
|
|
|
export function scrollImage(leds: Image, interval: number, stride: number): void {
|
2016-04-14 16:39:24 +02:00
|
|
|
if (!leds) panic(PanicCode.MICROBIT_NULL_DEREFERENCE);
|
|
|
|
|
2016-04-12 17:55:20 +02:00
|
|
|
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
|
|
|
|
})
|
2016-04-14 16:39:24 +02:00
|
|
|
}
|
2016-04-04 01:52:57 +02:00
|
|
|
}
|