sensortoy/www/js/device/CC2650/cc2650_accelerometer.js
Martin Donnelly 939a7aff4c init
2016-05-20 17:10:40 +01:00

170 lines
6.0 KiB
JavaScript

/**
*
* 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 <br/>' +
'X: ' + this.sensorMpu9250GyroConvert(a[0]).toFixed(5) + '<br/>' +
'Y: ' + this.sensorMpu9250GyroConvert(a[1]) + '<br/>' +
'Z: ' + this.sensorMpu9250GyroConvert(a[2]) + '<br/>' +
'Accel <br/>' +
'X: ' + this.sensorMpu9250AccConvert(a[3]) + '<br/>' +
'Y: ' + this.sensorMpu9250AccConvert(a[4]) + '<br/>' +
'Z: ' + this.sensorMpu9250AccConvert(a[5]) + '<br/>' +
'Mag <br/>' +
'X: ' + a[3] + '<br/>' +
'Y: ' + a[4] + '<br/>' +
'Z: ' + a[5] + '<br/>' ;
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.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 = $('<div />', {
class: 'mui-panel', id: this.frames[modeID]
});
$('<div />', { class: 'mui-row'}).append($('<div />', { class: 'mui-col-xs-12 mui--text-title mui-ellipsis-2', text: title})).appendTo(frame);
$('#frames').append(frame);
this.$id[modeID] = $('#' + this.frames[modeID]);
// Call the parent displayForm first...
var row = $('<div />', {class: 'mui-row'});
$('<div />', { class: 'mui-col-xs-3 mui--text-accent', text: 'X'}).appendTo(row);
$('<div />', { class: 'mui-col-xs-3 mui--text-accent', text: 'Y'}).appendTo(row);
$('<div />', { class: 'mui-col-xs-3 mui--text-accent', text: 'Z'}).appendTo(row);
this.$id[modeID].append(row);
row = $('<div />', {class: 'mui-row'});
$('<div />', { class: 'mui-col-xs-3 mui--text-white', text: '--', id: this.frames[modeID] + '-x' }).appendTo(row);
$('<div />', { class: 'mui-col-xs-3 mui--text-white', text: '--', id: this.frames[modeID] + '-y'}).appendTo(row);
$('<div />', { class: 'mui-col-xs-3 mui--text-white', text: '--', id: this.frames[modeID] + '-z'}).appendTo(row);
this.$id[modeID].append(row);
this.$result[this.frames[modeID] + '-x'] = $('#' + this.frames[modeID] + '-x');
this.$result[this.frames[modeID] + '-y'] = $('#' + this.frames[modeID] + '-y');
this.$result[this.frames[modeID] + '-z'] = $('#' + this.frames[modeID] + '-z');
};
};
inheritsFrom(CC2650_ACCEL, CAPABILITY);