From c4787e102817146690b7bf2476e42e12dd2b8809 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Tue, 11 Oct 2016 11:41:51 -0700 Subject: [PATCH] support for rgb led in simulator --- libs/core/basic.cpp | 20 ++++++++++++++++++++ libs/core/basic.ts | 41 ++++++++++++++++++++++------------------- libs/core/shims.d.ts | 7 +++++++ sim/state/rgbled.ts | 1 + sim/visuals/microbit.ts | 12 ++++++++++-- 5 files changed, 60 insertions(+), 21 deletions(-) diff --git a/libs/core/basic.cpp b/libs/core/basic.cpp index 44c577ba..0eaea567 100644 --- a/libs/core/basic.cpp +++ b/libs/core/basic.cpp @@ -6,6 +6,26 @@ */ //% color=#0078D7 weight=100 namespace basic { + /** + * Sets the color on the build-in LED. Set to 0 to turn off. + */ + //% blockId=device_set_led_color block="set led to %color=color_id" icon="\uf00a" + //% weight=50 + void setLedColor(int color) { + if (!color) { + uBit.rgb.Off(); + return; + } + + int w = (color >> 24) & 0xFF; + int r = (color >> 16) & 0xFF; + int g = (color >> 8) & 0xFF; + int b = (color) & 0xFF; + + uBit.rgb.Set_Color(r,g,b,w); + uBit.rgb.On(); + uBit.rgb.Send_to_LED(); + } /** * Scroll a number on the screen. If the number fits on the screen (i.e. is a single digit), do not scroll. diff --git a/libs/core/basic.ts b/libs/core/basic.ts index 63dfdf4f..636b2a35 100644 --- a/libs/core/basic.ts +++ b/libs/core/basic.ts @@ -4,31 +4,31 @@ enum Colors { //% blockIdentity=basic.color //% block=red - Red = 0xFF0000, + Red = 0x00FF0000, //% blockIdentity=basic.color //% block=orange - Orange = 0xFFA500, + Orange = 0x00FFA500, //% blockIdentity=basic.color //% block=yellow - Yellow = 0xFFFF00, + Yellow = 0x00FFFF00, //% blockIdentity=basic.color //% block=green - Green = 0x00FF00, + Green = 0x0000FF00, //% blockIdentity=basic.color //% block=blue - Blue = 0x0000FF, + Blue = 0x000000FF, //% blockIdentity=basic.color //% block=indigo - Indigo = 0x4b0082, + Indigo = 0x004b0082, //% blockIdentity=basic.color //% block=violet - Violet = 0x8a2be2, + Violet = 0x008a2be2, //% blockIdentity=basic.color //% block=purple - Purple = 0xFF00FF, + Purple = 0x00FF00FF, //% blockIdentity=basic.color //% block=white - White = 0xFFFFFF + White = 0xFF00000 } /** @@ -36,16 +36,6 @@ enum Colors { */ //% 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 */ @@ -53,4 +43,17 @@ namespace basic { export function color(c: Colors): number { return c; } + + /** + * Converts red, green, blue channels into a RGB color + * @param red value of the red channel between 0 and 255. eg: 255 + * @param green value of the green channel between 0 and 255. eg: 255 + * @param blue value of the blue channel between 0 and 255. eg: 255 + * @param white value of the white channel between 0 and 255. eg: 0 + */ + //% weight=1 + //% blockId="core_rgb" block="red %red|green %green|blue %blue|white %white" + export function rgbw(red: number, green: number, blue: number, white:number): number { + return ((white & 0xFF) << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF); + } } \ No newline at end of file diff --git a/libs/core/shims.d.ts b/libs/core/shims.d.ts index 42dc3fa1..df8e0ef7 100644 --- a/libs/core/shims.d.ts +++ b/libs/core/shims.d.ts @@ -128,6 +128,13 @@ declare interface Image { //% color=#0078D7 weight=100 declare namespace basic { + /** + * Sets the color on the build-in LED. Set to 0 to turn off. + */ + //% blockId=device_set_led_color block="set led to %color=color_id" icon="\uf00a" + //% weight=50 shim=basic::setLedColor + function setLedColor(color: number): void; + /** * Scroll a number on the screen. If the number fits on the screen (i.e. is a single digit), do not scroll. * @param interval speed of scroll; eg: 150, 100, 200, -100 diff --git a/sim/state/rgbled.ts b/sim/state/rgbled.ts index bb88b0f5..c7744162 100644 --- a/sim/state/rgbled.ts +++ b/sim/state/rgbled.ts @@ -1,5 +1,6 @@ namespace pxsim.basic { export function setLedColor(c: number) { board().rgbLedState = c; + runtime.queueDisplayUpdate() } } \ No newline at end of file diff --git a/sim/visuals/microbit.ts b/sim/visuals/microbit.ts index 210699e2..0b091be6 100644 --- a/sim/visuals/microbit.ts +++ b/sim/visuals/microbit.ts @@ -352,8 +352,16 @@ namespace pxsim.visuals { 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)); + this.rgbLed = svg.child(this.g, "circle", { cx: 170, cy: 190, r:5 }); + const c = state.rgbLedState; + const b = c & 0xFF; + const g = (c >> 8) & 0xFF; + const r = (c >> 16) & 0xFF; + const w = (c >> 24) & 0xFF; + const ch = `rgba(${r}, ${g}, ${b}, 1)`; + svg.fill(this.rgbLed, ch); + } else if (this.rgbLed) { + svg.fill(this.rgbLed, 'white'); } }