@sdeck/core 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/deck.d.ts +80 -13
- package/dist/deck.d.ts.map +1 -1
- package/dist/detect.d.ts.map +1 -1
- package/dist/image.d.ts +4 -2
- package/dist/image.d.ts.map +1 -1
- package/dist/index.js +168 -58
- package/dist/index.js.map +8 -8
- package/dist/layout.d.ts +4 -0
- package/dist/layout.d.ts.map +1 -1
- package/dist/manager.d.ts +2 -2
- package/dist/manager.d.ts.map +1 -1
- package/dist/virtual.d.ts +10 -1
- package/dist/virtual.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/deck.ts +191 -56
- package/src/detect.ts +2 -3
- package/src/image.ts +6 -3
- package/src/layout.ts +20 -0
- package/src/manager.ts +2 -2
- package/src/virtual.ts +50 -21
package/src/deck.ts
CHANGED
|
@@ -7,9 +7,9 @@ import {
|
|
|
7
7
|
type Size,
|
|
8
8
|
} from "./element.ts";
|
|
9
9
|
import { Framebuffer, type Rect, type RGB, solidRgb } from "./framebuffer.ts";
|
|
10
|
-
import { encodeJpeg, type ImageOptions, type ImageSource, toRgb } from "./image.ts";
|
|
10
|
+
import { encodeJpeg, type ImageOptions, type ImageSource, type Rotation, toRgb } from "./image.ts";
|
|
11
11
|
import type { Renderable } from "./index.ts";
|
|
12
|
-
import { composeScreenRaw, type DeckLayout, type ScreenLayout } from "./layout.ts";
|
|
12
|
+
import { composeScreenRaw, type DeckLayout, type ScreenLayout, slotRect } from "./layout.ts";
|
|
13
13
|
import type { HidTransport } from "./transport.ts";
|
|
14
14
|
|
|
15
15
|
/** Static description of a Stream Deck model. */
|
|
@@ -27,11 +27,30 @@ export interface DeckModel {
|
|
|
27
27
|
* content larger; the extra room along one side is layout space. Without this, elements are
|
|
28
28
|
* laid out at the slot's own pixels.
|
|
29
29
|
*/
|
|
30
|
-
design?: Readonly<{ key?: Size; sensor?: Size; window?: Size }>;
|
|
31
|
-
/**
|
|
32
|
-
rotation:
|
|
30
|
+
design?: Readonly<{ key?: Size; sensor?: Size; dial?: Size; window?: Size }>;
|
|
31
|
+
/** Clockwise rotation the device expects images in. */
|
|
32
|
+
rotation: Rotation;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
/** A place on the window, in its own upright pixels. */
|
|
36
|
+
export interface Point {
|
|
37
|
+
x: number;
|
|
38
|
+
y: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** What an input report says. */
|
|
42
|
+
export type DeckInput =
|
|
43
|
+
/** Which keys, and after them which sensors, are down now. */
|
|
44
|
+
| { type: "buttons"; pressed: readonly boolean[] }
|
|
45
|
+
/** Which dials are pressed now. */
|
|
46
|
+
| { type: "dials"; pressed: readonly boolean[] }
|
|
47
|
+
/** How far each dial turned since the last report, clockwise being positive. */
|
|
48
|
+
| { type: "turn"; ticks: readonly number[] }
|
|
49
|
+
/** A finger touched the window, and is gone again. */
|
|
50
|
+
| { type: "tap"; at: Point; long: boolean }
|
|
51
|
+
/** A finger slid across the window. */
|
|
52
|
+
| { type: "swipe"; from: Point; to: Point };
|
|
53
|
+
|
|
35
54
|
export interface DeckEvents {
|
|
36
55
|
/** A key was pressed. */
|
|
37
56
|
keyDown: [index: number];
|
|
@@ -41,21 +60,38 @@ export interface DeckEvents {
|
|
|
41
60
|
sensorDown: [index: number];
|
|
42
61
|
/** A capacitive sensor was released. */
|
|
43
62
|
sensorUp: [index: number];
|
|
63
|
+
/** A dial was pressed. */
|
|
64
|
+
dialDown: [index: number];
|
|
65
|
+
/** A dial was released. */
|
|
66
|
+
dialUp: [index: number];
|
|
67
|
+
/** A dial was turned by this many steps, clockwise being positive. */
|
|
68
|
+
dialTurn: [index: number, ticks: number];
|
|
69
|
+
/** The part of the window that belongs to a dial was tapped, or held for a moment. */
|
|
70
|
+
dialTap: [index: number, long: boolean];
|
|
71
|
+
/** The window was tapped, or held for a moment. */
|
|
72
|
+
windowTap: [at: Point, long: boolean];
|
|
73
|
+
/** A finger slid across the window. */
|
|
74
|
+
windowSwipe: [from: Point, to: Point];
|
|
44
75
|
/** Pixels reached the device: the region (upright screen coordinates) that was written. */
|
|
45
76
|
draw: [region: Rect];
|
|
77
|
+
/** The deck is closing. Keys held down get no release. */
|
|
78
|
+
close: [];
|
|
46
79
|
error: [error: Error];
|
|
47
80
|
}
|
|
48
81
|
|
|
49
82
|
type Listener<K extends keyof DeckEvents> = (...args: DeckEvents[K]) => void;
|
|
50
83
|
|
|
51
|
-
/** Options for `mountKey`, `mountSensor`, `mountWindow` and `mountScreen`. */
|
|
84
|
+
/** Options for `mountKey`, `mountSensor`, `mountDial`, `mountWindow` and `mountScreen`. */
|
|
52
85
|
export type MountOptions = ElementOptions & Pick<ImageOptions, "quality" | "background">;
|
|
53
86
|
|
|
54
87
|
export const toError = (error: unknown): Error =>
|
|
55
88
|
error instanceof Error ? error : new Error(String(error));
|
|
56
89
|
|
|
57
|
-
/**
|
|
58
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Every part of the screen the deck tracks: keys, strips, the window and everything else. Where
|
|
92
|
+
* there are dials, the window is what their parts of it make up together.
|
|
93
|
+
*/
|
|
94
|
+
type Slot = `key:${number}` | `sensor:${number}` | `dial:${number}` | "window" | "rest";
|
|
59
95
|
|
|
60
96
|
interface PendingFrame {
|
|
61
97
|
quality?: number | undefined;
|
|
@@ -82,11 +118,19 @@ export abstract class StreamDeck {
|
|
|
82
118
|
keyUp: new Set(),
|
|
83
119
|
sensorDown: new Set(),
|
|
84
120
|
sensorUp: new Set(),
|
|
121
|
+
dialDown: new Set(),
|
|
122
|
+
dialUp: new Set(),
|
|
123
|
+
dialTurn: new Set(),
|
|
124
|
+
dialTap: new Set(),
|
|
125
|
+
windowTap: new Set(),
|
|
126
|
+
windowSwipe: new Set(),
|
|
85
127
|
draw: new Set(),
|
|
128
|
+
close: new Set(),
|
|
86
129
|
error: new Set(),
|
|
87
130
|
};
|
|
88
131
|
readonly #unsubscribe: Array<() => void>;
|
|
89
|
-
#pressed: boolean[] = [];
|
|
132
|
+
#pressed: readonly boolean[] = [];
|
|
133
|
+
#dials: readonly boolean[] = [];
|
|
90
134
|
#closed = false;
|
|
91
135
|
#roots = new Set<ElementRoot>();
|
|
92
136
|
/** Serialises writes so multi-packet image uploads never interleave. */
|
|
@@ -95,6 +139,7 @@ export abstract class StreamDeck {
|
|
|
95
139
|
/** Upright pixels as they are (or, once queued writes finish, will be) on the LCD. */
|
|
96
140
|
readonly #screen: Framebuffer;
|
|
97
141
|
readonly #allSlots: readonly Slot[];
|
|
142
|
+
readonly #windowSlots: readonly Slot[];
|
|
98
143
|
/** Slots whose device pixels are known to match the framebuffer. */
|
|
99
144
|
readonly #known = new Set<Slot>();
|
|
100
145
|
/** Bumped whenever a slot's framebuffer pixels change. */
|
|
@@ -109,10 +154,12 @@ export abstract class StreamDeck {
|
|
|
109
154
|
this.transport = transport;
|
|
110
155
|
this.model = model;
|
|
111
156
|
this.#screen = new Framebuffer(model.layout.screen);
|
|
157
|
+
const dials = model.layout.dials ?? [];
|
|
158
|
+
this.#windowSlots = dials.length > 0 ? dials.map((_, i): Slot => `dial:${i}`) : ["window"];
|
|
112
159
|
this.#allSlots = [
|
|
113
160
|
...model.layout.keys.map((_, i): Slot => `key:${i}`),
|
|
114
161
|
...model.layout.sensors.map((_, i): Slot => `sensor:${i}`),
|
|
115
|
-
...(model.layout.window ?
|
|
162
|
+
...(model.layout.window ? this.#windowSlots : []),
|
|
116
163
|
"rest",
|
|
117
164
|
];
|
|
118
165
|
this.#unsubscribe = [
|
|
@@ -129,12 +176,19 @@ export abstract class StreamDeck {
|
|
|
129
176
|
get sensorCount(): number {
|
|
130
177
|
return this.model.layout.sensors.length;
|
|
131
178
|
}
|
|
179
|
+
get dialCount(): number {
|
|
180
|
+
return this.model.layout.dials?.length ?? 0;
|
|
181
|
+
}
|
|
132
182
|
get keySize(): Size {
|
|
133
183
|
return this.#size(this.model.layout.keys[0], "keys");
|
|
134
184
|
}
|
|
135
185
|
get sensorSize(): Size {
|
|
136
186
|
return this.#size(this.model.layout.sensors[0], "sensor strips");
|
|
137
187
|
}
|
|
188
|
+
/** The part of the window that belongs to a dial. */
|
|
189
|
+
get dialSize(): Size {
|
|
190
|
+
return this.#size(this.model.layout.dials?.[0], "dials");
|
|
191
|
+
}
|
|
138
192
|
get windowSize(): Size {
|
|
139
193
|
return this.#size(this.model.layout.window, "a window");
|
|
140
194
|
}
|
|
@@ -149,10 +203,12 @@ export abstract class StreamDeck {
|
|
|
149
203
|
* How a live element on a slot is drawn: `size` is the room it lays out in, and `scale` how
|
|
150
204
|
* many times larger that appears (see {@link DeckModel.design}).
|
|
151
205
|
*/
|
|
152
|
-
slotDrawing(
|
|
206
|
+
slotDrawing(
|
|
207
|
+
kind: "key" | "sensor" | "dial" | "window",
|
|
208
|
+
index = 0,
|
|
209
|
+
): { size: Size; scale: number } {
|
|
153
210
|
const { layout, design } = this.model;
|
|
154
|
-
const rect =
|
|
155
|
-
kind === "window" ? layout.window : (kind === "key" ? layout.keys : layout.sensors)[index];
|
|
211
|
+
const rect = slotRect(layout, kind, index);
|
|
156
212
|
const size = this.#size(rect, kind === "window" ? "a window" : `${kind} ${index}`);
|
|
157
213
|
const base = design?.[kind];
|
|
158
214
|
if (!base) return { size, scale: 1 };
|
|
@@ -206,26 +262,41 @@ export abstract class StreamDeck {
|
|
|
206
262
|
// The `send*` methods are called inside the write queue, in order; they write straight to the
|
|
207
263
|
// transport and must not enqueue.
|
|
208
264
|
|
|
209
|
-
/**
|
|
210
|
-
protected abstract parseInput(data: Uint8Array):
|
|
211
|
-
protected abstract
|
|
212
|
-
protected abstract
|
|
213
|
-
protected
|
|
265
|
+
/** What an input report says, or `undefined` for one that says nothing of interest. */
|
|
266
|
+
protected abstract parseInput(data: Uint8Array): DeckInput | undefined;
|
|
267
|
+
protected abstract sendKeyImage(index: number, image: Uint8Array): Promise<void>;
|
|
268
|
+
protected abstract sendScreenImage(image: Uint8Array): Promise<void>;
|
|
269
|
+
protected sendWindowImage(_image: Uint8Array): Promise<void> {
|
|
214
270
|
return Promise.reject(new Error(`${this.model.name} has no window`));
|
|
215
271
|
}
|
|
216
|
-
/** Sends
|
|
217
|
-
protected
|
|
272
|
+
/** Sends an image to part of the window; `region` is relative to the window. */
|
|
273
|
+
protected sendWindowRegionImage(_region: Rect, _image: Uint8Array): Promise<void> {
|
|
218
274
|
return Promise.reject(new Error(`${this.model.name} cannot update part of the window`));
|
|
219
275
|
}
|
|
220
276
|
/** Fills a key with a colour. Override if the model has a command for it. */
|
|
221
277
|
protected async sendFillKey(index: number, color: RGB): Promise<void> {
|
|
222
278
|
const size = this.#size(this.model.layout.keys[index], "keys");
|
|
223
|
-
await this.
|
|
279
|
+
await this.sendKeyImage(index, await this.encodeImage(solidRgb(size, color), size));
|
|
224
280
|
}
|
|
225
281
|
/** Fills the screen with a colour. Override if the model has a command for it. */
|
|
226
282
|
protected async sendFillScreen(color: RGB): Promise<void> {
|
|
227
283
|
const size = this.screenSize;
|
|
228
|
-
await this.
|
|
284
|
+
await this.sendScreenImage(await this.encodeImage(solidRgb(size, color), size));
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Turns upright RGB pixels into what the device takes: a JPEG, rotated as the model needs.
|
|
288
|
+
* Override if it takes something else.
|
|
289
|
+
*/
|
|
290
|
+
protected encodeImage(rgb: Uint8Array, size: Size, quality?: number): Promise<Uint8Array> {
|
|
291
|
+
return encodeJpeg(rgb, size, { quality, rotation: this.model.rotation });
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Prepares a frame of the whole screen, and returns what sends it. Override if the model has no
|
|
295
|
+
* command for the whole screen.
|
|
296
|
+
*/
|
|
297
|
+
protected prepareFrame(screen: Framebuffer, quality?: number): () => Promise<void> {
|
|
298
|
+
const image = this.#encode(screen.data.slice(), this.screenSize, quality);
|
|
299
|
+
return async () => this.sendScreenImage(await image);
|
|
229
300
|
}
|
|
230
301
|
/** Runs (queued, before the transport closes) when `close()` is called, e.g. to show a logo. */
|
|
231
302
|
protected async onClosing(): Promise<void> {}
|
|
@@ -257,20 +328,36 @@ export abstract class StreamDeck {
|
|
|
257
328
|
const rgb = await toRgb(image, rect, options);
|
|
258
329
|
if (!current()) return;
|
|
259
330
|
await this.#commit([slot], rect, rgb, () => {
|
|
260
|
-
const
|
|
261
|
-
return async () => this.
|
|
331
|
+
const encoded = this.#encode(rgb, rect, options?.quality);
|
|
332
|
+
return async () => this.sendKeyImage(index, await encoded);
|
|
262
333
|
});
|
|
263
334
|
}
|
|
264
335
|
|
|
265
336
|
/** Sets the window image (scaled to the window size). */
|
|
266
337
|
async setWindowImage(image: ImageSource, options?: ImageOptions): Promise<void> {
|
|
267
338
|
const rect = this.#windowRect();
|
|
268
|
-
const current = this.#claim(
|
|
339
|
+
const current = this.#claim(this.#windowSlots);
|
|
269
340
|
const rgb = await toRgb(image, rect, options);
|
|
270
341
|
if (!current()) return;
|
|
271
|
-
await this.#commit(
|
|
272
|
-
const
|
|
273
|
-
return async () => this.
|
|
342
|
+
await this.#commit(this.#windowSlots, rect, rgb, () => {
|
|
343
|
+
const encoded = this.#encode(rgb, rect, options?.quality);
|
|
344
|
+
return async () => this.sendWindowImage(await encoded);
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Sets the part of the window that belongs to a dial (scaled to its size). */
|
|
349
|
+
async setDialImage(index: number, image: ImageSource, options?: ImageOptions): Promise<void> {
|
|
350
|
+
this.assertDial(index);
|
|
351
|
+
const slot: Slot = `dial:${index}`;
|
|
352
|
+
const rect = this.model.layout.dials?.[index] as Rect;
|
|
353
|
+
const win = this.#windowRect();
|
|
354
|
+
const current = this.#claim([slot]);
|
|
355
|
+
const rgb = await toRgb(image, rect, options);
|
|
356
|
+
if (!current()) return;
|
|
357
|
+
await this.#commit([slot], rect, rgb, () => {
|
|
358
|
+
const encoded = this.#encode(rgb, rect, options?.quality);
|
|
359
|
+
const region = { ...rect, x: rect.x - win.x, y: rect.y - win.y };
|
|
360
|
+
return async () => this.sendWindowRegionImage(region, await encoded);
|
|
274
361
|
});
|
|
275
362
|
}
|
|
276
363
|
|
|
@@ -285,9 +372,9 @@ export abstract class StreamDeck {
|
|
|
285
372
|
): Promise<void> {
|
|
286
373
|
const rect = this.#windowRegion(region);
|
|
287
374
|
const rgb = await toRgb(image, rect, options);
|
|
288
|
-
await this.#commit(
|
|
289
|
-
const
|
|
290
|
-
return async () => this.
|
|
375
|
+
await this.#commit(this.#windowSlots, rect, rgb, () => {
|
|
376
|
+
const encoded = this.#encode(rgb, rect, options?.quality);
|
|
377
|
+
return async () => this.sendWindowRegionImage(region, await encoded);
|
|
291
378
|
});
|
|
292
379
|
}
|
|
293
380
|
|
|
@@ -351,30 +438,30 @@ export abstract class StreamDeck {
|
|
|
351
438
|
);
|
|
352
439
|
}
|
|
353
440
|
|
|
354
|
-
// Low-level variants: send a JPEG that is already the exact size and device rotation
|
|
355
|
-
// not decoded, so the deck does not know these pixels: a later strip redraw replaces them with
|
|
441
|
+
// Low-level variants: send a JPEG that is already the exact size and device rotation (a deck of
|
|
442
|
+
// the older kind, such as the Mini, takes a BMP instead). They are not decoded, so the deck does not know these pixels: a later strip redraw replaces them with
|
|
356
443
|
// what was last drawn there through the other methods.
|
|
357
444
|
|
|
358
445
|
/** Sends a JPEG that is already key-sized (and rotated as the device needs) to a key. */
|
|
359
446
|
setKeyJpeg(index: number, jpeg: Uint8Array): Promise<void> {
|
|
360
447
|
this.assertKey(index);
|
|
361
|
-
return this.#raw([`key:${index}`], () => this.
|
|
448
|
+
return this.#raw([`key:${index}`], () => this.sendKeyImage(index, jpeg));
|
|
362
449
|
}
|
|
363
450
|
|
|
364
451
|
/** Sends a screen-sized, device-rotated JPEG to the LCD as-is. */
|
|
365
452
|
setScreenJpeg(jpeg: Uint8Array): Promise<void> {
|
|
366
|
-
return this.#raw(this.#allSlots, () => this.
|
|
453
|
+
return this.#raw(this.#allSlots, () => this.sendScreenImage(jpeg));
|
|
367
454
|
}
|
|
368
455
|
|
|
369
456
|
/** Sends a window-sized, device-rotated JPEG to the window as-is. */
|
|
370
457
|
setWindowJpeg(jpeg: Uint8Array): Promise<void> {
|
|
371
|
-
return this.#raw(
|
|
458
|
+
return this.#raw(this.#windowSlots, () => this.sendWindowImage(jpeg));
|
|
372
459
|
}
|
|
373
460
|
|
|
374
461
|
/** Sends a `region.width`×`region.height`, device-rotated JPEG to part of the window as-is. */
|
|
375
462
|
setWindowRegionJpeg(region: Rect, jpeg: Uint8Array): Promise<void> {
|
|
376
463
|
this.#windowRegion(region);
|
|
377
|
-
return this.#raw(
|
|
464
|
+
return this.#raw(this.#windowSlots, () => this.sendWindowRegionImage(region, jpeg));
|
|
378
465
|
}
|
|
379
466
|
|
|
380
467
|
// -- Reading back ------------------------------------------------------------
|
|
@@ -396,9 +483,9 @@ export abstract class StreamDeck {
|
|
|
396
483
|
// -- Drawing internals -------------------------------------------------------
|
|
397
484
|
|
|
398
485
|
#encode(rgb: Uint8Array, size: Size, quality?: number): Promise<Uint8Array> {
|
|
399
|
-
const
|
|
400
|
-
|
|
401
|
-
return
|
|
486
|
+
const image = this.encodeImage(rgb, size, quality);
|
|
487
|
+
image.catch(() => {}); // awaited inside the queue; never an unhandled rejection
|
|
488
|
+
return image;
|
|
402
489
|
}
|
|
403
490
|
|
|
404
491
|
#windowRegion(region: Rect): Rect {
|
|
@@ -513,8 +600,7 @@ export abstract class StreamDeck {
|
|
|
513
600
|
const frame = this.#frame;
|
|
514
601
|
this.#frame = undefined;
|
|
515
602
|
const gens = this.#gens(this.#allSlots);
|
|
516
|
-
const
|
|
517
|
-
const write = this.enqueue(async () => this.sendScreenJpeg(await jpeg));
|
|
603
|
+
const write = this.enqueue(this.prepareFrame(this.#screen, frame.quality));
|
|
518
604
|
try {
|
|
519
605
|
await this.#settle(write, gens, this.#screenRect);
|
|
520
606
|
for (const waiter of frame.waiters) waiter.resolve();
|
|
@@ -561,6 +647,18 @@ export abstract class StreamDeck {
|
|
|
561
647
|
);
|
|
562
648
|
}
|
|
563
649
|
|
|
650
|
+
/** Mounts a live element tree on the part of the window that belongs to a dial. See `mountKey`. */
|
|
651
|
+
mountDial(index: number, element: Renderable, options?: MountOptions): Promise<ElementRoot> {
|
|
652
|
+
this.assertDial(index);
|
|
653
|
+
return this.#mount(
|
|
654
|
+
"dial",
|
|
655
|
+
index,
|
|
656
|
+
(frame) => this.setDialImage(index, frame, { ...options, fit: "fill" }),
|
|
657
|
+
element,
|
|
658
|
+
options,
|
|
659
|
+
);
|
|
660
|
+
}
|
|
661
|
+
|
|
564
662
|
/** Mounts a live element tree on the window. See `mountKey`. */
|
|
565
663
|
mountWindow(element: Renderable, options?: MountOptions): Promise<ElementRoot> {
|
|
566
664
|
return this.#mount(
|
|
@@ -588,7 +686,7 @@ export abstract class StreamDeck {
|
|
|
588
686
|
}
|
|
589
687
|
|
|
590
688
|
async #mount(
|
|
591
|
-
kind: "key" | "sensor" | "window" | "screen",
|
|
689
|
+
kind: "key" | "sensor" | "dial" | "window" | "screen",
|
|
592
690
|
index: number,
|
|
593
691
|
send: (frame: RawImage) => Promise<void>,
|
|
594
692
|
element: Renderable,
|
|
@@ -635,6 +733,7 @@ export abstract class StreamDeck {
|
|
|
635
733
|
/** Unmounts live roots, lets the model tidy up (e.g. restore the boot logo) and closes the device. */
|
|
636
734
|
async close(): Promise<void> {
|
|
637
735
|
if (this.#closed) return;
|
|
736
|
+
this.emit("close");
|
|
638
737
|
await Promise.all([...this.#roots].map((root) => root.unmount()));
|
|
639
738
|
this.#closed = true;
|
|
640
739
|
for (const stop of this.#unsubscribe) stop();
|
|
@@ -661,6 +760,12 @@ export abstract class StreamDeck {
|
|
|
661
760
|
}
|
|
662
761
|
}
|
|
663
762
|
|
|
763
|
+
protected assertDial(index: number): void {
|
|
764
|
+
if (!Number.isInteger(index) || index < 0 || index >= this.dialCount) {
|
|
765
|
+
throw new RangeError(`dial index must be 0–${Math.max(0, this.dialCount - 1)}, got ${index}`);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
664
769
|
/** Runs `task` after every earlier write, so multi-packet uploads never interleave. */
|
|
665
770
|
protected enqueue<T>(task: () => Promise<T>): Promise<T> {
|
|
666
771
|
if (this.#closed) return Promise.reject(new Error(`${this.model.name} is closed`));
|
|
@@ -674,20 +779,50 @@ export abstract class StreamDeck {
|
|
|
674
779
|
}
|
|
675
780
|
|
|
676
781
|
#handleInput(data: Uint8Array): void {
|
|
677
|
-
const
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
} else {
|
|
688
|
-
this.emit(pressed ? "sensorDown" : "sensorUp", i - keys);
|
|
782
|
+
const input = this.parseInput(data);
|
|
783
|
+
switch (input?.type) {
|
|
784
|
+
case "buttons": {
|
|
785
|
+
const before = this.#pressed;
|
|
786
|
+
this.#pressed = input.pressed;
|
|
787
|
+
changes(before, input.pressed, (i, down) => {
|
|
788
|
+
if (i < this.keyCount) this.emit(down ? "keyDown" : "keyUp", i);
|
|
789
|
+
else this.emit(down ? "sensorDown" : "sensorUp", i - this.keyCount);
|
|
790
|
+
});
|
|
791
|
+
break;
|
|
689
792
|
}
|
|
793
|
+
case "dials": {
|
|
794
|
+
const before = this.#dials;
|
|
795
|
+
this.#dials = input.pressed;
|
|
796
|
+
changes(before, input.pressed, (i, down) => this.emit(down ? "dialDown" : "dialUp", i));
|
|
797
|
+
break;
|
|
798
|
+
}
|
|
799
|
+
case "turn":
|
|
800
|
+
input.ticks.forEach((ticks, i) => {
|
|
801
|
+
if (ticks !== 0) this.emit("dialTurn", i, ticks);
|
|
802
|
+
});
|
|
803
|
+
break;
|
|
804
|
+
case "tap": {
|
|
805
|
+
this.emit("windowTap", input.at, input.long);
|
|
806
|
+
const { dials = [], window } = this.model.layout;
|
|
807
|
+
const x = (window?.x ?? 0) + input.at.x;
|
|
808
|
+
const dial = dials.findIndex((r) => x >= r.x && x < r.x + r.width);
|
|
809
|
+
if (dial >= 0) this.emit("dialTap", dial, input.long);
|
|
810
|
+
break;
|
|
811
|
+
}
|
|
812
|
+
case "swipe":
|
|
813
|
+
this.emit("windowSwipe", input.from, input.to);
|
|
814
|
+
break;
|
|
690
815
|
}
|
|
691
|
-
this.#pressed = states;
|
|
692
816
|
}
|
|
693
817
|
}
|
|
818
|
+
|
|
819
|
+
/** Calls `changed` for everything that was pressed or released. */
|
|
820
|
+
function changes(
|
|
821
|
+
before: readonly boolean[],
|
|
822
|
+
now: readonly boolean[],
|
|
823
|
+
changed: (index: number, down: boolean) => void,
|
|
824
|
+
): void {
|
|
825
|
+
now.forEach((down, i) => {
|
|
826
|
+
if (down !== (before[i] === true)) changed(i, down);
|
|
827
|
+
});
|
|
828
|
+
}
|
package/src/detect.ts
CHANGED
|
@@ -150,9 +150,8 @@ export async function openDeck<D extends StreamDeck>(
|
|
|
150
150
|
await sleep(Math.min(pollIntervalMs, remaining), signal);
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
.join(" or ");
|
|
153
|
+
const all = asArray(drivers).map(({ model }) => model.name);
|
|
154
|
+
const names = all.length > 3 ? "deck" : all.join(" or ");
|
|
156
155
|
const filtered = options.path !== undefined || options.serialNumber !== undefined;
|
|
157
156
|
throw new Error(`No ${names} found${filtered ? " matching the given path/serialNumber" : ""}`, {
|
|
158
157
|
cause: lastError,
|
package/src/image.ts
CHANGED
|
@@ -69,9 +69,12 @@ export async function prepareImage(
|
|
|
69
69
|
.flatten({ background });
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/** Degrees clockwise. */
|
|
73
|
+
export type Rotation = 0 | 90 | 180 | 270;
|
|
74
|
+
|
|
72
75
|
export interface DeviceImageOptions extends ImageOptions {
|
|
73
|
-
/** Rotation the device expects (default 0).
|
|
74
|
-
rotation?:
|
|
76
|
+
/** Rotation the device expects (default 0). Most Stream Decks need 180. */
|
|
77
|
+
rotation?: Rotation;
|
|
75
78
|
}
|
|
76
79
|
|
|
77
80
|
/**
|
|
@@ -125,7 +128,7 @@ export async function encodeJpeg(
|
|
|
125
128
|
): Promise<Uint8Array> {
|
|
126
129
|
const { quality = 90, rotation = 0 } = options;
|
|
127
130
|
let pipeline = sharp(rgb, { raw: { width: size.width, height: size.height, channels: 3 } });
|
|
128
|
-
if (rotation
|
|
131
|
+
if (rotation !== 0) pipeline = pipeline.rotate(rotation);
|
|
129
132
|
const jpeg = await pipeline
|
|
130
133
|
.jpeg({ quality, progressive: false, chromaSubsampling: "4:4:4" })
|
|
131
134
|
.toBuffer();
|
package/src/layout.ts
CHANGED
|
@@ -14,6 +14,26 @@ export interface DeckLayout {
|
|
|
14
14
|
sensors: readonly Rect[];
|
|
15
15
|
/** A separate wide display area, if the model has one. */
|
|
16
16
|
window?: Rect | undefined;
|
|
17
|
+
/** The part of the window that belongs to each dial, if the model has any. They fill it. */
|
|
18
|
+
dials?: readonly Rect[] | undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Where a slot is on the screen, if the deck has it. */
|
|
22
|
+
export function slotRect(
|
|
23
|
+
layout: DeckLayout,
|
|
24
|
+
kind: "key" | "sensor" | "dial" | "window",
|
|
25
|
+
index = 0,
|
|
26
|
+
): Rect | undefined {
|
|
27
|
+
switch (kind) {
|
|
28
|
+
case "key":
|
|
29
|
+
return layout.keys[index];
|
|
30
|
+
case "sensor":
|
|
31
|
+
return layout.sensors[index];
|
|
32
|
+
case "dial":
|
|
33
|
+
return layout.dials?.[index];
|
|
34
|
+
case "window":
|
|
35
|
+
return layout.window;
|
|
36
|
+
}
|
|
17
37
|
}
|
|
18
38
|
|
|
19
39
|
/** What to draw where. Anything left out is filled with the background colour. */
|
package/src/manager.ts
CHANGED
|
@@ -86,7 +86,7 @@ export function watchDeck(deck: StreamDeck, options: WatchOptions = {}): DeckWat
|
|
|
86
86
|
export type DeckStatus = "waiting" | "connected" | "stopped";
|
|
87
87
|
|
|
88
88
|
export interface ManageDeckOptions<D extends StreamDeck> extends WatchOptions {
|
|
89
|
-
/** Waits for and opens a deck, e.g. `(signal) =>
|
|
89
|
+
/** Waits for and opens a deck, e.g. `(signal) => openStreamDeck({ signal })`. */
|
|
90
90
|
open(signal: AbortSignal): Promise<D>;
|
|
91
91
|
/**
|
|
92
92
|
* Runs after every (re)connect to put things on the deck. May return a cleanup, run when the
|
|
@@ -125,7 +125,7 @@ export interface DeckHandle<D extends StreamDeck> {
|
|
|
125
125
|
*
|
|
126
126
|
* ```ts
|
|
127
127
|
* const handle = manageDeck({
|
|
128
|
-
* open: (signal) =>
|
|
128
|
+
* open: (signal) => openStreamDeck({ signal }),
|
|
129
129
|
* setup: (deck) => deck.setBrightness(80),
|
|
130
130
|
* });
|
|
131
131
|
* process.once("SIGINT", () => handle.stop());
|
package/src/virtual.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* phone showing a deck). Draw on it as usual, read the result with `deck.readScreen()`, and
|
|
4
4
|
* drive its input with `press`, `touch`, ...
|
|
5
5
|
*/
|
|
6
|
-
import { type DeckModel, StreamDeck } from "./deck.ts";
|
|
6
|
+
import { type DeckInput, type DeckModel, type Point, StreamDeck } from "./deck.ts";
|
|
7
7
|
import { createMemoryTransport, type MemoryTransport } from "./memory.ts";
|
|
8
8
|
|
|
9
9
|
export type VirtualDeckModel = Pick<DeckModel, "layout"> &
|
|
@@ -15,6 +15,13 @@ export interface VirtualDeck {
|
|
|
15
15
|
release(key: number): void;
|
|
16
16
|
touch(sensor: number): void;
|
|
17
17
|
untouch(sensor: number): void;
|
|
18
|
+
pressDial(dial: number): void;
|
|
19
|
+
releaseDial(dial: number): void;
|
|
20
|
+
/** Turns a dial by this many steps, clockwise being positive. */
|
|
21
|
+
turnDial(dial: number, ticks: number): void;
|
|
22
|
+
/** Taps the window, or holds it for a moment. */
|
|
23
|
+
tapWindow(at: Point, long?: boolean): void;
|
|
24
|
+
swipeWindow(from: Point, to: Point): void;
|
|
18
25
|
/** Simulates unplugging the device. */
|
|
19
26
|
unplug(): void;
|
|
20
27
|
}
|
|
@@ -27,16 +34,16 @@ class SoftwareDeck extends StreamDeck {
|
|
|
27
34
|
this.#serial = serial;
|
|
28
35
|
}
|
|
29
36
|
|
|
30
|
-
/**
|
|
31
|
-
protected parseInput(data: Uint8Array):
|
|
32
|
-
return
|
|
37
|
+
/** There is no device to have a format of its own: reports are the input as JSON. */
|
|
38
|
+
protected parseInput(data: Uint8Array): DeckInput {
|
|
39
|
+
return JSON.parse(new TextDecoder().decode(data)) as DeckInput;
|
|
33
40
|
}
|
|
34
41
|
|
|
35
42
|
// The screen is read straight from the framebuffer, so nothing needs sending.
|
|
36
|
-
protected async
|
|
37
|
-
protected async
|
|
38
|
-
protected override async
|
|
39
|
-
protected override async
|
|
43
|
+
protected async sendKeyImage(): Promise<void> {}
|
|
44
|
+
protected async sendScreenImage(): Promise<void> {}
|
|
45
|
+
protected override async sendWindowImage(): Promise<void> {}
|
|
46
|
+
protected override async sendWindowRegionImage(): Promise<void> {}
|
|
40
47
|
protected override async sendFillKey(): Promise<void> {}
|
|
41
48
|
protected override async sendFillScreen(): Promise<void> {}
|
|
42
49
|
|
|
@@ -61,24 +68,46 @@ export function createVirtualDeck(
|
|
|
61
68
|
...model,
|
|
62
69
|
};
|
|
63
70
|
const deck = new SoftwareDeck(transport, full, options.serialNumber ?? "VIRTUAL");
|
|
64
|
-
const
|
|
65
|
-
|
|
71
|
+
const send = (input: DeckInput) =>
|
|
72
|
+
transport.input(new TextEncoder().encode(JSON.stringify(input)));
|
|
73
|
+
return { deck, ...inputDriver(deck, send), unplug: () => transport.fail() };
|
|
74
|
+
}
|
|
66
75
|
|
|
67
|
-
|
|
68
|
-
|
|
76
|
+
/** Drives a deck's input by reporting the state of everything on it after each change. */
|
|
77
|
+
export function inputDriver(
|
|
78
|
+
deck: StreamDeck,
|
|
79
|
+
send: (input: DeckInput) => void,
|
|
80
|
+
): Omit<VirtualDeck, "deck" | "unplug"> {
|
|
81
|
+
const { keyCount, sensorCount, dialCount } = deck;
|
|
82
|
+
const buttons = new Array<boolean>(keyCount + sensorCount).fill(false);
|
|
83
|
+
const dials = new Array<boolean>(dialCount).fill(false);
|
|
84
|
+
const check = (kind: string, index: number, count: number) => {
|
|
69
85
|
if (!Number.isInteger(index) || index < 0 || index >= count) {
|
|
70
86
|
throw new RangeError(`there is no ${kind} ${index}`);
|
|
71
87
|
}
|
|
72
|
-
states[kind === "key" ? index : keys.length + index] = pressed ? 1 : 0;
|
|
73
|
-
transport.input(states.slice());
|
|
74
88
|
};
|
|
75
|
-
|
|
89
|
+
const button = (kind: "key" | "sensor", index: number, down: boolean) => {
|
|
90
|
+
check(kind, index, kind === "key" ? keyCount : sensorCount);
|
|
91
|
+
buttons[kind === "key" ? index : keyCount + index] = down;
|
|
92
|
+
send({ type: "buttons", pressed: [...buttons] });
|
|
93
|
+
};
|
|
94
|
+
const dial = (index: number, down: boolean) => {
|
|
95
|
+
check("dial", index, dialCount);
|
|
96
|
+
dials[index] = down;
|
|
97
|
+
send({ type: "dials", pressed: [...dials] });
|
|
98
|
+
};
|
|
76
99
|
return {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
100
|
+
press: (index) => button("key", index, true),
|
|
101
|
+
release: (index) => button("key", index, false),
|
|
102
|
+
touch: (index) => button("sensor", index, true),
|
|
103
|
+
untouch: (index) => button("sensor", index, false),
|
|
104
|
+
pressDial: (index) => dial(index, true),
|
|
105
|
+
releaseDial: (index) => dial(index, false),
|
|
106
|
+
turnDial(index, ticks) {
|
|
107
|
+
check("dial", index, dialCount);
|
|
108
|
+
send({ type: "turn", ticks: dials.map((_, i) => (i === index ? ticks : 0)) });
|
|
109
|
+
},
|
|
110
|
+
tapWindow: (at, long = false) => send({ type: "tap", at, long }),
|
|
111
|
+
swipeWindow: (from, to) => send({ type: "swipe", from, to }),
|
|
83
112
|
};
|
|
84
113
|
}
|