Skip to content

Commit 991f40a

Browse files
Merge pull request #299 from brilliantlabsAR/pairing_flow
Reduced camera image noise (pink vertical lines) by constraining RGB gain values Changed the default camera auto exposure settings so that photos are better out of the box Updated the text displayed during pairing and firmware update to better reflect the status
2 parents 8dd9f13 + fdf435f commit 991f40a

File tree

5 files changed

+135
-44
lines changed

5 files changed

+135
-44
lines changed

source/application/bluetooth.c

+2
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ void SD_EVT_IRQHandler(void)
332332
bond_storage,
333333
(uint32_t *)&bond.keyset.keys_own.p_enc_key->enc_info,
334334
sizeof(bond.keyset.keys_own.p_enc_key->enc_info));
335+
336+
status = show_pairing_screen(true, false);
335337
}
336338

337339
break;

source/application/lua_libraries/camera.c

+89-25
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ static struct camera_auto_last_values
5050
double green_gain;
5151
double blue_gain;
5252
} last = {
53-
.shutter = 500.0f,
53+
.shutter = 1600.0f,
5454
.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,
5858
};
5959

6060
static struct camera_capture_settings
@@ -561,11 +561,12 @@ static int lua_camera_auto(lua_State *L)
561561
}
562562

563563
// Default auto exposure settings
564-
camera_metering_mode_t metering = AVERAGE;
565-
double target_exposure = 0.18;
566-
double exposure_speed = 0.50;
567-
double shutter_limit = 1600.0;
568-
double analog_gain_limit = 60.0;
564+
camera_metering_mode_t metering = CENTER_WEIGHTED;
565+
double target_exposure = 0.1;
566+
double exposure_speed = 0.45;
567+
double shutter_limit = 16383.0;
568+
double analog_gain_limit = 16.0;
569+
double rgb_gain_limit = 287.0;
569570

570571
// Default white balance settings
571572
double white_balance_speed = 0.5;
@@ -655,6 +656,17 @@ static int lua_camera_auto(lua_State *L)
655656

656657
lua_pop(L, 1);
657658
}
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+
}
658670
}
659671

660672
// Get current brightness from FPGA
@@ -668,6 +680,25 @@ static int lua_camera_auto(lua_State *L)
668680
double matrix_g = metering_data[4] / 255.0f;
669681
double matrix_b = metering_data[5] / 255.0f;
670682

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+
671702
double spot_average = (spot_r + spot_g + spot_b) / 3.0;
672703
double matrix_average = (matrix_r + matrix_g + matrix_b) / 3.0;
673704
double center_weighted_average = (spot_average +
@@ -769,6 +800,10 @@ static int lua_camera_auto(lua_State *L)
769800
? matrix_g / last.green_gain
770801
: matrix_b / last.blue_gain);
771802

803+
// scale normalized RGB values to the gain scale
804+
max_rgb *= 256.0;
805+
806+
// target per-channel gains that we blend towards
772807
double red_gain = max_rgb / matrix_r * last.red_gain;
773808
double green_gain = max_rgb / matrix_g * last.green_gain;
774809
double blue_gain = max_rgb / matrix_b * last.blue_gain;
@@ -777,19 +812,6 @@ static int lua_camera_auto(lua_State *L)
777812
double blending_factor = (scene_brightness - white_balance_min_activation) /
778813
(white_balance_max_activation -
779814
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-
}
793815
if (blending_factor > 1.0)
794816
{
795817
blending_factor = 1.0;
@@ -811,9 +833,51 @@ static int lua_camera_auto(lua_State *L)
811833
(blue_gain - last.blue_gain) +
812834
last.blue_gain;
813835

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);
836+
double max_rgb_gain = last.red_gain > last.green_gain
837+
? (last.red_gain > last.blue_gain
838+
? last.red_gain
839+
: last.blue_gain)
840+
: (last.green_gain > last.blue_gain
841+
? last.green_gain
842+
: last.blue_gain);
843+
844+
// Scale per-channel gains so the largest channel is at most rgb_gain_limit
845+
if (max_rgb_gain > rgb_gain_limit)
846+
{
847+
double scale_factor = rgb_gain_limit / max_rgb_gain;
848+
last.red_gain *= scale_factor;
849+
last.green_gain *= scale_factor;
850+
last.blue_gain *= scale_factor;
851+
}
852+
853+
if (last.red_gain > 1023.0)
854+
{
855+
last.red_gain = 1023.0;
856+
}
857+
if (last.red_gain <= 0.0)
858+
{
859+
last.red_gain = 0.0001;
860+
}
861+
if (last.green_gain > 1023.0)
862+
{
863+
last.green_gain = 1023.0;
864+
}
865+
if (last.green_gain <= 0.0)
866+
{
867+
last.green_gain = 0.0001;
868+
}
869+
if (last.blue_gain > 1023.0)
870+
{
871+
last.blue_gain = 1023.0;
872+
}
873+
if (last.blue_gain <= 0.0)
874+
{
875+
last.blue_gain = 0.0001;
876+
}
877+
878+
uint16_t red_gain_uint16 = (uint16_t)(last.red_gain);
879+
uint16_t green_gain_uint16 = (uint16_t)(last.green_gain);
880+
uint16_t blue_gain_uint16 = (uint16_t)(last.blue_gain);
817881

