@@ -50,11 +50,11 @@ static struct camera_auto_last_values
50
50
double green_gain ;
51
51
double blue_gain ;
52
52
} last = {
53
- .shutter = 500 .0f ,
53
+ .shutter = 4096 .0f ,
54
54
.analog_gain = 1.0f ,
55
- .red_gain = 1.9f ,
56
- .green_gain = 1 .0f ,
57
- .blue_gain = 2.2f ,
55
+ .red_gain = 121.6f ,
56
+ .green_gain = 64 .0f ,
57
+ .blue_gain = 140.8f ,
58
58
};
59
59
60
60
static struct camera_capture_settings
@@ -564,8 +564,9 @@ static int lua_camera_auto(lua_State *L)
564
564
camera_metering_mode_t metering = AVERAGE ;
565
565
double target_exposure = 0.18 ;
566
566
double exposure_speed = 0.50 ;
567
- double shutter_limit = 1600.0 ;
568
- double analog_gain_limit = 60.0 ;
567
+ double shutter_limit = 8192.0 ;
568
+ double analog_gain_limit = 16.0 ;
569
+ double rgb_gain_limit = 141.0 ;
569
570
570
571
// Default white balance settings
571
572
double white_balance_speed = 0.5 ;
@@ -655,6 +656,17 @@ static int lua_camera_auto(lua_State *L)
655
656
656
657
lua_pop (L , 1 );
657
658
}
659
+
660
+ if (lua_getfield (L , 1 , "rgb_gain_limit" ) != LUA_TNIL )
661
+ {
662
+ rgb_gain_limit = luaL_checknumber (L , -1 );
663
+ if (rgb_gain_limit < 0.0 || rgb_gain_limit > 1023.0 )
664
+ {
665
+ luaL_error (L , "rgb_gain_limit must be between 0 and 1023" );
666
+ }
667
+
668
+ lua_pop (L , 1 );
669
+ }
658
670
}
659
671
660
672
// Get current brightness from FPGA
@@ -668,6 +680,25 @@ static int lua_camera_auto(lua_State *L)
668
680
double matrix_g = metering_data [4 ] / 255.0f ;
669
681
double matrix_b = metering_data [5 ] / 255.0f ;
670
682
683
+ if (spot_r == 0.0 ) {
684
+ spot_r = 0.0001 ;
685
+ }
686
+ if (spot_g == 0.0 ) {
687
+ spot_g = 0.0001 ;
688
+ }
689
+ if (spot_b == 0.0 ) {
690
+ spot_b = 0.0001 ;
691
+ }
692
+ if (matrix_r == 0.0 ) {
693
+ matrix_r = 0.0001 ;
694
+ }
695
+ if (matrix_g == 0.0 ) {
696
+ matrix_g = 0.0001 ;
697
+ }
698
+ if (matrix_b == 0.0 ) {
699
+ matrix_b = 0.0001 ;
700
+ }
701
+
671
702
double spot_average = (spot_r + spot_g + spot_b ) / 3.0 ;
672
703
double matrix_average = (matrix_r + matrix_g + matrix_b ) / 3.0 ;
673
704
double center_weighted_average = (spot_average +
@@ -777,19 +808,6 @@ static int lua_camera_auto(lua_State *L)
777
808
double blending_factor = (scene_brightness - white_balance_min_activation ) /
778
809
(white_balance_max_activation -
779
810
white_balance_min_activation );
780
-
781
- if (red_gain > 1023.0 )
782
- {
783
- red_gain = 1023.0 ;
784
- }
785
- if (green_gain > 1023.0 )
786
- {
787
- green_gain = 1023.0 ;
788
- }
789
- if (blue_gain > 1023.0 )
790
- {
791
- blue_gain = 1023.0 ;
792
- }
793
811
if (blending_factor > 1.0 )
794
812
{
795
813
blending_factor = 1.0 ;
@@ -811,9 +829,51 @@ static int lua_camera_auto(lua_State *L)
811
829
(blue_gain - last .blue_gain ) +
812
830
last .blue_gain ;
813
831
814
- uint16_t red_gain_uint16 = (uint16_t )(last .red_gain * 256.0 );
815
- uint16_t green_gain_uint16 = (uint16_t )(last .green_gain * 256.0 );
816
- uint16_t blue_gain_uint16 = (uint16_t )(last .blue_gain * 256.0 );
832
+ double max_rgb_gain = last .red_gain > last .green_gain
833
+ ? (last .red_gain > last .blue_gain
834
+ ? last .red_gain
835
+ : last .blue_gain )
836
+ : (last .green_gain > last .blue_gain
837
+ ? last .green_gain
838
+ : last .blue_gain );
839
+
840
+ // Scale per-channel gains so the largest channel is at most rgb_gain_limit
841
+ if (max_rgb_gain > rgb_gain_limit )
842
+ {
843
+ double scale_factor = rgb_gain_limit / max_rgb_gain ;
844
+ last .red_gain *= scale_factor ;
845
+ last .green_gain *= scale_factor ;
846
+ last .blue_gain *= scale_factor ;
847
+ }
848
+
849
+ if (last .red_gain > 1023.0 )
850
+ {
851
+ last .red_gain = 1023.0 ;
852
+ }
853
+ if (last .red_gain <= 0.0 )
854
+ {
855
+ last .red_gain = 0.0001 ;
856
+ }
857
+ if (last .green_gain > 1023.0 )
858
+ {
859
+ last .green_gain = 1023.0 ;
860
+ }
861
+ if (last .green_gain <= 0.0 )
862
+ {
863
+ last .green_gain = 0.0001 ;
864
+ }
865
+ if (last .blue_gain > 1023.0 )
866
+ {
867
+ last .blue_gain = 1023.0 ;
868
+ }
869
+ if (last .blue_gain <= 0.0 )
870
+ {
871
+ last .blue_gain = 0.0001 ;
872
+ }
873
+
874
+ uint16_t red_gain_uint16 = (uint16_t )(last .red_gain );
875
+ uint16_t green_gain_uint16 = (uint16_t )(last .green_gain );
876
+ uint16_t blue_gain_uint16 = (uint16_t )(last .blue_gain );
817
877
818
878
check_error (i2c_write (CAMERA , 0x5180 , 0x03 , red_gain_uint16 >> 8 ).fail );
819
879
check_error (i2c_write (CAMERA , 0x5181 , 0xFF , red_gain_uint16 ).fail );
0 commit comments