Skip to content
This repository was archived by the owner on Dec 8, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion compiler/src/specgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ class ClientRegister<T> implements ds.ClientRegister<T> {
}

emit(newValue: T) {
if (this.value !== newValue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was there a problem with this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a bit misleading because in the case of axes ({x, y}), the equality check is not really useful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, but this is generic - what if people use for say booleans?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.value = newValue
this.emitter?.emit(this.value)
}
this.value = newValue
this.emitter?.emit(this.value)
}
}

Expand Down
36 changes: 36 additions & 0 deletions packages/core/src/gamepad.ts
Original file line number Diff line number Diff line change
@@ -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<{ x: number; y: number }>

/**
* Button (or combo) register
*/
button(value: ds.GamepadButtons): ds.ClientRegister<boolean>
}
}

ds.Gamepad.prototype.axes = function axes() {
let r = (this as any).__axes as ds.ClientRegister<{ x: number; y: number }>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can extend the ds.Gamepad interface (I know it's a class, but I think you can do interface) and add this field

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do i have to? :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah ;)

if (!r) {
;(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
}

ds.Gamepad.prototype.button = function button(value: ds.GamepadButtons) {
const key = `__${value}`
let r = (this as any)[key] as ds.ClientRegister<boolean>
if (!r) {
;(this as any)[key] = r = ds.clientRegister(false)
this.reading.subscribe(rv => r.emit((rv[0] & value) === value))
}
return r
}
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ import "./rotaryencoder"
import "./button"
import "./magneticfieldlevel"
import "./buzzer"
import "./ledstrip"
import "./ledstrip"
import "./gamepad"
11 changes: 11 additions & 0 deletions packages/sampleprj/src/maingamepad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Gamepad, GamepadButtons } from "@devicescript/core"

const gamepad = new Gamepad()

const axes = gamepad.axes()
axes.subscribe(({ x, y }) => console.log({ x, y }))

const A = gamepad.button(GamepadButtons.A)
A.subscribe(a => console.log({ a }))
const B = gamepad.button(GamepadButtons.B)
B.subscribe(b => console.log({ b }))
32 changes: 32 additions & 0 deletions website/docs/api/clients-custom/gamepad-registers.mdp
Original file line number Diff line number Diff line change
@@ -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<{ 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 }) => {
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<GamepadButtons>`

```ts
import { Gamepad, GamepadButtons } from "@devicescript/core"

const gamepad = new Gamepad()
// ...
gamepad.button(GamepadButtons.Down).subscribe(async pressed => {
console.log({ pressed })
})
```