From e4acc9ade6ba4d23a8c6a6db7910550f45c2e75f Mon Sep 17 00:00:00 2001 From: pelikhan Date: Fri, 21 Jul 2023 13:58:47 +0200 Subject: [PATCH 1/5] gamepad client apis --- packages/core/src/events.ts | 6 ++--- packages/core/src/gamepad.ts | 36 +++++++++++++++++++++++++++ packages/core/src/index.ts | 3 ++- packages/sampleprj/src/maingamepad.ts | 10 ++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 packages/core/src/gamepad.ts create mode 100644 packages/sampleprj/src/maingamepad.ts diff --git a/packages/core/src/events.ts b/packages/core/src/events.ts index 5f1aebfb90..b48db5f235 100644 --- a/packages/core/src/events.ts +++ b/packages/core/src/events.ts @@ -72,10 +72,8 @@ class ClientRegister implements ds.ClientRegister { } emit(newValue: T) { - if (this.value !== newValue) { - this.value = newValue - this.emitter?.emit(this.value) - } + this.value = newValue + this.emitter?.emit(this.value) } } diff --git a/packages/core/src/gamepad.ts b/packages/core/src/gamepad.ts new file mode 100644 index 0000000000..84ed6d6611 --- /dev/null +++ b/packages/core/src/gamepad.ts @@ -0,0 +1,36 @@ +import * as ds from "@devicescript/core" + +declare module "@devicescript/core" { + interface Gamepad { + /** + * The thumbstick position register if any + */ + axes(): ds.ClientRegister<[number, number]> + + /** + * Button (or combo) register + */ + button(value: ds.GamepadButtons): ds.ClientRegister + } +} + +ds.Gamepad.prototype.axes = function axes() { + let r = (this as any).__axes as ds.ClientRegister<[number, number]> + if (!r) { + ;(this as any).__axes = r = ds.clientRegister([0, 0]) + this.reading.subscribe(([, x, y]) => r.emit([x, y])) + } + return r +} + +ds.Gamepad.prototype.button = function button(value: ds.GamepadButtons) { + const key = `__${value}` + let r = (this as any)[key] as ds.ClientRegister + if (!r) { + ;(this as any)[key] = r = ds.clientRegister(false) + this.reading.subscribe(([buttons]) => + r.emit((buttons & value) === value) + ) + } + return r +} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 84e3d9bffc..5374d219a2 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -14,4 +14,5 @@ import "./rotaryencoder" import "./button" import "./magneticfieldlevel" import "./buzzer" -import "./ledstrip" \ No newline at end of file +import "./ledstrip" +import "./gamepad" \ No newline at end of file diff --git a/packages/sampleprj/src/maingamepad.ts b/packages/sampleprj/src/maingamepad.ts new file mode 100644 index 0000000000..0a28ea5aa1 --- /dev/null +++ b/packages/sampleprj/src/maingamepad.ts @@ -0,0 +1,10 @@ +import { delay, Gamepad, GamepadButtons, Led } from "@devicescript/core" +import { rgb } from "@devicescript/runtime" + +const gamepad = new Gamepad() + +const axes = gamepad.axes() +axes.subscribe(([x, y]) => console.log({ x, y })) + +const down = gamepad.button(GamepadButtons.Down) +down.subscribe(down => console.log({ down })) From 65abf941bae79d97c9843f117f817e1cf2c6aba9 Mon Sep 17 00:00:00 2001 From: pelikhan Date: Fri, 21 Jul 2023 16:04:03 +0200 Subject: [PATCH 2/5] adding docs --- compiler/src/specgen.ts | 9 +++++- .../api/clients-custom/gamepad-registers.mdp | 32 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 website/docs/api/clients-custom/gamepad-registers.mdp diff --git a/compiler/src/specgen.ts b/compiler/src/specgen.ts index d317199cb5..d24a7c4c30 100644 --- a/compiler/src/specgen.ts +++ b/compiler/src/specgen.ts @@ -505,7 +505,14 @@ ${varname}.${sig} const regs = info.packets .filter(pkt => isRegister(pkt.kind)) .filter(pkt => !pkt.derived && !pkt.internal && !pkt.lowLevel) - if (regs?.length) r.push("## Registers", "") + if (regs?.length) { + r.push( + "## Registers", + "", + `{@import optional ../clients-custom/${info.shortId}-registers.mdp}`, + "" + ) + } regs.forEach(pkt => { const cmt = addComment(pkt) const nobuild = status === "stable" && !pkt.client ? "" : "skip" diff --git a/website/docs/api/clients-custom/gamepad-registers.mdp b/website/docs/api/clients-custom/gamepad-registers.mdp new file mode 100644 index 0000000000..e359890b97 --- /dev/null +++ b/website/docs/api/clients-custom/gamepad-registers.mdp @@ -0,0 +1,32 @@ +### axes {#ro:axes} + +An array representing the controls with axes present on the device (e.g. analog thumb sticks), +as `[x, y]`. Each entry in the array is a floating point value in the range ` -1.0 – 1.0``, representing the axis position from the lowest value ( `-1.0`) to the highest value (`1.0`). + +- type: `ClientRegister<[number, number]>` (packing format `i1.15 i1.15`) + +```ts +import { Gamepad } from "@devicescript/core" + +const gamepad = new Gamepad() +// ... +gamepad.axes.subscribe(async ([x, y]) => { + console.log({ x, y }) +}) +``` + +### button {#ro:button} + +A client register for the requested button or combination of buttons. The value is `true` if the button is pressed, `false` otherwise. + +- type: `ClientRegister` + +```ts +import { Gamepad, GamepadButtons } from "@devicescript/core" + +const gamepad = new Gamepad() +// ... +gamepad.button(GamepadButtons.Down).subscribe(async pressed => { + console.log({ pressed }) +}) +``` From d281751a153f3123c0eb7c3b7ad66cc33a5280ac Mon Sep 17 00:00:00 2001 From: pelikhan Date: Fri, 21 Jul 2023 16:06:14 +0200 Subject: [PATCH 3/5] updated code --- packages/core/src/gamepad.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/core/src/gamepad.ts b/packages/core/src/gamepad.ts index 84ed6d6611..c264e883ec 100644 --- a/packages/core/src/gamepad.ts +++ b/packages/core/src/gamepad.ts @@ -18,7 +18,7 @@ ds.Gamepad.prototype.axes = function axes() { let r = (this as any).__axes as ds.ClientRegister<[number, number]> if (!r) { ;(this as any).__axes = r = ds.clientRegister([0, 0]) - this.reading.subscribe(([, x, y]) => r.emit([x, y])) + this.reading.subscribe(rv => r.emit([rv[1], rv[2]])) } return r } @@ -28,9 +28,7 @@ ds.Gamepad.prototype.button = function button(value: ds.GamepadButtons) { let r = (this as any)[key] as ds.ClientRegister if (!r) { ;(this as any)[key] = r = ds.clientRegister(false) - this.reading.subscribe(([buttons]) => - r.emit((buttons & value) === value) - ) + this.reading.subscribe(rv => r.emit((rv[0] & value) === value)) } return r } From e2aeafdffdcadb3b407e519134724d223230859d Mon Sep 17 00:00:00 2001 From: pelikhan Date: Fri, 21 Jul 2023 16:07:54 +0200 Subject: [PATCH 4/5] fix codegen of sample --- packages/sampleprj/src/maingamepad.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sampleprj/src/maingamepad.ts b/packages/sampleprj/src/maingamepad.ts index 0a28ea5aa1..d425c3de04 100644 --- a/packages/sampleprj/src/maingamepad.ts +++ b/packages/sampleprj/src/maingamepad.ts @@ -4,7 +4,7 @@ import { rgb } from "@devicescript/runtime" const gamepad = new Gamepad() const axes = gamepad.axes() -axes.subscribe(([x, y]) => console.log({ x, y })) +axes.subscribe(pos => console.log({ x: pos[0], y: pos[1] })) const down = gamepad.button(GamepadButtons.Down) down.subscribe(down => console.log({ down })) From 1258a762f11476ffd5688fbf072966906c378a4c Mon Sep 17 00:00:00 2001 From: pelikhan Date: Fri, 21 Jul 2023 16:17:09 +0200 Subject: [PATCH 5/5] fixed sample --- packages/core/src/gamepad.ts | 10 ++++++---- packages/sampleprj/src/maingamepad.ts | 11 ++++++----- website/docs/api/clients-custom/gamepad-registers.mdp | 4 ++-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/core/src/gamepad.ts b/packages/core/src/gamepad.ts index c264e883ec..408c9edf65 100644 --- a/packages/core/src/gamepad.ts +++ b/packages/core/src/gamepad.ts @@ -5,7 +5,7 @@ declare module "@devicescript/core" { /** * The thumbstick position register if any */ - axes(): ds.ClientRegister<[number, number]> + axes(): ds.ClientRegister<{ x: number; y: number }> /** * Button (or combo) register @@ -15,10 +15,12 @@ declare module "@devicescript/core" { } ds.Gamepad.prototype.axes = function axes() { - let r = (this as any).__axes as ds.ClientRegister<[number, number]> + let r = (this as any).__axes as ds.ClientRegister<{ x: number; y: number }> if (!r) { - ;(this as any).__axes = r = ds.clientRegister([0, 0]) - this.reading.subscribe(rv => r.emit([rv[1], rv[2]])) + ;(this as any).__axes = r = ds.clientRegister<{ x: number; y: number }>( + { x: 0, y: 0 } + ) + this.reading.subscribe(rv => r.emit({ x: rv[1], y: rv[2] })) } return r } diff --git a/packages/sampleprj/src/maingamepad.ts b/packages/sampleprj/src/maingamepad.ts index d425c3de04..f64a30db7b 100644 --- a/packages/sampleprj/src/maingamepad.ts +++ b/packages/sampleprj/src/maingamepad.ts @@ -1,10 +1,11 @@ -import { delay, Gamepad, GamepadButtons, Led } from "@devicescript/core" -import { rgb } from "@devicescript/runtime" +import { Gamepad, GamepadButtons } from "@devicescript/core" const gamepad = new Gamepad() const axes = gamepad.axes() -axes.subscribe(pos => console.log({ x: pos[0], y: pos[1] })) +axes.subscribe(({ x, y }) => console.log({ x, y })) -const down = gamepad.button(GamepadButtons.Down) -down.subscribe(down => console.log({ down })) +const A = gamepad.button(GamepadButtons.A) +A.subscribe(a => console.log({ a })) +const B = gamepad.button(GamepadButtons.B) +B.subscribe(b => console.log({ b })) diff --git a/website/docs/api/clients-custom/gamepad-registers.mdp b/website/docs/api/clients-custom/gamepad-registers.mdp index e359890b97..e6aee4b77a 100644 --- a/website/docs/api/clients-custom/gamepad-registers.mdp +++ b/website/docs/api/clients-custom/gamepad-registers.mdp @@ -3,14 +3,14 @@ An array representing the controls with axes present on the device (e.g. analog thumb sticks), as `[x, y]`. Each entry in the array is a floating point value in the range ` -1.0 – 1.0``, representing the axis position from the lowest value ( `-1.0`) to the highest value (`1.0`). -- type: `ClientRegister<[number, number]>` (packing format `i1.15 i1.15`) +- type: `ClientRegister<{ x: number; y: number }>` (packing format `i1.15 i1.15`) ```ts import { Gamepad } from "@devicescript/core" const gamepad = new Gamepad() // ... -gamepad.axes.subscribe(async ([x, y]) => { +gamepad.axes.subscribe(async ({ x, y }) => { console.log({ x, y }) }) ```