-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathJVS.cpp
298 lines (283 loc) · 6.7 KB
/
JVS.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* JVS.cpp
*
* Created on: 14/apr/2014
* Author: k4roshi
*/
#include "Arduino.h"
#include "JVS.h"
#include "constants.h"
JVS::JVS(HardwareSerial& serial) :
_Uart(serial) // Need to initialize references before body
{
coins1 = 0;
coins2 = 0;
coin_pressed_at = 0;
initialized = false;
shift_mode = false;
pressed_smth = false;
old_key = 0;
}
void JVS::reset() {
char str[] = { CMD_RESET, CMD_RESET_ARG };
this->write_packet(BROADCAST, str, 2);
delay(ASSIGN_DELAY);
Serial.println("RESET");
}
void JVS::assign(int attempt) {
char str[] = { CMD_ASSIGN_ADDR, attempt };
this->cmd(BROADCAST, str, 2);
Serial.println("ADDR");
}
void JVS::init(int board) {
Serial.println("ADDR");
char str[] = { CMD_ASSIGN_ADDR, board };
this->cmd(BROADCAST, str, 2);
Serial.println("REQ");
char str1[] = { CMD_REQUEST_ID };
this->cmd(board, str1, 1);
Serial.println("CMD");
char str2[] = { CMD_COMMAND_VERSION };
this->cmd(board, str2, 1);
Serial.println("JVS");
char str3[] = { CMD_JVS_VERSION };
this->cmd(board, str3, 1);
Serial.println("CMS");
char str4[] = { CMD_COMMS_VERSION };
this->cmd(board, str4, 1);
Serial.println("CAP");
char str5[] = { CMD_CAPABILITIES };
this->cmd(board, str5, 1);
initialized = true;
}
void JVS::switches(int board) {
char str[] = { CMD_READ_DIGITAL, 0x02, 0x02, CMD_READ_COINS, 0x02, CMD_READ_ANALOG, 0x04};
//char str[ ] = { 0x20, 0x02, 0x02, 0x21, 0x02, 0x22, 0x08};
this->write_packet(board, str, sizeof str);
char incomingByte;
while (!_Uart.available()) {
}
while (_Uart.read() != 0xE0) {
} // wait for sync
while (_Uart.read() != 0) {
} // only if it's for me
while (!_Uart.available()) {
} // wait for length
int length = _Uart.read();
int counter = 0;
int x = 512;
int y = 512;
int z = 512;
int zA = 512;
int coin1 = 0;
int key = 0;
bool old_shift = shift_mode;
if (DEBUG_MODE) {
Serial.print("swthc: E0 0 ");
Serial.print(length, HEX);
}
while (counter < length) {
while (!_Uart.available()) {
}
incomingByte = _Uart.read();
if (DEBUG_MODE){
Serial.print(" ");
Serial.print(incomingByte, HEX);
}
switch (counter) {
// fourth byte (first three bytes are sync and
case 3:
// p1 b1
shift_mode = bitRead(incomingByte, 7);
if (shift_mode != old_shift) {
if (shift_mode == 0 && !pressed_smth)
//never pressed anything, wants start
key = KEY_1;
else
pressed_smth = false;
}
if (shift_mode) {
// Serial.println("shiftmode on");
if(bitRead(incomingByte, 1))
key = KEY_5;
if(bitRead(incomingByte, 2))
key = KEY_TAB;
if(bitRead(incomingByte, 3))
key = KEY_ENTER;
if(bitRead(incomingByte, 4))
key = KEY_P;
if (key)
pressed_smth = true;
} else {
Joystick.button(1,bitRead(incomingByte, 1));
Joystick.button(2,bitRead(incomingByte, 0));
if bitRead(incomingByte, 2)
x += 511;
if bitRead(incomingByte, 3)
x -= 512;
Joystick.X(x);
if bitRead(incomingByte, 4)
y += 511;
if bitRead(incomingByte, 5)
y -= 512;
Joystick.Y(y);
Joystick.button(7,bitRead(incomingByte, 6));
}
break;
case 4:
// p1 b2
if (shift_mode) {
} else {
Joystick.button(9,bitRead(incomingByte, 2));
Joystick.button(10,bitRead(incomingByte, 3));
Joystick.button(11,bitRead(incomingByte, 4));
Joystick.button(12,bitRead(incomingByte, 5));
Joystick.button(13,bitRead(incomingByte, 6));
Joystick.button(14,bitRead(incomingByte, 7));
}
break;
case 5:
// p2 b1
if (shift_mode) {
if(bitRead(incomingByte, 7))
key = KEY_ESC;
if (key)
pressed_smth = true;
} else {
Joystick.button(17,bitRead(incomingByte, 0));
Joystick.button(18,bitRead(incomingByte, 1));
if bitRead(incomingByte, 2)
zA += 511;
if bitRead(incomingByte, 3)
zA -= 512;
Joystick.Zrotate(zA);
if bitRead(incomingByte, 4)
z += 511;
if bitRead(incomingByte, 5)
z -= 512;
Joystick.Z(z);
Joystick.button(23,bitRead(incomingByte, 6));
if bitRead(incomingByte, 7)
Keyboard.press(KEY_2);
else
Keyboard.release(KEY_2);
}
break;
case 6:
// p2 b2
if (shift_mode) {
} else {
Joystick.button(25,bitRead(incomingByte, 2));
Joystick.button(26,bitRead(incomingByte, 3));
Joystick.button(27,bitRead(incomingByte, 4));
Joystick.button(28,bitRead(incomingByte, 5));
Joystick.button(29,bitRead(incomingByte, 6));
Joystick.button(30,bitRead(incomingByte, 7));
}
break;
case 8:
// coins 1 status
break;
case 9:
// coins1
if (incomingByte > coins1) {
// added coin
coin1 = 1;
coins1 = incomingByte;
}
if (coin1){
coin_pressed_at = millis();
Keyboard.press(KEY_5);
} else if (coin_pressed_at > 0){
if (millis() - coin_pressed_at > 50){
coin_pressed_at = 0;
Keyboard.release(KEY_5);
}
}
break;
case 10:
// coins2 status
break;
case 11:
// coins2
break;
}
counter++;
Joystick.send_now();
}
if (key){
if (key != old_key){
old_key = key;
// Serial.print("pressed key: ");
// Serial.println(key, HEX);
Keyboard.set_key1(key);
}
} else {
if (old_key){
// Serial.print("old_key: ");
// Serial.println(old_key, HEX);
Keyboard.set_key1(0);
old_key = 0;
}
}
Keyboard.send_now();
delay(SWCH_DELAY);
// if (coins1 > 0){
// char str1[ ] = {CMD_DECREASE_COIN};
// this->cmd(board, str1, 1);
// }
if (DEBUG_MODE)
Serial.println();
}
int* JVS::cmd(char destination, char data[], int size) {
this->write_packet(destination, data, size);
char incomingByte;
while (!_Uart.available()) {
}
while (_Uart.read() != 0xE0) {
} // wait for sync
while (_Uart.read() != 0) {
} // only if it's for me
while (!_Uart.available()) {
} // wait for length
int length = _Uart.read();
Serial.print("Received: E0 0 ");
Serial.print(length, HEX);
int counter = 0;
int * res = (int *) malloc(length * sizeof(int));
while (counter < length) {
while (!_Uart.available()) {
}
incomingByte = _Uart.read();
res[counter] = incomingByte;
// actually do something with incomingByte
Serial.print(" ");
Serial.print(res[counter], HEX);
counter++;
}
Serial.println();
delay(CMD_DELAY);
return res;
}
void JVS::write_packet(char destination, char data[], int size) {
_Uart.write(SYNC);
_Uart.write(destination);
_Uart.write(size + 1);
char sum = destination + size + 1;
for (int i = 0; i < size; i++) {
if (data[i] == SYNC || data[i] == ESCAPE) {
_Uart.write(ESCAPE);
_Uart.write(data[i] - 1);
} else {
_Uart.write(data[i]);
}
sum = (sum + data[i]) % 256;
}
_Uart.write(sum);
_Uart.flush();
}
//Button debugs
//Serial.print("1P1: ");
//for (int i = 0; i<=7; i++)
// Serial.print(bitRead(incomingByte, i));
//Serial.println();