818882
check_error(i2c_write(CAMERA, 0x5180, 0x03, red_gain_uint16 >> 8).fail);
819883
check_error(i2c_write(CAMERA, 0x5181, 0xFF, red_gain_uint16).fail);

source/application/lua_libraries/system.c

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636

3737
static int lua_update(lua_State *L)
3838
{
39+
int status = show_pairing_screen(false, true);
40+
if (status != LUA_OK)
41+
{
42+
const char *lua_error = lua_tostring(L, -1);
43+
luaL_error(L, "%s", lua_error);
44+
}
3945
check_error(sd_power_gpregret_set(0, 0xB1));
4046
NVIC_SystemReset();
4147
return 0;

source/application/luaport.c

+34-17
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,39 @@ void lua_break_signal_interrupt(void)
6363
1);
6464
}
6565

66+
int show_pairing_screen(bool is_paired, bool is_update)
67+
{
68+
int status;
69+
if(L_global == NULL)
70+
{
71+
return LUA_ERRRUN;
72+
}
73+
if (is_update)
74+
{
75+
status = luaL_dostring(L_global, "frame.display.text('Frame Update', 200, 140);"
76+
"frame.display.show();");
77+
}
78+
else if (!is_paired)
79+
{
80+
status = luaL_dostring(L_global, "frame.display.text('Ready to Pair', 200, 140);"
81+
"frame.display.text('Frame '..frame.bluetooth.address():sub(-2, -1), 245, 210, { color = 'GREEN' });"
82+
"frame.display.show();");
83+
}
84+
else
85+
{
86+
status = luaL_dostring(L_global, "frame.display.text('Frame is Paired', 185, 140);"
87+
"frame.display.text('Frame '..frame.bluetooth.address():sub(-2, -1), 245, 210, { color = 'ORANGE' });"
88+
"frame.display.show();");
89+
}
90+
91+
if (status != LUA_OK)
92+
{
93+
lua_pop(L_global, -1);
94+
error();
95+
}
96+
return status;
97+
}
98+
6699
void run_lua(bool is_paired)
67100
{
68101
lua_State *L = luaL_newstate();
@@ -127,24 +160,8 @@ void run_lua(bool is_paired)
127160
}
128161

129162
// Show splash screen
130-
if (!is_paired)
131-
{
132-
status = luaL_dostring(L, "frame.display.text('Ready to Pair', 200, 140);"
133-
"frame.display.text('Frame '..frame.bluetooth.address():sub(-2, -1), 245, 210, { color = 'GREEN' });"
134-
"frame.display.show();");
135-
}
136-
else
137-
{
138-
status = luaL_dostring(L, "frame.display.text('Frame is Paired', 185, 140);"
139-
"frame.display.text('Frame '..frame.bluetooth.address():sub(-2, -1), 245, 210, { color = 'ORANGE' });"
140-
"frame.display.show();");
141-
}
163+
status = show_pairing_screen(is_paired, false);
142164

143-
if (status != LUA_OK)
144-
{
145-
lua_pop(L, -1);
146-
error();
147-
}
148165

149166
// Run REPL
150167
while (true)

source/application/luaport.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <stdio.h>
3030
#include "bluetooth.h"
3131
#include "nrfx_log.h"
32-
32+
#include "lua.h"
3333
#define lua_writestring(s, l) bluetooth_send_data((uint8_t *)s, l)
3434
#define lua_writeline()
3535
#define lua_writestringerror(s, p) printf(s, p)
@@ -38,4 +38,6 @@ void lua_write_to_repl(uint8_t *buffer, uint8_t length);
3838

3939
void lua_break_signal_interrupt(void);
4040

41-
void run_lua(bool is_paired);
41+
void run_lua(bool is_paired);
42+
43+
int show_pairing_screen(bool is_paired, bool is_update);

0 commit comments

Comments
 (0)