@sdeck/core 0.4.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 +78 -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 +166 -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 +187 -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,6 +60,18 @@ 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];
|
|
46
77
|
/** The deck is closing. Keys held down get no release. */
|
|
@@ -50,14 +81,17 @@ export interface DeckEvents {
|
|
|
50
81
|
|
|
51
82
|
type Listener<K extends keyof DeckEvents> = (...args: DeckEvents[K]) => void;
|
|
52
83
|
|
|
53
|
-
/** Options for `mountKey`, `mountSensor`, `mountWindow` and `mountScreen`. */
|
|
84
|
+
/** Options for `mountKey`, `mountSensor`, `mountDial`, `mountWindow` and `mountScreen`. */
|
|
54
85
|
export type MountOptions = ElementOptions & Pick<ImageOptions, "quality" | "background">;
|
|
55
86
|
|
|
56
87
|
export const toError = (error: unknown): Error =>
|
|
57
88
|
error instanceof Error ? error : new Error(String(error));
|
|
58
89
|
|
|
59
|
-
/**
|
|
60
|
-
|
|
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";
|
|
61
95
|
|
|
62
96
|
interface PendingFrame {
|
|
63
97
|
quality?: number | undefined;
|
|
@@ -84,12 +118,19 @@ export abstract class StreamDeck {
|
|
|
84
118
|
keyUp: new Set(),
|
|
85
119
|
sensorDown: new Set(),
|
|
86
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(),
|
|
87
127
|
draw: new Set(),
|
|
88
128
|
close: new Set(),
|
|
89
129
|
error: new Set(),
|
|
90
130
|
};
|
|
91
131
|
readonly #unsubscribe: Array<() => void>;
|
|
92
|
-
#pressed: boolean[] = [];
|
|
132
|
+
#pressed: readonly boolean[] = [];
|
|
133
|
+
#dials: readonly boolean[] = [];
|
|
93
134
|
#closed = false;
|
|
94
135
|
#roots = new Set<ElementRoot>();
|
|
95
136
|
/** Serialises writes so multi-packet image uploads never interleave. */
|
|
@@ -98,6 +139,7 @@ export abstract class StreamDeck {
|
|
|
98
139
|
/** Upright pixels as they are (or, once queued writes finish, will be) on the LCD. */
|
|
99
140
|
readonly #screen: Framebuffer;
|
|
100
141
|
readonly #allSlots: readonly Slot[];
|
|
142
|
+
readonly #windowSlots: readonly Slot[];
|
|
101
143
|
/** Slots whose device pixels are known to match the framebuffer. */
|
|
102
144
|
readonly #known = new Set<Slot>();
|
|
103
145
|
/** Bumped whenever a slot's framebuffer pixels change. */
|
|
@@ -112,10 +154,12 @@ export abstract class StreamDeck {
|
|
|
112
154
|
this.transport = transport;
|
|
113
155
|
this.model = model;
|
|
114
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"];
|
|
115
159
|
this.#allSlots = [
|
|
116
160
|
...model.layout.keys.map((_, i): Slot => `key:${i}`),
|
|
117
161
|
...model.layout.sensors.map((_, i): Slot => `sensor:${i}`),
|
|
118
|
-
...(model.layout.window ?
|
|
162
|
+
...(model.layout.window ? this.#windowSlots : []),
|
|
119
163
|
"rest",
|
|
120
164
|
];
|
|
121
165
|
this.#unsubscribe = [
|
|
@@ -132,12 +176,19 @@ export abstract class StreamDeck {
|
|
|
132
176
|
get sensorCount(): number {
|
|
133
177
|
return this.model.layout.sensors.length;
|
|
134
178
|
}
|
|
179
|
+
get dialCount(): number {
|
|
180
|
+
return this.model.layout.dials?.length ?? 0;
|
|
181
|
+
}
|
|
135
182
|
get keySize(): Size {
|
|
136
183
|
return this.#size(this.model.layout.keys[0], "keys");
|
|
137
184
|
}
|
|
138
185
|
get sensorSize(): Size {
|
|
139
186
|
return this.#size(this.model.layout.sensors[0], "sensor strips");
|
|
140
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
|
+
}
|
|
141
192
|
get windowSize(): Size {
|
|
142
193
|
return this.#size(this.model.layout.window, "a window");
|
|
143
194
|
}
|
|
@@ -152,10 +203,12 @@ export abstract class StreamDeck {
|
|
|
152
203
|
* How a live element on a slot is drawn: `size` is the room it lays out in, and `scale` how
|
|
153
204
|
* many times larger that appears (see {@link DeckModel.design}).
|
|
154
205
|
*/
|
|
155
|
-
slotDrawing(
|
|
206
|
+
slotDrawing(
|
|
207
|
+
kind: "key" | "sensor" | "dial" | "window",
|
|
208
|
+
index = 0,
|
|
209
|
+
): { size: Size; scale: number } {
|
|
156
210
|
const { layout, design } = this.model;
|
|
157
|
-
const rect =
|
|
158
|
-
kind === "window" ? layout.window : (kind === "key" ? layout.keys : layout.sensors)[index];
|
|
211
|
+
const rect = slotRect(layout, kind, index);
|
|
159
212
|
const size = this.#size(rect, kind === "window" ? "a window" : `${kind} ${index}`);
|
|
160
213
|
const base = design?.[kind];
|
|
161
214
|
if (!base) return { size, scale: 1 };
|
|
@@ -209,26 +262,41 @@ export abstract class StreamDeck {
|
|
|
209
262
|
// The `send*` methods are called inside the write queue, in order; they write straight to the
|
|
210
263
|
// transport and must not enqueue.
|
|
211
264
|
|
|
212
|
-
/**
|
|
213
|
-
protected abstract parseInput(data: Uint8Array):
|
|
214
|
-
protected abstract
|
|
215
|
-
protected abstract
|
|
216
|
-
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> {
|
|
217
270
|
return Promise.reject(new Error(`${this.model.name} has no window`));
|
|
218
271
|
}
|
|
219
|
-
/** Sends
|
|
220
|
-
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> {
|
|
221
274
|
return Promise.reject(new Error(`${this.model.name} cannot update part of the window`));
|
|
222
275
|
}
|
|
223
276
|
/** Fills a key with a colour. Override if the model has a command for it. */
|
|
224
277
|
protected async sendFillKey(index: number, color: RGB): Promise<void> {
|
|
225
278
|
const size = this.#size(this.model.layout.keys[index], "keys");
|
|
226
|
-
await this.
|
|
279
|
+
await this.sendKeyImage(index, await this.encodeImage(solidRgb(size, color), size));
|
|
227
280
|
}
|
|
228
281
|
/** Fills the screen with a colour. Override if the model has a command for it. */
|
|
229
282
|
protected async sendFillScreen(color: RGB): Promise<void> {
|
|
230
283
|
const size = this.screenSize;
|
|
231
|
-
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);
|
|
232
300
|
}
|
|
233
301
|
/** Runs (queued, before the transport closes) when `close()` is called, e.g. to show a logo. */
|
|
234
302
|
protected async onClosing(): Promise<void> {}
|
|
@@ -260,20 +328,36 @@ export abstract class StreamDeck {
|
|
|
260
328
|
const rgb = await toRgb(image, rect, options);
|
|
261
329
|
if (!current()) return;
|
|
262
330
|
await this.#commit([slot], rect, rgb, () => {
|
|
263
|
-
const
|
|
264
|
-
return async () => this.
|
|
331
|
+
const encoded = this.#encode(rgb, rect, options?.quality);
|
|
332
|
+
return async () => this.sendKeyImage(index, await encoded);
|
|
265
333
|
});
|
|
266
334
|
}
|
|
267
335
|
|
|
268
336
|
/** Sets the window image (scaled to the window size). */
|
|
269
337
|
async setWindowImage(image: ImageSource, options?: ImageOptions): Promise<void> {
|
|
270
338
|
const rect = this.#windowRect();
|
|
271
|
-
const current = this.#claim(
|
|
339
|
+
const current = this.#claim(this.#windowSlots);
|
|
272
340
|
const rgb = await toRgb(image, rect, options);
|
|
273
341
|
if (!current()) return;
|
|
274
|
-
await this.#commit(
|
|
275
|
-
const
|
|
276
|
-
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);
|
|
277
361
|
});
|
|
278
362
|
}
|
|
279
363
|
|
|
@@ -288,9 +372,9 @@ export abstract class StreamDeck {
|
|
|
288
372
|
): Promise<void> {
|
|
289
373
|
const rect = this.#windowRegion(region);
|
|
290
374
|
const rgb = await toRgb(image, rect, options);
|
|
291
|
-
await this.#commit(
|
|
292
|
-
const
|
|
293
|
-
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);
|
|
294
378
|
});
|
|
295
379
|
}
|
|
296
380
|
|
|
@@ -354,30 +438,30 @@ export abstract class StreamDeck {
|
|
|
354
438
|
);
|
|
355
439
|
}
|
|
356
440
|
|
|
357
|
-
// Low-level variants: send a JPEG that is already the exact size and device rotation
|
|
358
|
-
// 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
|
|
359
443
|
// what was last drawn there through the other methods.
|
|
360
444
|
|
|
361
445
|
/** Sends a JPEG that is already key-sized (and rotated as the device needs) to a key. */
|
|
362
446
|
setKeyJpeg(index: number, jpeg: Uint8Array): Promise<void> {
|
|
363
447
|
this.assertKey(index);
|
|
364
|
-
return this.#raw([`key:${index}`], () => this.
|
|
448
|
+
return this.#raw([`key:${index}`], () => this.sendKeyImage(index, jpeg));
|
|
365
449
|
}
|
|
366
450
|
|
|
367
451
|
/** Sends a screen-sized, device-rotated JPEG to the LCD as-is. */
|
|
368
452
|
setScreenJpeg(jpeg: Uint8Array): Promise<void> {
|
|
369
|
-
return this.#raw(this.#allSlots, () => this.
|
|
453
|
+
return this.#raw(this.#allSlots, () => this.sendScreenImage(jpeg));
|
|
370
454
|
}
|
|
371
455
|
|
|
372
456
|
/** Sends a window-sized, device-rotated JPEG to the window as-is. */
|
|
373
457
|
setWindowJpeg(jpeg: Uint8Array): Promise<void> {
|
|
374
|
-
return this.#raw(
|
|
458
|
+
return this.#raw(this.#windowSlots, () => this.sendWindowImage(jpeg));
|
|
375
459
|
}
|
|
376
460
|
|
|
377
461
|
/** Sends a `region.width`×`region.height`, device-rotated JPEG to part of the window as-is. */
|
|
378
462
|
setWindowRegionJpeg(region: Rect, jpeg: Uint8Array): Promise<void> {
|
|
379
463
|
this.#windowRegion(region);
|
|
380
|
-
return this.#raw(
|
|
464
|
+
return this.#raw(this.#windowSlots, () => this.sendWindowRegionImage(region, jpeg));
|
|
381
465
|
}
|
|
382
466
|
|
|
383
467
|
// -- Reading back ------------------------------------------------------------
|
|
@@ -399,9 +483,9 @@ export abstract class StreamDeck {
|
|
|
399
483
|
// -- Drawing internals -------------------------------------------------------
|
|
400
484
|
|
|
401
485
|
#encode(rgb: Uint8Array, size: Size, quality?: number): Promise<Uint8Array> {
|
|
402
|
-
const
|
|
403
|
-
|
|
404
|
-
return
|
|
486
|
+
const image = this.encodeImage(rgb, size, quality);
|
|
487
|
+
image.catch(() => {}); // awaited inside the queue; never an unhandled rejection
|
|
488
|
+
return image;
|
|
405
489
|
}
|
|
406
490
|
|
|
407
491
|
#windowRegion(region: Rect): Rect {
|
|
@@ -516,8 +600,7 @@ export abstract class StreamDeck {
|
|
|
516
600
|
const frame = this.#frame;
|
|
517
601
|
this.#frame = undefined;
|
|
518
602
|
const gens = this.#gens(this.#allSlots);
|
|
519
|
-
const
|
|
520
|
-
const write = this.enqueue(async () => this.sendScreenJpeg(await jpeg));
|
|
603
|
+
const write = this.enqueue(this.prepareFrame(this.#screen, frame.quality));
|
|
521
604
|
try {
|
|
522
605
|
await this.#settle(write, gens, this.#screenRect);
|
|
523
606
|
for (const waiter of frame.waiters) waiter.resolve();
|
|
@@ -564,6 +647,18 @@ export abstract class StreamDeck {
|
|
|
564
647
|
);
|
|
565
648
|
}
|
|
566
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
|
+
|
|
567
662
|
/** Mounts a live element tree on the window. See `mountKey`. */
|
|
568
663
|
mountWindow(element: Renderable, options?: MountOptions): Promise<ElementRoot> {
|
|
569
664
|
return this.#mount(
|
|
@@ -591,7 +686,7 @@ export abstract class StreamDeck {
|
|
|
591
686
|
}
|
|
592
687
|
|
|
593
688
|
async #mount(
|
|
594
|
-
kind: "key" | "sensor" | "window" | "screen",
|
|
689
|
+
kind: "key" | "sensor" | "dial" | "window" | "screen",
|
|
595
690
|
index: number,
|
|
596
691
|
send: (frame: RawImage) => Promise<void>,
|
|
597
692
|
element: Renderable,
|
|
@@ -665,6 +760,12 @@ export abstract class StreamDeck {
|
|
|
665
760
|
}
|
|
666
761
|
}
|
|
667
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
|
+
|
|
668
769
|
/** Runs `task` after every earlier write, so multi-packet uploads never interleave. */
|
|
669
770
|
protected enqueue<T>(task: () => Promise<T>): Promise<T> {
|
|
670
771
|
if (this.#closed) return Promise.reject(new Error(`${this.model.name} is closed`));
|
|
@@ -678,20 +779,50 @@ export abstract class StreamDeck {
|
|
|
678
779
|
}
|
|
679
780
|
|
|
680
781
|
#handleInput(data: Uint8Array): void {
|
|
681
|
-
const
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
} else {
|
|
692
|
-
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;
|
|
693
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;
|
|
694
815
|
}
|
|
695
|
-
this.#pressed = states;
|
|
696
816
|
}
|
|
697
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
|
}
|