Skip to content

Instantly share code, notes, and snippets.

@astagi
Created December 23, 2019 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astagi/b59c53b7d85079960c1b8ef285c97763 to your computer and use it in GitHub Desktop.
Save astagi/b59c53b7d85079960c1b8ef285c97763 to your computer and use it in GitHub Desktop.
R2D2
const MSG_ROTATE = [0x0A,0x17,0x0F];
let convertDegreeToHex = (degree) => {
var view = new DataView(new ArrayBuffer(4));
view.setFloat32(0, degree);
return Array
.apply(null, { length: 4 })
.map((_, i) => view.getUint8(i))
}
let droidAddress = 'd7:1b:52:17:7b:d6';
connectTheDroid(droidAddress).then(characteristic => {
characteristic.subscribe(async(error) => {
if (error) {
console.error('Error subscribing to char.');
} else {
console.log("Wait for init!");
await writePacket(characteristic, buildPacket(MSG_INIT), true, 5000);
console.log('Rotate the droid!');
for (let degrees = -160 ; degrees <= 180 ; degrees+=5) {
await writePacket(
characteristic,
buildPacket(MSG_ROTATE, convertDegreeToHex(degrees)),
false,
);
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment