Skip to content

Commit

Permalink
Handle trackers/button presses individually
Browse files Browse the repository at this point in the history
JovannMC committed May 13, 2024
1 parent 1e375e0 commit c4ed260
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -524,31 +524,37 @@ function startDeviceListeners() {
mainWindow.webContents.send("device-mag", { trackerName, magStatus });
});

let clickCount = 0;
let clickTimeout: NodeJS.Timeout = null;
let clickCounts: { [key: string]: number } = {};
let clickTimeouts: { [key: string]: NodeJS.Timeout } = {};

device.on("button", (trackerName, buttonPressed, isOn) => {
if (!isOn || !buttonPressed) return;

clickCount++;
if (clickTimeout !== null) clearTimeout(clickTimeout);
let key = `${trackerName}-${buttonPressed}`;

clickTimeout = setTimeout(() => {
if (clickCount === 1) {
if (!clickCounts[key]) {
clickCounts[key] = 0;
}

clickCounts[key]++;
if (clickTimeouts[key] !== undefined) clearTimeout(clickTimeouts[key]);

clickTimeouts[key] = setTimeout(() => {
if (clickCounts[key] === 1) {
log(`Single click ${buttonPressed} button from ${trackerName}`);
sendYawReset();
} else if (clickCount === 2) {
} else if (clickCounts[key] === 2) {
log(`Double click ${buttonPressed} button from ${trackerName}`);
sendFullReset();
} else if (clickCount === 3) {
} else if (clickCounts[key] === 3) {
log(`Triple click ${buttonPressed} button from ${trackerName}`);
sendMountingReset();
} else {
log(`Four click ${buttonPressed} button from ${trackerName}`);
sendPauseTracking();
}

clickCount = 0;
clickCounts[key] = 0;
}, 500);
});

0 comments on commit c4ed260

Please sign in to comment.