Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/touch pad #61

Merged
merged 5 commits into from
Feb 8, 2017
Merged
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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ controller.on('square:release', () => console.log('square release'));
// value : values will be from 0 to 120 for directions right, forward and up and from 0 to -120 for left, backwards and down.
//}

//DualShock 4 TouchPad
//finger 1 is x1 finger 2 is x2
controller.on('touchpad:x1:active', () => console.log('touchpad one finger active'));

controller.on('touchpad:x2:active', () => console.log('touchpad two fingers active'));

controller.on('touchpad:x2:inactive', () => console.log('touchpad back to single finger'));

controller.on('touchpad:x1', data => console.log('touchpad x1:', data.x, data.y));

controller.on('touchpad:x2', data => console.log('touchpad x2:', data.x, data.y));


//right-left movement
controller.on('rightLeft:motion', data => console.log(data));

Expand Down Expand Up @@ -132,7 +145,7 @@ npm install dualshock-controller
```

```bash
npm install node-hid --driver=hidraw
npm install node-hid --driver=hidraw --build-from-source
```

#### <a name="create-udev-rules"></a> Create udev rules
Expand Down
15 changes: 14 additions & 1 deletion controllerConfigurations/dualShock4-alternate-driver.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,18 @@
"flashOn": 9,
"flashOff": 10
}
}
},
"touchPad": [{
"name":"x1",
"activePin": 35,
"dataPinA": 37,
"dataPinB": 36,
"dataPinC": 38
},{
"name": "x2",
"activePin": 39,
"dataPinA": 41,
"dataPinB": 40,
"dataPinC": 42
}]
}
15 changes: 14 additions & 1 deletion controllerConfigurations/dualShock4-generic-driver.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,18 @@
"flashOn": 9,
"flashOff": 10
}
}
},
"touchPad": [{
"name":"x1",
"activePin": 35,
"dataPinA": 37,
"dataPinB": 36,
"dataPinC": 38
},{
"name": "x2",
"activePin": 39,
"dataPinA": 41,
"dataPinB": 40,
"dataPinC": 42
}]
}
15 changes: 14 additions & 1 deletion controllerConfigurations/dualShock4.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,18 @@
"flashOn": 9,
"flashOff": 10
}
}
},
"touchPad": [{
"name":"x1",
"activePin": 35,
"dataPinA": 37,
"dataPinB": 36,
"dataPinC": 38
},{
"name": "x2",
"activePin": 39,
"dataPinA": 41,
"dataPinB": 40,
"dataPinC": 42
}]
}
9 changes: 7 additions & 2 deletions src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const util = require('util'),
Buttons = require('./buttons'),
Status = require('./status'),
HID = require('node-hid'),
config = require('./config');
config = require('./config'),
TouchPad = require('./touchpad');

//generic controller object, it will need a controller Configuration with a buttons array passed into its connect function.
const Controller = function() {
Expand All @@ -20,7 +21,8 @@ const Controller = function() {
analogs = new Analogs(this),
buttons = new Buttons(this),
gyro = new Gyro(this),
status = new Status(this);
status = new Status(this),
touchPad = new TouchPad(this);

let device = null;

Expand Down Expand Up @@ -96,6 +98,9 @@ const Controller = function() {
if (controllerConfig.status) {
status.process(data);
}
if (controllerConfig.touchPad) {
touchPad.process(data);
}
};

const isController = function(device) {
Expand Down
52 changes: 52 additions & 0 deletions src/touchpad.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const config = require('./config');

function genpBufferFromConf(tpAxis) {
return {
name: tpAxis.name,
active: false,
data: {
x: 0,
y: 0
}
};
}

module.exports = function TouchPad(controller) {
const touchPad = config.getControllerConfig().touchPad;
let pBuffer = {};

function processIsActive(buffer, tpAxis) {
const active = buffer[tpAxis.activePin] < 128;
const axisBuffer = pBuffer[tpAxis.name];
const evt = active ? 'active' : 'inactive';

if (active !== axisBuffer.active) {
controller.emit(`touchpad:${tpAxis.name}:${evt}`);
}

axisBuffer.active = active;
}

function processData(buffer, tpAxis) {
const axisBuffer = pBuffer[tpAxis.name];

if (axisBuffer.active) {
axisBuffer.data.x = ((buffer[tpAxis.dataPinA] & 15) << 8 | buffer[tpAxis.dataPinB]);
axisBuffer.data.y = buffer[tpAxis.dataPinC] << 4 | ((buffer[tpAxis.dataPinA] & 240) >> 4);
controller.emit(`touchpad:${tpAxis.name}`, axisBuffer.data);
}

}

this.process = function process(buffer) {
for (let i = 0; i < touchPad.length; i++) {
//if we have not built a pBuffer profile for this axis lets build it.
if (!pBuffer[touchPad[i].name]) {
pBuffer[touchPad[i].name] = genpBufferFromConf(touchPad[i]);
}

processIsActive(buffer, touchPad[i]);
processData(buffer, touchPad[i]);
}
};
};