@blackmagic-controller/core
Version:
An npm module for interfacing with the Blackmagic usb/bluetooth controllers
100 lines • 3.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultInputService = void 0;
const util_js_1 = require("../../util.js");
class DefaultInputService {
// readonly #deviceProperties: Readonly<BlackmagicControllerProperties>
#eventSource;
#options;
#pushedButtons = new Set();
#buttonControlsByEncoded;
#buttonControlsById;
#tbarControl;
#jogControl;
constructor(deviceProperties, eventSource, options) {
// this.#deviceProperties = deviceProperties
this.#eventSource = eventSource;
this.#options = options;
this.#buttonControlsByEncoded = {};
this.#buttonControlsById = {};
for (const control of deviceProperties.CONTROLS) {
if (control.type === 'tbar' && !this.#tbarControl) {
this.#tbarControl = control;
}
if (control.type === 'jog' && !this.#jogControl) {
this.#jogControl = control;
}
if (control.type === 'button') {
this.#buttonControlsByEncoded[control.encodedIndex] = control;
this.#buttonControlsById[control.id] = control;
}
}
}
handleInput(data) {
const view = (0, util_js_1.uint8ArrayToDataView)(data);
switch (view.getUint8(0)) {
case this.#options.buttonReportId:
console.log('button presses', Buffer.from(data));
this.#handleButtonInput(view);
break;
case this.#options.tbarReportId:
this.#handleTBarInput(view);
break;
case this.#options.jogReportId:
this.#handleJogInput(view);
break;
case this.#options.batteryReportId:
this.#handleBatteryLevel(view);
break;
}
}
#handleButtonInput(view) {
const pushedControls = [];
const pushedControlIds = new Set();
for (let i = 1; i < view.byteLength; i += 2) {
const value = view.getUint16(i, true);
if (value === 0)
break;
const control = this.#buttonControlsByEncoded[value];
if (!control)
continue;
pushedControlIds.add(control.id);
pushedControls.push(control);
}
// Check for key ups
for (const keyId of this.#pushedButtons) {
// Check if still pressed
if (pushedControlIds.has(keyId))
continue;
const control = this.#buttonControlsById[keyId];
if (control)
this.#eventSource.emit('up', control);
this.#pushedButtons.delete(keyId);
}
for (const control of pushedControls) {
// Check if already pressed
if (this.#pushedButtons.has(control.id))
continue;
this.#pushedButtons.add(control.id);
this.#eventSource.emit('down', control);
}
}
#handleTBarInput(view) {
if (!this.#tbarControl)
return;
const value = view.getUint16(1, true);
this.#eventSource.emit('tbar', this.#tbarControl, value / 4096);
}
#handleJogInput(view) {
if (!this.#jogControl)
return;
const value = view.getInt32(2, true);
this.#eventSource.emit('jog', this.#jogControl, value); // TODO - some scaling
}
#handleBatteryLevel(view) {
const value = view.getUint8(2); // TODO - test this
this.#eventSource.emit('batteryLevel', value / 100);
}
}
exports.DefaultInputService = DefaultInputService;
//# sourceMappingURL=default.js.map