diff --git a/README.md b/README.md index a91b5d0..d29db9a 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,16 @@ It's not supposed to be easy to setup, but if you need help just contact me. For Project highly depends on linux (uses sys class to control the LEDs) and requires root (non-root operations are setuid'd internally). +## Start + +To start it up, you need to run it as root and pass `--loader=ts-node/esm` to node: + +```sh +sudo node --loader=ts-node/esm +``` + +where `` is the file to run (`index.ts` in one of the folders). + ## Spotify colors Uses DBUS which needs your username. For now just edit the utils function getUsername to return your username. diff --git a/joystick.ts b/joystick.ts index d01c1b2..dd20e93 100644 --- a/joystick.ts +++ b/joystick.ts @@ -1,5 +1,7 @@ import { exec, readUTF, writeUTF } from "./utils.js"; import * as fs from "fs"; +import struct from "python-struct"; +import chunker from "stream-chunker"; interface IJoyStick { name: string; @@ -24,6 +26,12 @@ export class JoyStick implements IJoyStick { this.dev = data.dev; } + createEventStream() { + const stream = fs.createReadStream(this.dev); + return stream + .pipe(chunker(struct.sizeOf("LhBB"))); + } + async getColors() { const [ red, green, blue ] = (await Promise.all([ readUTF(`/sys/class/leds/${this.led}:red/brightness`), diff --git a/package.json b/package.json index e804019..5601d3a 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,11 @@ { "dependencies": { "@types/node": "^16.9.1", + "@types/python-struct": "^1.0.0", "got": "^11.8.2", "node-vibrant": "^3.2.1-alpha.1", + "python-struct": "^1.1.3", + "stream-chunker": "^1.2.8", "ts-node": "^10.2.1", "typescript": "^4.4.3" }, diff --git a/taikoColors/index.ts b/taikoColors/index.ts new file mode 100644 index 0000000..4740e57 --- /dev/null +++ b/taikoColors/index.ts @@ -0,0 +1,49 @@ +import { JoyStick } from "../joystick.js"; +import struct from "python-struct"; + +const devices = await JoyStick.getList(); +enum map { + x = 0, + circle, + triangle, + square, + L1, + R1, + L2, + R2, + share, + options, + ps, + L3, + R3 +} +enum EventTypes { + button = 1, + axis = 2 +} + +for(const device of devices) { + device.setColors({ + red: 0, + green: 0, + blue: 0 + }); + let pressed = new Set(); + device.createEventStream().on("data", (buf) => { + const data = struct.unpack("LhBB", buf); + if(data[2] !== EventTypes.button) return; + if(data[1] === 1) { + pressed.add(Number(data[3])); + console.log("Pressed", data[3]); + } else { + pressed.delete(Number(data[3])); + console.log("Unpressed", data[3]); + } + + device.setColors({ + red: (pressed.has(map.R1) || pressed.has(map.R2)) ? 255 : 0, + green: 0, + blue: (pressed.has(map.L1) || pressed.has(map.L2)) ? 255 : 0 + }); + }); +} \ No newline at end of file