From d83c11fde112bdc19a5d814053915afe62fdd360 Mon Sep 17 00:00:00 2001 From: James Devine Date: Tue, 7 Jul 2020 12:06:37 +0100 Subject: [PATCH 1/7] add RGBLED client --- pxt.json | 3 ++- rgbledclient.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ services.ts | 4 +++- 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 rgbledclient.ts diff --git a/pxt.json b/pxt.json index 8374e47..d7f49c7 100644 --- a/pxt.json +++ b/pxt.json @@ -17,7 +17,8 @@ "main.ts", "services.ts", "jdflash.ts", - "README.md" + "README.md", + "rgbledclient.ts" ], "testFiles": [ "test.ts" diff --git a/rgbledclient.ts b/rgbledclient.ts new file mode 100644 index 0000000..1e1ebe0 --- /dev/null +++ b/rgbledclient.ts @@ -0,0 +1,48 @@ +// Add your code here +namespace jd_class { + export const RGB_LED = 0xe7aeb0fc; +} + +const enum JDRGBLEDCommand { + SetColor = 0x80 +} + +namespace jacdac { + //% fixedInstances + export class RGBLEDClient extends Client { + constructor(requiredDevice: string = null) { + super("RGBLED", jd_class.RGB_LED, requiredDevice); + } + + /** + * Set the brightness of the strip. This flag only applies to future operation. + * @param brightness a measure of LED brightness in 0-255. eg: 20 + */ + //% blockId="jdrgbled_set_brightness" block="set %strip brightness %brightness" + //% brightness.min=0 brightness.max=255 + //% weight=2 blockGap=8 + //% group="Light" + setBrightness(brightness: number): void { + this.setRegInt(REG_INTENSITY, brightness) + } + + /** + * Set the colour of the RGB led in hex format. + * + * Upper most byte is red, middle byte green, lower byte blue. + */ + //% blockId="jdrgbled_set_color" block="set %strip color %colorcode" + //% brightness.min=0 brightness.max=255 + //% weight=2 blockGap=8 + //% group="RGBLED" + setColor(colorCode: number): void { + let buf = Buffer.create(4); + buf.setNumber(NumberFormat.Int32LE, 0, colorCode) + this.sendCommand(JDPacket.from(JDRGBLEDCommand.SetColor, buf)); + } + + private currAnimation = 0 + } + //% fixedInstance whenUsed block="light client" + export const rgbledClient = new RGBLEDClient(); +} \ No newline at end of file diff --git a/services.ts b/services.ts index e6b3096..fc8c99a 100644 --- a/services.ts +++ b/services.ts @@ -7,8 +7,10 @@ class ServiceDesc { } const serviceDescs = [ - new ServiceDesc(jd_class.ACCELEROMETER, "acc", + new ServiceDesc(jd_class.ACCELEROMETER, "acc", num => jacdac.accelerometerClient.setStreaming(num & 1 ? true : false)), + new ServiceDesc(jd_class.RGB_LED, "RGBLED", + num => jacdac.rgbledClient.setColor(randint(0, 0xffffffff))), new ServiceDesc(jd_class.LIGHT, "light", (num) => { const cl = jacdac.lightClient cl.setBrightness(10) From eae4e7cf1f76eabac6c171423728796dd0986144 Mon Sep 17 00:00:00 2001 From: James Devine Date: Wed, 8 Jul 2020 11:24:18 +0100 Subject: [PATCH 2/7] Update rgbledclient.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: MichaƂ Moskal --- rgbledclient.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rgbledclient.ts b/rgbledclient.ts index 1e1ebe0..cab9d1a 100644 --- a/rgbledclient.ts +++ b/rgbledclient.ts @@ -1,4 +1,3 @@ -// Add your code here namespace jd_class { export const RGB_LED = 0xe7aeb0fc; } @@ -45,4 +44,4 @@ namespace jacdac { } //% fixedInstance whenUsed block="light client" export const rgbledClient = new RGBLEDClient(); -} \ No newline at end of file +} From 6e41f6c8af055999f7d2fa0560ed7e599f7dacd1 Mon Sep 17 00:00:00 2001 From: James Devine Date: Thu, 16 Jul 2020 11:21:58 +0100 Subject: [PATCH 3/7] remove rgbled client I expect pxt-jacdac-services will need bumping --- rgbledclient.ts | 47 ----------------------------------------------- 1 file changed, 47 deletions(-) delete mode 100644 rgbledclient.ts diff --git a/rgbledclient.ts b/rgbledclient.ts deleted file mode 100644 index cab9d1a..0000000 --- a/rgbledclient.ts +++ /dev/null @@ -1,47 +0,0 @@ -namespace jd_class { - export const RGB_LED = 0xe7aeb0fc; -} - -const enum JDRGBLEDCommand { - SetColor = 0x80 -} - -namespace jacdac { - //% fixedInstances - export class RGBLEDClient extends Client { - constructor(requiredDevice: string = null) { - super("RGBLED", jd_class.RGB_LED, requiredDevice); - } - - /** - * Set the brightness of the strip. This flag only applies to future operation. - * @param brightness a measure of LED brightness in 0-255. eg: 20 - */ - //% blockId="jdrgbled_set_brightness" block="set %strip brightness %brightness" - //% brightness.min=0 brightness.max=255 - //% weight=2 blockGap=8 - //% group="Light" - setBrightness(brightness: number): void { - this.setRegInt(REG_INTENSITY, brightness) - } - - /** - * Set the colour of the RGB led in hex format. - * - * Upper most byte is red, middle byte green, lower byte blue. - */ - //% blockId="jdrgbled_set_color" block="set %strip color %colorcode" - //% brightness.min=0 brightness.max=255 - //% weight=2 blockGap=8 - //% group="RGBLED" - setColor(colorCode: number): void { - let buf = Buffer.create(4); - buf.setNumber(NumberFormat.Int32LE, 0, colorCode) - this.sendCommand(JDPacket.from(JDRGBLEDCommand.SetColor, buf)); - } - - private currAnimation = 0 - } - //% fixedInstance whenUsed block="light client" - export const rgbledClient = new RGBLEDClient(); -} From 6a965cd754e51b89c81779ac9fdc2cd9b83c89d7 Mon Sep 17 00:00:00 2001 From: James Devine Date: Thu, 16 Jul 2020 11:44:25 +0100 Subject: [PATCH 4/7] remove rbgclient from pxt.json --- pxt.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pxt.json b/pxt.json index d7f49c7..8374e47 100644 --- a/pxt.json +++ b/pxt.json @@ -17,8 +17,7 @@ "main.ts", "services.ts", "jdflash.ts", - "README.md", - "rgbledclient.ts" + "README.md" ], "testFiles": [ "test.ts" From 988f1521f01764aa7b0755c01225c0572f6c7c56 Mon Sep 17 00:00:00 2001 From: James Devine Date: Thu, 16 Jul 2020 11:45:59 +0100 Subject: [PATCH 5/7] match light type with current pxt-jacdac-services master --- services.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services.ts b/services.ts index fc8c99a..7d8390d 100644 --- a/services.ts +++ b/services.ts @@ -14,7 +14,7 @@ const serviceDescs = [ new ServiceDesc(jd_class.LIGHT, "light", (num) => { const cl = jacdac.lightClient cl.setBrightness(10) - cl.setStrip(128, jacdac.LightType.WS2812B_SK9822) + cl.setStrip(128, jacdac.LightType.WS2812B_GRB) const duration = 30 * 1000 //cl.showAnimation(new jacdac.lightanimation.ColorWipe, duration) From a7c8d661adf3b0db5dd846a9c511faca14ab34e7 Mon Sep 17 00:00:00 2001 From: James Devine Date: Thu, 16 Jul 2020 13:00:07 +0100 Subject: [PATCH 6/7] add ability to specify custom views --- services.ts | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/services.ts b/services.ts index 7d8390d..0a765dd 100644 --- a/services.ts +++ b/services.ts @@ -2,14 +2,17 @@ class ServiceDesc { constructor( public classNum: number, public name: string, - public testFn?: (num: number) => void - ) { } + public testFn?: (num: number) => void, + public viewFn?: (d: jacdac.Device, s: ServiceDesc) => void + ) { + this.viewFn = viewFn || sensorView + } } const serviceDescs = [ new ServiceDesc(jd_class.ACCELEROMETER, "acc", num => jacdac.accelerometerClient.setStreaming(num & 1 ? true : false)), - new ServiceDesc(jd_class.RGB_LED, "RGBLED", + new ServiceDesc(jd_class.RGB_LED, "RGBLED", num => jacdac.rgbledClient.setColor(randint(0, 0xffffffff))), new ServiceDesc(jd_class.LIGHT, "light", (num) => { const cl = jacdac.lightClient @@ -64,8 +67,7 @@ const serviceDescs = [ new ServiceDesc(jd_class.LOGGER, "logger"), new ServiceDesc(jd_class.ROTARY_ENCODER, "crank", num => jacdac.rotaryEncoderClient.setStreaming(num & 1 ? true : false)), - new ServiceDesc(jd_class.BUTTON, "btn", - num => jacdac.rotaryEncoderClient.setStreaming(num & 1 ? true : false)), + new ServiceDesc(jd_class.BUTTON, "btn", num =>{}, buttonView), new ServiceDesc(jd_class.MUSIC, "music", num => jacdac.musicClient.playMelody(music.jumpDown, 20)), ] @@ -96,6 +98,39 @@ function sensorView(d: jacdac.Device, s: ServiceDesc) { client.destroy() } +function buttonView(d: jacdac.Device, s: ServiceDesc) { + let buttonState = "" + + jacdac.buttonClient.onEvent(JDButton.Click, () => { + buttonState += ", Click" + }) + + jacdac.buttonClient.onEvent(JDButton.LongClick, () => { + buttonState += ", Long click" + }) + + jacdac.buttonClient.onEvent(JDButton.Down, () => { + buttonState = "Down" + }) + + jacdac.buttonClient.onEvent(JDButton.Up, () => { + buttonState = "Up" + }) + + jacdac.buttonClient.onEvent(JDButton.Hold, () => { + buttonState = "Hold" + }) + + menu.show({ + title: "Device: " + d.shortId + " / " + s.name, + update: opts => { + opts.elements = [buttonState] + if (!d.isConnected) + menu.exit(opts) + } + }) +} + function hexNum(n: number) { const hex = "0123456789abcdef" let r = "0x" @@ -151,7 +186,7 @@ function deviceView(d: jacdac.Device) { opts.elements.push(menu.item("Identify", () => identify(d))) opts.elements.push(menu.item("---", noop)) opts.elements = opts.elements.concat(services.map(s => menu.item(s.name, () => { - sensorView(d, s) + s.viewFn(d,s) }, opts => { if (s.testFn) { s.testFn(++num) From fff5bb8e2fbee53c9558135c0a531606e539bed0 Mon Sep 17 00:00:00 2001 From: James Devine Date: Thu, 16 Jul 2020 13:07:08 +0100 Subject: [PATCH 7/7] convert button state to menu item --- services.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/services.ts b/services.ts index 0a765dd..2e787e8 100644 --- a/services.ts +++ b/services.ts @@ -99,26 +99,26 @@ function sensorView(d: jacdac.Device, s: ServiceDesc) { } function buttonView(d: jacdac.Device, s: ServiceDesc) { - let buttonState = "" + const buttonState = menu.item("State: ", () => { }) - jacdac.buttonClient.onEvent(JDButton.Click, () => { - buttonState += ", Click" + jacdac.buttonClient.onEvent(JDButtonEvent.Click, () => { + buttonState.name += ", Click" }) - jacdac.buttonClient.onEvent(JDButton.LongClick, () => { - buttonState += ", Long click" + jacdac.buttonClient.onEvent(JDButtonEvent.LongClick, () => { + buttonState.name += ", Long click" }) - jacdac.buttonClient.onEvent(JDButton.Down, () => { - buttonState = "Down" + jacdac.buttonClient.onEvent(JDButtonEvent.Down, () => { + buttonState.name = "Down" }) - jacdac.buttonClient.onEvent(JDButton.Up, () => { - buttonState = "Up" + jacdac.buttonClient.onEvent(JDButtonEvent.Up, () => { + buttonState.name = "Up" }) - jacdac.buttonClient.onEvent(JDButton.Hold, () => { - buttonState = "Hold" + jacdac.buttonClient.onEvent(JDButtonEvent.Hold, () => { + buttonState.name = "Hold" }) menu.show({