97 lines
2.3 KiB
JavaScript
97 lines
2.3 KiB
JavaScript
|
/**
|
|||
|
*
|
|||
|
* User: Martin Donnelly
|
|||
|
* Date: 2016-05-20
|
|||
|
* Time: 10:13
|
|||
|
*
|
|||
|
*/
|
|||
|
/* global CAPABILITY, inheritsFrom */
|
|||
|
/* global ble */
|
|||
|
/* jshint browser: true , devel: true*/
|
|||
|
|
|||
|
var BUTTON = function(deviceId) {
|
|||
|
'use strict';
|
|||
|
this.name = 'Button';
|
|||
|
this.deviceID = deviceId;
|
|||
|
this.capabilityID = 'FFE0';
|
|||
|
this.serviceDef = {
|
|||
|
service: 'FFE0',
|
|||
|
data: 'FFE1' // Bit 2: side key, Bit 1- right key, Bit 0 –left key
|
|||
|
};
|
|||
|
|
|||
|
this.buttonMatrix = {
|
|||
|
LEFT_BUTTON: 1, // 0001
|
|||
|
RIGHT_BUTTON: 2, // 0010
|
|||
|
REED_SWITCH: 4 // 0100
|
|||
|
};
|
|||
|
|
|||
|
this.onButtonData = function(data) {
|
|||
|
console.log('+ onButtonData');
|
|||
|
console.log(data);
|
|||
|
var state = new Uint8Array(data);
|
|||
|
var message = '';
|
|||
|
|
|||
|
if (state === 0) {
|
|||
|
message = 'No buttons are pressed.';
|
|||
|
}
|
|||
|
|
|||
|
if (state & this.buttonMatrix.LEFT_BUTTON) {
|
|||
|
message += 'Left button is pressed. ';
|
|||
|
}
|
|||
|
|
|||
|
if (state & this.buttonMatrix.RIGHT_BUTTON) {
|
|||
|
message += 'Right button is pressed.';
|
|||
|
}
|
|||
|
|
|||
|
if (state & this.buttonMatrix.REED_SWITCH) {
|
|||
|
message += 'Reed switch is activated.<br/>';
|
|||
|
}
|
|||
|
|
|||
|
this.state = message;
|
|||
|
|
|||
|
if (this.$result !== null)
|
|||
|
{
|
|||
|
this.$result.text(this.state);
|
|||
|
}
|
|||
|
console.log('ButtonState: ', this.state);
|
|||
|
|
|||
|
console.log('- onButtonData');
|
|||
|
};
|
|||
|
|
|||
|
this.startService = function() {
|
|||
|
'use strict';
|
|||
|
if (this.deviceID !== null) {
|
|||
|
|
|||
|
console.log('Starting Button Service on ', this.deviceID);
|
|||
|
console.log(this.serviceDef);
|
|||
|
ble.startNotification(this.deviceID, this.serviceDef.service, this.serviceDef.data, this.onButtonData.bind(this), this.onError);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
this.insertFrame();
|
|||
|
};
|
|||
|
|
|||
|
this.insertFrame = function() {
|
|||
|
|
|||
|
var self = this;
|
|||
|
console.log('Overloading...');
|
|||
|
// Call the parent displayForm first...
|
|||
|
this.superClass_.insertFrame.call(self);
|
|||
|
|
|||
|
var detail = this.frameID + '-d';
|
|||
|
var row = $('<div />', {class: 'mui-row'});
|
|||
|
|
|||
|
$('<div />', { class: 'mui-col-xs-4 mui--text-accent', text: 'Status:'}).appendTo(row);
|
|||
|
|
|||
|
$('<div />', { class: 'mui-col-xs-8 mui--text-white', id: detail}).appendTo(row);
|
|||
|
|
|||
|
this.$id.append(row);
|
|||
|
this.$result = $('#'+detail);
|
|||
|
|
|||
|
};
|
|||
|
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
inheritsFrom(BUTTON, CAPABILITY);
|