-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbmControl.ino
More file actions
493 lines (396 loc) · 13.3 KB
/
bmControl.ino
File metadata and controls
493 lines (396 loc) · 13.3 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
BM Control
Copyright (c) 2016 Jesse Garrison
This program allows for easy OSC and Serial control of Black Magic Cameras over SDI.
Board Used:
* Arduino Ethernet Rev 3
Shield used:
* Black Magic Arduino SDI Shield
3rd Party Libraries Used:
* Black Magic SDI Control Library
* OSC (CNMAT)
OSC Message Formatting:
/bmc/[camera number]/[command] [value(s)]
Licensed under GPL 3.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Libraries //
#include <SPI.h>
#include <Wire.h>
#include <BMDSDIControl.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <OSCBundle.h>
// Constants //
#define DEFAULT_OSC_RECEIVE_PORT 8888
#define DEFAULT_IP_ADDRESS 192,168,0,66
#define STATUS_LED_PIN 9
#define NUM_CAMERAS 2 //number of cameras to be controlled
// Ethernet Stuff //
EthernetUDP Udp;
IPAddress targetIp(192,168,0,255);
byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0x63, 0x69};
byte ip[] = {DEFAULT_IP_ADDRESS};
char packetBuffer[256]; //buffer to hold incoming packet,
char sendBuffer[] = "acknowledged..."; // a string to send back
// OSC Stuff //
int oscReceivePort = DEFAULT_OSC_RECEIVE_PORT;
float replyPort = 8000; //where to respond to pings and gets
byte replyIp[] = { 192,168,0,255 };
// Camera Communication Stuff //
const int shieldAddress = 0x6E;
BMD_SDICameraControl_I2C sdiCam(shieldAddress);
// Camera State //
struct Camera{
int id;
float aperture;
float focus;
int exposure;
int sensorGain;
int whiteBalance;
float lift[4];
float gamma[4];
float gain[4];
float hue;
float saturation;
};
Camera cameras[NUM_CAMERAS+1];
//debug stuff
int debugApt = 0; //testing aperture
// ---- Main ---- //
void setup() {
// Serial.begin(115200);
// Serial.println("Hi.");
// Network Setup //
Ethernet.begin(mac, ip);
Udp.begin(oscReceivePort);
// Serial.println("Enet Enabled.");
// Camera Setup //
sdiCam.begin();
Wire.setClock(400000); // Set max I2C speed
sdiCam.setOverride(true);
// Serial.println("SDI Enabled.");
}
// --- OSC Messages --- //
void parseBmcMsg(OSCMessage &_msg, int offset) {
char address[256];
_msg.getAddress(address,0);
int cam = getAddressSegment(address,1).toInt(); //get address segment 1
String cmd = getAddressSegment(address,2); //get address segment 2
// Serial.println(cam);
// Serial.println(cmd);
if (cmd == "aperture") {
setAperture(cam, _msg.getFloat(0));
} else if (cmd == "focus") {
setFocus(cam, _msg.getFloat(0));
} else if (cmd == "exposure") {
setExposure(cam, _msg.getInt(0));
} else if (cmd == "sensorGain") {
setSensorGain(cam, _msg.getInt(0));
} else if (cmd == "whiteBalance") {
setWhiteBalance(cam, _msg.getInt(0));
} else if (cmd == "lift") {
float input[4];
input[0] = _msg.getFloat(0);
input[1] = _msg.getFloat(1);
input[2] = _msg.getFloat(2);
input[3] = _msg.getFloat(3);
setLift(cam, input);
} else if (cmd == "gamma") {
float input[4];
input[0] = _msg.getFloat(0);
input[1] = _msg.getFloat(1);
input[2] = _msg.getFloat(2);
input[3] = _msg.getFloat(3);
setGamma(cam, input);
}else if (cmd == "gain") {
float input[4];
input[0] = _msg.getFloat(0);
input[1] = _msg.getFloat(1);
input[2] = _msg.getFloat(2);
input[3] = _msg.getFloat(3);
setGain(cam, input);
} else if(cmd== "hue"){
setHue(cam, _msg.getFloat(0));
} else if (cmd=="saturation"){
setSat(cam, _msg.getFloat(0));
}else {
// Serial.println("[ERR] command not regognized...");
char buff[16];
_msg.getAddress(buff,0);
// Serial.println(buff);
}
}
void bigMess(OSCMessage &_msg, int val){
char address[256];
_msg.getAddress(address,0);
int cam = getAddressSegment(address,1).toInt(); //get address segment 1
// float thisFloat = _msg.getFloat(0);
setFocus(cam, _msg.getFloat(0));
setAperture(cam, _msg.getFloat(1));
// setExposure(cam, _msg.getInt(0));
setSat(cam, _msg.getFloat(2));
setHue(cam, _msg.getFloat(3));
setSensorGain(cam, _msg.getInt(4));
setWhiteBalance(cam, _msg.getFloat(5));
// char msgString[256];
// _msg.getString(0,msgString,256 );
// float thisFloat = getAddressSegment(msgString,0).toFloat();
// setFocus(cam, thisFloat);
// thisFloat = getAddressSegment(msgString,1).toFloat();
// setAperture(cam, thisFloat);
//// setExposure(cam, _msg.getInt(0));
// thisFloat = getAddressSegment(msgString,2).toFloat();
// setSat(cam, thisFloat);
// thisFloat = getAddressSegment(msgString,3).toFloat();
// setHue(cam, thisFloat);
// float thisInt = getAddressSegment(msgString,4).toInt();
//
// setSensorGain(cam, thisInt);
// thisInt = getAddressSegment(msgString,4).toInt();
// setWhiteBalance(cam, thisInt);
// float lift[4];
// lift[0] = thisFloat;
// lift[1] = thisFloat;
// lift[2] = thisFloat;
// lift[3] = thisFloat;
// setLift(cam, lift);
//
// float gamma[4];
// gamma[0] = _msg.getFloat(8);
// gamma[1] = _msg.getFloat(8);
// gamma[2] = _msg.getFloat(8);
// gamma[3] = _msg.getFloat(8);
// setGamma(cam, gamma);
// float gain[4];
// gain[0] = _msg.getFloat(8);
// gain[1] = _msg.getFloat(8);
// gain[2] = _msg.getFloat(8);
// gain[3] = _msg.getFloat(8);
// setGain(cam, gain);
// Serial.print("got here: ");
// Serial.println(_msg.getFloat(1));
}
void pingPong(OSCMessage &_msg, int val) {
// blink();
// Serial.println("pinged.");
OSCMessage reply("/pong");
// reply.add((int32_t)1);
Udp.beginPacket(replyIp,replyPort);
reply.send(Udp);
Udp.endPacket();
reply.empty();
}
void setReplyIp(OSCMessage &_msg, int val){
replyIp[0] = _msg.getInt(0);
replyIp[1] = _msg.getInt(1);
replyIp[2] = _msg.getInt(2);
replyIp[3] = _msg.getInt(3);
}
void setReplyPort(OSCMessage &_msg, int val) {
replyPort = _msg.getInt(0);
}
//void getStatus(OSCMessage &_msg, int camNum){
// camNum = _msg.getInt(0);
// OSCBundle reply;
// //reply.beginMessage(makeStatusAddr(camNum,"aperture"));
// reply.add(makeStatusAddr(camNum,"aperture")).add((float)cameras[camNum].aperture);
// Udp.beginPacket(replyIp,replyPort);
// reply.send(Udp);
// Udp.endPacket();
// reply.empty();
//
// reply.add(makeStatusAddr(camNum,"focus")).add((float)cameras[camNum].focus);
// Udp.beginPacket(replyIp,replyPort);
// reply.send(Udp);
// Udp.endPacket();
// reply.empty();
//
// reply.add(makeStatusAddr(camNum,"exposure")).add((int32_t)cameras[camNum].exposure);
// Udp.beginPacket(replyIp,replyPort);
// reply.send(Udp);
// Udp.endPacket();
// reply.empty();
//
// reply.add(makeStatusAddr(camNum,"sensorGain")).add((int32_t)cameras[camNum].sensorGain);
// Udp.beginPacket(replyIp,replyPort);
// reply.send(Udp);
// Udp.endPacket();
// reply.empty();
//
// reply.add(makeStatusAddr(camNum,"whiteBalance")).add((int32_t)cameras[camNum].whiteBalance);
// Udp.beginPacket(replyIp,replyPort);
// reply.send(Udp);
// Udp.endPacket();
// reply.empty();
//
// reply.add(makeStatusAddr(camNum,"lift")).add((int32_t)cameras[camNum].lift[0]).add((float)cameras[camNum].lift[1]).add((float)cameras[camNum].lift[2]).add((float)cameras[camNum].lift[3]);
// Udp.beginPacket(replyIp,replyPort);
// reply.send(Udp);
// Udp.endPacket();
// reply.empty();
//
// reply.add(makeStatusAddr(camNum,"gamma")).add((int32_t)cameras[camNum].gamma[0]).add((float)cameras[camNum].gamma[1]).add((float)cameras[camNum].gamma[2]).add((float)cameras[camNum].gamma[3]);
// Udp.beginPacket(replyIp,replyPort);
// reply.send(Udp);
// Udp.endPacket();
// reply.empty();
//
// reply.add(makeStatusAddr(camNum,"gain")).add((int32_t)cameras[camNum].gain[0]).add((float)cameras[camNum].gain[1]).add((float)cameras[camNum].gain[2]).add((float)cameras[camNum].gain[3]);
// Udp.beginPacket(replyIp,replyPort);
// reply.send(Udp);
// Udp.endPacket();
// reply.empty();
//
// reply.add(makeStatusAddr(camNum,"whiteBalance")).add((int32_t)cameras[camNum].hue);
// Udp.beginPacket(replyIp,replyPort);
// reply.send(Udp);
// Udp.endPacket();
// reply.empty();
//
// reply.add(makeStatusAddr(camNum,"whiteBalance")).add((int32_t)cameras[camNum].saturation);
// Udp.beginPacket(replyIp,replyPort);
// reply.send(Udp);
// Udp.endPacket();
// reply.empty();
//
//}
// ---- LOOP ---- //
void loop() {
// check for new bundles ///
OSCBundle bndl;
int size;
// --- receive a bundle --- //
if( (size = Udp.parsePacket())>0) {
// Serial.println("Packet Received...");
while(size--){
bndl.fill(Udp.read());
// Serial.print(".");
}
if(!bndl.hasError()){
// Serial.println("no err");
if(bndl.size() > 0) {
static int32_t sequencenumber=0;
bndl.route("/ping", pingPong);
bndl.route("/bmc", parseBmcMsg);
bndl.route("/bmcRawVoid", writeRawVoid );
// bndl.route("/getStatus", getStatus);
bndl.route("/setReplyPort", setReplyPort);
bndl.route("/setReplyIp", setReplyIp);
bndl.route("/bigMess", bigMess);
// Serial.println("routed.");
}
} else{
// Serial.println("Error.");
}
// Serial.print(".");
}
// --- //
// blink();
}
// ---- Camera Commands ---- //
void setAperture(int _camera, float _value) {
// FORMAT: Float (-1 - 16.0)
cameras[_camera].aperture = _value;
sdiCam.writeCommandFixed16(_camera, 0, 2, 0, cameras[_camera].aperture );
}
void setFocus(int _camera, float _value) {
// FORMAT: Float (0.0=near, 1.0=far)
cameras[_camera].focus = _value;
sdiCam.writeCommandFixed16(_camera, 0, 0, 0, cameras[_camera].focus ); //
}
void setExposure(int _camera, int _value) {
// FORMAT: Int32 (time in "us" (sp?))
cameras[_camera].exposure = _value;
sdiCam.writeCommandInt32(_camera, 1, 5, 0, cameras[_camera].exposure); //
}
void setSensorGain(int _camera, int _value) {
// FORMAT: Int8 (1x, 2x, 4x, 8x, 16x gain)
cameras[_camera].sensorGain = _value;
sdiCam.writeCommandInt8(_camera, 1, 1, 0, cameras[_camera].sensorGain); //
}
void setWhiteBalance(int _camera, int _value){
// FORMAT: Int16: White Balance in Kelvin (3200 - 7500)
cameras[_camera].whiteBalance = _value;
sdiCam.writeCommandInt16(_camera, 1, 2, 0, cameras[_camera].whiteBalance); //
}
void setLift(int _camera, float _rgbl[4]){
// FORMAT: float [4] RGBL, -4.0 - 4.0
cameras[_camera].lift[0] = _rgbl[0];
cameras[_camera].lift[1] = _rgbl[1];
cameras[_camera].lift[2] = _rgbl[2];
cameras[_camera].lift[3] = _rgbl[3];
sdiCam.writeCommandFixed16(_camera, 8, 0, 0, cameras[_camera].lift ); //
}
void setGamma(int _camera, float _rgbl[4]){
// FORMAT: float [4] RGBL, -4.0 - 4.0
cameras[_camera].gamma[0] = _rgbl[0];
cameras[_camera].gamma[1] = _rgbl[1];
cameras[_camera].gamma[2] = _rgbl[2];
cameras[_camera].gamma[3] = _rgbl[3];
sdiCam.writeCommandFixed16(_camera, 8, 1, 0, cameras[_camera].gamma ); //
}
void setGain(int _camera, float _rgbl[4]){
// FORMAT: float [4] RGBL, -4.0 - 4.0
cameras[_camera].gain[0] = _rgbl[0];
cameras[_camera].gain[1] = _rgbl[1];
cameras[_camera].gain[2] = _rgbl[2];
cameras[_camera].gain[3] = _rgbl[3];
sdiCam.writeCommandFixed16(_camera, 8, 1, 0, cameras[_camera].gain ); //
}
void setHue(int _camera, float _value){
cameras[_camera].hue = _value;
float colorCombined[2] = {cameras[_camera].hue, cameras[_camera].saturation};
sdiCam.writeCommandFixed16(_camera,8,6,0,colorCombined);
}
void setSat(int _camera, float _value){
cameras[_camera].saturation = _value;
float colorCombined[2] = {cameras[_camera].hue, cameras[_camera].saturation};
sdiCam.writeCommandFixed16(_camera,8,6,0,colorCombined);
}
// ---- Raw Commands ---- //
void writeRawVoid(OSCMessage &_msg, int val) {
int _cam = 1; //camera
int _cat = 0; //cateogry
int _param = 0; //parameter
sdiCam.writeCommandVoid(_cam, _cat, _param);
}
void rawFixed16(OSCMessage *_msg) {
}
// ---- Utility Functions ---- //
void blink() {
digitalWrite(STATUS_LED_PIN, HIGH);
delay(50);
digitalWrite(STATUS_LED_PIN, LOW);
delay(50);
}
String getAddressSegment(String _fullAddress, int segment){ //for getting individual elements of OSC addresses
int startSlashIndex=0;
int endSlashIndex;
for(int i=0;i<segment;i++){
startSlashIndex= _fullAddress.indexOf("/", startSlashIndex+1);
}
endSlashIndex = _fullAddress.indexOf("/", startSlashIndex+1);
// Serial.print("substring: ");
// Serial.println(_fullAddress.substring(startSlashIndex+1, endSlashIndex));
return _fullAddress.substring(startSlashIndex+1, endSlashIndex);
}
char* makeStatusAddr(int _camNum, String _variable){
char buffer[256];
String addr("/status/");
addr+=_camNum;
addr+="/";
addr+=_variable;
addr.toCharArray(buffer, 256);
// Serial.println(buffer);
return buffer;
}