sensortoy/app/js/device/CC2650/cc2650_barometer.js
2016-06-20 17:13:02 +01:00

191 lines
5.8 KiB
JavaScript

/**
*
* User: Martin Donnelly
* Date: 2016-05-20
* Time: 10:13
*
*/
/* global CAPABILITY, inheritsFrom, capabilityManager, EJS */
/* global ble */
/* jshint browser: true , devel: true*/
var CC2650_BAR = function(p) {
'use strict';
this.name = 'Barometer';
this.deviceID = p.deviceID || null;
this.target = p.target || null;
this.capabilityID = 'F000AA40-0451-4000-B000-000000000000';
this.serviceDef = {
service: 'F000AA40-0451-4000-B000-000000000000',
data: 'F000AA41-0451-4000-B000-000000000000',
notification: 'F0002902-0451-4000-B000-000000000000',
configuration: 'F000AA42-0451-4000-B000-000000000000',
period: 'F000AA43-0451-4000-B000-000000000000'
};
this.data = {temp: [], pressure: []};
this.$result = {temp: null, pressure: null};
this.setFrame();
this.startService = function() {
if (this.deviceID !== null) {
console.log('Starting CC2650 Barometer Service on ', this.deviceID);
console.log(this.serviceDef);
this.insertFrame();
ble.startNotification(this.deviceID, this.serviceDef.service, this.serviceDef.data, this.onBarometerData.bind(this), this.onError);
// Turn on barometer
var barometerConfig = new Uint8Array(1);
barometerConfig[0] = 0x01;
ble.write(this.deviceID, this.serviceDef.service, this.serviceDef.configuration, barometerConfig.buffer,
function() { console.log('Started barometer.'); },this.onError);
}
};
this.sensorBarometerConvert = function(data) {
return (data / 100);
};
this.onBarometerData = function(data) {
var pStr;
var tStr;
var message;
var a = new Uint8Array(data);
// 0-2 Temp
// 3-5 Pressure
var temp, pressure;
temp = this.sensorBarometerConvert(a[0] | (a[1] << 8) | (a[2] << 16));
pressure = this.sensorBarometerConvert(a[3] | (a[4] << 8) | (a[5] << 16));
tStr = temp + '°C';
pStr = pressure + 'hPa';
message = 'Temperature <br/>' + tStr +
'Pressure <br/>' + pStr ;
this.data.temp = this.storeData(temp, this.data.temp);
this.data.pressure = this.storeData(pressure, this.data.pressure);
this.$result.temp.text(tStr);
this.$result.pressure.text(pStr);
this.state = message;
// Console.log('Barometer:', this.state);
};
this.animateGraph = function() {
/* This.simpleGraph(this.data.temp, 'temp');
this.simpleGraph(this.data.pressure, 'pressure');*/
var arcTemp = this.frameID + 'temp-arc';
var arcPressure = this.frameID + 'pressure-arc';
this.updateArc(this.data.temp, 'temp' , arcTemp, '°C', 50);
this.updateArc(this.data.pressure, 'pressure' , arcPressure, 'hPa');
};
this.insertFrame = function() {
var self = this;
// Call the parent displayForm first...
this.superClass_.insertFrame.call(self);
var temp = this.frameID + '-t';
var pressure = this.frameID + '-p';
var arcB = this.frameID + 'temp-arcB';
var arcTemp = this.frameID + 'temp-arc';
var pressureArcB = this.frameID + 'pressure-arcB';
var arcPressure = this.frameID + 'pressure-arc';
var settings = {data: {baseID: this.frameID}};
var html = new EJS({url: './partials/cc2650_barometer_dial.ejs'}).render(settings);
this.$id.append(html);
document.getElementById(arcB).setAttribute('d', this.describeArc(150, 150, 100, 0, 240));
document.getElementById(arcTemp).setAttribute('d', this.describeArc(150, 150, 100, 0, this.setArc(0)));
document.getElementById(pressureArcB).setAttribute('d', this.describeArc(150, 150, 100, 0, 240));
document.getElementById(arcPressure).setAttribute('d', this.describeArc(150, 150, 100, 0, this.setArc(0)));
/* Var row = $('<div />', {class: 'mui-row'});
$('<div />', { class: 'mui-col-xs-3 mui--text-accent mui--text-right', text: 'Temp:'}).appendTo(row);
$('<div />', { class: 'mui-col-xs-3 mui--text-dark', id: temp}).appendTo(row);
$('<div />', { class: 'mui-col-xs-3 mui--text-accent mui--text-right', text: 'Pressure:'}).appendTo(row);
$('<div />', { class: 'mui-col-xs-3 mui--text-dark', id: pressure}).appendTo(row);
this.$id.append(row);
if (/ipad/i.test(device.model)) {
tabBody = $('<div>', {class: 'mui-row'});
tabBody.append($('<div>',{class: 'mui-col-md-6'}).append(this.generateBlankGraph('temp'))) ;
tabBody.append($('<div>',{class: 'mui-col-md-6'}).append(this.generateBlankGraph('pressure'))) ;
this.$id.append(tabBody);
} else {
var tabBody = $('<ul>',
{class: 'mui-tabs__bar mui-tabs__bar--justified'});
$('<li>', {class: 'mui--is-active tabOnWhite'}).append($('<a>',
{
text: 'Temperature',
'data-mui-toggle': 'tab',
'data-mui-controls': (temp + '-pane')
})).appendTo(tabBody);
$('<li>', {class: 'tabOnWhite'}).append($('<a>',
{
text: 'Pressure',
'data-mui-toggle': 'tab',
'data-mui-controls': (pressure + '-pane')
})).appendTo(tabBody);
this.$id.append(tabBody);
blankChart = this.generateBlankGraph('temp');
this.$id.append($('<div>',
{class: 'mui-tabs__pane mui--is-active', id: (temp + '-pane')}).append(
blankChart));
blankChart = this.generateBlankGraph('pressure');
this.$id.append($('<div>',
{
class: 'mui-tabs__pane',
id: (pressure + '-pane')
}).append(blankChart));
}*/
this.$result.temp = $('#' + temp);
this.$result.pressure = $('#' + pressure);
};
};
inheritsFrom(CC2650_BAR, CAPABILITY);
capabilityManager.register({id: 'F000AA40-0451-4000-B000-000000000000', module: CC2650_BAR});