UNPKG

@elgato-stream-deck/core

Version:

An npm module for interfacing with the Elgato Stream Deck

101 lines 4.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Gen2InputService = void 0; const gen1_js_1 = require("./gen1.js"); const util_js_1 = require("../../util.js"); class Gen2InputService extends gen1_js_1.ButtonOnlyInputService { #eventSource; #encoderControls; #encoderState; #lcdSegmentControls; constructor(deviceProperties, eventSource) { super(deviceProperties, eventSource); this.#eventSource = eventSource; this.#encoderControls = deviceProperties.CONTROLS.filter((control) => control.type === 'encoder'); const maxIndex = Math.max(-1, ...this.#encoderControls.map((control) => control.index)); this.#encoderState = new Array(maxIndex + 1).fill(false); this.#lcdSegmentControls = deviceProperties.CONTROLS.filter((control) => control.type === 'lcd-segment'); } handleInput(data) { const inputType = data[0]; switch (inputType) { case 0x00: // Button super.handleInput(data); break; case 0x02: // LCD this.#handleLcdSegmentInput(data); break; case 0x03: // Encoder this.#handleEncoderInput(data); break; case 0x04: // NFC this.#handleNfcRead(data); break; } } #handleLcdSegmentInput(data) { // Future: This will need to handle selecting the correct control const lcdSegmentControl = this.#lcdSegmentControls.find((control) => control.id === 0); if (!lcdSegmentControl) return; const bufferView = (0, util_js_1.uint8ArrayToDataView)(data); const position = { x: bufferView.getUint16(5, true), y: bufferView.getUint16(7, true), }; switch (data[3]) { case 1: // short press this.#eventSource.emit('lcdShortPress', lcdSegmentControl, position); break; case 2: // long press this.#eventSource.emit('lcdLongPress', lcdSegmentControl, position); break; case 3: { // swipe const positionTo = { x: bufferView.getUint16(9, true), y: bufferView.getUint16(11, true), }; this.#eventSource.emit('lcdSwipe', lcdSegmentControl, position, positionTo); break; } } } #handleEncoderInput(data) { switch (data[3]) { case 0x00: // press/release for (const encoderControl of this.#encoderControls) { const keyPressed = Boolean(data[4 + encoderControl.hidIndex]); const stateChanged = keyPressed !== this.#encoderState[encoderControl.index]; if (stateChanged) { this.#encoderState[encoderControl.index] = keyPressed; if (keyPressed) { this.#eventSource.emit('down', encoderControl); } else { this.#eventSource.emit('up', encoderControl); } } } break; case 0x01: // rotate for (const encoderControl of this.#encoderControls) { const intArray = new Int8Array(data.buffer, data.byteOffset, data.byteLength); const value = intArray[4 + encoderControl.hidIndex]; if (value !== 0) { this.#eventSource.emit('rotate', encoderControl, value); } } break; } } #handleNfcRead(data) { if (!this.deviceProperties.HAS_NFC_READER) return; const length = data[1] + data[2] * 256; const id = new TextDecoder('ascii').decode(data.subarray(3, 3 + length)); this.#eventSource.emit('nfcRead', id); } } exports.Gen2InputService = Gen2InputService; //# sourceMappingURL=gen2.js.map