johnny-five
Version:
The JavaScript Arduino Programming Framework.
213 lines (169 loc) • 3.98 kB
JavaScript
var dualShock = require("dualshock-controller");
var controller = dualShock({
config: "dualShock3",
analogStickSmoothing: true
});
controller.isConnected = false;
controller.on("right:move", function(position) {
console.log( position );
});
controller.connect();
return;
function to(deg) {
if (controller.isConnected) {
deg = deg | 0;
this.position = deg;
console.log( "%s -> %d", this.id, deg );
}
}
// just a placeholder
var inits = [
{ pin: 10, id: "rf", startAt: 91 },
{ pin: 11, id: "rk", startAt: 92 },
{ pin: 12, id: "rh", startAt: 98 },
{ pin: 7, id: "lf", startAt: 83, isInverted: true },
{ pin: 8, id: "lk", startAt: 89, isInverted: true },
{ pin: 9, id: "lh", startAt: 89, isInverted: true }
];
var legs = {};
inits.forEach(function(init) {
legs[init.id] = {
id: init.id,
startAt: init.startAt,
position: init.startAt,
to: to
};
});
function scale(x, fromLow, fromHigh, toLow, toHigh) {
return (x - fromLow) * (toHigh - toLow) /
(fromHigh - fromLow) + toLow;
}
function degrees(center, val) {
if (val >= 128) {
return scale(val, 128, 255, center - 1, 0);
}
return scale(val, 127, 0, center, 180);
}
var labels = [ "Foot", "Knee", "Hip" ];
var Group = {
stash: {
side: -1,
index: 0
},
isGrouped: false,
get side() {
return Group.stash.side;
},
set side(value) {
// -1 = both, 0 = right, 1 = left
var which = value === -1 ?
"both" : (value ? "right" : "left");
console.log(
"------- %s %s -------",
which.toUpperCase(),
labels[Group.stash.index].toUpperCase()
);
Group.stash.side = value;
},
get index() {
return Group.stash.index;
},
set index(value) {
console.log(
"Controlling %s", this.sets[value].name
);
Group.stash.index = value;
},
sets: [
// Feet
{ name: "Feet",
pins: [ 7, 10 ],
parts: [ "lf", "rf" ],
values: [ null, null ]
},
// Knees
{ name: "Knees",
pins: [ 8, 11 ],
parts: [ "lk", "rk" ],
values: [ null, null ]
},
// Hips
{ name: "Hips",
pins: [ 9, 12 ],
parts: [ "lh", "rh" ],
values: [ null, null ]
}
]
};
controller.on("connected", function() {
controller.isConnected = true;
});
controller.on("triangle:press", function() {
var which = Group.side === -1 ?
"both" : (Group.side ? "right" : "left");
var part = Group.sets[Group.index].name;
console.log(
"Controlling %s %s", which, part
);
});
controller.on("circle:press", function() {
Group.side = -1;
});
controller.on("dpadLeft:press", function() {
Group.side = 0;
});
controller.on("dpadRight:press", function() {
Group.side = 1;
});
controller.on("dpadUp:press", function() {
var next = Group.index + 1;
if (next === Group.sets.length) {
next = 0;
}
Group.index = next;
});
controller.on("dpadDown:press", function() {
var next = Group.index - 1;
if (next === -1) {
next = Group.sets.length - 1;
}
Group.index = next;
});
controller.on("left:move", function(position) {
var part = Group.sets[Group.index].parts[0];
var pos = degrees(legs[part].startAt, position.y);
legs[part].to(pos);
});
controller.on("right:move", function(position) {
var part = Group.sets[Group.index].parts[1];
var pos = degrees(legs[part].startAt, position.y);
legs[part].to(pos);
});
controller.on("l1:press", function() {
tuner(-1);
});
controller.on("r1:press", function() {
tuner(1);
});
function tuner(step) {
// Cannot control both at a time
if (Group.side === -1) {
return;
}
var set = Group.sets[Group.index];
var part = set.parts[Group.side];
var value = set.values[Group.side];
if (value === null) {
value = legs[part].startAt;
}
if (step < 0) {
value--;
} else {
value++;
}
process.nextTick(function() {
legs[part].to(value);
});
set.values[Group.side] = value;
}
controller.connect();