taiko colors

This commit is contained in:
Daniel Bulant 2021-09-13 20:18:10 +02:00
parent fd5bc47cb7
commit f27f15b6ca
4 changed files with 70 additions and 0 deletions

View file

@ -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 <file>
```
where `<file>` 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.

View file

@ -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`),

View file

@ -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"
},

49
taikoColors/index.ts Normal file
View file

@ -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<number>();
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
});
});
}