-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathIRRecv.js
More file actions
160 lines (138 loc) · 3.76 KB
/
IRRecv.js
File metadata and controls
160 lines (138 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
+(function (global, factory) {
if (typeof exports === 'undefined') {
factory(global.webduino || {});
} else {
module.exports = factory;
}
}(this, function (scope) {
'use strict';
var Module = scope.Module,
BoardEvent = scope.BoardEvent,
proto;
var IRRecvEvent = {
/**
* Fires when receiving data.
*
* @event IRRecvEvent.MESSAGE
*/
MESSAGE: 'message',
/**
* Fires when error occured while receiving data.
*
* @event IRRecvEvent.MESSAGE_ERROR
*/
MESSAGE_ERROR: 'messageError'
};
/**
* The IRRecv Class.
*
* @namespace webduino.module
* @class IRRecv
* @constructor
* @param {webduino.Board} board The board that the IRLed is attached to.
* @param {Integer} pin The pin that the IRLed is connected to.
* @extends webduino.Module
*/
function IRRecv(board, pin) {
Module.call(this);
this._board = board;
this._pin = pin;
this._messageHandler = onMessage.bind(this);
this._recvCallback = function () {};
this._recvErrorCallback = function () {};
this._board.send([0xf0, 0x04, 0x0A, 0x01, 0xf7]);
}
function onMessage(event) {
var recvChk = [0x04, 0x10];
var msg = event.message;
var data = msg.slice(2);
var str = '';
var i, tp, len;
for (i = 0, len = recvChk.length; i < len; i++) {
if (recvChk[i] !== msg[i]) {
return false;
}
}
for (i = 0; i < data.length; i++) {
tp = String.fromCharCode(data[i]);
str += tp.toLowerCase();
}
if (str !== 'ffffffff') {
this.emit(IRRecvEvent.MESSAGE, str);
} else {
this.emit(IRRecvEvent.MESSAGE_ERROR, str, msg);
}
}
IRRecv.prototype = proto = Object.create(Module.prototype, {
constructor: {
value: IRRecv
},
/**
* The state indicating whether the IRLed is receiving.
*
* @attribute state
* @type {String} `on` or `off`
*/
state: {
get: function () {
return this._state;
},
set: function (val) {
this._state = val;
}
}
});
/**
* Start detection.
*
* @method receive
* @param {Function} [callback] Detection callback.
* @param {Function} [errorCallback] Error callback while Detection.
*/
/**
* Start detection.
*
* @method on
* @param {Function} [callback] Detection callback.
* @param {Function} [errorCallback] Error callback while Detection.
* @deprecated `on()` is deprecated, use `receive()` instead.
*/
proto.receive = proto.on = function (callback, errorCallback) {
var aryCode = [0xf0, 0x04, 0x0A, 0x00];
if (typeof callback !== 'function') {
callback = function () {};
}
if (typeof errorCallback !== 'function') {
errorCallback = function () {};
}
if (this._pin) {
aryCode.push(this._pin.number, 0xf7);
this._board.send(aryCode);
this._state = 'on';
this._recvCallback = function (value) {
callback(value);
};
this._recvErrorCallback = function (value, msg) {
errorCallback(value, msg);
};
this._board.on(BoardEvent.SYSEX_MESSAGE, this._messageHandler);
this.addListener(IRRecvEvent.MESSAGE, this._recvCallback);
this.addListener(IRRecvEvent.MESSAGE_ERROR, this._recvErrorCallback);
}
};
/**
* Stop detection.
*
* @method off
*/
proto.off = function () {
this._board.send([0xf0, 0x04, 0x0A, 0x01, 0xf7]);
this._state = 'off';
this._board.removeListener(BoardEvent.SYSEX_MESSAGE, this._messageHandler);
this.removeListener(IRRecvEvent.MESSAGE, this._recvCallback);
this.removeListener(IRRecvEvent.MESSAGE_ERROR, this._recvErrorCallback);
this._recvCallback = null;
this._recvErrorCallback = null;
};
scope.module.IRRecv = IRRecv;
}));