-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SerialCmdDecoder.js
107 lines (94 loc) · 3.49 KB
/
SerialCmdDecoder.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
101
102
103
104
105
106
107
var SerialCmdDecoder = (function() {
var RX_MODE = {
CMD:0,
RAW:1
};
var rawDataCmds = [];
/** @type {number[]} uint8 number */
var rxRawBuffer = [];
/** @type {number[]} uint8 number */
var rxCmdBuffer = "";
var lastRxCmd = "";
var lastRxCmdParams = [];
var mode = RX_MODE.CMD;
var cmdTerminator = '\n';
var cmdParamDeliminator = ' ';
var rawTotalLength = 0;
var rawCurrentCount = 0;
var CB_decoded = undefined;
var CB_UI_object_not_found = undefined;
var foundUiObject = {};
/**
*
* @param {Uint8Array} data
*/
function decode(data)
{
//console.log(OSC.getDataArrayAsAsciiAndHex(data));
//return;
for (var i=0;i<data.length;i++)
{
var byte = data[i];
if (mode === RX_MODE.CMD) {
//if (byte != cmdTerminator)
rxCmdBuffer += String.fromCharCode(byte);
if (rxCmdBuffer.endsWith(cmdTerminator) == false) continue;
//else
//{
//if (rxCmdBuffer == cmdTerminator) continue; // empty cmd
lastRxCmdParams = rxCmdBuffer.split(cmdParamDeliminator);
rxCmdBuffer = "";
lastRxCmd = lastRxCmdParams[0];
if (lastRxCmd == cmdTerminator) continue;
//console.log(lastRxCmdParams);
foundUiObject = RED.nodes.namedNode(lastRxCmd);
if (foundUiObject == undefined){
if (CB_UI_object_not_found != undefined)
CB_UI_object_not_found(lastRxCmdParams);
continue;
}
if (foundUiObject._def.uiObject == undefined) continue;
if (foundUiObject.type == "UI_Image") {
//if (rawDataCmds.includes(lastRxCmd)) {
rawTotalLength = parseInt(lastRxCmdParams[1]);
//console.log("rawTotalLength:"+rawTotalLength);
rawCurrentCount = 0;
if (rawTotalLength > 0) {
mode = RX_MODE.RAW;
//console.log("raw rx enabled");
rxRawBuffer = [];
}
}
else {
//console.log(lastRxCmdParams);
if (CB_decoded != undefined)
CB_decoded(lastRxCmdParams,undefined,foundUiObject);
}
//}
}
else if (mode === RX_MODE.RAW)
{
rxRawBuffer.push(byte);
rawCurrentCount++;
if (rawCurrentCount == rawTotalLength) {
mode = RX_MODE.CMD;
//console.log("raw rx disabled");
if (CB_decoded != undefined)
CB_decoded(lastRxCmdParams,rxRawBuffer,foundUiObject);
}
}
}
}
function init(params)
{
CB_decoded = params.CB_decoded;
CB_UI_object_not_found = params.CB_UI_object_not_found;
rawDataCmds = params.rawDataCmds;
cmdTerminator = params.cmdTerminator;
cmdParamDeliminator = params.cmdParamDeliminator;
}
return {
init,
decode
};
})(); // SerialReceiver namespace