-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathd_BacpacCommands.ino
More file actions
391 lines (385 loc) · 10.4 KB
/
d_BacpacCommands.ino
File metadata and controls
391 lines (385 loc) · 10.4 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#define MEWPRO_VERSION_STRING "2016021200"
void printHex(uint8_t d, boolean upper)
{
char t;
char a = upper ? 'A' : 'a';
t = d >> 4 | '0';
if (t > '9') {
t += a - '9' - 1;
}
Serial.print(t);
t = d & 0xF | '0';
if (t > '9') {
t += a - '9' - 1;
}
Serial.print(t);
}
void cameraCommand()
{
switch ((RECV(1) << 8) + RECV(2)) {
case GET_CAMERA_INFO:
memcpy((char *)buf, "\x25\x00\x01\x04\x0cHD3.11.03.03\x14HERO3+ Black Edition", 0x26); // the dongle will act as a HERO3+ Black
// memcpy((char *)buf, "\x20\x00\x01\x04\x0fHD4.01.02.00.00\x0cHERO4 Silver", 0x21); // not supported yet
// memcpy((char *)buf, "\x1f\x00\x01\x04\x0fHD4.02.02.00.00\x0bHERO4 Black", 0x20); // not supported yet
SendBufToBacpac();
break;
case GET_CAMERA_WIFI_STATUS:
if (isMaster) {
buf[0] = 2; buf[1] = 0; buf[2] = 0; // wifi off
} else {
buf[0] = 1; buf[1] = 1; // NAK
}
SendBufToBacpac();
queueIn("WI0"); // wifi LED off
break;
/*********************************************************************
data structure for {GET|SET}_CAMERA_SETTING is the same as follows: */
//
// buf[] RECV() meaning and/or relating bacpac command
// -----+-----+-----------------------------------------------------
// 0x00 0x00 packet length (0x27 or 0x28)
// 0x01 always 0
// 0x01 always 'T'
// 0x02 always 'D'
// TM SET_BACPAC_DATE_TIME if [3:8] == 0 then no need to set time
// 0x02 0x03 year (0-99)
// 0x03 0x04 month (1-12)
// 0x04 0x05 day (1-31)
// 0x05 0x06 hour (0-23)
// 0x06 0x07 minute (0-59)
// 0x07 0x08 second (0-59)
// 0x08 0x09 CM SET_BACPAC_MODE
// 0x09 0x0a PR SET_BACPAC_PHOTO_RESOLUTION
// 0x0a 0x0b VR SET_BACPAC_VIDEORESOLUTION (Defunct; always 0xff)
// 0x0b 0x0c VV SET_BACPAC_VIDEORESOLUTION_VV
// 0x0c 0x0d FS SET_BACPAC_FRAMES_PER_SEC
// 0x0d 0x0e FV SET_BACPAC_FOV
// 0x0e 0x0f EX SET_BACPAC_EXPOSURE
// 0x0f 0x10 TI SET_BACPAC_PHOTO_XSEC
// 0x10 0x11 TS SET_BACPAC_TIME_LAPSE (Defunct; always 0xff)
// 0x11 0x12 BS SET_BACPAC_BEEP_SOUND
// 0x12 0x13 VM SET_BACPAC_NTSC_PAL
// 0x13 0x14 DS SET_BACPAC_ONSCREEN_DISPLAY
// 0x14 0x15 LB SET_BACPAC_LEDBLINK
// 0x15 0x16 PN SET_BACPAC_PHOTO_INVIDEO
// 0x16 0x17 LO SET_BACPAC_LOOPING_MODE
// 0x17 0x18 CS SET_BACPAC_CONTINUOUS_SHOT
// 0x18 0x19 BU SET_BACPAC_BURST_RATE
// 0x19 0x1a PT SET_BACPAC_PROTUNE_MODE
// 0x1a 0x1b AO SET_BACPAC_AUTO_POWER_OFF
// 0x1b 0x1c WB SET_BACPAC_WHITE_BALANCE
// 0x1c 0x1d (reserved)
// 0x1d 0x1e (reserved)
// 0x1e 0x1f (reserved)
// 0x1f 0x20 (reserved)
// 0x20 0x21 (reserved)
// 0x21 0x22 (reserved)
// 0x22 0x23 UP SET_BACPAC_FLIP_MIRROR
// 0x23 0x24 DM SET_BACPAC_DEFAULT_MODE
// 0x24 0x25 CO SET_BACPAC_PROTUNE_COLOR
// 0x25 0x26 GA SET_BACPAC_PROTUNE_GAIN
// 0x26 0x27 SP SET_BACPAC_PROTUNE_SHARPNESS
// 0x27 0x28 EV SET_BACPAC_PROTUNE_EXPOSURE_VALUE
// -----+-----+-----------------------------------------------------
case GET_CAMERA_SETTING:
if (!heartBeatIsOn) {
for (int i = 0; i < TD_BUFFER_SIZE; i++) { // initialize td[] from PGMMEM constant array
td[i] = pgm_read_byte(td_init + i);
}
}
memcpy(buf + 2, td + 3, TD_BUFFER_SIZE - 3);
buf[0] = 0x27; buf[1] = 0;
SendBufToBacpac();
if (isMaster) {
if (heartBeatIsOn) { // send to slaves
Serial.print("TD");
for (int i = 3; i < TD_BUFFER_SIZE; i++) {
printHex(td[i], true);
}
Serial.println("");
}
heartBeatIsOn = true;
}
break;
case SET_CAMERA_SETTING:
__debug(F("camera settings received"));
for (int i = 0; i < TD_BUFFER_SIZE; i++) {
td[i] = RECV(i);
}
// Upside is always up
td[TD_FLIP_MIRROR] = 1;
//
if (heartBeatIsOn) { // send to slaves
Serial.print("TD");
for (int i = 3; i < TD_BUFFER_SIZE; i++) {
printHex(td[i], true);
}
Serial.println("");
}
buf[0] = 1; buf[1] = 0; // ok
SendBufToBacpac();
if (!heartBeatIsOn) { // I'm slave
// Apply received TD
buf[0] = 0x83; buf[1] = 'F'; buf[2] = 'N'; buf[3] = 0x0C;
// finished!
SendBufToBacpac(); // report to Primary Camera
heartBeatIsOn = true;
}
break;
/*********************************************************************/
case SET_CAMERA_VIDEO_OUTPUT:
buf[0] = 2; buf[1] = 0; buf[2] = RECV(3);
SendBufToBacpac();
break;
case SET_CAMERA_SLAVE_SETTINGS:
if (isMaster) {
// received slave camera configurations
} else {
switch (RECV(3)) {
case 0:
heartBeatIsOn = false;
break;
case 1:
heartBeatIsOn = true;
break;
default:
break;
}
}
buf[0] = 1; buf[1] = 0; // ok
SendBufToBacpac();
break;
case SET_CAMERA_POWER_STATE:
if (RECV(3) == 0) {
if (!dontSendPW) {
Serial.println(F("PW00")); // send to slaves
digitalWrite(BUILTINLED, HIGH);
digitalWrite(HBUSRDY, LOW);
}
dontSendPW = false;
heartBeatIsOn = false;
}
#ifdef SOFTWARE_RESET
wdt_enable(WDTO_1S);
while (1); // loop forever
// never reach here
#endif
break;
case SET_CAMERA_FAULT:
switch (RECV(3)) {
case 0x01:
__debug(F("low battery"));
buf[0] = 1; buf[1] = 0;
SendBufToBacpac();
goto commandError;
case 0x03:
__debug(F("No SD"));
break;
case 0x04:
case 0x05:
__debug(F("cameras incompatible"));
break;
case 0x06:
__debug(F("3D Sync Lost"));
buf[0] = 2; buf[1] = 0; buf[2] = 1;
SendBufToBacpac();
// panic!!
digitalWrite(BUILTINLED, HIGH);
digitalWrite(HBUSRDY, LOW); // show bacpac as if the camera has rebooted
delay(200);
digitalWrite(HBUSRDY, HIGH);
goto commandError;
case 0x0a:
__debug(F("microSD write error received")); // ??
break;
case 0x0b:
__debug(F("capture end received"));
break;
case 0x0c:
__debug(F("SC_APPLIED_TD received"));
break;
default:
__debug(F("Unknown notification received"));
break;
}
buf[0] = 2; buf[1] = 0; buf[2] = RECV(3); // "ok"
SendBufToBacpac();
break;
case SET_CAMERA_3D_SYNCHRONIZE:
// Master shutter button depressed
if (1) { // send to slaves
Serial.print("SY");
printHex(RECV(3), true);
Serial.println("");
}
buf[0] = 0x83; buf[1] = 'S'; buf[2] = 'R'; buf[3] = RECV(3);
if (buf[3] == 1 && (!isMaster || td[TD_MODE] != MODE_TIMELAPSE)) {
delay(1000); // start of a capture requres additional delay
}
delay(140);
SendBufToBacpac();
if (buf[3] == 0 && td[TD_MODE] != MODE_TIMELAPSE) {
queueIn("SR3"); // notify video saved
delay(2000);
}
if (td[TD_MODE] == MODE_TIMELAPSE) {
switch (RECV(3)) {
case 1:
case 2:
timelapse = 1900;
break;
default:
timelapse = 0;
}
previous_shutter = millis();
}
break;
case 0: // varidation string
default:
break;
}
commandError:
recvb = (recvb + (recv[recvb] & 0x7F) + 1) % MEWPRO_BUFFER_LENGTH;
}
void readEEPROM()
{
char c;
int i;
if (debug) {
Serial.print(F("eeprom:"));
}
readEEPROM_RETRY:
for (i = 0; i < 16; i++) {
WIRE.beginTransmission(I2CEEPROM);
WIRE.write((byte)i);
WIRE.endTransmission(I2C_NOSTOP);
WIRE.requestFrom(I2CEEPROM, 1, I2C_STOP);
if (WIRE.available()) {
c = WIRE.read();
if (i == 0) {
switch (c) {
case 4:
isMaster = true;
break;
case 5:
isMaster = false;
break;
default:
goto readEEPROM_RETRY;
}
}
if (debug) {
Serial.print(" ");
printHex(c, false);
}
} else {
goto readEEPROM_RETRY;
}
}
if (debug) {
Serial.println("");
}
// notify SMARTY that ROM has read successfully
digitalWrite(PWRBTN, LOW); pinMode(PWRBTN, OUTPUT);
delay(200);
pinMode(PWRBTN, INPUT_PULLUP);
}
void _I2CmasterRead()
{
int datalen;
int i = 1;
WIRE.requestFrom(SMARTY, TD_BUFFER_SIZE, I2C_STOP);
if (WIRE.available()) {
recv[recve] = WIRE.read();
} else {
return;
}
datalen = recv[recve] & 0x7f;
while (datalen-- && WIRE.available()) {
recv[(recve + i++) % MEWPRO_BUFFER_LENGTH] = WIRE.read();
}
_printInput(recve);
recve = (recve + i) % MEWPRO_BUFFER_LENGTH;
}
void checkCommands()
{
if (recvq) {
recvq = false;
_I2CmasterRead();
cameraCommand();
}
while (inputAvailable()) {
static boolean shiftable;
byte c = myRead();
switch (c) {
case ' ':
continue;
case '\r':
case '\n':
serialfirst = false;
if (bufp != 1) {
buf[0] = bufp - 1 | 0x80;
bufp = 1;
SendBufToBacpac();
}
return;
case '&':
bufp = 1;
debug = !debug;
serialfirst = false;
__debug(F("debug messages on"));
while (inputAvailable()) {
if (myRead() == '\n') {
return;
}
}
return;
case '@':
bufp = 1;
serialfirst = false;
__debug(F("camera power on"));
poweron = true;
if (1) {
Serial.println("");
Serial.println("@"); // power on slaves
}
while (inputAvailable()) {
if (myRead() == '\n') {
return;
}
}
return;
case '/':
serialfirst = false;
Serial.println(MEWPRO_VERSION_STRING);
return;
default:
if (bufp >= 3 && isxdigit(c)) {
c -= '0';
if (c >= 10) {
c = (c & 0x0f) + 9;
}
}
if (bufp < 4) {
shiftable = true;
buf[bufp++] = c;
} else {
if (shiftable) { // TM requires six args; "TM0e080a0b2d03" sets time to 2014 Aug 10 11:45:03
buf[bufp-1] = (buf[bufp-1] << 4) + c;
} else {
buf[bufp++] = c;
}
shiftable = !shiftable;
}
break;
}
}
}
void SendHeartBeat()
{
buf[0] = 0x83; buf[1] = 'H'; buf[2] = 'B'; buf[3] = 0xff;
SendBufToBacpac();
//if (!isMaster) {
// queueIn("XS0202011D000001E3000404FFFF00"); if wifi on
//}
}