Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions joycon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// joycon.js
const EventEmitter = require('events');
const HID = require('node-hid');

class JoyConModule extends EventEmitter {
constructor() {
super();
this.device = null;
this.gravity = { x: 0, y: 0, z: 0 };
this.filterCoeff = 0.8;
this.lastAState = false;
this._processData = this._processData.bind(this);
}

startListening() {
try {
// nintendo vendor id
const devices = HID.devices().filter(d => d.vendorId === 1406);
if (devices.length === 0) {
return false;
}

this.device = new HID.HID(devices[0].path);

// Wake up and enable IMU (Standard Full Mode 0x30)
this.device.write([0x01, 0x00, 0x00, 0x01, 0x40, 0x40, 0x00, 0x01, 0x40, 0x40, 0x03, 0x30]);
this.device.write([0x01, 0x01, 0x00, 0x01, 0x40, 0x40, 0x00, 0x01, 0x40, 0x40, 0x40, 0x01]);

this.device.on("data", (data) => this._processData(data));
return true;
} catch (err) {
return false;
}
}

_processData(data) {
if (data[0] !== 0x30) return;

// ── 1. READ BUTTONS (Byte 3 contains Y, X, B, A for Right Joy-Con) ──
// The A button is the 4th bit (0x08)
const currentAState = (data[3] & 0x08) !== 0;

// Check for "Falling Edge" (Button just got pressed down)
if (currentAState && !this.lastAState) {
this.emit('buttonA');
}
this.lastAState = currentAState;

// Loop through all 3 internal high-frequency sub-frames (spaced 5ms apart)
const frameOffsets = [13, 25, 37];

for (const offset of frameOffsets) {
const rawAccX = data.readInt16LE(offset);
const rawAccY = data.readInt16LE(offset + 2);
const rawAccZ = data.readInt16LE(offset + 4);
const gyroPitch = data.readInt16LE(offset + 8);

// High-Pass Filter: Isolates gravity from true moving force
this.gravity.x = this.filterCoeff * this.gravity.x + (1 - this.filterCoeff) * rawAccX;
this.gravity.y = this.filterCoeff * this.gravity.y + (1 - this.filterCoeff) * rawAccY;
this.gravity.z = this.filterCoeff * this.gravity.z + (1 - this.filterCoeff) * rawAccZ;

const userAccZ = rawAccZ - this.gravity.z;

// Threshold evaluations
const violentForwardThrust = userAccZ < -3500;
const crispWristSnap = Math.abs(gyroPitch) > 5000;

if (violentForwardThrust && crispWristSnap) {
this.emit('whip');
break;
}
}
}
}

module.exports = new JoyConModule();
13 changes: 13 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const fs = require('fs');
const os = require('os');
const { execFile } = require('child_process');
const joycon = require('./joycon');

// ── Win32 FFI (Windows only) ────────────────────────────────────────────────
let keybd_event, VkKeyScanA;
Expand Down Expand Up @@ -135,6 +136,8 @@ function createOverlay() {
hasShadow: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true, // Keep standard secure settings
nodeIntegration: false,
},
});
overlay.setAlwaysOnTop(true, 'screen-saver');
Expand Down Expand Up @@ -285,6 +288,16 @@ app.whenReady().then(async () => {
])
);
tray.on('click', toggleOverlay);
joycon.startListening();
joycon.on('buttonA', () => {
toggleOverlay();
});

joycon.on('whip', () => {
if (overlay) {
overlay.webContents.send('joycon-whip-event');
}
});
});

app.on('window-all-closed', e => e.preventDefault()); // keep alive in tray
4 changes: 4 additions & 0 deletions overlay.html
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@
if (whip && !dropping) dropping = true;
});

window.bridge.onJoyconWhip(() => {
playCrackSound();
window.bridge.whipCrack();
});
</script>
</body>
</html>
39 changes: 38 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"dependencies": {
"electron": "^33.0.0",
"koffi": "^2.9.0"
"koffi": "^2.9.0",
"node-hid": "^3.3.0"
}
}
1 change: 1 addition & 0 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ contextBridge.exposeInMainWorld('bridge', {
hideOverlay: () => ipcRenderer.send('hide-overlay'),
onSpawnWhip: (fn) => ipcRenderer.on('spawn-whip', () => fn()),
onDropWhip: (fn) => ipcRenderer.on('drop-whip', () => fn()),
onJoyconWhip: (fn) => ipcRenderer.on('joycon-whip-event', () => fn()),
});