/**
*
* User: Martin Donnelly
* Date: 2016-05-20
* Time: 10:13
*
*/
/* global CAPABILITY, inheritsFrom */
/* global ble */
/* jshint browser: true , devel: true*/
var CC2650_ACCEL = function(deviceId) {
'use strict';
this.name = 'Accelerometer';
this.deviceID = deviceId;
this.capabilityID = 'F000AA80-0451-4000-B000-000000000000';
this.serviceDef = {
service: 'F000AA80-0451-4000-B000-000000000000',
data: 'F000AA81-0451-4000-B000-000000000000', // Read/notify 3 bytes X : Y : Z
notification: 'F0002902-0451-4000-B000-000000000000',
configuration: 'F000AA82-0451-4000-B000-000000000000', // Read/write 1 byte
period: 'F000AA83-0451-4000-B000-000000000000' // Read/write 1 byte Period = [Input*10]ms
};
this.frames = {};
this.$id = {};
this.$result = {};
this.sensorMpu9250GyroConvert = function(data) {
return data / (65536 / 500);
};
this.sensorMpu9250AccConvert = function(data) {
// Change /2 to match accel range...i.e. 16 g would be /16
return data / (32768 / 2);
};
this.onAccelerometerData = function(data) {
// Console.log(data);
var message;
var a = new Int16Array(data);
//0 gyro x
//1 gyro y
//2 gyro z
//3 accel x
//4 accel y
//5 accel z
//6 mag x
//7 mag y
//8 mag z
// TODO get a template to line this up
// TODO round or format numbers for better display
message = 'Gyro
' +
'X: ' + this.sensorMpu9250GyroConvert(a[0]).toFixed(5) + '
' +
'Y: ' + this.sensorMpu9250GyroConvert(a[1]) + '
' +
'Z: ' + this.sensorMpu9250GyroConvert(a[2]) + '
' +
'Accel
' +
'X: ' + this.sensorMpu9250AccConvert(a[3]) + '
' +
'Y: ' + this.sensorMpu9250AccConvert(a[4]) + '
' +
'Z: ' + this.sensorMpu9250AccConvert(a[5]) + '
' +
'Mag
' +
'X: ' + a[3] + '
' +
'Y: ' + a[4] + '
' +
'Z: ' + a[5] + '
' ;
this.state = message;
this.$result[this.frames.gyroID + '-x'].text(this.sensorMpu9250GyroConvert(a[0]).toFixed(5));
this.$result[this.frames.gyroID + '-y'].text(this.sensorMpu9250GyroConvert(a[1]).toFixed(5));
this.$result[this.frames.gyroID + '-z'].text(this.sensorMpu9250GyroConvert(a[2]).toFixed(5));
this.$result[this.frames.accelID + '-x'].text(this.sensorMpu9250AccConvert(a[3]).toFixed(5));
this.$result[this.frames.accelID + '-y'].text(this.sensorMpu9250AccConvert(a[4]).toFixed(5));
this.$result[this.frames.accelID + '-z'].text(this.sensorMpu9250AccConvert(a[5]).toFixed(5));
this.$result[this.frames.magID + '-x'].text(a[3]);
this.$result[this.frames.magID + '-y'].text(a[4]);
this.$result[this.frames.magID + '-z'].text(a[5]);
// Console.log(this.state);
};
this.startService = function() {
'use strict';
if (this.deviceID !== null) {
console.log('Starting CC2650 Accelerometer Service on ', this.deviceID);
console.log(this.serviceDef);
//Ble.startNotification(this.deviceID, , this.onButtonData.bind(this), this.onError);
ble.startNotification(this.deviceID, this.serviceDef.service, this.serviceDef.data, this.onAccelerometerData.bind(this), this.onError);
// Turn accelerometer on
var configData = new Uint16Array(1);
// Turn on gyro, accel, and mag, 2G range, Disable wake on motion
configData[0] = 0x007F;
ble.write(this.deviceID, this.serviceDef.service, this.serviceDef.configuration, configData.buffer,
function() { console.log('Started accelerometer.'); }, this.onError);
var periodData = new Uint8Array(1);
periodData[0] = 0x0A;
// Ble.write(deviceId, accelerometer.service, accelerometer.period, periodData.buffer,
// function() { console.log("Configured accelerometer period."); },app.onError);
this.setInternalID();
this.insertFrame('gyro');
this.insertFrame('accel');
this.insertFrame('mag');
}
};
this.animateGraph = function() {
// Nothing to animate yet
return -1;
};
this.insertFrame = function(mode) {
var frame;
var title;
var modeID = mode + 'ID';
this.frames[modeID] = this.frameID + '-' + mode;
var titles = {gyro: 'Gyroscope', accel: 'Accelerometer', mag: 'Magnetometer'};
console.log('FrameID: ' , this.frames[modeID]);
title = [titles[mode], ' - ', this.deviceID].join(' ');
frame = $('