-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcommand.js
More file actions
48 lines (40 loc) · 989 Bytes
/
command.js
File metadata and controls
48 lines (40 loc) · 989 Bytes
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
var Promise = require('promise');
function CCCommand(src, dest, command, data)
{
if(src instanceof Uint8Array)
{
// parse command
var buffer = src;
this.src = buffer[2];
this.dest = buffer[0];
this.command = buffer[3];
this.data = buffer.slice(4, buffer[1]+4);
// TODO: checksum
}
else
{
// create command
this.src = typeof src != undefined ? src : 1;
this.dest = typeof dest != undefined ? dest : 2;
this.command = command;
this.data = data;
}
}
CCCommand.prototype =
{
toBuffer: function toBuffer()
{
var buffer = new Uint8Array(5 + this.data.length);
buffer[0] = this.dest;
buffer[1] = this.data.length;
buffer[2] = this.src;
buffer[3] = this.command;
buffer.set(this.data, 4);
var sum = 0;
for (var i=0; i < (buffer.length - 1); ++i)
sum += (buffer[i]);
buffer[this.data.length+4] = 0x100 - sum%0x100;
return buffer;
}
};
module.exports = exports = CCCommand;