-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcamera.cpp
More file actions
298 lines (257 loc) · 8.45 KB
/
camera.cpp
File metadata and controls
298 lines (257 loc) · 8.45 KB
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
/**
* camera.cpp - Camera control functions
*
* Copyright (c) 2019, David Imhoff <dimhoff.devel@gmail.com>
*
* 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 author 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 AUTHOR ``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 AUTHOR 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 "config.h"
#include "Arduino.h"
#include "esp_camera.h"
#include "driver/rtc_io.h" // rtc_gpio_hold_en()
#include "camera.h"
#include "io_defs.h"
#include "configuration.h"
/**
* Configure the camera based on current system configuration
*/
bool camera_reconfigure()
{
int res;
sensor_t *s = esp_camera_sensor_get();
res = s->set_framesize(s, cfg.getFrameSize());
if (res != 0) {
Serial.printf("Unable to set 'frame size': return code %d\n", res);
return false;
}
res = s->set_quality(s, cfg.getQuality());
if (res != 0) {
Serial.printf("Unable to set 'quality': return code %d\n", res);
return false;
}
res = s->set_contrast(s, cfg.getContrast());
if (res != 0) {
Serial.printf("Unable to set 'contrast': return code %d\n", res);
return false;
}
res = s->set_brightness(s, cfg.getBrightness());
if (res != 0) {
Serial.printf("Unable to set 'brightness': return code %d\n", res);
return false;
}
res = s->set_saturation(s, cfg.getSaturation());
if (res != 0) {
Serial.printf("Unable to set 'saturation': return code %d\n", res);
return false;
}
res = s->set_colorbar(s, cfg.getColorBar());
if (res != 0) {
Serial.printf("Unable to set 'colorbar': return code %d\n", res);
return false;
}
res = s->set_hmirror(s, cfg.getHMirror());
if (res != 0) {
Serial.printf("Unable to set 'hmirror': return code %d\n", res);
return false;
}
res = s->set_vflip(s, cfg.getVFlip());
if (res != 0) {
Serial.printf("Unable to set 'vflip': return code %d\n", res);
return false;
}
res = s->set_whitebal(s, cfg.getAwb());
if (res != 0) {
Serial.printf("Unable to set 'whitebal': return code %d\n", res);
return false;
}
res = s->set_awb_gain(s, cfg.getAwbGain());
if (res != 0) {
Serial.printf("Unable to set 'awb_gain': return code %d\n", res);
return false;
}
res = s->set_wb_mode(s, cfg.getWhiteBalanceMode());
if (res != 0) {
Serial.printf("Unable to set 'wb_mode': return code %d\n", res);
return false;
}
res = s->set_gain_ctrl(s, cfg.getAgc());
if (res != 0) {
Serial.printf("Unable to set 'gain_ctrl': return code %d\n", res);
return false;
}
res = s->set_agc_gain(s, cfg.getAgcGain());
if (res != 0) {
Serial.printf("Unable to set 'agc_gain': return code %d\n", res);
return false;
}
res = s->set_gainceiling(s, cfg.getGainCeiling());
if (res != 0) {
Serial.printf("Unable to set 'gainceiling': return code %d\n", res);
return false;
}
res = s->set_exposure_ctrl(s, cfg.getAec());
if (res != 0) {
Serial.printf("Unable to set 'exposure_ctrl': return code %d\n", res);
return false;
}
res = s->set_aec_value(s, cfg.getExposureValue());
if (res != 0) {
Serial.printf("Unable to set 'aec_value': return code %d\n", res);
return false;
}
res = s->set_aec2(s, cfg.getAec2());
if (res != 0) {
Serial.printf("Unable to set 'aec2': return code %d\n", res);
return false;
}
res = s->set_ae_level(s, cfg.getAeLevel());
if (res != 0) {
Serial.printf("Unable to set 'ae_level': return code %d\n", res);
return false;
}
res = s->set_dcw(s, cfg.getDcw());
if (res != 0) {
Serial.printf("Unable to set 'dcw': return code %d\n", res);
return false;
}
res = s->set_bpc(s, cfg.getBlackPixelCancellation());
if (res != 0) {
Serial.printf("Unable to set 'bpc': return code %d\n", res);
return false;
}
res = s->set_wpc(s, cfg.getWhitePixelCancellation());
if (res != 0) {
Serial.printf("Unable to set 'wpc': return code %d\n", res);
return false;
}
res = s->set_raw_gma(s, cfg.getRawGamma());
if (res != 0) {
Serial.printf("Unable to set 'raw_gma': return code %d\n", res);
return false;
}
res = s->set_lenc(s, cfg.getLensCorrection());
if (res != 0) {
Serial.printf("Unable to set 'lenc': return code %d\n", res);
return false;
}
res = s->set_special_effect(s, cfg.getSpecialEffect());
if (res != 0) {
Serial.printf("Unable to set 'special_effect': return code %d\n", res);
return false;
}
return true;
}
bool camera_init()
{
esp_err_t err = ESP_FAIL;
camera_config_t config;
// Enable camera 1.2 and 2.8 Volt
// TODO: this pin is no longer switching the PSU's if WITH_CAM_PWDN PCB PATCH is applied.
rtc_gpio_hold_dis(gpio_num_t(CAM_PWR_GPIO_NUM)); // TODO: move to after init to prevent glitch???
pinMode(CAM_PWR_GPIO_NUM, OUTPUT);
digitalWrite(CAM_PWR_GPIO_NUM, LOW);
// TODO: maybe wait for voltage to stabelize?
#if PWDN_GPIO_NUM >= 0
// Wakeup camera
rtc_gpio_hold_dis(gpio_num_t(PWDN_GPIO_NUM)); // TODO: move to after init to prevent glitch???
pinMode(PWDN_GPIO_NUM, OUTPUT);
digitalWrite(PWDN_GPIO_NUM, LOW);
#endif // PWDN_GPIO_NUM >= 0
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
//init with high specs to pre-allocate larger buffers
if (psramFound()) {
Serial.println("PSRAM found, using UXGA frame size");
config.frame_size = FRAMESIZE_UXGA;
config.jpeg_quality = 10;
config.fb_count = 2;
} else {
Serial.println("No PSRAM found, using SVGA frame size");
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
}
err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return false;
}
return camera_reconfigure();
}
void camera_deinit()
{
// Turn off camera power
esp_camera_deinit();
#if PWDN_GPIO_NUM >= 0
digitalWrite(PWDN_GPIO_NUM, HIGH); // esp_camera_deinit() doesn't power down camera...
#endif // PWDN_GPIO_NUM >= 0
#ifdef WITH_EVIL_CAM_PWR_SHUTDOWN
digitalWrite(CAM_PWR_GPIO_NUM, HIGH);
#endif // WITH_CAM_PWR_SHUTDOWN
}
camera_fb_t *camera_capture()
{
camera_fb_t *fb;
#ifdef WITH_FLASH
if (cfg.getEnableFlash()) {
digitalWrite(FLASH_GPIO_NUM, HIGH);
delay(100);
}
#endif // WITH_FLASH
// Take some shots to train the AGC/AWB
Serial.print("Training:");
for (int i=cfg.getTrainingShots(); i != 0; i--) {
Serial.printf(" %d", i);
fb = esp_camera_fb_get();
esp_camera_fb_return(fb);
}
Serial.println(" Done");
// Take picture
Serial.print("Taking picture... ");
fb = esp_camera_fb_get();
// Disable Flash
#ifdef WITH_FLASH
if (cfg.getEnableFlash()) {
digitalWrite(FLASH_GPIO_NUM, LOW);
}
#endif // WITH_FLASH
return fb;
}