sensortoy/www/js/device/CC2650/cc2650_barometer.js

104 lines
2.9 KiB
JavaScript

/**
*
* User: Martin Donnelly
* Date: 2016-05-20
* Time: 10:13
*
*/
/* global CAPABILITY, inheritsFrom */
/* global ble */
/* jshint browser: true , devel: true*/
var CC2650_BAR = function(deviceId) {
'use strict';
this.name = 'Barometer';
this.deviceID = deviceId;
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.$result = {temp: null, pressure: null};
this.startService = function() {
'use strict';
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;
console.log(data);
var message;
var a = new Uint8Array(data);
//0-2 Temp
//3-5 Pressure
tStr = this.sensorBarometerConvert(a[0] | (a[1] << 8) | (a[2] << 16)) + '°C';
pStr = this.sensorBarometerConvert(a[3] | (a[4] << 8) | (a[5] << 16)) + 'hPa';
message = 'Temperature <br/>' + tStr +
'Pressure <br/>' + pStr ;
this.$result.temp.text(tStr);
this.$result.pressure.text(pStr);
this.state = message;
console.log('Barometer:', this.state);
};
this.insertFrame = function() {
var self = this;
console.log('Overloading...');
// Call the parent displayForm first...
this.superClass_.insertFrame.call(self);
var temp = this.frameID + '-t';
var pressure = this.frameID + '-p';
var row = $('<div />', {class: 'mui-row'});
$('<div />', { class: 'mui-col-xs-3 mui--text-accent', text: 'Temp:'}).appendTo(row);
$('<div />', { class: 'mui-col-xs-3 mui--text-white', id: temp}).appendTo(row);
$('<div />', { class: 'mui-col-xs-3 mui--text-accent', text: 'Pressure:'}).appendTo(row);
$('<div />', { class: 'mui-col-xs-3 mui--text-white', id: pressure}).appendTo(row);
this.$id.append(row);
this.$result.temp = $('#' + temp);
this.$result.pressure = $('#' + pressure);
};
};
inheritsFrom(CC2650_BAR, CAPABILITY);