-
Notifications
You must be signed in to change notification settings - Fork 2
/
prepareMessages.js
194 lines (168 loc) · 5.82 KB
/
prepareMessages.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2018 NTT Group
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be included in all copies
// or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
module.exports = function() {
return {
"doc" : function() {
var output = "Available Commands:\n";
output = output + "s [speed] [accel] - Set speed\n";
output = output + " - speed, default: 500, valid: 250? - 1000?\n";
output = output + " - accel, default: 12500, valid: 6250? - 25000?\n";
output = output + "e - End / set speed to zero\n";
output = output + "c [offset] - Change lange\n";
output = output + " - offset, default: 0, valid: (-68) - (+68)\n";
output = output + "o [offset] - Set initial offset\n";
output = output + " - offset, default: 0, valid: (-68) - (+68)\n";
output = output + "l - Change lights\n";
output = output + "lp - Change lights pattern\n";
output = output + "sdk [on/off] - Turn on/off SDK\n";
output = output + " - on/off, default: on, valid: on, off\n";
output = output + "ping - Ping car\n";
output = output + "bat - Get battery level\n";
output = output + "ver - Get version\n";
output = output + "q - Disconnect and quit\n";
output = output + "help - Get help documentation";
return output;
},
"format" : function(command) {
var message = null;
var cmd;
var commandArray;
if (command.indexOf(' ') == -1) {
cmd = command;
}
else {
commandArray = command.split(' ');
if (commandArray.length > 0) {
cmd = commandArray[0];
}
}
if (cmd == "s") { // set speed
// input parameter 1: speed, default: 500, valid: 250? - 1000?
// input parameter 2: accel, default: 12500, valid: 6250? - 25000?
var speed = 500;
var accel = 12500;
if (commandArray) {
if (commandArray.length > 1) {
speed = commandArray[1];
}
if (commandArray.length > 2) {
accel = commandArray[2];
}
}
message = new Buffer(7);
message.writeUInt8(0x06, 0);
message.writeUInt8(0x24, 1);
message.writeInt16LE(speed, 2);
message.writeInt16LE(accel, 4);
}
if (cmd == "e") { // end/set speed to zero
message = new Buffer(7);
message.writeUInt8(0x06, 0);
message.writeUInt8(0x24, 1);
message.writeInt16LE(0x00, 2);
message.writeInt16LE(12500, 4);
}
if (cmd == "c") { // change lane
var lane = 1;
//lane offsets where 68 is most inner and -68 most outer
var laneOffsets = [68, 23, -23, -68];
if (commandArray) {
if (commandArray.length > 1) {
lane = commandArray[1];
if(lane < 0 || lane > 4)
lane = 1;
}
}
var laneOffset = laneOffsets[lane-1];
message = new Buffer(12);
message.writeUInt8(11, 0);
message.writeUInt8(0x25, 1);
message.writeInt16LE(250, 2);
message.writeInt16LE(1000, 4);
message.writeFloatLE(laneOffset, 6);
}
if (cmd == "o") { // set offset
// example: o -23
// input parameter 1: offset, default: 0, valid: -68 - 68
var offset = -68.0;
if (commandArray) {
if (commandArray.length > 1) {
offset = commandArray[1];
}
}
message = new Buffer(6);
message.writeUInt8(0x05, 0);
message.writeUInt8(0x2c, 1);
message.writeFloatLE(-68.0, 2);
}
if (cmd == "l") { // change lights
//On
message = new Buffer(3);
message.writeUInt8(0x02, 0);
message.writeUInt8(0x1d, 1);
message.writeUInt8(140, 2);
}
if (cmd == "lp") { // change lights pattern
//off
message = new Buffer(3);
message.writeUInt8(0x02, 0);
message.writeUInt8(0x1d, 1);
message.writeUInt8(15, 2);
}
if (cmd == "sdk") { // turn on sdk
var value = "on";
if (commandArray) {
if (commandArray.length > 1) {
paramValue = commandArray[1];
if (paramValue == "off") value = "off";
}
}
message = new Buffer(4);
message.writeUInt8(0x03, 0);
message.writeUInt8(0x90, 1);
if (value == "on") {
message.writeUInt8(0x01, 2);
}
else {
message.writeUInt8(0x00, 2);
console.log('off');
}
message.writeUInt8(0x01, 2);
}
if (cmd == "ping") { // ping
message = new Buffer(2);
message.writeUInt8(0x01, 0);
message.writeUInt8(0x16, 1);
}
if (cmd == "bat") { // request battery level
message = new Buffer(2);
message.writeUInt8(0x01, 0);
message.writeUInt8(0x1a, 1);
}
if (cmd == "ver") { // request version
message = new Buffer(2);
message.writeUInt8(0x01, 0);
message.writeUInt8(0x18, 1);
}
if (cmd == "q") { // quit - stop and disconnect
message = new Buffer(2);
message.writeUInt8(0x01, 0);
message.writeUInt8(0x0d, 1);
}
return message;
}
};
};