pxt-calliope/sim/dalboard.ts

226 lines
8.7 KiB
TypeScript
Raw Normal View History

2016-09-02 00:37:39 +02:00
/// <reference path="../node_modules/pxt-core/built/pxtsim.d.ts"/>
2020-08-19 22:03:58 +02:00
/// <reference path="../libs/core/dal.d.ts"/>
/// <reference path="../libs/core/enums.d.ts"/>
2016-09-02 00:37:39 +02:00
namespace pxsim {
export class DalBoard extends CoreBoard
2021-11-25 17:27:39 +01:00
implements CommonBoard
, RadioBoard
, LightBoard
, MicrophoneBoard
, ControlMessageBoard {
// state & update logic for component services
ledMatrixState: LedMatrixState;
edgeConnectorState: EdgeConnectorState;
serialState: SerialState;
accelerometerState: AccelerometerState;
compassState: CompassState;
thermometerState: ThermometerState;
lightSensorState: LightSensorState;
buttonPairState: ButtonPairState;
radioState: RadioState;
2020-08-19 22:03:58 +02:00
microphoneState: AnalogSensorState;
lightState: pxt.Map<CommonNeoPixelState>;
2016-09-27 22:35:48 +02:00
rgbLedState: number;
2016-10-27 22:47:39 +02:00
speakerState: SpeakerState;
2016-11-07 20:11:38 +01:00
fileSystem: FileSystemState;
2021-11-25 17:27:39 +01:00
logoTouch: Button;
speakerEnabled: boolean = true;
controlMessageState: ControlMessageState;
2017-12-15 23:45:38 +01:00
// visual
viewHost: visuals.BoardHost;
2017-12-15 23:45:38 +01:00
view: SVGElement;
2021-11-25 17:27:39 +01:00
// board hardware version
hardwareVersion = 1;
constructor() {
super()
// components
2020-08-19 22:03:58 +02:00
this.lightState = {};
2016-11-07 20:11:38 +01:00
this.fileSystem = new FileSystemState();
2021-11-25 17:27:39 +01:00
this.controlMessageState = new ControlMessageState(this);
2016-09-13 06:29:55 +02:00
this.builtinParts["ledmatrix"] = this.ledMatrixState = new LedMatrixState(runtime);
this.builtinParts["buttonpair"] = this.buttonPairState = new ButtonPairState({
ID_BUTTON_A: DAL.MICROBIT_ID_BUTTON_A,
ID_BUTTON_B: DAL.MICROBIT_ID_BUTTON_B,
ID_BUTTON_AB: DAL.MICROBIT_ID_BUTTON_AB,
BUTTON_EVT_UP: DAL.MICROBIT_BUTTON_EVT_UP,
BUTTON_EVT_CLICK: DAL.MICROBIT_BUTTON_EVT_CLICK
});
2016-09-13 21:48:07 +02:00
this.builtinParts["edgeconnector"] = this.edgeConnectorState = new EdgeConnectorState({
pins: [
2021-11-25 17:27:39 +01:00
DAL.MICROBIT_ID_IO_P12,
2016-09-13 21:48:07 +02:00
DAL.MICROBIT_ID_IO_P0,
DAL.MICROBIT_ID_IO_P1,
2021-11-25 17:27:39 +01:00
DAL.MICROBIT_ID_IO_P16,
2016-09-13 21:48:07 +02:00
DAL.MICROBIT_ID_IO_P2,
DAL.MICROBIT_ID_IO_P3,
DAL.MICROBIT_ID_IO_P4,
DAL.MICROBIT_ID_IO_P5,
DAL.MICROBIT_ID_IO_P6,
DAL.MICROBIT_ID_IO_P7,
DAL.MICROBIT_ID_IO_P8,
2016-09-13 21:48:07 +02:00
DAL.MICROBIT_ID_IO_P9,
DAL.MICROBIT_ID_IO_P10,
2021-11-25 17:27:39 +01:00
DAL.MICROBIT_ID_IO_P11,
DAL.MICROBIT_ID_IO_P13,
DAL.MICROBIT_ID_IO_P14,
DAL.MICROBIT_ID_IO_P15,
2016-09-13 21:48:07 +02:00
0,
0,
DAL.MICROBIT_ID_IO_P19,
DAL.MICROBIT_ID_IO_P20,
DAL.MICROBIT_ID_IO_P21
2017-01-13 08:05:29 +01:00
],
servos: {
"P0": DAL.MICROBIT_ID_IO_P12,
"P1": DAL.MICROBIT_ID_IO_P0,
"P2": DAL.MICROBIT_ID_IO_P1,
"P3": DAL.MICROBIT_ID_IO_P16
2017-01-13 08:05:29 +01:00
}
2016-09-13 21:48:07 +02:00
});
2021-11-25 17:27:39 +01:00
this.builtinParts["radio"] = this.radioState = new RadioState(runtime, this, {
ID_RADIO: DAL.MICROBIT_ID_RADIO,
RADIO_EVT_DATAGRAM: DAL.MICROBIT_RADIO_EVT_DATAGRAM
});
2021-11-25 17:27:39 +01:00
this.builtinParts["microphone"] = this.microphoneState = new AnalogSensorState(3001 /* DEVICE_ID_MICROPHONE */, 0, 255, 75, 180);
2016-09-13 06:29:55 +02:00
this.builtinParts["accelerometer"] = this.accelerometerState = new AccelerometerState(runtime);
2021-11-25 17:27:39 +01:00
this.builtinParts["serial"] = this.serialState = new SerialState(runtime, this);
2016-09-13 06:29:55 +02:00
this.builtinParts["thermometer"] = this.thermometerState = new ThermometerState();
this.builtinParts["lightsensor"] = this.lightSensorState = new LightSensorState();
this.builtinParts["compass"] = this.compassState = new CompassState();
2016-10-27 22:47:39 +02:00
this.builtinParts["speaker"] = this.speakerState = new SpeakerState();
2017-01-13 08:05:29 +01:00
this.builtinParts["microservo"] = this.edgeConnectorState;
2016-09-13 18:44:58 +02:00
this.builtinVisuals["buttonpair"] = () => new visuals.ButtonPairView();
this.builtinVisuals["ledmatrix"] = () => new visuals.LedMatrixView();
2016-11-01 16:16:03 +01:00
this.builtinVisuals["microservo"] = () => new visuals.MicroServoView();
2016-09-13 18:44:58 +02:00
2020-08-19 22:03:58 +02:00
this.builtinParts["neopixel"] = (pin: Pin) => { return this.neopixelState(pin.id); };
this.builtinVisuals["neopixel"] = () => new visuals.NeoPixelView(pxsim.parsePinString);
this.builtinPartVisuals["neopixel"] = (xy: visuals.Coord) => visuals.mkNeoPixelPart(xy);
2016-09-13 18:44:58 +02:00
this.builtinPartVisuals["buttonpair"] = (xy: visuals.Coord) => visuals.mkBtnSvg(xy);
this.builtinPartVisuals["ledmatrix"] = (xy: visuals.Coord) => visuals.mkLedMatrixSvg(xy, 8, 8);
2016-11-01 16:16:03 +01:00
this.builtinPartVisuals["microservo"] = (xy: visuals.Coord) => visuals.mkMicroServoPart(xy);
}
2021-11-25 17:27:39 +01:00
ensureHardwareVersion(version: number) {
if (version > this.hardwareVersion) {
this.hardwareVersion = version;
this.updateView();
}
}
2021-11-25 17:27:39 +01:00
initAsync(msg: SimulatorRunMessage): Promise<void> {
2016-09-02 00:37:39 +02:00
super.initAsync(msg);
const boardDef = msg.boardDefinition;
2016-09-13 18:59:34 +02:00
const cmpsList = msg.parts;
const cmpDefs = msg.partDefinitions || {};
2016-09-13 18:59:34 +02:00
const fnArgs = msg.fnArgs;
2021-11-25 17:27:39 +01:00
const v2Parts: pxt.Map<boolean> = {
"microphone": true,
"logotouch": true,
"builtinspeaker": true,
"v2": true
};
if (msg.builtinParts) {
const v2PartsUsed = msg.builtinParts.filter(k => v2Parts[k])
if (v2PartsUsed.length) {
console.log(`detected v2 feature`, v2PartsUsed);
cmpsList.push(...v2PartsUsed);
this.hardwareVersion = 2;
}
}
2016-09-27 22:35:48 +02:00
const opts: visuals.BoardHostOpts = {
state: this,
boardDef: boardDef,
partsList: cmpsList,
partDefs: cmpDefs,
2016-08-31 20:34:49 +02:00
fnArgs: fnArgs,
maxWidth: "100%",
maxHeight: "100%",
2017-12-15 23:45:38 +01:00
highContrast: msg.highContrast
2016-09-13 18:59:34 +02:00
};
2021-11-25 17:27:39 +01:00
this.viewHost = new visuals.BoardHost(pxsim.visuals.mkBoardView({
2017-12-15 23:45:38 +01:00
visual: boardDef.visual,
boardDef: boardDef,
2017-12-15 23:45:38 +01:00
highContrast: msg.highContrast
}), opts);
document.body.innerHTML = ""; // clear children
document.body.appendChild(this.view = this.viewHost.getView());
return Promise.resolve();
}
2017-12-15 23:45:38 +01:00
2020-08-19 22:03:58 +02:00
tryGetNeopixelState(pinId: number): CommonNeoPixelState {
return this.lightState[pinId];
}
neopixelState(pinId: number): CommonNeoPixelState {
if (pinId === undefined) {
pinId = DAL.MICROBIT_ID_IO_P0;
}
let state = this.lightState[pinId];
if (!state) state = this.lightState[pinId] = new CommonNeoPixelState();
return state;
}
screenshotAsync(width?: number): Promise<ImageData> {
return this.viewHost.screenshotAsync(width);
2017-12-15 23:45:38 +01:00
}
}
2016-09-10 00:01:29 +02:00
export function initRuntimeWithDalBoard() {
U.assert(!runtime.board);
let b = new DalBoard();
runtime.board = b;
runtime.postError = (e) => {
led.setBrightness(255);
let img = board().ledMatrixState.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();
}
}
if (!pxsim.initCurrentRuntime) {
pxsim.initCurrentRuntime = initRuntimeWithDalBoard;
2016-09-13 06:29:55 +02:00
}
2016-09-10 00:01:29 +02:00
export function board(): DalBoard {
2016-09-10 00:01:29 +02:00
return runtime.board as DalBoard;
2016-09-13 06:29:55 +02:00
}
2020-08-19 22:03:58 +02:00
export function parsePinString(gpioPin: string): Pin {
if (gpioPin == "*")
return board().edgeConnectorState.getPin(DAL.MICROBIT_ID_IO_P0);
const m = /^(Analog|Digital)Pin\.P(\d)+/.exec(gpioPin);
if (!m)
return undefined;
const pinNum = parseInt(m[2]);
return board().edgeConnectorState.pins[pinNum]
}
}