-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (57 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Basic example of interfacing a PS3 controller on a Raspberry Pi using NodeJS
*
* © 2018 Wouter De Schuyter
* wouter.de.schuyter@gmail.com
* https://wouterdeschuyter.be/
*/
// Libs
const joystick = require('joystick');
const raspi = require('raspi');
const gpio = require('raspi-gpio');
const pwm = require('raspi-soft-pwm');
// Init PS3 controller, 0 = /dev/input/js0
const ps3Controller = new joystick(0);
// Init Raspi board
raspi.init(() => {
// Define digital outputs
const ledYellow = new gpio.DigitalOutput('P1-22');
const ledRed = new gpio.DigitalOutput('P1-24');
const ledBlue = new gpio.DigitalOutput('P1-21');
const ledGreen = new gpio.DigitalOutput('P1-23');
// Define our software pwm outputs
const pwmOutput1 = new pwm.SoftPWM('P1-19');
const pwmOutput2 = new pwm.SoftPWM('P1-26');
// On button press (triggers when pressed and when released)
ps3Controller.on('button', button => {
switch (button.number) {
case 0: // cross
ledBlue.write(button.value);
break;
case 1: // circle
ledYellow.write(button.value);
break;
case 2: // triangle
ledGreen.write(button.value);
break;
case 3: // square
ledRed.write(button.value);
break;
}
});
// On axis movement
ps3Controller.on('axis', axis => {
// Max value in both directions
const max = 32767;
// Value between 0 and 1, default: 0.5
const value = (axis.value * -1) / max / 2 + 0.5;
switch (axis.number) {
case 1: // left y-axis
pwmOutput1.write(value);
break;
case 4: // right y-axis
pwmOutput2.write(value);
break;
}
});
});