Skip to content

Instantly share code, notes, and snippets.

@Caaz
Created January 30, 2018 23:24
Show Gist options
  • Save Caaz/1f69b2b3ed308f1c2540dce4469207e5 to your computer and use it in GitHub Desktop.
Save Caaz/1f69b2b3ed308f1c2540dce4469207e5 to your computer and use it in GitHub Desktop.
Controller shenanigans
const HID = require('node-hid')
const Bitfield = require("bitfield")
const SimpleADB = require('simple-adb').SimpleADB;
require('log-timestamp')
const sadb = new SimpleADB();
const devices = HID.devices()
const device = new HID.HID(devices[0].path)
const names = ['zr','zl','r','l','x','a','b','y','null','null','record','home','cr','cl','plus','minus']
const keymap = {
up: 19,
down: 20,
left: 21,
right: 22,
home: 3,
record: 26,
plus: 82,
a:66,
x:52,
y:53,
b:4
}
let controller = []
let readable = { }
function update_button(key, state) {
if((readable[key] == null) || (readable[key].state !== state)) {
readable[key] = {
time: Date.now(),
state: state
}
console.log('state change on '+key+': '+(state?'true':'false'))
if(state) [
sadb.execAdbShellCommand(['input','keyevent',keymap[key]])
]
}
}
function btn_update() {
for(i = 0; i < names.length; i++) {
update_button(names[i], controller[i])
}
if(controller[20]) {
update_button('up',false)
update_button('down',false)
update_button('right',false)
update_button('left',false)
} else {
const dpad = (controller[21]?1:0) + (controller[22]?2:0) + (controller[23]?4:0)
switch(dpad) {
case 0:
update_button('up',true)
update_button('down',false)
update_button('right',false)
update_button('left',false)
break
case 1:
update_button('up',false)
update_button('down',true)
update_button('right',false)
update_button('left',false)
break
case 2:
update_button('up',false)
update_button('down',false)
update_button('right',true)
update_button('left',false)
break
case 3:
update_button('up',false)
update_button('down',false)
update_button('right',false)
update_button('left',true)
break
case 4:
update_button('up',true)
update_button('down',false)
update_button('right',true)
update_button('left',false)
break
case 5:
update_button('up',false)
update_button('down',true)
update_button('right',false)
update_button('left',true)
break
case 6:
update_button('up',false)
update_button('down',true)
update_button('right',true)
update_button('left',false)
break
case 7:
update_button('up',true)
update_button('down',false)
update_button('right',false)
update_button('left',true)
break
}
}
}
device.on("data", (data) => {
const field = Bitfield(data)
for (i = data.length*8; i >= 0; i--) {
let got = field.get(i)
if (controller[i] != got) {
controller[i] = got
}
}
btn_update()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment