225 lines
6.5 KiB
JavaScript
225 lines
6.5 KiB
JavaScript
|
/*
|
|||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|||
|
* or more contributor license agreements. See the NOTICE file
|
|||
|
* distributed with this work for additional information
|
|||
|
* regarding copyright ownership. The ASF licenses this file
|
|||
|
* to you under the Apache License, Version 2.0 (the
|
|||
|
* "License"); you may not use this file except in compliance
|
|||
|
* with the License. You may obtain a copy of the License at
|
|||
|
*
|
|||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
|
*
|
|||
|
* Unless required by applicable law or agreed to in writing,
|
|||
|
* software distributed under the License is distributed on an
|
|||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|||
|
* KIND, either express or implied. See the License for the
|
|||
|
* specific language governing permissions and limitations
|
|||
|
* under the License.
|
|||
|
*/
|
|||
|
|
|||
|
/* global BATTERY, BUTTON */
|
|||
|
|
|||
|
var app = {
|
|||
|
activeServices: [],
|
|||
|
serviceList: {
|
|||
|
1800: 'Generic Access',
|
|||
|
1801: 'Generic Attribute',
|
|||
|
'180a': 'Device Information',
|
|||
|
|
|||
|
FFE0: 'Button',
|
|||
|
'F000AA00-0451-4000-B000-000000000000': 'Temperature',
|
|||
|
'F000AA20-0451-4000-B000-000000000000': 'Humidity',
|
|||
|
'F000AA80-0451-4000-B000-000000000000': 'Accelerometer',
|
|||
|
'F000AA40-0451-4000-B000-000000000000': 'Barometer'
|
|||
|
|
|||
|
},
|
|||
|
list: {},
|
|||
|
button: {
|
|||
|
service: 'FFE0',
|
|||
|
data: 'FFE1', // Bit 2: side key, Bit 1- right key, Bit 0 –left key
|
|||
|
},
|
|||
|
accelerometer: {
|
|||
|
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
|
|||
|
},
|
|||
|
barometer: {
|
|||
|
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'
|
|||
|
|
|||
|
},
|
|||
|
// Application Constructor
|
|||
|
initialize: function() {
|
|||
|
this.bindEvents();
|
|||
|
},
|
|||
|
doScan: function() {
|
|||
|
'use strict';
|
|||
|
|
|||
|
$('#tbody').empty();
|
|||
|
ble.startScan([], function(device) {
|
|||
|
console.log(JSON.stringify(device));
|
|||
|
|
|||
|
var newId = device.id.replace(/:/g, '');
|
|||
|
console.log(newId);
|
|||
|
|
|||
|
this.list[newId] = device.id;
|
|||
|
|
|||
|
var newTR = $('<tr id="' + newId + '" class="clickRow">');
|
|||
|
|
|||
|
newTR.append($('<td>').text(device.id));
|
|||
|
|
|||
|
if (device.hasOwnProperty('name')) {
|
|||
|
newTR.append($('<td>').text(device.name));
|
|||
|
} else {
|
|||
|
newTR.append($('<td>').text('*** Unknown'));
|
|||
|
}
|
|||
|
|
|||
|
newTR.append($('<td>').text(device.rssi));
|
|||
|
|
|||
|
$('#tbody').append(newTR);
|
|||
|
|
|||
|
$('#output').append(JSON.stringify(device) + '<br/>');
|
|||
|
|
|||
|
}.bind(this), function(e) {
|
|||
|
'use strict';
|
|||
|
console.error(e);
|
|||
|
});
|
|||
|
|
|||
|
|
|||
|
setTimeout(ble.stopScan,
|
|||
|
5000,
|
|||
|
function() { console.log('Scan complete'); },
|
|||
|
function() { console.log('stopScan failed'); }
|
|||
|
);
|
|||
|
|
|||
|
},
|
|||
|
// Bind Event Listeners
|
|||
|
//
|
|||
|
// Bind any events that are required on startup. Common events are:
|
|||
|
// 'load', 'deviceready', 'offline', and 'online'.
|
|||
|
bindEvents: function() {
|
|||
|
var self = this;
|
|||
|
document.addEventListener('deviceready', this.onDeviceReady, false);
|
|||
|
$('#scan').on('click', function() {
|
|||
|
'use strict';
|
|||
|
this.doScan();
|
|||
|
}.bind(this));
|
|||
|
|
|||
|
|
|||
|
|
|||
|
$('#tbody').on('click','tr', function() {
|
|||
|
'use strict';
|
|||
|
var tID = $(this).context.id;
|
|||
|
|
|||
|
var id = self.list[tID];
|
|||
|
|
|||
|
console.log(tID, id);
|
|||
|
|
|||
|
self.connect(id);
|
|||
|
});
|
|||
|
|
|||
|
|
|||
|
},
|
|||
|
// Deviceready Event Handler
|
|||
|
//
|
|||
|
// The scope of 'this' is the event. In order to call the 'receivedEvent'
|
|||
|
// function, we must explicitly call 'app.receivedEvent(...);'
|
|||
|
onDeviceReady: function() {
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}, serviceDiscovery: function(services) {
|
|||
|
'use strict';
|
|||
|
|
|||
|
console.log(services);
|
|||
|
|
|||
|
},
|
|||
|
sensorMpu9250GyroConvert: function(data) {
|
|||
|
return data / (65536 / 500);
|
|||
|
},
|
|||
|
|
|||
|
sensorMpu9250AccConvert: function(data) {
|
|||
|
// Change /2 to match accel range...i.e. 16 g would be /16
|
|||
|
return data / (32768 / 2);
|
|||
|
}
|
|||
|
|
|||
|
,connect: function(deviceId) {
|
|||
|
|
|||
|
$('#results').slideUp();
|
|||
|
console.log('Connect to ', deviceId);
|
|||
|
|
|||
|
var onConnect = function(a) {
|
|||
|
var services = [];
|
|||
|
|
|||
|
'use strict';
|
|||
|
|
|||
|
console.log('A:', a);
|
|||
|
|
|||
|
services = a.services;
|
|||
|
|
|||
|
for (var t = 0; t < services.length;t++) {
|
|||
|
|
|||
|
var ident = services[t].toUpperCase();
|
|||
|
|
|||
|
switch (ident) {
|
|||
|
case '180F':
|
|||
|
var batteryStat = new BATTERY(deviceId);
|
|||
|
batteryStat.startService();
|
|||
|
batteryStat.readBatteryState();
|
|||
|
app.activeServices.push(batteryStat);
|
|||
|
|
|||
|
break;
|
|||
|
case 'FFE0':
|
|||
|
var buttonState = new BUTTON(deviceId);
|
|||
|
buttonState.startService();
|
|||
|
app.activeServices.push(buttonState);
|
|||
|
break;
|
|||
|
|
|||
|
case 'F000AA80-0451-4000-B000-000000000000':
|
|||
|
var cc2650_accel = new CC2650_ACCEL(deviceId);
|
|||
|
cc2650_accel.startService();
|
|||
|
app.activeServices.push(cc2650_accel);
|
|||
|
break;
|
|||
|
|
|||
|
case 'F000AA40-0451-4000-B000-000000000000':
|
|||
|
var cc2650_bar = new CC2650_BAR(deviceId);
|
|||
|
cc2650_bar.startService();
|
|||
|
app.activeServices.push(cc2650_bar);
|
|||
|
break;
|
|||
|
|
|||
|
case 'F000AA70-0451-4000-B000-000000000000':
|
|||
|
var cc2650_lux = new CC2650_LUX(deviceId);
|
|||
|
cc2650_lux.startService();
|
|||
|
app.activeServices.push(cc2650_lux);
|
|||
|
break;
|
|||
|
|
|||
|
default:
|
|||
|
console.log('Unknown service: ', ident);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
};
|
|||
|
|
|||
|
ble.connect(deviceId, onConnect, function(e) {
|
|||
|
'use strict';
|
|||
|
console.error(e);
|
|||
|
});
|
|||
|
|
|||
|
},
|
|||
|
onError: function(reason) {
|
|||
|
console.error('ERROR: ' + reason); // Real apps should use notification.alert
|
|||
|
},
|
|||
|
updateGyro: function(g) {
|
|||
|
'use strict';
|
|||
|
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
app.initialize();
|