sensortoy/app/js/standards/button.js

102 lines
2.4 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, capabilityManager */
2016-05-20 16:10:40 +00:00
/* global ble */
/* jshint browser: true , devel: true*/
2016-06-01 15:03:59 +00:00
var BUTTON = function(p) {
2016-05-20 16:10:40 +00:00
'use strict';
this.name = 'Button';
2016-06-01 15:03:59 +00:00
this.deviceID = p.deviceID || null;
this.target = p.target || null;
2016-05-20 16:10:40 +00:00
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
};
2016-06-01 15:03:59 +00:00
this.setFrame();
2016-05-20 16:10:40 +00:00
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);
2016-06-01 15:03:59 +00:00
$('<div />', { class: 'mui-col-xs-8 mui--text-dark', id: detail}).appendTo(row);
2016-05-20 16:10:40 +00:00
this.$id.append(row);
this.$result = $('#'+detail);
};
};
inheritsFrom(BUTTON, CAPABILITY);
2016-06-01 15:03:59 +00:00
capabilityManager.register({id: 'FFE0', module: BUTTON});