merging microbit changes
This commit is contained in:
@ -12,9 +12,11 @@ namespace pxsim {
|
||||
lightSensorState: LightSensorState;
|
||||
buttonPairState: ButtonPairState;
|
||||
radioState: RadioState;
|
||||
// TODO: not singletons
|
||||
neopixelState: NeoPixelState;
|
||||
rgbLedState: number;
|
||||
speakerState: SpeakerState;
|
||||
microServoState: MicroServoState;
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
@ -61,14 +63,17 @@ namespace pxsim {
|
||||
this.builtinParts["compass"] = this.compassState = new CompassState();
|
||||
this.builtinParts["neopixel"] = this.neopixelState = new NeoPixelState();
|
||||
this.builtinParts["speaker"] = this.speakerState = new SpeakerState();
|
||||
this.builtinParts["microservo"] = this.microServoState = new MicroServoState();
|
||||
|
||||
this.builtinVisuals["buttonpair"] = () => new visuals.ButtonPairView();
|
||||
this.builtinVisuals["ledmatrix"] = () => new visuals.LedMatrixView();
|
||||
this.builtinVisuals["neopixel"] = () => new visuals.NeoPixelView();
|
||||
this.builtinVisuals["microservo"] = () => new visuals.MicroServoView();
|
||||
|
||||
this.builtinPartVisuals["buttonpair"] = (xy: visuals.Coord) => visuals.mkBtnSvg(xy);
|
||||
this.builtinPartVisuals["ledmatrix"] = (xy: visuals.Coord) => visuals.mkLedMatrixSvg(xy, 8, 8);
|
||||
this.builtinPartVisuals["neopixel"] = (xy: visuals.Coord) => visuals.mkNeoPixelPart(xy);
|
||||
this.builtinPartVisuals["microservo"] = (xy: visuals.Coord) => visuals.mkMicroServoPart(xy);
|
||||
}
|
||||
|
||||
receiveMessage(msg: SimulatorMessage) {
|
||||
|
@ -11,7 +11,7 @@ namespace pxsim.input {
|
||||
if (!pin) return;
|
||||
pin.isTouched();
|
||||
pxtcore.registerWithDal(pin.id, DAL.MICROBIT_BUTTON_EVT_UP, handler);
|
||||
}
|
||||
}
|
||||
|
||||
export function pinIsPressed(pinId: number): boolean {
|
||||
let pin = getPin(pinId);
|
||||
@ -73,7 +73,11 @@ namespace pxsim.pins {
|
||||
|
||||
export function servoWritePin(pinId: number, value: number) {
|
||||
analogSetPeriod(pinId, 20000);
|
||||
// TODO
|
||||
// TODO: per pin state
|
||||
if (board().microServoState.angle != value) {
|
||||
board().microServoState.angle = value;
|
||||
runtime.queueDisplayUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
export function servoSetPulse(pinId: number, micros: number) {
|
||||
|
@ -9,6 +9,7 @@ namespace pxsim {
|
||||
brigthness = 255;
|
||||
displayMode = DisplayMode.bw;
|
||||
font: Image = createFont();
|
||||
disabled: boolean;
|
||||
|
||||
animationQ: AnimationQueue;
|
||||
|
||||
@ -284,4 +285,8 @@ namespace pxsim.led {
|
||||
board().ledMatrixState.image.copyTo(0, 5, img, 0);
|
||||
return img;
|
||||
}
|
||||
export function enable(on: boolean) {
|
||||
board().ledMatrixState.disabled = !on;
|
||||
runtime.queueDisplayUpdate();
|
||||
}
|
||||
}
|
@ -138,21 +138,13 @@ namespace pxsim.radio {
|
||||
}
|
||||
|
||||
export function writeValueToSerial(): void {
|
||||
let b = board();
|
||||
let p = b.radioState.bus.datagram.recv();
|
||||
const b = board();
|
||||
writePacketToSerial(b, b.radioState.bus.datagram.recv())
|
||||
}
|
||||
|
||||
switch(p.payload.type) {
|
||||
case PacketPayloadType.NUMBER:
|
||||
b.writeSerial(`{"t":${p.time},"s":${p.serial},"v":${p.payload.numberData}}`)
|
||||
break;
|
||||
case PacketPayloadType.VALUE:
|
||||
b.writeSerial(`{"t":${p.time},"s":${p.serial},"n":"${p.payload.stringData}","v":${p.payload.numberData}}`)
|
||||
break;
|
||||
case PacketPayloadType.STRING:
|
||||
b.writeSerial(`{"t":${p.time},"s":${p.serial},"n":"${p.payload.stringData}"}`)
|
||||
break;
|
||||
default:
|
||||
}
|
||||
export function writeReceivedPacketToSerial(): void {
|
||||
const b = board();
|
||||
writePacketToSerial(b, b.radioState.bus.datagram.lastReceived);
|
||||
}
|
||||
|
||||
export function sendValue(name: string, value: number) {
|
||||
@ -200,4 +192,19 @@ namespace pxsim.radio {
|
||||
export function receivedTime(): number {
|
||||
return board().radioState.bus.datagram.lastReceived.time;
|
||||
}
|
||||
|
||||
function writePacketToSerial(b: DalBoard, p: PacketBuffer) {
|
||||
switch(p.payload.type) {
|
||||
case PacketPayloadType.NUMBER:
|
||||
b.writeSerial(`{"t":${p.time},"s":${p.serial},"v":${p.payload.numberData}}\r\n`)
|
||||
break;
|
||||
case PacketPayloadType.VALUE:
|
||||
b.writeSerial(`{"t":${p.time},"s":${p.serial},"n":"${p.payload.stringData}","v":${p.payload.numberData}}\r\n`)
|
||||
break;
|
||||
case PacketPayloadType.STRING:
|
||||
b.writeSerial(`{"t":${p.time},"s":${p.serial},"n":"${p.payload.stringData}"}\r\n`)
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
@ -3,9 +3,9 @@
|
||||
|
||||
namespace pxsim.visuals {
|
||||
export function mkLedMatrixSvg(xy: Coord, rows: number, cols: number):
|
||||
{el: SVGGElement, y: number, x: number, w: number, h: number, leds: SVGElement[], ledsOuter: SVGElement[], background: SVGElement} {
|
||||
let result: {el: SVGGElement, y: number, x: number, w: number, h: number, leds: SVGElement[], ledsOuter: SVGElement[], background: SVGElement}
|
||||
= {el: null, y: 0, x: 0, w: 0, h: 0, leds: [], ledsOuter: [], background: null};
|
||||
{ el: SVGGElement, y: number, x: number, w: number, h: number, leds: SVGElement[], ledsOuter: SVGElement[], background: SVGElement } {
|
||||
let result: { el: SVGGElement, y: number, x: number, w: number, h: number, leds: SVGElement[], ledsOuter: SVGElement[], background: SVGElement }
|
||||
= { el: null, y: 0, x: 0, w: 0, h: 0, leds: [], ledsOuter: [], background: null };
|
||||
result.el = <SVGGElement>svg.elt("g");
|
||||
let width = cols * PIN_DIST;
|
||||
let height = rows * PIN_DIST;
|
||||
@ -19,7 +19,7 @@ namespace pxsim.visuals {
|
||||
result.y = top;
|
||||
result.w = width;
|
||||
result.h = height;
|
||||
result.background = svg.child(result.el, "rect", {class: "sim-display", x: left, y: top, width: width, height: height})
|
||||
result.background = svg.child(result.el, "rect", { class: "sim-display", x: left, y: top, width: width, height: height })
|
||||
|
||||
// ledsOuter
|
||||
result.leds = [];
|
||||
@ -101,8 +101,16 @@ namespace pxsim.visuals {
|
||||
}
|
||||
|
||||
public updateState() {
|
||||
let bw = this.state.displayMode == pxsim.DisplayMode.bw
|
||||
let img = this.state.image;
|
||||
if (this.state.disabled) {
|
||||
this.leds.forEach((led, i) => {
|
||||
let sel = (<SVGStylable><any>led)
|
||||
sel.style.opacity = 0 + "";
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const bw = this.state.displayMode == pxsim.DisplayMode.bw
|
||||
const img = this.state.image;
|
||||
this.leds.forEach((led, i) => {
|
||||
let sel = (<SVGStylable><any>led)
|
||||
let dx = i % this.DRAW_SIZE;
|
||||
|
@ -1335,12 +1335,19 @@ namespace pxsim.visuals {
|
||||
svg.fill(this.buttons[index], btn.pressed ? (btn.virtual ? theme.virtualButtonDown : theme.buttonDown) : (btn.virtual ? theme.virtualButtonUp : theme.buttonUps[index]));
|
||||
});
|
||||
|
||||
let bw = state.ledMatrixState.displayMode == pxsim.DisplayMode.bw
|
||||
let img = state.ledMatrixState.image;
|
||||
this.leds.forEach((led, i) => {
|
||||
let sel = (<SVGStylable><any>led)
|
||||
sel.style.opacity = ((bw ? img.data[i] > 0 ? 255 : 0 : img.data[i]) / 255.0) + "";
|
||||
})
|
||||
if (state.ledMatrixState.disabled) {
|
||||
this.leds.forEach((led, i) => {
|
||||
const sel = (<SVGStylable><any>led)
|
||||
sel.style.opacity = "0";
|
||||
})
|
||||
} else {
|
||||
const bw = state.ledMatrixState.displayMode == pxsim.DisplayMode.bw
|
||||
const img = state.ledMatrixState.image;
|
||||
this.leds.forEach((led, i) => {
|
||||
const sel = (<SVGStylable><any>led)
|
||||
sel.style.opacity = ((bw ? img.data[i] > 0 ? 255 : 0 : img.data[i]) / 255.0) + "";
|
||||
})
|
||||
}
|
||||
this.updatePins();
|
||||
this.updateTilt();
|
||||
this.updateHeading();
|
||||
|
Reference in New Issue
Block a user