aboutsummaryrefslogtreecommitdiff
path: root/ctrl.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'ctrl.mjs')
-rwxr-xr-xctrl.mjs186
1 files changed, 98 insertions, 88 deletions
diff --git a/ctrl.mjs b/ctrl.mjs
index bcab1c5..1773cfb 100755
--- a/ctrl.mjs
+++ b/ctrl.mjs
@@ -1,14 +1,13 @@
#!/usr/bin/env node
-import { registerFont } from 'canvas';
-registerFont('./ocr-a-ext.ttf', { family: 'ocr' });
+import { registerFont } from "canvas";
-import { discover } from 'loupedeck'
+registerFont("./ocr-a-ext.ttf", { family: "ocr" });
-import { readFile } from 'fs/promises';
-import { parse } from 'yaml'
-import { sendCommand } from './xplane.mjs';
-import { isNumberObject } from 'util/types';
+import { discover, HAPTIC } from "loupedeck";
+import { readFile } from "fs/promises";
+import { parse } from "yaml";
+import { sendCommand } from "./xplane.mjs";
const pages = parse(await readFile("./profile.yaml", "utf8"));
@@ -17,16 +16,18 @@ let currentPage;
let pressed = new Set();
let highlighted = new Set();
-// Detects and opens first connected device
-const device = await discover()
+// detects and opens first connected device
+const device = await discover();
-const isNumber = (x) => { return !isNaN(x); }
+const isNumber = (x) => {
+ return !isNaN(x);
+};
const isObject = (obj) => {
- return obj != null && obj.constructor.name === "Object"
+ return obj != null && obj.constructor.name === "Object";
};
-const doAction = (labeled, type) => {
+const takeAction = (labeled, type) => {
if (!isObject(labeled)) {
return;
}
@@ -34,7 +35,7 @@ const doAction = (labeled, type) => {
if (actionSpec === undefined) {
return;
}
- if (actionSpec.hasOwnProperty('xplane_cmd')) {
+ if (actionSpec.hasOwnProperty("xplane_cmd")) {
sendCommand(actionSpec.xplane_cmd);
}
};
@@ -44,14 +45,14 @@ const rectifyLabel = (label) => {
let font = "22px ocr";
if (isObject(label)) {
text = label.text;
- if (label.hasOwnProperty('size')) {
+ if (label.hasOwnProperty("size")) {
font = `${label.size}px ocr`;
}
} else {
text = label.toString();
}
- return { text, font }
-}
+ return { text, font };
+};
const drawKey = (key, label, down) => {
device.drawKey(key, (c) => {
@@ -68,25 +69,30 @@ const drawKey = (key, label, down) => {
c.strokeStyle = fg;
c.strokeRect(padding, padding, w - padding * 2, h - padding * 2);
c.font = font;
- const { width, actualBoundingBoxAscent, actualBoundingBoxDescent } = c.measureText(text);
+ const { width, actualBoundingBoxAscent, actualBoundingBoxDescent } =
+ c.measureText(text);
const x_axis = (w - width) / 2;
- const y_axis = h / 2 + (actualBoundingBoxAscent - actualBoundingBoxDescent) / 2;
+ const y_axis =
+ h / 2 + (actualBoundingBoxAscent - actualBoundingBoxDescent) / 2;
c.fillText(text, x_axis, y_axis);
- })
+ });
};
const drawSideKnobs = (side, labels, highlight) => {
device.drawScreen(side, (c) => {
+ const light = pages[currentPage].hasOwnProperty("color")
+ ? pages[currentPage].color
+ : "white";
if (!highlight) {
highlight = [false, false, false];
}
for (let i = 0; i < 3; i++) {
const hl = highlight[i];
- const y_offset = i * c.canvas.height / 3;
+ const y_offset = (i * c.canvas.height) / 3;
const x_padding = 8;
const y_padding = 3;
- const bg = hl ? "white" : "black";
- const fg = hl ? "black" : "white";
+ const bg = hl ? light : "black";
+ const fg = hl ? "black" : light;
const w = c.canvas.width;
const h = c.canvas.height / 3;
c.fillStyle = bg;
@@ -94,76 +100,60 @@ const drawSideKnobs = (side, labels, highlight) => {
c.fillStyle = fg;
c.lineWidth = 2;
c.strokeStyle = fg;
- c.strokeRect(x_padding, y_padding + y_offset, w - x_padding * 2, h - y_padding * 2);
+ c.strokeRect(
+ x_padding,
+ y_padding + y_offset,
+ w - x_padding * 2,
+ h - y_padding * 2,
+ );
const { text, font } = rectifyLabel(labels[i]);
c.font = font;
- const { width, actualBoundingBoxAscent, actualBoundingBoxDescent } = c.measureText(text);
+ const { width, actualBoundingBoxAscent, actualBoundingBoxDescent } =
+ c.measureText(text);
const x_axis = (h - width) / 2;
- const y_axis = w / 2 + (actualBoundingBoxAscent - actualBoundingBoxDescent) / 2;
- c.rotate(90 * Math.PI / 180);
+ const y_axis =
+ w / 2 +
+ (actualBoundingBoxAscent - actualBoundingBoxDescent) / 2;
+ c.rotate((90 * Math.PI) / 180);
+ c.fillStyle = hl ? 'black' : 'white';
c.fillText(text, x_axis + y_offset, -(w - y_axis));
c.resetTransform();
}
- })
+ });
};
const loadPage = (page) => {
const { left, right, keys } = page || {};
if (!left) {
- return
+ return;
}
- drawSideKnobs('left', left);
- drawSideKnobs('right', right);
+ drawSideKnobs("left", left);
+ drawSideKnobs("right", right);
for (let i = 0; i < 12; i++) {
drawKey(i, keys[i], false);
}
};
// Observe connect events
-device.on('connect', () => {
- console.info('Connection successful!')
- currentPage = 1;
- loadPage(pages[currentPage]);
-})
-
-// React to button presses
-device.on('down', ({ id }) => {
- if (isNumber(id)) {
- console.info(`switch to page: ${id}`)
- if (id == 0) {
- return
- }
- currentPage = id;
- loadPage(pages[currentPage]);
- } else {
- const { left, right, keys } = pages[currentPage] || {};
- if (!left) {
- return
- }
- let pos = {"T": 0, "C": 1, "B": 2}[id.substring(4, 5)];
- let side = {"L": ['left', left], "R": ['right', right]}[id.substring(5, 6)];
- let mask = [false, false, false];
- mask[pos] = true;
- drawSideKnobs(side[0], side[1], mask);
- if (!highlighted.has(id)) {
- highlighted.add(id);
- setTimeout(() => {
- drawSideKnobs(side[0], side[1], [false, false, false]);
- highlighted.delete(id);
- }, 200);
- }
- doAction(side[1][pos], 'pressed');
+device.on("connect", async () => {
+ console.info("connected");
+ currentPage = pages[0].hasOwnProperty('default') ? pages[0].default : 1;
+ for (let i = 0; i < pages.length; i++) {
+ const color = pages[i].hasOwnProperty("color")
+ ? pages[i].color
+ : "white";
+ await device.setButtonColor({ id: i, color: pages[i].color });
}
-})
+ loadPage(pages[currentPage]);
+});
-// React to knob turns
-device.on('rotate', ({ id, delta }) => {
+const handleKnobEvent = (id) => {
const { left, right, keys } = pages[currentPage] || {};
if (!left) {
- return
+ return;
}
- let pos = {"T": 0, "C": 1, "B": 2}[id.substring(4, 5)];
- let side = {"L": ['left', left], "R": ['right', right]}[id.substring(5, 6)];
+ let pos = { T: 0, C: 1, B: 2 }[id.substring(4, 5)];
+ let side = { L: ["left", left], R: ["right", right] }[id.substring(5, 6)];
let mask = [false, false, false];
mask[pos] = true;
drawSideKnobs(side[0], side[1], mask);
@@ -174,11 +164,32 @@ device.on('rotate', ({ id, delta }) => {
highlighted.delete(id);
}, 200);
}
- doAction(side[1][pos], delta > 0 ? 'inc' : 'dec');
-})
+ return side[1][pos];
+};
+
+// React to button presses
+device.on("down", ({ id }) => {
+ if (isNumber(id)) {
+ console.info(`switch to page: ${id}`);
+ if (id == 0) {
+ return;
+ }
+ currentPage = id;
+ loadPage(pages[currentPage]);
+ } else {
+ takeAction(handleKnobEvent(id), "pressed");
+ }
+});
+
+// React to knob turns
+device.on("rotate", ({ id, delta }) => {
+ takeAction(handleKnobEvent(id), delta > 0 ? "inc" : "dec");
+});
const clearStaleButton = (touches) => {
- const s = new Set(touches.map(o => o.target.key).filter(k => k !== undefined));
+ const s = new Set(
+ touches.map((o) => o.target.key).filter((k) => k !== undefined),
+ );
for (const key of pressed.keys()) {
if (!s.has(key)) {
drawKey(key, pages[currentPage].keys[key], false);
@@ -187,36 +198,35 @@ const clearStaleButton = (touches) => {
}
};
-device.on('touchstart', ({ changedTouches, touches }) => {
+device.on("touchstart", ({ changedTouches, touches }) => {
clearStaleButton(changedTouches);
const target = changedTouches[0].target;
if (target.key === undefined) {
- return
+ return;
}
pressed.add(target.key);
const key = pages[currentPage].keys[target.key];
drawKey(target.key, key, true);
- doAction(key, 'pressed');
- //device.vibrate()
-})
+ takeAction(key, "pressed");
+ device.vibrate(HAPTIC.REV_FASTEST);
+});
-device.on('touchmove', ({ changedTouches, touches }) => {
+device.on("touchmove", ({ changedTouches, touches }) => {
clearStaleButton(changedTouches);
-})
+});
-device.on('touchend', ({ changedTouches, touches }) => {
+device.on("touchend", ({ changedTouches, touches }) => {
clearStaleButton(changedTouches);
const target = changedTouches[0].target;
if (target.key === undefined) {
- return
+ return;
}
pressed.delete(target.key);
- drawKey(target.key, pages[currentPage].keys[target.key], false)
-})
+ drawKey(target.key, pages[currentPage].keys[target.key], false);
+});
-process.on('SIGINT', () => {
+process.on("SIGINT", () => {
device.close().then(() => {
- console.info("shutdown")
- process.exit()
- })
-})
+ process.exit();
+ });
+});