forked from larsdehlwes/highspeed-raspiraw_rpi4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raspiraw.c
1696 lines (1524 loc) · 42.4 KB
/
raspiraw.c
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2015, Raspberry Pi Foundation
Copyright (c) 2015, Dave Stevenson
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define VERSION_STRING "0.0.2"
#define _GNU_SOURCE
#include <ctype.h>
#include <fcntl.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#define I2C_SLAVE_FORCE 0x0706
#include "interface/vcos/vcos.h"
#include "bcm_host.h"
#include "interface/mmal/mmal.h"
#include "interface/mmal/mmal_buffer.h"
#include "interface/mmal/mmal_logging.h"
#include "interface/mmal/util/mmal_default_components.h"
#include "interface/mmal/util/mmal_util.h"
#include "interface/mmal/util/mmal_util_params.h"
#include "interface/mmal/util/mmal_connection.h"
#include "RaspiCLI.h"
#include <sys/ioctl.h>
#include "raw_header.h"
#define DEFAULT_I2C_DEVICE 0
#define I2C_DEVICE_NAME_LEN 13 // "/dev/i2c-XXX"+NULL
static char i2c_device_name[I2C_DEVICE_NAME_LEN];
struct brcm_raw_header *brcm_header = NULL;
enum bayer_order {
//Carefully ordered so that an hflip is ^1,
//and a vflip is ^2.
BAYER_ORDER_BGGR,
BAYER_ORDER_GBRG,
BAYER_ORDER_GRBG,
BAYER_ORDER_RGGB
};
struct sensor_regs {
uint16_t reg;
uint16_t data;
};
struct mode_def
{
struct sensor_regs *regs;
int num_regs;
int width;
int height;
MMAL_FOURCC_T encoding;
enum bayer_order order;
int native_bit_depth;
uint8_t image_id;
uint8_t data_lanes;
unsigned int min_vts;
int line_time_ns;
uint32_t timing1;
uint32_t timing2;
uint32_t timing3;
uint32_t timing4;
uint32_t timing5;
uint32_t term1;
uint32_t term2;
int black_level;
};
struct sensor_def
{
char *name;
struct mode_def *modes;
int num_modes;
struct sensor_regs *stop;
int num_stop_regs;
uint8_t i2c_addr; // Device I2C slave address
int i2c_addressing; // Length of register address values
int i2c_data_size; // Length of register data to write
// Detecting the device
int i2c_ident_length; // Length of I2C ID register
uint16_t i2c_ident_reg; // ID register address
uint16_t i2c_ident_value; // ID register value
// Flip configuration
uint16_t vflip_reg; // Register for VFlip
int vflip_reg_bit; // Bit in that register for VFlip
uint16_t hflip_reg; // Register for HFlip
int hflip_reg_bit; // Bit in that register for HFlip
int flips_dont_change_bayer_order; // Some sensors do not change the
// Bayer order by adjusting X/Y starts
// to compensate.
uint16_t exposure_reg;
int exposure_reg_num_bits;
uint16_t vts_reg;
int vts_reg_num_bits;
uint16_t gain_reg;
int gain_reg_num_bits;
uint16_t xos_reg;
int xos_reg_num_bits;
uint16_t yos_reg;
int yos_reg_num_bits;
};
#define NUM_ELEMENTS(a) (sizeof(a) / sizeof(a[0]))
#include "ov5647_modes.h"
#include "imx219_modes.h"
#include "adv7282m_modes.h"
const struct sensor_def *sensors[] = {
&ov5647,
&imx219,
&adv7282,
NULL
};
enum {
CommandHelp,
CommandMode,
CommandHFlip,
CommandVFlip,
CommandExposure,
CommandGain,
CommandOutput,
CommandWriteHeader,
CommandTimeout,
CommandSaveRate,
CommandBitDepth,
CommandCameraNum,
CommandExposureus,
CommandI2cBus,
CommandAwbGains,
CommandRegs,
CommandHinc,
CommandVinc,
CommandVoinc,
CommandHoinc,
CommandBin44,
CommandFps,
CommandWidth,
CommandHeight,
CommandLeft,
CommandTop,
CommandVts,
CommandLine,
CommandWriteHeader0,
CommandWriteHeaderG,
CommandWriteTimestamps,
CommandWriteEmpty,
};
static COMMAND_LIST cmdline_commands[] =
{
{ CommandHelp, "-help", "?", "This help information", 0 },
{ CommandMode, "-mode", "md", "Set sensor mode <mode>", 1 },
{ CommandHFlip, "-hflip", "hf", "Set horizontal flip", 0},
{ CommandVFlip, "-vflip", "vf", "Set vertical flip", 0},
{ CommandExposure, "-ss", "e", "Set the sensor exposure time (not calibrated units)", 0 },
{ CommandGain, "-gain", "g", "Set the sensor gain code (not calibrated units)", 0 },
{ CommandOutput, "-output", "o", "Set the output filename", 0 },
{ CommandWriteHeader, "-header", "hd", "Write the BRCM header to the output file", 0 },
{ CommandTimeout, "-timeout", "t", "Time (in ms) before shutting down (if not specified, set to 5s)", 1 },
{ CommandSaveRate, "-saverate", "sr", "Save every Nth frame", 1 },
{ CommandBitDepth, "-bitdepth", "b", "Set output raw bit depth (8, 10, 12 or 16, if not specified, set to sensor native)", 1 },
{ CommandCameraNum, "-cameranum", "c", "Set camera number to use (0=CAM0, 1=CAM1).", 1 },
{ CommandExposureus, "-expus", "eus", "Set the sensor exposure time in micro seconds.", -1 },
{ CommandI2cBus, "-i2c", "y", "Set the I2C bus to use.", -1 },
{ CommandAwbGains, "-awbgains", "awbg", "Set the AWB gains to use.", 1 },
{ CommandRegs, "-regs", "r", "Change (current mode) regs", 0 },
{ CommandHinc, "-hinc", "hi", "Set horizontal odd/even inc reg", -1},
{ CommandVinc, "-vinc", "vi", "Set vertical odd/even inc reg", -1}, // ov5647
{ CommandVoinc, "-voinc", "voi","Set vertical odd inc reg", -1}, // imx219
{ CommandHoinc, "-hoinc", "hoi","Set horizontal odd inc reg", -1}, // imx219
{ CommandBin44, "-bin44", "b44","Set 4x4 binning", -1}, // imx219
{ CommandFps, "-fps", "f", "Set framerate regs", -1},
{ CommandWidth, "-width", "w", "Set current mode width", -1},
{ CommandHeight, "-height", "h", "Set current mode height", -1},
{ CommandLeft, "-left", "lt", "Set current mode left", -1},
{ CommandTop, "-top", "tp", "Set current mode top", -1},
{ CommandWriteHeader0, "-header0", "hd0","Sets filename to write the BRCM header to", 0 },
{ CommandWriteHeaderG, "-headerg", "hdg","Sets filename to write the .pgm header to", 0 },
{ CommandWriteTimestamps,"-tstamps", "ts", "Sets filename to write timestamps to", 0 },
{ CommandWriteEmpty, "-empty", "emp","Write empty output files", 0 },
};
static int cmdline_commands_size = sizeof(cmdline_commands) / sizeof(cmdline_commands[0]);
typedef struct pts_node {
int idx;
int64_t pts;
struct pts_node *nxt;
} *PTS_NODE_T;
typedef struct {
int mode;
int hflip;
int vflip;
int exposure;
int gain;
char *output;
int capture;
int write_header;
int timeout;
int saverate;
int bit_depth;
int camera_num;
int exposure_us;
int i2c_bus;
double awb_gains_r;
double awb_gains_b;
char *regs;
int hinc;
int vinc;
int voinc;
int hoinc;
int bin44;
double fps;
int width;
int height;
int left;
int top;
char *write_header0;
char *write_headerg;
char *write_timestamps;
int write_empty;
PTS_NODE_T ptsa;
PTS_NODE_T ptso;
} RASPIRAW_PARAMS_T;
void update_regs(const struct sensor_def *sensor, struct mode_def *mode, int hflip, int vflip, int exposure, int gain);
static int i2c_rd(int fd, uint8_t i2c_addr, uint16_t reg, uint8_t *values, uint32_t n, const struct sensor_def *sensor)
{
int err;
uint8_t buf[2] = { reg >> 8, reg & 0xff };
struct i2c_rdwr_ioctl_data msgset;
struct i2c_msg msgs[2] = {
{
.addr = i2c_addr,
.flags = 0,
.len = 2,
.buf = buf,
},
{
.addr = i2c_addr,
.flags = I2C_M_RD,
.len = n,
.buf = values,
},
};
if (sensor->i2c_addressing == 1)
{
msgs[0].len = 1;
}
msgset.msgs = msgs;
msgset.nmsgs = 2;
err = ioctl(fd, I2C_RDWR, &msgset);
//vcos_log_error("Read i2c addr %02X, reg %04X (len %d), value %02X, err %d", i2c_addr, msgs[0].buf[0], msgs[0].len, values[0], err);
if (err != msgset.nmsgs)
return -1;
return 0;
}
const struct sensor_def * probe_sensor(void)
{
int fd;
const struct sensor_def **sensor_list = &sensors[0];
const struct sensor_def *sensor = NULL;
fd = open(i2c_device_name, O_RDWR);
if (!fd)
{
vcos_log_error("Couldn't open I2C device");
return NULL;
}
while(*sensor_list != NULL)
{
uint16_t reg = 0;
sensor = *sensor_list;
vcos_log_error("Probing sensor %s on addr %02X", sensor->name, sensor->i2c_addr);
if (sensor->i2c_ident_length <= 2)
{
if (!i2c_rd(fd, sensor->i2c_addr, sensor->i2c_ident_reg, (uint8_t*)®, sensor->i2c_ident_length, sensor))
{
if (reg == sensor->i2c_ident_value)
{
vcos_log_error("Found sensor %s at address %02X", sensor->name, sensor->i2c_addr);
break;
}
}
}
sensor_list++;
sensor = NULL;
}
return sensor;
}
void send_regs(int fd, const struct sensor_def *sensor, const struct sensor_regs *regs, int num_regs)
{
int i;
for (i=0; i<num_regs; i++)
{
if (regs[i].reg == 0xFFFF)
{
if (ioctl(fd, I2C_SLAVE_FORCE, regs[i].data) < 0)
{
vcos_log_error("Failed to set I2C address to %02X", regs[i].data);
}
}
else if (regs[i].reg == 0xFFFE)
{
vcos_sleep(regs[i].data);
}
else
{
if (sensor->i2c_addressing == 1)
{
unsigned char msg[3] = {regs[i].reg, regs[i].data & 0xFF };
int len = 2;
if (sensor->i2c_data_size == 2)
{
msg[1] = (regs[i].data>>8) & 0xFF;
msg[2] = regs[i].data & 0xFF;
len = 3;
}
if (write(fd, msg, len) != len)
{
vcos_log_error("Failed to write register index %d (%02X val %02X)", i, regs[i].reg, regs[i].data);
}
}
else
{
unsigned char msg[4] = {regs[i].reg>>8, regs[i].reg, regs[i].data};
int len = 3;
if (sensor->i2c_data_size == 2)
{
msg[2] = regs[i].data >> 8;
msg[3] = regs[i].data;
len = 4;
}
if (write(fd, msg, len) != len)
{
vcos_log_error("Failed to write register index %d", i);
}
}
}
}
}
void start_camera_streaming(const struct sensor_def *sensor, struct mode_def *mode)
{
int fd;
fd = open(i2c_device_name, O_RDWR);
if (!fd)
{
vcos_log_error("Couldn't open I2C device");
return;
}
if (ioctl(fd, I2C_SLAVE_FORCE, sensor->i2c_addr) < 0)
{
vcos_log_error("Failed to set I2C address");
return;
}
send_regs(fd, sensor, mode->regs, mode->num_regs);
close(fd);
vcos_log_error("Now streaming...");
}
void stop_camera_streaming(const struct sensor_def *sensor)
{
int fd;
fd = open(i2c_device_name, O_RDWR);
if (!fd)
{
vcos_log_error("Couldn't open I2C device");
return;
}
if (ioctl(fd, I2C_SLAVE_FORCE, sensor->i2c_addr) < 0)
{
vcos_log_error("Failed to set I2C address");
return;
}
send_regs(fd, sensor, sensor->stop, sensor->num_stop_regs);
close(fd);
}
/**
* Allocates and generates a filename based on the
* user-supplied pattern and the frame number.
* On successful return, finalName and tempName point to malloc()ed strings
* which must be freed externally. (On failure, returns nulls that
* don't need free()ing.)
*
* @param finalName pointer receives an
* @param pattern sprintf pattern with %d to be replaced by frame
* @param frame for timelapse, the frame number
* @return Returns a MMAL_STATUS_T giving result of operation
*/
MMAL_STATUS_T create_filenames(char** finalName, char * pattern, int frame)
{
*finalName = NULL;
if (0 > asprintf(finalName, pattern, frame))
{
return MMAL_ENOMEM; // It may be some other error, but it is not worth getting it right
}
return MMAL_SUCCESS;
}
int running = 0;
static void callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
static int count = 0;
vcos_log_error("Buffer %p returned, filled %d, timestamp %llu, flags %04X", buffer, buffer->length, buffer->pts, buffer->flags);
if (running)
{
RASPIRAW_PARAMS_T *cfg = (RASPIRAW_PARAMS_T *)port->userdata;
if (!(buffer->flags&MMAL_BUFFER_HEADER_FLAG_CODECSIDEINFO) &&
(((count++)%cfg->saverate)==0))
{
// Save every Nth frame
// SD card access is too slow to do much more.
FILE *file;
char *filename = NULL;
if (create_filenames(&filename, cfg->output, count) == MMAL_SUCCESS)
{
file = fopen(filename, "wb");
if (file)
{
if (cfg->ptso) // make sure previous malloc() was successful
{
cfg->ptso->idx = count;
cfg->ptso->pts = buffer->pts;
cfg->ptso->nxt = malloc(sizeof(*cfg->ptso->nxt));
cfg->ptso = cfg->ptso->nxt;
}
if (!cfg->write_empty)
{
if (cfg->write_header)
fwrite(brcm_header, BRCM_RAW_HEADER_LENGTH, 1, file);
fwrite(buffer->data, buffer->length, 1, file);
}
fclose(file);
}
free(filename);
}
}
buffer->length = 0;
mmal_port_send_buffer(port, buffer);
}
else
mmal_buffer_header_release(buffer);
}
uint32_t order_and_bit_depth_to_encoding(enum bayer_order order, int bit_depth)
{
//BAYER_ORDER_BGGR,
//BAYER_ORDER_GBRG,
//BAYER_ORDER_GRBG,
//BAYER_ORDER_RGGB
const uint32_t depth8[] = {
MMAL_ENCODING_BAYER_SBGGR8,
MMAL_ENCODING_BAYER_SGBRG8,
MMAL_ENCODING_BAYER_SGRBG8,
MMAL_ENCODING_BAYER_SRGGB8
};
const uint32_t depth10[] = {
MMAL_ENCODING_BAYER_SBGGR10P,
MMAL_ENCODING_BAYER_SGBRG10P,
MMAL_ENCODING_BAYER_SGRBG10P,
MMAL_ENCODING_BAYER_SRGGB10P
};
const uint32_t depth12[] = {
MMAL_ENCODING_BAYER_SBGGR12P,
MMAL_ENCODING_BAYER_SGBRG12P,
MMAL_ENCODING_BAYER_SGRBG12P,
MMAL_ENCODING_BAYER_SRGGB12P,
};
const uint32_t depth16[] = {
MMAL_ENCODING_BAYER_SBGGR16,
MMAL_ENCODING_BAYER_SGBRG16,
MMAL_ENCODING_BAYER_SGRBG16,
MMAL_ENCODING_BAYER_SRGGB16,
};
if (order < 0 || order > 3)
{
vcos_log_error("order out of range - %d", order);
return 0;
}
switch(bit_depth)
{
case 8:
return depth8[order];
case 10:
return depth10[order];
case 12:
return depth12[order];
case 16:
return depth16[order];
}
vcos_log_error("%d not one of the handled bit depths", bit_depth);
return 0;
}
/**
* Parse the incoming command line and put resulting parameters in to the state
*
* @param argc Number of arguments in command line
* @param argv Array of pointers to strings from command line
* @param state Pointer to state structure to assign any discovered parameters to
* @return non-0 if failed for some reason, 0 otherwise
*/
static int parse_cmdline(int argc, char **argv, RASPIRAW_PARAMS_T *cfg)
{
// Parse the command line arguments.
// We are looking for --<something> or -<abbreviation of something>
int valid = 1;
int i;
for (i = 1; i < argc && valid; i++)
{
int command_id, num_parameters, len;
if (!argv[i])
continue;
if (argv[i][0] != '-')
{
valid = 0;
continue;
}
// Assume parameter is valid until proven otherwise
valid = 1;
command_id = raspicli_get_command_id(cmdline_commands, cmdline_commands_size, &argv[i][1], &num_parameters);
// If we found a command but are missing a parameter, continue (and we will drop out of the loop)
if (command_id != -1 && num_parameters > 0 && (i + 1 >= argc) )
continue;
// We are now dealing with a command line option
switch (command_id)
{
case CommandHelp:
raspicli_display_help(cmdline_commands, cmdline_commands_size);
// exit straight away if help requested
return -1;
case CommandMode:
if (sscanf(argv[i + 1], "%d", &cfg->mode) != 1)
valid = 0;
else
i++;
break;
case CommandHFlip:
cfg->hflip = 1;
break;
case CommandVFlip:
cfg->vflip = 1;
break;
case CommandExposure:
if (sscanf(argv[i + 1], "%d", &cfg->exposure) != 1)
valid = 0;
else
i++;
break;
case CommandGain:
if (sscanf(argv[i + 1], "%d", &cfg->gain) != 1)
valid = 0;
else
i++;
break;
case CommandOutput: // output filename
{
len = strlen(argv[i + 1]);
if (len)
{
//We use sprintf to append the frame number for timelapse mode
//Ensure that any %<char> is either %% or %d.
const char *percent = argv[i+1];
while(valid && *percent && (percent=strchr(percent, '%')) != NULL)
{
int digits=0;
percent++;
while(isdigit(*percent))
{
percent++;
digits++;
}
if (!((*percent == '%' && !digits) || *percent == 'd'))
{
valid = 0;
fprintf(stderr, "Filename contains %% characters, but not %%d or %%%% - sorry, will fail\n");
}
percent++;
}
cfg->output = malloc(len + 10); // leave enough space for any timelapse generated changes to filename
vcos_assert(cfg->output);
if (cfg->output)
strncpy(cfg->output, argv[i + 1], len+1);
i++;
cfg->capture = 1;
}
else
valid = 0;
break;
}
case CommandWriteHeader:
cfg->write_header = 1;
break;
case CommandTimeout: // Time to run for in milliseconds
if (sscanf(argv[i + 1], "%u", &cfg->timeout) == 1)
{
i++;
}
else
valid = 0;
break;
case CommandSaveRate:
if (sscanf(argv[i + 1], "%u", &cfg->saverate) == 1)
{
i++;
}
else
valid = 0;
break;
case CommandBitDepth:
if (sscanf(argv[i + 1], "%u", &cfg->bit_depth) == 1)
{
i++;
}
else
valid = 0;
break;
case CommandCameraNum:
if (sscanf(argv[i + 1], "%u", &cfg->camera_num) == 1)
{
i++;
if (cfg->camera_num !=0 && cfg->camera_num != 1)
{
fprintf(stderr, "Invalid camera number specified (%d)."
" It should be 0 or 1.\n", cfg->camera_num);
valid = 0;
}
}
else
valid = 0;
break;
case CommandExposureus:
if (sscanf(argv[i + 1], "%d", &cfg->exposure_us) != 1)
valid = 0;
else
i++;
break;
case CommandI2cBus:
if (sscanf(argv[i + 1], "%d", &cfg->i2c_bus) != 1)
valid = 0;
else
i++;
break;
case CommandAwbGains:
{
double r,b;
int args;
args = sscanf(argv[i + 1], "%lf,%lf", &r,&b);
if (args != 2 || r > 8.0 || b > 8.0)
valid = 0;
cfg->awb_gains_r = r;
cfg->awb_gains_b = b;
i++;
break;
}
case CommandRegs: // register changes
{
len = strlen(argv[i + 1]);
cfg->regs = malloc(len+1);
vcos_assert(cfg->regs);
strncpy(cfg->regs, argv[i + 1], len+1);
i++;
break;
}
case CommandHinc:
if (strlen(argv[i+1]) != 2 ||
sscanf(argv[i + 1], "%x", &cfg->hinc) != 1)
valid = 0;
else
i++;
break;
case CommandVinc:
if (strlen(argv[i+1]) != 2 ||
sscanf(argv[i + 1], "%x", &cfg->vinc) != 1)
valid = 0;
else
i++;
break;
case CommandVoinc:
if (strlen(argv[i+1]) != 2 ||
sscanf(argv[i + 1], "%x", &cfg->voinc) != 1)
valid = 0;
else
i++;
break;
case CommandHoinc:
if (strlen(argv[i+1]) != 2 ||
sscanf(argv[i + 1], "%x", &cfg->hoinc) != 1)
valid = 0;
else
i++;
break;
case CommandBin44:
cfg->bin44 = 1;
break;
case CommandFps:
if (sscanf(argv[i + 1], "%lf", &cfg->fps) != 1)
valid = 0;
else
i++;
break;
case CommandWidth:
if (sscanf(argv[i + 1], "%d", &cfg->width) != 1)
valid = 0;
else
i++;
break;
case CommandHeight:
if (sscanf(argv[i + 1], "%d", &cfg->height) != 1)
valid = 0;
else
i++;
break;
case CommandLeft:
if (sscanf(argv[i + 1], "%d", &cfg->left) != 1)
valid = 0;
else
i++;
break;
case CommandTop:
if (sscanf(argv[i + 1], "%d", &cfg->top) != 1)
valid = 0;
else
i++;
break;
case CommandWriteHeader0:
len = strlen(argv[i + 1]);
cfg->write_header0 = malloc(len + 1);
vcos_assert(cfg->write_header0);
strncpy(cfg->write_header0, argv[i + 1], len+1);
i++;
break;
case CommandWriteHeaderG:
len = strlen(argv[i + 1]);
cfg->write_headerg = malloc(len + 1);
vcos_assert(cfg->write_headerg);
strncpy(cfg->write_headerg, argv[i + 1], len+1);
i++;
break;
case CommandWriteTimestamps:
len = strlen(argv[i + 1]);
cfg->write_timestamps = malloc(len + 1);
vcos_assert(cfg->write_timestamps);
strncpy(cfg->write_timestamps, argv[i + 1], len+1);
i++;
cfg->ptsa = malloc(sizeof(*cfg->ptsa));
cfg->ptso = cfg->ptsa;
break;
case CommandWriteEmpty:
cfg->write_empty = 1;
break;
default:
valid = 0;
break;
}
}
if (!valid)
{
fprintf(stderr, "Invalid command line option (%s)\n", argv[i-1]);
return 1;
}
return 0;
}
//The process first loads the cleaned up dump of the registers
//than updates the known registers to the proper values
//based on: http://www.seeedstudio.com/wiki/images/3/3c/Ov5647_full.pdf
enum operation {
EQUAL, //Set bit to value
SET, //Set bit
CLEAR, //Clear bit
XOR //Xor bit
};
void modReg(struct mode_def *mode, uint16_t reg, int startBit, int endBit, int value, enum operation op);
int main(int argc, char** argv) {
RASPIRAW_PARAMS_T cfg = {
.mode = 0,
.hflip = 0,
.vflip = 0,
.exposure = -1,
.gain = -1,
.output = NULL,
.capture = 0,
.write_header = 0,
.timeout = 5000,
.saverate = 20,
.bit_depth = -1,
.camera_num = -1,
.exposure_us = -1,
.i2c_bus = DEFAULT_I2C_DEVICE,
.regs = NULL,
.hinc = -1,
.vinc = -1,
.voinc = -1,
.hoinc = -1,
.bin44 = 0,
.fps = -1,
.width = -1,
.height = -1,
.left = -1,
.top = -1,
.write_header0 = NULL,
.write_headerg = NULL,
.write_timestamps = NULL,
.write_empty = 0,
.ptsa = NULL,
.ptso = NULL,
};
uint32_t encoding;
const struct sensor_def *sensor;
struct mode_def *sensor_mode = NULL;
bcm_host_init();
vcos_log_register("RaspiRaw", VCOS_LOG_CATEGORY);
if (argc == 1)
{
fprintf(stdout, "\n%s Camera App %s\n\n", basename(argv[0]), VERSION_STRING);
raspicli_display_help(cmdline_commands, cmdline_commands_size);
exit(-1);
}
// Parse the command line and put options in to our status structure
if (parse_cmdline(argc, argv, &cfg))
{
exit(-1);
}
snprintf(i2c_device_name, sizeof(i2c_device_name), "/dev/i2c-%d", cfg.i2c_bus);
printf("Using i2C device %s\n", i2c_device_name);
sensor = probe_sensor();
if (!sensor)
{
vcos_log_error("No sensor found. Aborting");
return -1;
}
if (cfg.mode >= 0 && cfg.mode < sensor->num_modes)
{
sensor_mode = &sensor->modes[cfg.mode];
}
if (!sensor_mode)
{
vcos_log_error("Invalid mode %d - aborting", cfg.mode);
return -2;
}
if (cfg.regs)
{
int r,b;
char *p,*q;
p=strtok(cfg.regs, ";");
while (p)
{
vcos_assert(strlen(p)>6);
vcos_assert(p[4]==',');
vcos_assert(strlen(p)%2);
p[4]='\0'; q=p+5;
sscanf(p,"%4x",&r);
while(*q)
{
vcos_assert(isxdigit(q[0]));
vcos_assert(isxdigit(q[1]));
sscanf(q,"%2x",&b);
vcos_log_error("%04x: %02x",r,b);
modReg(sensor_mode, r, 0, 7, b, EQUAL);
++r;
q+=2;
}
p=strtok(NULL,";");
}
}
if (cfg.hinc >= 0)
{
if (!strcmp(sensor->name, "ov5647"))
modReg(sensor_mode, 0x3814, 0, 7, cfg.hinc, EQUAL);
}
if (cfg.vinc >= 0)
{
if (!strcmp(sensor->name, "ov5647"))
modReg(sensor_mode, 0x3815, 0, 7, cfg.vinc, EQUAL);
}
if (cfg.voinc >= 0)