Skip to content

Commit e562d92

Browse files
committed
Initial implementation of API/user config controlled auto exposure and gain settings
1 parent dfcb17e commit e562d92

File tree

7 files changed

+89
-14
lines changed

7 files changed

+89
-14
lines changed

ESP/ini/dev_config.ini

+8-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ build_flags =
2828
-DENABLE_ADHOC=${wifi.enableadhoc}
2929
-DADHOC_CHANNEL=${wifi.adhocchannel}
3030
-DWIFI_CHANNEL=${wifi.channel}
31-
-DDEBUG_ESP_PORT=Serial
32-
'-DMDNS_HOSTNAME=${wifi.mdnsname}'
31+
-DSIMPLE_AUTO_EXPOSURE_ON=${autoexposure.simple_auto_exposure_on}
32+
-DFANCY_AUTO_EXPOSURE_ON=${autoexposure.fancy_auto_exposure_on}
33+
-DAE_LEVEL=${autoexposure.ae_level}
34+
-DAEC_VALUE=${autoexposure.aec_value}
35+
-DAUTO_GAIN_ON=${autoexposure.auto_gain_on}
36+
-DBRIGHTNESS=${autoexposure.brightness}
37+
-DDEBUG_ESP_PORT=Serial ; set the debug port
38+
'-DMDNS_HOSTNAME=${wifi.mdnsname}' ; Set the OTA password
3339
'-DWIFI_SSID=${wifi.ssid}'
3440
'-DWIFI_PASSWORD=${wifi.password}'
3541
'-DWIFI_AP_SSID=${wifi.ap_ssid}'

ESP/ini/user_config.ini

+11
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,14 @@ otaserverip = "openiristracker.local"
1717
otalogin = "openiris"
1818
otapassword = "12345678"
1919
otaserverport = 3232
20+
21+
[autoexposure]
22+
simple_auto_exposure_on = 0 ; should the auto exposure be enabled, 0 - off, 1 - on
23+
fancy_auto_exposure_on = 0 ; should the more advanced auto exposure be enabled, 0 - on, 1 - off
24+
25+
ae_level = 0 ; auto exposure level, -2 to 2, 0 is the default
26+
aec_value = 300 ; auto exposure control, a sort of brightness, 0 - 1200
27+
28+
auto_gain_on = 0 ; should the auto gain be enabled, 0 - off, 1 - on
29+
30+
brightness = 2 ; controls the brightness, -2 to 2, to is the default

ESP/lib/src/data/config/project_config.cpp

+31-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ void ProjectConfig::initConfig() {
5555
.href = 0,
5656
.framesize = 4,
5757
.quality = 7,
58-
.brightness = 2,
58+
.brightness = BRIGHTNESS,
59+
.simple_auto_exposure_on = SIMPLE_AUTO_EXPOSURE_ON,
60+
.fancy_auto_exposure_on = FANCY_AUTO_EXPOSURE_ON,
61+
.ae_level = AE_LEVEL,
62+
.aec_value = AEC_VALUE,
63+
.auto_gain_on = AUTO_GAIN_ON,
5964
};
6065
}
6166

@@ -132,6 +137,12 @@ void ProjectConfig::cameraConfigSave() {
132137
putInt("framesize", this->config.camera.framesize);
133138
putInt("quality", this->config.camera.quality);
134139
putInt("brightness", this->config.camera.brightness);
140+
putInt("brightness", this->config.camera.brightness );
141+
putInt("simple_auto_exposure_on", this->config.camera.fancy_auto_exposure_on );
142+
putInt("fancy_auto_exposure_on", this->config.camera.fancy_auto_exposure_on );
143+
putInt("ae_level", this->config.camera.ae_level );
144+
putInt("aec_value", this->config.camera.aec_value );
145+
putInt("auto_gain_on ", this->config.camera.auto_gain_on );
135146
}
136147

