sensortoy/www/js/device/CC2650/cc2650_humidity.js

116 lines
2.8 KiB
JavaScript
Raw Normal View History

/**
*
* User: Martin Donnelly
* Date: 2016-05-20
* Time: 10:13
*
*/
/* global CAPABILITY, inheritsFrom */
/* global ble */
/* jshint browser: true , devel: true*/
var CC2650_HUM = function(deviceId) {
'use strict';
this.name = 'Humidity';
this.deviceID = deviceId;
this.capabilityID = 'F000AA20-0451-4000-B000-000000000000';
this.serviceDef = {
service: 'F000AA20-0451-4000-B000-000000000000',
data: 'F000AA21-0451-4000-B000-000000000000',
notification: 'F0002902-0451-4000-B000-000000000000',
configuration: 'F000AA22-0451-4000-B000-000000000000',
period: 'F000AA23-0451-4000-B000-000000000000'
};
this.$result = {temp: null, humidity: null};
this.startService = function() {
'use strict';
if (this.deviceID !== null) {
console.log('Starting CC2650 Humidity Service on ', this.deviceID);
console.log(this.serviceDef);
this.insertFrame();
ble.startNotification(this.deviceID,
this.serviceDef.service,
this.serviceDef.data,
this.onHumidityData.bind(this),
this.onError);
//Turn on barometer
var humidityConfig = new Uint8Array(1);
humidityConfig[0] = 0x01;
ble.write(this.deviceID,
this.serviceDef.service,
this.serviceDef.configuration,
humidityConfig.buffer,
function() { console.log('Started Humidity.'); },
this.onError);
}
};
this.onHumidityData = function(data) {
var hStr;
var tStr;
console.log(data);
var message;
var raw = new Uint16Array(data);
//-- calculate temperature [°C]
var temp = (raw[0] / 65536) * 165 - 40;
//-- calculate relative humidity [%RH]
var hum = (raw[1] / 65536) * 100;
tStr = temp + '°C';
hStr = hum + '%RH';
message = 'Temperature <br/>' + tStr + 'Humidity <br/>' + pStr;
this.$result.temp.text(tStr);
this.$result.humidity.text(hStr);
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 humidity = this.frameID + '-h';
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: 'Humidity:'
}).appendTo(row);
$('<div />',
{class: 'mui-col-xs-3 mui--text-white', id: humidity}).appendTo(row);
this.$id.append(row);
this.$result.temp = $('#' + temp);
this.$result.humidity = $('#' + humidity);
};
};
inheritsFrom(CC2650_HUM, CAPABILITY);