-
Notifications
You must be signed in to change notification settings - Fork 1
/
RaspiCamControl.c
executable file
·1550 lines (1336 loc) · 49.6 KB
/
RaspiCamControl.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) 2013, Broadcom Europe Ltd
Copyright (c) 2013, James Hughes
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.
*/
#include <stdio.h>
#include <memory.h>
#include <ctype.h>
#include "interface/vcos/vcos.h"
#include "interface/vmcs_host/vc_vchi_gencmd.h"
#include "interface/mmal/mmal.h"
#include "interface/mmal/mmal_logging.h"
#include "interface/mmal/util/mmal_util.h"
#include "interface/mmal/util/mmal_util_params.h"
#include "interface/mmal/util/mmal_default_components.h"
#include "RaspiCamControl.h"
#include "RaspiCLI.h"
/// Structure to cross reference exposure strings against the MMAL parameter equivalent
static XREF_T exposure_map[] =
{
{"auto", MMAL_PARAM_EXPOSUREMODE_AUTO},
{"night", MMAL_PARAM_EXPOSUREMODE_NIGHT},
{"nightpreview", MMAL_PARAM_EXPOSUREMODE_NIGHTPREVIEW},
{"backlight", MMAL_PARAM_EXPOSUREMODE_BACKLIGHT},
{"spotlight", MMAL_PARAM_EXPOSUREMODE_SPOTLIGHT},
{"sports", MMAL_PARAM_EXPOSUREMODE_SPORTS},
{"snow", MMAL_PARAM_EXPOSUREMODE_SNOW},
{"beach", MMAL_PARAM_EXPOSUREMODE_BEACH},
{"verylong", MMAL_PARAM_EXPOSUREMODE_VERYLONG},
{"fixedfps", MMAL_PARAM_EXPOSUREMODE_FIXEDFPS},
{"antishake", MMAL_PARAM_EXPOSUREMODE_ANTISHAKE},
{"fireworks", MMAL_PARAM_EXPOSUREMODE_FIREWORKS}
};
static const int exposure_map_size = sizeof(exposure_map) / sizeof(exposure_map[0]);
/// Structure to cross reference awb strings against the MMAL parameter equivalent
static XREF_T awb_map[] =
{
{"off", MMAL_PARAM_AWBMODE_OFF},
{"auto", MMAL_PARAM_AWBMODE_AUTO},
{"sun", MMAL_PARAM_AWBMODE_SUNLIGHT},
{"cloud", MMAL_PARAM_AWBMODE_CLOUDY},
{"shade", MMAL_PARAM_AWBMODE_SHADE},
{"tungsten", MMAL_PARAM_AWBMODE_TUNGSTEN},
{"fluorescent", MMAL_PARAM_AWBMODE_FLUORESCENT},
{"incandescent", MMAL_PARAM_AWBMODE_INCANDESCENT},
{"flash", MMAL_PARAM_AWBMODE_FLASH},
{"horizon", MMAL_PARAM_AWBMODE_HORIZON}
};
static const int awb_map_size = sizeof(awb_map) / sizeof(awb_map[0]);
/// Structure to cross reference image effect against the MMAL parameter equivalent
static XREF_T imagefx_map[] =
{
{"none", MMAL_PARAM_IMAGEFX_NONE},
{"negative", MMAL_PARAM_IMAGEFX_NEGATIVE},
{"solarise", MMAL_PARAM_IMAGEFX_SOLARIZE},
{"sketch", MMAL_PARAM_IMAGEFX_SKETCH},
{"denoise", MMAL_PARAM_IMAGEFX_DENOISE},
{"emboss", MMAL_PARAM_IMAGEFX_EMBOSS},
{"oilpaint", MMAL_PARAM_IMAGEFX_OILPAINT},
{"hatch", MMAL_PARAM_IMAGEFX_HATCH},
{"gpen", MMAL_PARAM_IMAGEFX_GPEN},
{"pastel", MMAL_PARAM_IMAGEFX_PASTEL},
{"watercolour", MMAL_PARAM_IMAGEFX_WATERCOLOUR},
{"film", MMAL_PARAM_IMAGEFX_FILM},
{"blur", MMAL_PARAM_IMAGEFX_BLUR},
{"saturation", MMAL_PARAM_IMAGEFX_SATURATION},
{"colourswap", MMAL_PARAM_IMAGEFX_COLOURSWAP},
{"washedout", MMAL_PARAM_IMAGEFX_WASHEDOUT},
{"posterise", MMAL_PARAM_IMAGEFX_POSTERISE},
{"colourpoint", MMAL_PARAM_IMAGEFX_COLOURPOINT},
{"colourbalance", MMAL_PARAM_IMAGEFX_COLOURBALANCE},
{"cartoon", MMAL_PARAM_IMAGEFX_CARTOON}
};
static const int imagefx_map_size = sizeof(imagefx_map) / sizeof(imagefx_map[0]);
static XREF_T metering_mode_map[] =
{
{"average", MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE},
{"spot", MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT},
{"backlit", MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT},
{"matrix", MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX}
};
static const int metering_mode_map_size = sizeof(metering_mode_map)/sizeof(metering_mode_map[0]);
static XREF_T drc_mode_map[] =
{
{"off", MMAL_PARAMETER_DRC_STRENGTH_OFF},
{"low", MMAL_PARAMETER_DRC_STRENGTH_LOW},
{"med", MMAL_PARAMETER_DRC_STRENGTH_MEDIUM},
{"high", MMAL_PARAMETER_DRC_STRENGTH_HIGH}
};
static const int drc_mode_map_size = sizeof(drc_mode_map)/sizeof(drc_mode_map[0]);
static XREF_T stereo_mode_map[] =
{
{"off", MMAL_STEREOSCOPIC_MODE_NONE},
{"sbs", MMAL_STEREOSCOPIC_MODE_SIDE_BY_SIDE},
{"tb", MMAL_STEREOSCOPIC_MODE_TOP_BOTTOM},
};
static const int stereo_mode_map_size = sizeof(stereo_mode_map)/sizeof(stereo_mode_map[0]);
#define CommandSharpness 0
#define CommandContrast 1
#define CommandBrightness 2
#define CommandSaturation 3
#define CommandISO 4
#define CommandVideoStab 5
#define CommandEVComp 6
#define CommandExposure 7
#define CommandAWB 8
#define CommandImageFX 9
#define CommandColourFX 10
#define CommandMeterMode 11
#define CommandRotation 12
#define CommandHFlip 13
#define CommandVFlip 14
#define CommandROI 15
#define CommandShutterSpeed 16
#define CommandAwbGains 17
#define CommandDRCLevel 18
#define CommandStatsPass 19
#define CommandAnnotate 20
#define CommandStereoMode 21
#define CommandStereoDecimate 22
#define CommandStereoSwap 23
#define CommandAnnotateExtras 24
static COMMAND_LIST cmdline_commands[] =
{
{CommandSharpness, "-sharpness", "sh", "Set image sharpness (-100 to 100)", 1},
{CommandContrast, "-contrast", "co", "Set image contrast (-100 to 100)", 1},
{CommandBrightness, "-brightness","br", "Set image brightness (0 to 100)", 1},
{CommandSaturation, "-saturation","sa", "Set image saturation (-100 to 100)", 1},
{CommandISO, "-ISO", "ISO","Set capture ISO", 1},
{CommandVideoStab, "-vstab", "vs", "Turn on video stabilisation", 0},
{CommandEVComp, "-ev", "ev", "Set EV compensation", 1},
{CommandExposure, "-exposure", "ex", "Set exposure mode (see Notes)", 1},
{CommandAWB, "-awb", "awb","Set AWB mode (see Notes)", 1},
{CommandImageFX, "-imxfx", "ifx","Set image effect (see Notes)", 1},
{CommandColourFX, "-colfx", "cfx","Set colour effect (U:V)", 1},
{CommandMeterMode, "-metering", "mm", "Set metering mode (see Notes)", 1},
{CommandRotation, "-rotation", "rot","Set image rotation (0-359)", 1},
{CommandHFlip, "-hflip", "hf", "Set horizontal flip", 0},
{CommandVFlip, "-vflip", "vf", "Set vertical flip", 0},
{CommandROI, "-roi", "roi","Set region of interest (x,y,w,d as normalised coordinates [0.0-1.0])", 1},
{CommandShutterSpeed,"-shutter", "ss", "Set shutter speed in microseconds", 1},
{CommandAwbGains, "-awbgains", "awbg", "Set AWB gains - AWB mode must be off", 1},
{CommandDRCLevel, "-drc", "drc", "Set DRC Level", 1},
{CommandStatsPass, "-stats", "st", "Force recomputation of statistics on stills capture pass"},
{CommandAnnotate, "-annotate", "a", "Enable/Set annotate flags or text", 1},
{CommandStereoMode, "-stereo", "3d", "Select stereoscopic mode", 1},
{CommandStereoDecimate,"-decimate","dec", "Half width/height of stereo image"},
{CommandStereoSwap, "-3dswap", "3dswap", "Swap camera order for stereoscopic"},
{CommandAnnotateExtras,"-annotateex","ae", "Set extra annotation parameters (text size, text colour(hex YUV), bg colour(hex YUV))", 2},
};
static int cmdline_commands_size = sizeof(cmdline_commands) / sizeof(cmdline_commands[0]);
#define parameter_reset -99999
/**
* Update the passed in parameter according to the rest of the parameters
* passed in.
*
*
* @return 0 if reached end of cycle for this parameter, !0 otherwise
*/
static int update_cycle_parameter(int *option, int min, int max, int increment)
{
vcos_assert(option);
if (!option)
return 0;
if (*option == parameter_reset)
*option = min - increment;
*option += increment;
if (*option > max)
{
*option = parameter_reset;
return 0;
}
else
return 1;
}
/**
* Test/Demo code to cycle through a bunch of camera settings
* This code is pretty hacky so please don't complain!!
* It only does stuff that should have a visual impact (hence demo!)
* This will override any user supplied parameters
*
* Each call of this function will move on to the next setting
*
* @param camera Pointer to the camera to change settings on.
* @return 0 if reached end of complete sequence, !0 otherwise
*/
int raspicamcontrol_cycle_test(MMAL_COMPONENT_T *camera)
{
static int parameter = 0;
static int parameter_option = parameter_reset; // which value the parameter currently has
vcos_assert(camera);
// We are going to cycle through all the relevant entries in the parameter block
// and send options to the camera.
if (parameter == 0)
{
// sharpness
if (update_cycle_parameter(¶meter_option, -100, 100, 10))
raspicamcontrol_set_sharpness(camera, parameter_option);
else
{
raspicamcontrol_set_sharpness(camera, 0);
parameter++;
}
}
else
if (parameter == 1)
{
// contrast
if (update_cycle_parameter(¶meter_option, -100, 100, 10))
raspicamcontrol_set_contrast(camera, parameter_option);
else
{
raspicamcontrol_set_contrast(camera, 0);
parameter++;
}
}
else
if (parameter == 2)
{
// brightness
if (update_cycle_parameter(¶meter_option, 0, 100, 10))
raspicamcontrol_set_brightness(camera, parameter_option);
else
{
raspicamcontrol_set_brightness(camera, 50);
parameter++;
}
}
else
if (parameter == 3)
{
// contrast
if (update_cycle_parameter(¶meter_option, -100, 100, 10))
raspicamcontrol_set_saturation(camera, parameter_option);
else
{
parameter++;
raspicamcontrol_set_saturation(camera, 0);
}
}
else
if (parameter == 4)
{
// EV
if (update_cycle_parameter(¶meter_option, -10, 10, 4))
raspicamcontrol_set_exposure_compensation(camera, parameter_option);
else
{
raspicamcontrol_set_exposure_compensation(camera, 0);
parameter++;
}
}
else
if (parameter == 5)
{
// MMAL_PARAM_EXPOSUREMODE_T
if (update_cycle_parameter(¶meter_option, 0, exposure_map_size, 1))
raspicamcontrol_set_exposure_mode(camera, exposure_map[parameter_option].mmal_mode);
else
{
raspicamcontrol_set_exposure_mode(camera, MMAL_PARAM_EXPOSUREMODE_AUTO);
parameter++;
}
}
else
if (parameter == 6)
{
// MMAL_PARAM_AWB_T
if (update_cycle_parameter(¶meter_option, 0, awb_map_size, 1))
raspicamcontrol_set_awb_mode(camera, awb_map[parameter_option].mmal_mode);
else
{
raspicamcontrol_set_awb_mode(camera, MMAL_PARAM_AWBMODE_AUTO);
parameter++;
}
}
if (parameter == 7)
{
// MMAL_PARAM_IMAGEFX_T
if (update_cycle_parameter(¶meter_option, 0, imagefx_map_size, 1))
raspicamcontrol_set_imageFX(camera, imagefx_map[parameter_option].mmal_mode);
else
{
raspicamcontrol_set_imageFX(camera, MMAL_PARAM_IMAGEFX_NONE);
parameter++;
}
}
if (parameter == 8)
{
MMAL_PARAM_COLOURFX_T colfx = {0,0,0};
switch (parameter_option)
{
case parameter_reset :
parameter_option = 1;
colfx.u = 128;
colfx.v = 128;
break;
case 1 :
parameter_option = 2;
colfx.u = 100;
colfx.v = 200;
break;
case 2 :
parameter_option = parameter_reset;
colfx.enable = 0;
parameter++;
break;
}
raspicamcontrol_set_colourFX(camera, &colfx);
}
// Orientation
if (parameter == 9)
{
switch (parameter_option)
{
case parameter_reset:
raspicamcontrol_set_rotation(camera, 90);
parameter_option = 1;
break;
case 1 :
raspicamcontrol_set_rotation(camera, 180);
parameter_option = 2;
break;
case 2 :
raspicamcontrol_set_rotation(camera, 270);
parameter_option = 3;
break;
case 3 :
{
raspicamcontrol_set_rotation(camera, 0);
raspicamcontrol_set_flips(camera, 1,0);
parameter_option = 4;
break;
}
case 4 :
{
raspicamcontrol_set_flips(camera, 0,1);
parameter_option = 5;
break;
}
case 5 :
{
raspicamcontrol_set_flips(camera, 1, 1);
parameter_option = 6;
break;
}
case 6 :
{
raspicamcontrol_set_flips(camera, 0, 0);
parameter_option = parameter_reset;
parameter++;
break;
}
}
}
if (parameter == 10)
{
parameter = 1;
return 0;
}
return 1;
}
/**
* Convert string to the MMAL parameter for exposure mode
* @param str Incoming string to match
* @return MMAL parameter matching the string, or the AUTO option if no match found
*/
static MMAL_PARAM_EXPOSUREMODE_T exposure_mode_from_string(const char *str)
{
int i = raspicli_map_xref(str, exposure_map, exposure_map_size);
if( i != -1)
return (MMAL_PARAM_EXPOSUREMODE_T)i;
vcos_log_error("Unknown exposure mode: %s", str);
return MMAL_PARAM_EXPOSUREMODE_AUTO;
}
/**
* Convert string to the MMAL parameter for AWB mode
* @param str Incoming string to match
* @return MMAL parameter matching the string, or the AUTO option if no match found
*/
static MMAL_PARAM_AWBMODE_T awb_mode_from_string(const char *str)
{
int i = raspicli_map_xref(str, awb_map, awb_map_size);
if( i != -1)
return (MMAL_PARAM_AWBMODE_T)i;
vcos_log_error("Unknown awb mode: %s", str);
return MMAL_PARAM_AWBMODE_AUTO;
}
/**
* Convert string to the MMAL parameter for image effects mode
* @param str Incoming string to match
* @return MMAL parameter matching the strong, or the AUTO option if no match found
*/
MMAL_PARAM_IMAGEFX_T imagefx_mode_from_string(const char *str)
{
int i = raspicli_map_xref(str, imagefx_map, imagefx_map_size);
if( i != -1)
return (MMAL_PARAM_IMAGEFX_T)i;
vcos_log_error("Unknown image fx: %s", str);
return MMAL_PARAM_IMAGEFX_NONE;
}
/**
* Convert string to the MMAL parameter for exposure metering mode
* @param str Incoming string to match
* @return MMAL parameter matching the string, or the AUTO option if no match found
*/
static MMAL_PARAM_EXPOSUREMETERINGMODE_T metering_mode_from_string(const char *str)
{
int i = raspicli_map_xref(str, metering_mode_map, metering_mode_map_size);
if( i != -1)
return (MMAL_PARAM_EXPOSUREMETERINGMODE_T)i;
vcos_log_error("Unknown metering mode: %s", str);
return MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
}
/**
* Convert string to the MMAL parameter for DRC level
* @param str Incoming string to match
* @return MMAL parameter matching the string, or the AUTO option if no match found
*/
static MMAL_PARAMETER_DRC_STRENGTH_T drc_mode_from_string(const char *str)
{
int i = raspicli_map_xref(str, drc_mode_map, drc_mode_map_size);
if( i != -1)
return (MMAL_PARAMETER_DRC_STRENGTH_T)i;
vcos_log_error("Unknown DRC level: %s", str);
return MMAL_PARAMETER_DRC_STRENGTH_OFF;
}
/**
* Convert string to the MMAL parameter for exposure metering mode
* @param str Incoming string to match
* @return MMAL parameter matching the string, or the AUTO option if no match found
*/
static MMAL_STEREOSCOPIC_MODE_T stereo_mode_from_string(const char *str)
{
int i = raspicli_map_xref(str, stereo_mode_map, stereo_mode_map_size);
if( i != -1)
return (MMAL_STEREOSCOPIC_MODE_T)i;
vcos_log_error("Unknown metering mode: %s", str);
return MMAL_STEREOSCOPIC_MODE_NONE;
}
/**
* Parse a possible command pair - command and parameter
* @param arg1 Command
* @param arg2 Parameter (could be NULL)
* @return How many parameters were used, 0,1,2
*/
int raspicamcontrol_parse_cmdline(RASPICAM_CAMERA_PARAMETERS *params, const char *arg1, const char *arg2)
{
int command_id, used = 0, num_parameters;
if (!arg1)
return 0;
command_id = raspicli_get_command_id(cmdline_commands, cmdline_commands_size, arg1, &num_parameters);
// If invalid command, or we are missing a parameter, drop out
if (command_id==-1 || (command_id != -1 && num_parameters > 0 && arg2 == NULL))
return 0;
switch (command_id)
{
case CommandSharpness : // sharpness - needs single number parameter
sscanf(arg2, "%d", ¶ms->sharpness);
used = 2;
break;
case CommandContrast : // contrast - needs single number parameter
sscanf(arg2, "%d", ¶ms->contrast);
used = 2;
break;
case CommandBrightness : // brightness - needs single number parameter
sscanf(arg2, "%d", ¶ms->brightness);
used = 2;
break;
case CommandSaturation : // saturation - needs single number parameter
sscanf(arg2, "%d", ¶ms->saturation);
used = 2;
break;
case CommandISO : // ISO - needs single number parameter
sscanf(arg2, "%d", ¶ms->ISO);
used = 2;
break;
case CommandVideoStab : // video stabilisation - if here, its on
params->videoStabilisation = 1;
used = 1;
break;
case CommandEVComp : // EV - needs single number parameter
sscanf(arg2, "%d", ¶ms->exposureCompensation);
used = 2;
break;
case CommandExposure : // exposure mode - needs string
params->exposureMode = exposure_mode_from_string(arg2);
used = 2;
break;
case CommandAWB : // AWB mode - needs single number parameter
params->awbMode = awb_mode_from_string(arg2);
used = 2;
break;
case CommandImageFX : // Image FX - needs string
params->imageEffect = imagefx_mode_from_string(arg2);
used = 2;
break;
case CommandColourFX : // Colour FX - needs string "u:v"
sscanf(arg2, "%d:%d", ¶ms->colourEffects.u, ¶ms->colourEffects.v);
params->colourEffects.enable = 1;
used = 2;
break;
case CommandMeterMode:
params->exposureMeterMode = metering_mode_from_string(arg2);
used = 2;
break;
case CommandRotation : // Rotation - degree
sscanf(arg2, "%d", ¶ms->rotation);
used = 2;
break;
case CommandHFlip :
params->hflip = 1;
used = 1;
break;
case CommandVFlip :
params->vflip = 1;
used = 1;
break;
case CommandROI :
{
double x,y,w,h;
int args;
args = sscanf(arg2, "%lf,%lf,%lf,%lf", &x,&y,&w,&h);
if (args != 4 || x > 1.0 || y > 1.0 || w > 1.0 || h > 1.0)
{
return 0;
}
// Make sure we stay within bounds
if (x + w > 1.0)
w = 1 - x;
if (y + h > 1.0)
h = 1 - y;
params->roi.x = x;
params->roi.y = y;
params->roi.w = w;
params->roi.h = h;
used = 2;
break;
}
case CommandShutterSpeed : // Shutter speed needs single number parameter
{
sscanf(arg2, "%d", ¶ms->shutter_speed);
used = 2;
break;
}
case CommandAwbGains :
{
double r,b;
int args;
args = sscanf(arg2, "%lf,%lf", &r,&b);
if (args != 2 || r > 8.0 || b > 8.0)
{
return 0;
}
params->awb_gains_r = r;
params->awb_gains_b = b;
used = 2;
break;
}
case CommandDRCLevel:
{
params->drc_level = drc_mode_from_string(arg2);
used = 2;
break;
}
case CommandStatsPass:
{
params->stats_pass = MMAL_TRUE;
used = 1;
break;
}
case CommandAnnotate:
{
// If parameter is a number, assume its a bitmask, otherwise a string
if (isdigit(*arg2))
{
sscanf(arg2, "%u", ¶ms->enable_annotate);
}
else
{
params->enable_annotate = ANNOTATE_USER_TEXT;
//copy string char by char and replace "\n" with newline character
unsigned char c;
char const *s = arg2;
char *t = ¶ms->annotate_string[0];
int n=0;
while ((c = *s++) && n < MMAL_CAMERA_ANNOTATE_MAX_TEXT_LEN_V3-1)
{
if (c == '\\' && *s)
{
switch (c = *s++)
{
case 'n':
c = '\n';
break;
default:
c = '\\';
s--;
break;
}
}
*(t++) = c;
n++;
}
*t='\0';
//params->annotate_string[MMAL_CAMERA_ANNOTATE_MAX_TEXT_LEN_V3-1] = '\0';
}
used=2;
break;
}
case CommandAnnotateExtras:
{
// 3 parameters - text size (6-80), text colour (Hex VVUUYY) and background colour (Hex VVUUYY)
sscanf(arg2, "%u,%X,%X", ¶ms->annotate_text_size,
¶ms->annotate_text_colour,
¶ms->annotate_bg_colour);
used=2;
break;
}
case CommandStereoMode:
{
params->stereo_mode.mode = stereo_mode_from_string(arg2);
used = 2;
break;
}
case CommandStereoDecimate:
{
params->stereo_mode.decimate = MMAL_TRUE;
used = 1;
break;
}
case CommandStereoSwap:
{
params->stereo_mode.swap_eyes = MMAL_TRUE;
used = 1;
break;
}
}
return used;
}
/**
* Display help for command line options
*/
void raspicamcontrol_display_help()
{
int i;
fprintf(stderr, "\nImage parameter commands\n\n");
raspicli_display_help(cmdline_commands, cmdline_commands_size);
fprintf(stderr, "\n\nNotes\n\nExposure mode options :\n%s", exposure_map[0].mode );
for (i=1;i<exposure_map_size;i++)
{
fprintf(stderr, ",%s", exposure_map[i].mode);
}
fprintf(stderr, "\n\nAWB mode options :\n%s", awb_map[0].mode );
for (i=1;i<awb_map_size;i++)
{
fprintf(stderr, ",%s", awb_map[i].mode);
}
fprintf(stderr, "\n\nImage Effect mode options :\n%s", imagefx_map[0].mode );
for (i=1;i<imagefx_map_size;i++)
{
fprintf(stderr, ",%s", imagefx_map[i].mode);
}
fprintf(stderr, "\n\nMetering Mode options :\n%s", metering_mode_map[0].mode );
for (i=1;i<metering_mode_map_size;i++)
{
fprintf(stderr, ",%s", metering_mode_map[i].mode);
}
fprintf(stderr, "\n\nDynamic Range Compression (DRC) options :\n%s", drc_mode_map[0].mode );
for (i=1;i<drc_mode_map_size;i++)
{
fprintf(stderr, ",%s", drc_mode_map[i].mode);
}
fprintf(stderr, "\n");
}
/**
* Dump contents of camera parameter structure to stdout for debugging/verbose logging
*
* @param params Const pointer to parameters structure to dump
*/
void raspicamcontrol_dump_parameters(const RASPICAM_CAMERA_PARAMETERS *params)
{
const char *exp_mode = raspicli_unmap_xref(params->exposureMode, exposure_map, exposure_map_size);
const char *awb_mode = raspicli_unmap_xref(params->awbMode, awb_map, awb_map_size);
const char *image_effect = raspicli_unmap_xref(params->imageEffect, imagefx_map, imagefx_map_size);
const char *metering_mode = raspicli_unmap_xref(params->exposureMeterMode, metering_mode_map, metering_mode_map_size);
fprintf(stderr, "Sharpness %d, Contrast %d, Brightness %d\n", params->sharpness, params->contrast, params->brightness);
fprintf(stderr, "Saturation %d, ISO %d, Video Stabilisation %s, Exposure compensation %d\n", params->saturation, params->ISO, params->videoStabilisation ? "Yes": "No", params->exposureCompensation);
fprintf(stderr, "Exposure Mode '%s', AWB Mode '%s', Image Effect '%s'\n", exp_mode, awb_mode, image_effect);
fprintf(stderr, "Metering Mode '%s', Colour Effect Enabled %s with U = %d, V = %d\n", metering_mode, params->colourEffects.enable ? "Yes":"No", params->colourEffects.u, params->colourEffects.v);
fprintf(stderr, "Rotation %d, hflip %s, vflip %s\n", params->rotation, params->hflip ? "Yes":"No",params->vflip ? "Yes":"No");
fprintf(stderr, "ROI x %lf, y %f, w %f h %f\n", params->roi.x, params->roi.y, params->roi.w, params->roi.h);
}
/**
* Convert a MMAL status return value to a simple boolean of success
* ALso displays a fault if code is not success
*
* @param status The error code to convert
* @return 0 if status is sucess, 1 otherwise
*/
int mmal_status_to_int(MMAL_STATUS_T status)
{
if (status == MMAL_SUCCESS)
return 0;
else
{
switch (status)
{
case MMAL_ENOMEM : vcos_log_error("Out of memory"); break;
case MMAL_ENOSPC : vcos_log_error("Out of resources (other than memory)"); break;
case MMAL_EINVAL: vcos_log_error("Argument is invalid"); break;
case MMAL_ENOSYS : vcos_log_error("Function not implemented"); break;
case MMAL_ENOENT : vcos_log_error("No such file or directory"); break;
case MMAL_ENXIO : vcos_log_error("No such device or address"); break;
case MMAL_EIO : vcos_log_error("I/O error"); break;
case MMAL_ESPIPE : vcos_log_error("Illegal seek"); break;
case MMAL_ECORRUPT : vcos_log_error("Data is corrupt \attention FIXME: not POSIX"); break;
case MMAL_ENOTREADY :vcos_log_error("Component is not ready \attention FIXME: not POSIX"); break;
case MMAL_ECONFIG : vcos_log_error("Component is not configured \attention FIXME: not POSIX"); break;
case MMAL_EISCONN : vcos_log_error("Port is already connected "); break;
case MMAL_ENOTCONN : vcos_log_error("Port is disconnected"); break;
case MMAL_EAGAIN : vcos_log_error("Resource temporarily unavailable. Try again later"); break;
case MMAL_EFAULT : vcos_log_error("Bad address"); break;
default : vcos_log_error("Unknown status error"); break;
}
return 1;
}
}
/**
* Give the supplied parameter block a set of default values
* @params Pointer to parameter block
*/
void raspicamcontrol_set_defaults(RASPICAM_CAMERA_PARAMETERS *params)
{
vcos_assert(params);
params->sharpness = 0;
params->contrast = 0;
params->brightness = 50;
params->saturation = 0;
params->ISO = 0; // 0 = auto
params->videoStabilisation = 0;
params->exposureCompensation = 0;
params->exposureMode = MMAL_PARAM_EXPOSUREMODE_AUTO;
params->exposureMeterMode = MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
params->awbMode = MMAL_PARAM_AWBMODE_AUTO;
params->imageEffect = MMAL_PARAM_IMAGEFX_NONE;
params->colourEffects.enable = 0;
params->colourEffects.u = 128;
params->colourEffects.v = 128;
params->rotation = 0;
params->hflip = params->vflip = 0;
params->roi.x = params->roi.y = 0.0;
params->roi.w = params->roi.h = 1.0;
params->shutter_speed = 0; // 0 = auto
params->awb_gains_r = 0; // Only have any function if AWB OFF is used.
params->awb_gains_b = 0;
params->drc_level = MMAL_PARAMETER_DRC_STRENGTH_OFF;
params->stats_pass = MMAL_FALSE;
params->enable_annotate = 0;
params->annotate_string[0] = '\0';
params->annotate_text_size = 0; //Use firmware default
params->annotate_text_colour = -1; //Use firmware default
params->annotate_bg_colour = -1; //Use firmware default
params->stereo_mode.mode = MMAL_STEREOSCOPIC_MODE_NONE;
params->stereo_mode.decimate = MMAL_FALSE;
params->stereo_mode.swap_eyes = MMAL_FALSE;
}
/**
* Get all the current camera parameters from specified camera component
* @param camera Pointer to camera component
* @param params Pointer to parameter block to accept settings
* @return 0 if successful, non-zero if unsuccessful
*/
int raspicamcontrol_get_all_parameters(MMAL_COMPONENT_T *camera, RASPICAM_CAMERA_PARAMETERS *params)
{
vcos_assert(camera);
vcos_assert(params);
if (!camera || !params)
return 1;
/* TODO : Write these get functions
params->sharpness = raspicamcontrol_get_sharpness(camera);
params->contrast = raspicamcontrol_get_contrast(camera);
params->brightness = raspicamcontrol_get_brightness(camera);
params->saturation = raspicamcontrol_get_saturation(camera);
params->ISO = raspicamcontrol_get_ISO(camera);
params->videoStabilisation = raspicamcontrol_get_video_stabilisation(camera);
params->exposureCompensation = raspicamcontrol_get_exposure_compensation(camera);
params->exposureMode = raspicamcontrol_get_exposure_mode(camera);
params->awbMode = raspicamcontrol_get_awb_mode(camera);
params->imageEffect = raspicamcontrol_get_image_effect(camera);
params->colourEffects = raspicamcontrol_get_colour_effect(camera);
params->thumbnailConfig = raspicamcontrol_get_thumbnail_config(camera);
*/
return 0;
}
/**
* Set the specified camera to all the specified settings
* @param camera Pointer to camera component
* @param params Pointer to parameter block containing parameters
* @return 0 if successful, none-zero if unsuccessful.
*/
int raspicamcontrol_set_all_parameters(MMAL_COMPONENT_T *camera, const RASPICAM_CAMERA_PARAMETERS *params)
{
int result;
result = raspicamcontrol_set_saturation(camera, params->saturation);
result += raspicamcontrol_set_sharpness(camera, params->sharpness);
result += raspicamcontrol_set_contrast(camera, params->contrast);
result += raspicamcontrol_set_brightness(camera, params->brightness);
result += raspicamcontrol_set_ISO(camera, params->ISO);
result += raspicamcontrol_set_video_stabilisation(camera, params->videoStabilisation);
result += raspicamcontrol_set_exposure_compensation(camera, params->exposureCompensation);
result += raspicamcontrol_set_exposure_mode(camera, params->exposureMode);
result += raspicamcontrol_set_metering_mode(camera, params->exposureMeterMode);
result += raspicamcontrol_set_awb_mode(camera, params->awbMode);
result += raspicamcontrol_set_awb_gains(camera, params->awb_gains_r, params->awb_gains_b);
result += raspicamcontrol_set_imageFX(camera, params->imageEffect);
result += raspicamcontrol_set_colourFX(camera, ¶ms->colourEffects);
//result += raspicamcontrol_set_thumbnail_parameters(camera, ¶ms->thumbnailConfig); TODO Not working for some reason
result += raspicamcontrol_set_rotation(camera, params->rotation);
result += raspicamcontrol_set_flips(camera, params->hflip, params->vflip);
result += raspicamcontrol_set_ROI(camera, params->roi);
result += raspicamcontrol_set_shutter_speed(camera, params->shutter_speed);
result += raspicamcontrol_set_DRC(camera, params->drc_level);
result += raspicamcontrol_set_stats_pass(camera, params->stats_pass);
result += raspicamcontrol_set_annotate(camera, params->enable_annotate, params->annotate_string,
params->annotate_text_size,
params->annotate_text_colour,
params->annotate_bg_colour);
return result;
}
/**
* Adjust the saturation level for images
* @param camera Pointer to camera component
* @param saturation Value to adjust, -100 to 100
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_saturation(MMAL_COMPONENT_T *camera, int saturation)
{
int ret = 0;
if (!camera)
return 1;
if (saturation >= -100 && saturation <= 100)
{
MMAL_RATIONAL_T value = {saturation, 100};
ret = mmal_status_to_int(mmal_port_parameter_set_rational(camera->control, MMAL_PARAMETER_SATURATION, value));
}
else
{
vcos_log_error("Invalid saturation value");