Created
October 9, 2016 04:40
-
-
Save w4ilun/4ae91430543b5faec26ca5356b620aca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//BLE | |
let service = 0xBEEF; | |
let characteristic = 0xFEED; | |
let byteLength = 20; | |
//IMU global vars for processing | |
let yaw = 0.0; | |
let pitch = 0.0; | |
let roll = 0.0; | |
window.onload = () => { | |
document.getElementById('connect').onclick = connectBLE; | |
} | |
function connectBLE(){ | |
//BLE setup. Connect and get service/characteristic notifications | |
navigator.bluetooth.requestDevice({ filters: [{ services: [service] }] }) | |
.then(device => device.gatt.connect()) | |
.then(server => server.getPrimaryService(service)) | |
.then(service => service.getCharacteristic(characteristic)) | |
.then(characteristic => { | |
return characteristic.startNotifications() | |
.then(_ => { | |
characteristic.addEventListener('characteristicvaluechanged', | |
handleCharacteristicValueChanged); | |
}); | |
}) | |
.then(_ => { | |
console.log('Notifications have been started.'); | |
}) | |
.catch(error => { console.log(error); }); | |
function handleCharacteristicValueChanged(event){ | |
var value = event.target.value; | |
var str = ""; | |
//concat and split string for roll, pitch, yaw (e.g. "-0.58,2.20,328.76") | |
for(var i=0; i<byteLength; i++){ | |
str = str + String.fromCharCode(value.getUint8(i)); | |
} | |
var imu = str.split(','); | |
//update globals | |
roll = parseFloat(imu[0]); | |
pitch = parseFloat(imu[1]); | |
yaw = parseFloat(imu[2]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment