sensortoy/app/js/standards/button.js

102 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
*
* User: Martin Donnelly
* Date: 2016-05-20
* Time: 10:13
*
*/
/* global CAPABILITY, inheritsFrom, capabilityManager */
/* global ble */
/* jshint browser: true , devel: true*/
var BUTTON = function(p) {
'use strict';
this.name = 'Button';
this.deviceID = p.deviceID || null;
this.target = p.target || null;
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.setFrame();
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-dark', id: detail}).appendTo(row);
this.$id.append(row);
this.$result = $('#'+detail);
};
};
inheritsFrom(BUTTON, CAPABILITY);
capabilityManager.register({id: 'FFE0', module: BUTTON});