johnny-five
Version:
Firmata based Arduino Programming Framework.
169 lines (134 loc) • 3.46 kB
JavaScript
var XboxController = require("xbox-controller");
var controller = new XboxController();
// if (typeof WeakMap === "undefined") {
// throw new Error("this program must be rung with --harmony");
// }
function to(deg) {
deg = deg | 0;
this.position = deg;
console.log( this.id, deg );
}
// just a placeholder
var inits = [
{ pin: 12, id: "rh", startAt: 100 },
{ pin: 11, id: "rk", startAt: 90 },
{ pin: 10, id: "rf", startAt: 95 },
{ pin: 9, id: "lh", startAt: 95, isInverted: true },
{ pin: 8, id: "lk", startAt: 85, isInverted: true },
{ pin: 7, id: "lf", startAt: 85, 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 >= 0) {
return scale(val, 0, -32768, center, 180);
}
return scale(val, 0, 32768, center - 1, 0);
}
// var side = -1;
// var index = 0;
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 ? "left" : "right");
console.log(
"Controlling %s", which.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" ] },
// Knees
{ name: "Knees", pins: [ 8, 11 ], parts: [ "lk", "rk" ] },
// Hips
{ name: "Hips", pins: [ 9, 12 ], parts: [ "lh", "rh" ] }
]
};
controller.on("y: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("b:press", function() {
Group.side = -1;
});
controller.on("dleft:press", function() {
Group.side = 0;
});
controller.on("dright:press", function() {
Group.side = 1;
});
controller.on("dup:press", function() {
var next = Group.index + 1;
if (next === Group.sets.length) {
next = 0;
}
Group.index = next;
});
controller.on("ddown: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);
var pos = degrees(90, 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("lb:press", function() {
tuner(0);
});
controller.on("rb:press", function() {
tuner(1);
});
function tuner(index) {
// Cannot control both at a time with RB
if (Group.side === -1) {
return;
}
var part = Group.sets[Group.index].parts[index];
var step = index ? 1 : -1;
var position = legs[part].position + step;
legs[part].to(position);
}