-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLORA.js
100 lines (92 loc) · 2.46 KB
/
LORA.js
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
+(function (factory) {
if (typeof exports === 'undefined') {
factory(webduino || {});
} else {
module.exports = factory;
}
}(function (scope) {
'use strict';
var self;
var proto;
var sendArray = [];
var sending = false;
var sendAck = '';
var sendCallback;
var Module = scope.Module;
var _backlight;
function LORA(board, rstPin, address) {
Module.call(this);
this._board = board;
self = this;
self._rstPin = rstPin;
self._address = address;
self._callbackAckOK = function () {}
self._callbackAckErr = function () {}
self._callbackRecvAckOK = function () {}
self.recvData = '';
var cmd = [0xF0, 0x04, 0x22, 0x0 /*init*/ , rstPin, address, 0xF7];
board.send(cmd);
board.on(webduino.BoardEvent.SYSEX_MESSAGE,
function (event) {
var m = event.message;
//send Ack response
if (m.length == 4 && m[2] == 2) {
var state = m[3] - 48; //ascii to int
if (state == 0) {
self._callbackAckOK();
} else {
self._callbackAckErr();
}
}
//recv Ack response
if (m[2] == 4) {
var data = '';
for (var i = 3; i < m.length; i++) {
data += String.fromCharCode(m[i]);
}
self.recvData = data;
self._callbackRecvAckOK();
}
sending = false;
});
}
LORA.prototype = proto = Object.create(Module.prototype, {
constructor: {
value: LORA
}
});
proto.send = function (strData) {
var cmd = [0xF0, 0x04, 0x22, 0x01, this._address /*Address*/ ];
cmd = cmd.concat(toASCII(strData));
cmd.push(0xF7);
this._board.send(cmd);
}
proto.sendAck = function (strData, ackOK, ackErr) {
if (arguments.length == 2) {
self._callbackAckOK = ackOK;
}
if (arguments.length == 3) {
self._callbackAckOK = ackOK;
self._callbackAckErr = ackErr;
}
var cmd = [0xF0, 0x04, 0x22, 0x02, this._address /*Address*/ ];
cmd = cmd.concat(toASCII(strData));
cmd.push(0xF7);
this._board.send(cmd);
}
proto.recvAck = function (ackOK) {
if (arguments.length == 1) {
self._callbackRecvAckOK = ackOK;
}
var cmd = [0xF0, 0x04, 0x22, 0x04, this._address /*Address*/ , 0xF7];
this._board.send(cmd);
}
function toASCII(str) {
var data = [];
for (var i = 0; i < str.length; i++) {
data.push(str.charCodeAt(i));
}
return data;
}
scope.module.LORA = LORA;
}));