Skip to content

Instantly share code, notes, and snippets.

@Pan-Maciek
Created May 30, 2017 17:44
Show Gist options
  • Save Pan-Maciek/f6997e537f6b2ccf30a2b1af612a620e to your computer and use it in GitHub Desktop.
Save Pan-Maciek/f6997e537f6b2ccf30a2b1af612a620e to your computer and use it in GitHub Desktop.
var XBoxController = require('../src/controller')
var USB = require('usb')
// searching for an official xBox 360 controller
var device = USB.findByIds(1118, 654)
var controller = new XBoxController(device)
// handling button events and setting the leds to corresponding pattern
controller.on('button', function (key, val) {
console.log`Button ${key} has been ${val ? 'pressed' : 'released'}`
if (val)
switch (key) {
case 'a':
controller.setLed('LED_1')
break
case 'b':
controller.setLed('LED_2')
break
case 'y':
controller.setLed('LED_3')
break
case 'x':
controller.setLed('LED_4')
break
case 'xBox':
controller.setLed('ALTERNATING')
break
case 'start':
controller.setLed('ROTATING')
break
case 'back':
controller.setLed('BLINKING')
break
}
else controller.setLed('ALL_OFF')
})
// getting trigger values and using them for rumbler speeds
controller.on('trigger', function (val) {
console.log(`Trigger: right:${val.r}\tleft:${val.l}`)
controller.setRumbler(val.l, val.r)
})
// Setting custom dead zone for sticks
controller.dead = 10000
// just print the values of the sticks
function stick(side, val) {
var length = val.getLength().toFixed(2)
var angle = val.getAngle().toFixed(2)
console.log(`${side} stick: {x:${val.x},\ty:${val.y}};\t{l:${length},\ta:${angle}°}`)
}
controller.on('stick:left', function (val) {
stick('left', val)
})
controller.on('stick:right', function (val) {
stick('right', val)
})
// in case of errors, print them
controller.on('error', function (err) {
console.error(err)
})
// finally open the controller
controller.open()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment