@sdeck/core 0.4.0 → 0.5.1

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/src/layout.ts CHANGED
@@ -14,9 +14,33 @@ 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
+ /** More windows beside the first, numbered from 1. Only screens have them. */
20
+ windows?: readonly Rect[] | undefined;
21
+ /** The dials have places of their own rather than sharing out the window. */
22
+ separateDials?: boolean | undefined;
17
23
  }
18
24
 
19
- /** What to draw where. Anything left out is filled with the background colour. */
25
+ /** Where a slot is on the screen, if the deck has it. */
26
+ export function slotRect(
27
+ layout: DeckLayout,
28
+ kind: "key" | "sensor" | "dial" | "window",
29
+ index = 0,
30
+ ): Rect | undefined {
31
+ switch (kind) {
32
+ case "key":
33
+ return layout.keys[index];
34
+ case "sensor":
35
+ return layout.sensors[index];
36
+ case "dial":
37
+ return layout.dials?.[index];
38
+ case "window":
39
+ return index > 0 ? layout.windows?.[index - 1] : layout.window;
40
+ }
41
+ }
42
+
43
+ /** What to draw where. Anything left out is filled with the background color. */
20
44
  export interface ScreenLayout {
21
45
  /** Images by key index. Use `undefined` to skip a key. */
22
46
  keys?: ReadonlyArray<ImageSource | undefined>;
@@ -55,7 +79,7 @@ export function placements(
55
79
  return out;
56
80
  }
57
81
 
58
- /** Upright RGB pixels of `size` in one colour (any CSS colour sharp understands). */
82
+ /** Upright RGB pixels of `size` in one color (any CSS color sharp understands). */
59
83
  async function background(size: Size, color: string): Promise<Uint8Array> {
60
84
  const { data } = await sharp({ create: { ...size, channels: 3, background: color } })
61
85
  .raw()
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) => openStreamDeckNeo({ 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) => openStreamDeckNeo({ 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
- /** One byte per input, keys then sensors. */
31
- protected parseInput(data: Uint8Array): boolean[] {
32
- return Array.from(data, (byte) => byte === 1);
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 sendKeyJpeg(): Promise<void> {}
37
- protected async sendScreenJpeg(): Promise<void> {}
38
- protected override async sendWindowJpeg(): Promise<void> {}
39
- protected override async sendWindowRegionJpeg(): Promise<void> {}
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 { keys, sensors } = full.layout;
65
- const states = new Uint8Array(keys.length + sensors.length);
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
- const set = (kind: "key" | "sensor", index: number, pressed: boolean) => {
68
- const count = kind === "key" ? keys.length : sensors.length;
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
- deck,
78
- press: (index) => set("key", index, true),
79
- release: (index) => set("key", index, false),
80
- touch: (index) => set("sensor", index, true),
81
- untouch: (index) => set("sensor", index, false),
82
- unplug: () => transport.fail(),
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
  }