137148
bool ProjectConfig::reset() {
@@ -198,7 +209,12 @@ void ProjectConfig::load() {
198209
this->config.camera.href = getInt("href", 0);
199210
this->config.camera.framesize = getInt("framesize", 4);
200211
this->config.camera.quality = getInt("quality", 7);
201-
this->config.camera.brightness = getInt("brightness", 2);
212+
this->config.camera.brightness = getInt("brightness", BRIGHTNESS);
213+
this->config.camera.fancy_auto_exposure_on = getInt("simple_auto_exposure_on", SIMPLE_AUTO_EXPOSURE_ON);
214+
this->config.camera.fancy_auto_exposure_on = getInt("fancy_auto_exposure_on", FANCY_AUTO_EXPOSURE_ON);
215+
this->config.camera.ae_level = getInt("ae_level", AE_LEVEL);
216+
this->config.camera.aec_value = getInt("aec_value", AEC_VALUE);
217+
this->config.camera.auto_gain_on = getInt("auto_gain_on ", AUTO_GAIN_ON);
202218

203219
this->_already_loaded = true;
204220
this->notify(ObserverEvent::configLoaded);
@@ -238,13 +254,23 @@ void ProjectConfig::setCameraConfig(uint8_t* vflip,
238254
uint8_t* href,
239255
uint8_t* quality,
240256
uint8_t* brightness,
257+
bool simple_auto_exposure_on,
258+
bool fancy_auto_exposure_on,
259+
int ae_level,
260+
unsigned int aec_value,
261+
bool auto_gain_on,
241262
bool shouldNotify) {
242263
log_d("Updating camera config");
243264
this->config.camera.vflip = *vflip;
244265
this->config.camera.href = *href;
245266
this->config.camera.framesize = *framesize;
246267
this->config.camera.quality = *quality;
247268
this->config.camera.brightness = *brightness;
269+
this->config.camera.simple_auto_exposure_on = simple_auto_exposure_on;
270+
this->config.camera.fancy_auto_exposure_on = fancy_auto_exposure_on;
271+
this->config.camera.ae_level = ae_level;
272+
this->config.camera.aec_value = aec_value;
273+
this->config.camera.auto_gain_on = auto_gain_on;
248274

249275
log_d("Updating Camera config");
250276
if (shouldNotify)
@@ -372,9 +398,10 @@ std::string ProjectConfig::MDNSConfig_t::toRepresentation() {
372398
std::string ProjectConfig::CameraConfig_t::toRepresentation() {
373399
std::string json = Helpers::format_string(
374400
"\"camera_config\": {\"vflip\": %d,\"framesize\": %d,\"href\": "
375-
"%d,\"quality\": %d,\"brightness\": %d}",
401+
"%d,\"quality\": %d,\"brightness\": %d, \"simple_auto_exposure_on\": %d, \"fancy_auto_exposure_on\": %d,\"ae_level\": %d, \"aec_value\": %d, \"auto_gain_on\": %d }",
376402
this->vflip, this->framesize, this->href, this->quality,
377-
this->brightness);
403+
this->brightness, this->simple_auto_exposure_on, this->fancy_auto_exposure_on,
404+
this->aec_value, this->ae_level, this->auto_gain_on);
378405
return json;
379406
}
380407

ESP/lib/src/data/config/project_config.hpp

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class ProjectConfig : public Preferences, public ISubject {
4545
uint8_t framesize;
4646
uint8_t quality;
4747
uint8_t brightness;
48+
bool simple_auto_exposure_on;
49+
bool fancy_auto_exposure_on;
50+
int ae_level;
51+
int aec_value;
52+
bool auto_gain_on;
4853

4954
std::string toRepresentation();
5055
};
@@ -114,6 +119,11 @@ class ProjectConfig : public Preferences, public ISubject {
114119
uint8_t* href,
115120
uint8_t* quality,
116121
uint8_t* brightness,
122+
bool simple_auto_exposure_on,
123+
bool fancy_auto_exposure_on,
124+
int ae_level,
125+
unsigned int aec_value,
126+
bool auto_gain_on,
117127
bool shouldNotify);
118128
void setWifiConfig(const std::string& networkName,
119129
const std::string& ssid,

ESP/lib/src/io/camera/cameraHandler.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ void CameraHandler::setupBasicResolution()
7676

7777
void CameraHandler::setupCameraSensor()
7878
{
79+
ProjectConfig::CameraConfig_t *cameraConfig = configManager->getCameraConfig();
7980
camera_sensor = esp_camera_sensor_get();
8081
// fixes corrupted jpegs, https://github.com/espressif/esp32-camera/issues/203
8182
// documentation https://www.uctronics.com/download/cam_module/OV2640DS.pdf
8283
camera_sensor->set_reg(camera_sensor, 0xff, 0xff, 0x00); // banksel, here we're directly writing to the registers. 0xFF==0x00 is the first bank, there's also 0xFF==0x01
8384
camera_sensor->set_reg(camera_sensor, 0xd3, 0xff, 5); // clock
84-
camera_sensor->set_brightness(camera_sensor, 2); // -2 to 2
85+
camera_sensor->set_brightness(camera_sensor, cameraConfig->brightness); // -2 to 2
8586
camera_sensor->set_contrast(camera_sensor, 2); // -2 to 2
8687
camera_sensor->set_saturation(camera_sensor, -2); // -2 to 2
8788

@@ -91,13 +92,13 @@ void CameraHandler::setupCameraSensor()
9192
camera_sensor->set_wb_mode(camera_sensor, 0); // 0 to 4 - if awb_gain enabled (0 - Auto, 1 - Sunny, 2 - Cloudy, 3 - Office, 4 - Home)
9293

9394
// controls the exposure
94-
camera_sensor->set_exposure_ctrl(camera_sensor, 0); // 0 = disable , 1 = enable
95-
camera_sensor->set_aec2(camera_sensor, 0); // 0 = disable , 1 = enable
96-
camera_sensor->set_ae_level(camera_sensor, 0); // -2 to 2
97-
camera_sensor->set_aec_value(camera_sensor, 300); // 0 to 1200
95+
camera_sensor->set_exposure_ctrl(camera_sensor, cameraConfig->simple_auto_exposure_on); // 0 = disable , 1 = enable
96+
camera_sensor->set_aec2(camera_sensor, cameraConfig->fancy_auto_exposure_on); // 0 = disable , 1 = enable
97+
camera_sensor->set_ae_level(camera_sensor, cameraConfig->ae_level); // -2 to 2
98+
camera_sensor->set_aec_value(camera_sensor, cameraConfig->aec_value); // 0 to 1200
9899

99100
// controls the gain
100-
camera_sensor->set_gain_ctrl(camera_sensor, 0); // 0 = disable , 1 = enable
101+
camera_sensor->set_gain_ctrl(camera_sensor, cameraConfig->auto_gain_on); // 0 = disable , 1 = enable
101102

102103
// automatic gain control gain, controls by how much the resulting image should be amplified
103104
camera_sensor->set_agc_gain(camera_sensor, 2); // 0 to 30

ESP/lib/src/io/camera/cameraHandler.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class CameraHandler : public IObserver
1919
int setCameraResolution(framesize_t frameSize);
2020
int setVFlip(int direction);
2121
int setHFlip(int direction);
22-
int setVieWindow(int offsetX, int offsetY, int outputX, int outputY);
2322
void update(ObserverEvent::Event event);
2423
void resetCamera(bool type = 0);
2524

ESP/lib/src/network/api/baseAPI/baseAPI.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,17 @@ void BaseAPI::setCamera(AsyncWebServerRequest* request) {
284284
uint8_t temp_camera_hflip = 0;
285285
uint8_t temp_camera_quality = 0;
286286
uint8_t temp_camera_brightness = 0;
287+
uint8_t temp_simple_auto_exposure_on = SIMPLE_AUTO_EXPOSURE_ON;
288+
uint8_t temp_fancy_auto_exposure_on = FANCY_AUTO_EXPOSURE_ON;
289+
int temp_ae_level = AE_LEVEL;
290+
unsigned int temp_aec_value = AEC_VALUE;
291+
uint8_t temp_auto_gain_on = AUTO_GAIN_ON;
292+
287293

288294
int params = request->params();
295+
// TODO we probably should refactor this at some point, maybe some serialization of sorts, or direct mapping of
296+
// TODO get parameters? Can we even do that?
297+
289298
//! Using the else if statements to ensure that the values do not need to
290299
//! be set in a specific order This means the order of the URL params does
291300
//! not matter
@@ -301,13 +310,25 @@ void BaseAPI::setCamera(AsyncWebServerRequest* request) {
301310
temp_camera_quality = (uint8_t)param->value().toInt();
302311
} else if (param->name() == "brightness") {
303312
temp_camera_brightness = (uint8_t)param->value().toInt();
313+
} else if (param->name() == "simple_auto_exposure_on") {
314+
temp_simple_auto_exposure_on = (uint8_t)param->value().toInt();
315+
} else if (param->name() == "fancy_auto_exposure_on") {
316+
temp_fancy_auto_exposure_on = (uint8_t)param->value().toInt();
317+
} else if (param->name() == "ae_level") {
318+
temp_ae_level = param->value().toInt();
319+
} else if (param->name() == "aec_value") {
320+
temp_aec_value = (uint8_t)param->value().toInt();
321+
} else if (param->name() == "auto_gain_on") {
322+
temp_auto_gain_on = (uint8_t)param->value().toInt();
304323
}
305324
}
306325
// note: We're passing empty params by design, this is done to reset
307326
// specific fields
308327
projectConfig->setCameraConfig(&temp_camera_vflip, &temp_camera_framesize,
309328
&temp_camera_hflip, &temp_camera_quality,
310-
&temp_camera_brightness, true);
329+
&temp_camera_brightness, temp_simple_auto_exposure_on,
330+
temp_fancy_auto_exposure_on, temp_ae_level,
331+
temp_aec_value, temp_auto_gain_on, true);
311332

312333
request->send(200, MIMETYPE_JSON,
313334
"{\"msg\":\"Done. Camera Settings have been set.\"}");

0 commit comments

Comments
 (0)