diff --git a/libs/microbit/basic.ts b/libs/microbit/basic.ts new file mode 100644 index 00000000..63dfdf4f --- /dev/null +++ b/libs/microbit/basic.ts @@ -0,0 +1,56 @@ +/** + Well known colors +*/ +enum Colors { + //% blockIdentity=basic.color + //% block=red + Red = 0xFF0000, + //% blockIdentity=basic.color + //% block=orange + Orange = 0xFFA500, + //% blockIdentity=basic.color + //% block=yellow + Yellow = 0xFFFF00, + //% blockIdentity=basic.color + //% block=green + Green = 0x00FF00, + //% blockIdentity=basic.color + //% block=blue + Blue = 0x0000FF, + //% blockIdentity=basic.color + //% block=indigo + Indigo = 0x4b0082, + //% blockIdentity=basic.color + //% block=violet + Violet = 0x8a2be2, + //% blockIdentity=basic.color + //% block=purple + Purple = 0xFF00FF, + //% blockIdentity=basic.color + //% block=white + White = 0xFFFFFF +} + +/** + * Provides access to basic micro:bit functionality. + */ +//% color=#0078D7 weight=100 +namespace basic { + + /** + * Sets the color on the build-in LED + */ + //% blockId=device_set_led_color block="set led to %color=color_id" icon="\uf00a" + //% weight=50 + export function setLedColor(color: number) { + // TODO + } + + /** + * Converts the color name to a number + */ + //% blockId=color_id block="%c" shim=TD_ID + export function color(c: Colors): number { + return c; + } +} \ No newline at end of file diff --git a/libs/microbit/pxt.json b/libs/microbit/pxt.json index be53b812..34847ae0 100644 --- a/libs/microbit/pxt.json +++ b/libs/microbit/pxt.json @@ -14,6 +14,7 @@ "helpers.ts", "images.cpp", "basic.cpp", + "basic.ts", "input.cpp", "input.ts", "control.ts", diff --git a/sim/dalboard.ts b/sim/dalboard.ts index b888a7bc..61cb1157 100644 --- a/sim/dalboard.ts +++ b/sim/dalboard.ts @@ -13,6 +13,7 @@ namespace pxsim { buttonPairState: ButtonPairState; radioState: RadioState; neopixelState: NeoPixelState; + rgbLedState: number; constructor() { super() @@ -48,7 +49,7 @@ namespace pxsim { 0, 0, DAL.MICROBIT_ID_IO_P19, - DAL.MICROBIT_ID_IO_P20 + DAL.MICROBIT_ID_IO_P20 ] }); this.builtinParts["radio"] = this.radioState = new RadioState(runtime); @@ -61,11 +62,11 @@ namespace pxsim { this.builtinVisuals["buttonpair"] = () => new visuals.ButtonPairView(); this.builtinVisuals["ledmatrix"] = () => new visuals.LedMatrixView(); - this.builtinVisuals["neopixel"] = () => new visuals.NeoPixelView(); + this.builtinVisuals["neopixel"] = () => new visuals.NeoPixelView(); 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["neopixel"] = (xy: visuals.Coord) => visuals.mkNeoPixelPart(xy); } receiveMessage(msg: SimulatorMessage) { @@ -97,7 +98,7 @@ namespace pxsim { const cmpDefs = msg.partDefinitions || {}; const fnArgs = msg.fnArgs; - const opts : visuals.BoardHostOpts = { + const opts: visuals.BoardHostOpts = { state: this, boardDef: boardDef, partsList: cmpsList, diff --git a/sim/state/rgbled.ts b/sim/state/rgbled.ts new file mode 100644 index 00000000..bb88b0f5 --- /dev/null +++ b/sim/state/rgbled.ts @@ -0,0 +1,5 @@ +namespace pxsim.basic { + export function setLedColor(c: number) { + board().rgbLedState = c; + } +} \ No newline at end of file diff --git a/sim/visuals/microbit.ts b/sim/visuals/microbit.ts index 166617ff..94f9e05c 100644 --- a/sim/visuals/microbit.ts +++ b/sim/visuals/microbit.ts @@ -253,6 +253,7 @@ namespace pxsim.visuals { private shakeButton: SVGElement; public board: pxsim.DalBoard; private pinNmToCoord: Map = {}; + private rgbLed: SVGElement; constructor(public props: IBoardProps) { this.recordPinCoords(); @@ -341,11 +342,21 @@ namespace pxsim.visuals { this.updateTemperature(); this.updateButtonAB(); this.updateGestures(); + this.updateRgbLed(); if (!runtime || runtime.dead) svg.addClass(this.element, "grayscale"); else svg.removeClass(this.element, "grayscale"); } + private updateRgbLed() { + let state = this.board; + if (state.rgbLedState) { + if (!this.rgbLed) + this.rgbLed = svg.child(this.g, "circle", { cx: 170, cy: 200 }); + svg.fill(this.rgbLed, svg.toHtmlColor(state.rgbLedState)); + } + } + private updateGestures() { let state = this.board; if (state.accelerometerState.useShake && !this.shakeButton) { @@ -353,7 +364,7 @@ namespace pxsim.visuals { this.shakeButton = shake.inner; svg.fill(this.shakeButton, this.props.theme.virtualButtonUp) svg.buttonEvents(shake.outer, - ev => {}, + ev => { }, (ev) => { svg.fill(this.shakeButton, this.props.theme.virtualButtonDown) },