sensortoy/www/js/device/CC2650/cc2650_luxometer.js

101 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-05-20 16:10:40 +00:00
/**
*
* User: Martin Donnelly
* Date: 2016-05-20
* Time: 10:13
*
*/
/* global CAPABILITY, inheritsFrom */
/* global ble */
/* jshint browser: true , devel: true*/
var CC2650_LUX = function(deviceId) {
'use strict';
this.name = 'Luxometer';
this.deviceID = deviceId;
this.capabilityID = 'F000AA70-0451-4000-B000-000000000000';
this.serviceDef = {
service: 'F000AA70-0451-4000-B000-000000000000',
data: 'F000AA71-0451-4000-B000-000000000000',
notification: 'F0002902-0451-4000-B000-000000000000',
configuration: 'F000AA72-0451-4000-B000-000000000000',
period: 'F000AA73-0451-4000-B000-000000000000'
2016-05-20 16:10:40 +00:00
};
2016-05-20 16:10:40 +00:00
this.$result = {temp: null, pressure: null};
this.startService = function() {
'use strict';
if (this.deviceID !== null) {
console.log('Starting CC2650 Luxometer Service on ', this.deviceID);
console.log(this.serviceDef);
this.insertFrame();
2016-05-20 16:10:40 +00:00
ble.startNotification(this.deviceID,
this.serviceDef.service,
this.serviceDef.data,
this.onLuxData.bind(this),
this.onError);
2016-05-20 16:10:40 +00:00
//Turn on luxometer
2016-05-20 16:10:40 +00:00
var luxConfig = new Uint8Array(1);
luxConfig[0] = 0x01;
ble.write(this.deviceID,
this.serviceDef.service,
this.serviceDef.configuration,
luxConfig.buffer,
function() { console.log('Started luxometer.'); },
this.onError);
2016-05-20 16:10:40 +00:00
}
};
this.onLuxData = function(data) {
2016-05-20 16:10:40 +00:00
var m, e, lux;
console.log(data);
2016-05-20 16:10:40 +00:00
var raw = new Uint16Array(data);
2016-05-20 16:10:40 +00:00
m = raw & 0x0FFF;
e = (raw & 0xF000) >> 12;
2016-05-20 16:10:40 +00:00
lux = m * (0.01 * Math.pow(2.0, e));
2016-05-20 16:10:40 +00:00
this.state = [lux, 'lux'].join(' ');
2016-05-20 16:10:40 +00:00
this.$result.text(this.state);
2016-05-20 16:10:40 +00:00
console.log('Luxometer:', this.state);
};
2016-05-20 16:10:40 +00:00
this.insertFrame = function() {
var self = this;
console.log('Overloading...');
// Call the parent displayForm first...
this.superClass_.insertFrame.call(self);
var lux = this.frameID + '-l';
2016-05-20 16:10:40 +00:00
var row = $('<div />', {class: 'mui-row'});
2016-05-20 16:10:40 +00:00
$('<div />',
{class: 'mui-col-xs-4 mui--text-accent', text: 'Luminosity:'}).appendTo(
row);
2016-05-20 16:10:40 +00:00
$('<div />',
{class: 'mui-col-xs-8 mui--text-white', id: lux}).appendTo(row);
2016-05-20 16:10:40 +00:00
this.$id.append(row);
this.$result = $('#' + lux);
2016-05-20 16:10:40 +00:00
};
};
inheritsFrom(CC2650_LUX, CAPABILITY);