Skip to content

Commit 07a6de0

Browse files
committed
support old version irraw
1 parent aa5b061 commit 07a6de0

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

IrRaw-blockly.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
'use strict';
44

5+
/* this function for old version */
6+
window.getIrRaw = function (board, pinMapping) {
7+
return new webduino.module.IrRaw(board, pinMapping);
8+
};
9+
510
window.getIrRawSend = function (board, pinSendIR) {
611
return new webduino.module.IrRawSend(board, pinSendIR);
712
};

IrRaw.js

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/**
2+
* For old version
3+
*/
4+
+(function (factory) {
5+
if (typeof exports === 'undefined') {
6+
factory(webduino || {});
7+
} else {
8+
module.exports = factory;
9+
}
10+
}(function (scope) {
11+
'use strict';
12+
13+
var Module = scope.Module,
14+
BoardEvent = scope.BoardEvent;
15+
var proto;
16+
var sendLen = 32;
17+
var lastSendIR = false;
18+
var debugFlag = false;
19+
20+
function log(obj) {
21+
if (debugFlag) {
22+
console.log(obj);
23+
}
24+
}
25+
26+
function IrRaw(board, pinMapping) {
27+
Module.call(this);
28+
this._board = board;
29+
this.pinSendIR = this.pinRecvIR = -1;
30+
// this = this;
31+
if (typeof pinMapping === 'object') {
32+
if (pinMapping['send']) {
33+
this.pinSendIR = pinMapping['send'];
34+
}
35+
if (pinMapping['recv']) {
36+
this.pinRecvIR = pinMapping['recv'];
37+
}
38+
}
39+
onMessage(this);
40+
}
41+
42+
function onMessage(self) {
43+
self._board.on(webduino.BoardEvent.SYSEX_MESSAGE, function (event) {
44+
var m = event.message;
45+
//send IR data to Board
46+
if (m[0] == 0x04 && m[1] == 0x09 && m[2] == 0x0B) {
47+
log("send IR data to Board callback");
48+
if (lastSendIR) {
49+
//store OK
50+
lastSendIR = false;
51+
log("send pin:" + self.pinSendIR);
52+
self._board.send([0xf0, 0x04, 0x09, 0x0C, self.pinSendIR, 0xF7]);
53+
}
54+
}
55+
//trigger IR send
56+
else if (m[0] == 0x04 && m[1] == 0x09 && m[2] == 0x0C) {
57+
log("trigger IR send callback...");
58+
self.irSendCallback();
59+
}
60+
//record IR data
61+
else if (m[0] == 0x04 && m[1] == 0x09 && m[2] == 0x0D) {
62+
log("record IR callback...");
63+
var strInfo = '';
64+
for (var i = 3; i < m.length; i++) {
65+
strInfo += String.fromCharCode(m[i]);
66+
}
67+
self.irData = strInfo;
68+
self.irRecvCallback(self.irData);
69+
} else {
70+
log(event);
71+
}
72+
});
73+
}
74+
75+
76+
function send(startPos, data, self) {
77+
var CMD = [0xf0, 0x04, 0x09, 0x0A];
78+
var raw = [];
79+
raw = raw.concat(CMD);
80+
var n = '0000' + startPos.toString(16);
81+
n = n.substring(n.length - 4);
82+
for (var i = 0; i < 4; i++) {
83+
raw.push(n.charCodeAt(i));
84+
}
85+
raw.push(0xf7);
86+
// send Data //
87+
CMD = [0xf0, 0x04, 0x09, 0x0B];
88+
raw = raw.concat(CMD);
89+
for (i = 0; i < data.length; i++) {
90+
raw.push(data.charCodeAt(i));
91+
}
92+
raw.push(0xf7);
93+
// console.log(raw);
94+
self._board.send(raw);
95+
}
96+
97+
function sendIRCmd(cmd, len, self) {
98+
for (var i = 0; i < cmd.length; i = i + len) {
99+
var data = cmd.substring(i, i + len);
100+
send(i / 4, data, self);
101+
}
102+
lastSendIR = true;
103+
}
104+
105+
IrRaw.prototype = proto = Object.create(Module.prototype, {
106+
constructor: {
107+
value: IrRaw
108+
}
109+
});
110+
111+
proto.receive = function (callback) {
112+
console.log("receive:" + this.pinRecvIR);
113+
this.irRecvCallback = callback;
114+
if (this.pinRecvIR > 0) {
115+
// this._board.send([0xF0, 0x04, 0x09, 0x0D, this.pinRecvIR, 0xF7]);
116+
this._board.send([0xF0, 0x04, 0x0A, 0x00, 0x02, 0xF7]);
117+
log("wait for receiving...");
118+
}
119+
};
120+
121+
proto.send = function (data, callback) {
122+
console.log("send:" + this.pinSendIR);
123+
if (this.pinSendIR > 0) {
124+
sendIRCmd(data, sendLen, this);
125+
this.irSendCallback = callback;
126+
}
127+
}
128+
129+
proto.debug = function (val) {
130+
if (typeof val == 'boolean') {
131+
this.isDebug = val;
132+
}
133+
}
134+
135+
proto.sendPin = function (pin) {
136+
this.pinSendIR = pin;
137+
}
138+
proto.recvPin = function (pin) {
139+
this.pinRecvIR = pin;
140+
}
141+
142+
scope.module.IrRaw = IrRaw;
143+
}));

0 commit comments

Comments
 (0)