| 2 |
raymond |
1 |
#pragma once
|
|
|
2 |
#include <Arduino.h>
|
|
|
3 |
#include "esp_camera.h"
|
|
|
4 |
#include <esp_err.h>
|
|
|
5 |
|
|
|
6 |
// Select camera model
|
|
|
7 |
//#define CAMERA_MODEL_WROVER_KIT
|
|
|
8 |
//#define CAMERA_MODEL_ESP_EYE
|
|
|
9 |
//#define CAMERA_MODEL_M5STACK_PSRAM
|
|
|
10 |
//#define CAMERA_MODEL_M5STACK_V2_PSRAM
|
|
|
11 |
//#define CAMERA_MODEL_M5STACK_WIDE
|
|
|
12 |
//#define CAMERA_MODEL_M5STACK_ESP32CAM
|
|
|
13 |
#define CAMERA_MODEL_AI_THINKER
|
|
|
14 |
//#define CAMERA_MODEL_TTGO_T_JOURNAL
|
|
|
15 |
|
|
|
16 |
#if defined(CAMERA_MODEL_WROVER_KIT)
|
|
|
17 |
//
|
|
|
18 |
// ESP WROVER
|
|
|
19 |
// https://dl.espressif.com/dl/schematics/ESP-WROVER-KIT_SCH-2.pdf
|
|
|
20 |
//
|
|
|
21 |
#define PWDN_GPIO_NUM -1
|
|
|
22 |
#define RESET_GPIO_NUM -1
|
|
|
23 |
#define XCLK_GPIO_NUM 21
|
|
|
24 |
#define SIOD_GPIO_NUM 26
|
|
|
25 |
#define SIOC_GPIO_NUM 27
|
|
|
26 |
#define Y9_GPIO_NUM 35
|
|
|
27 |
#define Y8_GPIO_NUM 34
|
|
|
28 |
#define Y7_GPIO_NUM 39
|
|
|
29 |
#define Y6_GPIO_NUM 36
|
|
|
30 |
#define Y5_GPIO_NUM 19
|
|
|
31 |
#define Y4_GPIO_NUM 18
|
|
|
32 |
#define Y3_GPIO_NUM 5
|
|
|
33 |
#define Y2_GPIO_NUM 4
|
|
|
34 |
#define VSYNC_GPIO_NUM 25
|
|
|
35 |
#define HREF_GPIO_NUM 23
|
|
|
36 |
#define PCLK_GPIO_NUM 22
|
|
|
37 |
#define LED_PIN 2 // A status led on the RGB; could also use pin 0 or 4
|
|
|
38 |
#define LED_ON HIGH //
|
|
|
39 |
#define LED_OFF LOW //
|
|
|
40 |
// #define LAMP_PIN x // No LED FloodLamp.
|
|
|
41 |
|
|
|
42 |
#elif defined(CAMERA_MODEL_ESP_EYE)
|
|
|
43 |
//
|
|
|
44 |
// ESP-EYE
|
|
|
45 |
// https://twitter.com/esp32net/status/1085488403460882437
|
|
|
46 |
#define PWDN_GPIO_NUM -1
|
|
|
47 |
#define RESET_GPIO_NUM -1
|
|
|
48 |
#define XCLK_GPIO_NUM 4
|
|
|
49 |
#define SIOD_GPIO_NUM 18
|
|
|
50 |
#define SIOC_GPIO_NUM 23
|
|
|
51 |
#define Y9_GPIO_NUM 36
|
|
|
52 |
#define Y8_GPIO_NUM 37
|
|
|
53 |
#define Y7_GPIO_NUM 38
|
|
|
54 |
#define Y6_GPIO_NUM 39
|
|
|
55 |
#define Y5_GPIO_NUM 35
|
|
|
56 |
#define Y4_GPIO_NUM 14
|
|
|
57 |
#define Y3_GPIO_NUM 13
|
|
|
58 |
#define Y2_GPIO_NUM 34
|
|
|
59 |
#define VSYNC_GPIO_NUM 5
|
|
|
60 |
#define HREF_GPIO_NUM 27
|
|
|
61 |
#define PCLK_GPIO_NUM 25
|
|
|
62 |
#define LED_PIN 21 // Status led
|
|
|
63 |
#define LED_ON HIGH //
|
|
|
64 |
#define LED_OFF LOW //
|
|
|
65 |
// #define LAMP_PIN x // No LED FloodLamp.
|
|
|
66 |
|
|
|
67 |
#elif defined(CAMERA_MODEL_M5STACK_PSRAM)
|
|
|
68 |
//
|
|
|
69 |
// ESP32 M5STACK
|
|
|
70 |
//
|
|
|
71 |
#define PWDN_GPIO_NUM -1
|
|
|
72 |
#define RESET_GPIO_NUM 15
|
|
|
73 |
#define XCLK_GPIO_NUM 27
|
|
|
74 |
#define SIOD_GPIO_NUM 25
|
|
|
75 |
#define SIOC_GPIO_NUM 23
|
|
|
76 |
#define Y9_GPIO_NUM 19
|
|
|
77 |
#define Y8_GPIO_NUM 36
|
|
|
78 |
#define Y7_GPIO_NUM 18
|
|
|
79 |
#define Y6_GPIO_NUM 39
|
|
|
80 |
#define Y5_GPIO_NUM 5
|
|
|
81 |
#define Y4_GPIO_NUM 34
|
|
|
82 |
#define Y3_GPIO_NUM 35
|
|
|
83 |
#define Y2_GPIO_NUM 32
|
|
|
84 |
#define VSYNC_GPIO_NUM 22
|
|
|
85 |
#define HREF_GPIO_NUM 26
|
|
|
86 |
#define PCLK_GPIO_NUM 21
|
|
|
87 |
// M5 Stack status/illumination LED details unknown/unclear
|
|
|
88 |
// #define LED_PIN x // Status led
|
|
|
89 |
// #define LED_ON HIGH //
|
|
|
90 |
// #define LED_OFF LOW //
|
|
|
91 |
// #define LAMP_PIN x // LED FloodLamp.
|
|
|
92 |
|
|
|
93 |
#elif defined(CAMERA_MODEL_M5STACK_V2_PSRAM)
|
|
|
94 |
//
|
|
|
95 |
// ESP32 M5STACK V2
|
|
|
96 |
//
|
|
|
97 |
#define PWDN_GPIO_NUM -1
|
|
|
98 |
#define RESET_GPIO_NUM 15
|
|
|
99 |
#define XCLK_GPIO_NUM 27
|
|
|
100 |
#define SIOD_GPIO_NUM 22
|
|
|
101 |
#define SIOC_GPIO_NUM 23
|
|
|
102 |
#define Y9_GPIO_NUM 19
|
|
|
103 |
#define Y8_GPIO_NUM 36
|
|
|
104 |
#define Y7_GPIO_NUM 18
|
|
|
105 |
#define Y6_GPIO_NUM 39
|
|
|
106 |
#define Y5_GPIO_NUM 5
|
|
|
107 |
#define Y4_GPIO_NUM 34
|
|
|
108 |
#define Y3_GPIO_NUM 35
|
|
|
109 |
#define Y2_GPIO_NUM 32
|
|
|
110 |
#define VSYNC_GPIO_NUM 25
|
|
|
111 |
#define HREF_GPIO_NUM 26
|
|
|
112 |
#define PCLK_GPIO_NUM 21
|
|
|
113 |
// M5 Stack status/illumination LED details unknown/unclear
|
|
|
114 |
// #define LED_PIN x // Status led
|
|
|
115 |
// #define LED_ON HIGH //
|
|
|
116 |
// #define LED_OFF LOW //
|
|
|
117 |
// #define LAMP_PIN x // LED FloodLamp.
|
|
|
118 |
|
|
|
119 |
#elif defined(CAMERA_MODEL_M5STACK_WIDE)
|
|
|
120 |
//
|
|
|
121 |
// ESP32 M5STACK WIDE
|
|
|
122 |
//
|
|
|
123 |
#define PWDN_GPIO_NUM -1
|
|
|
124 |
#define RESET_GPIO_NUM 15
|
|
|
125 |
#define XCLK_GPIO_NUM 27
|
|
|
126 |
#define SIOD_GPIO_NUM 22
|
|
|
127 |
#define SIOC_GPIO_NUM 23
|
|
|
128 |
#define Y9_GPIO_NUM 19
|
|
|
129 |
#define Y8_GPIO_NUM 36
|
|
|
130 |
#define Y7_GPIO_NUM 18
|
|
|
131 |
#define Y6_GPIO_NUM 39
|
|
|
132 |
#define Y5_GPIO_NUM 5
|
|
|
133 |
#define Y4_GPIO_NUM 34
|
|
|
134 |
#define Y3_GPIO_NUM 35
|
|
|
135 |
#define Y2_GPIO_NUM 32
|
|
|
136 |
#define VSYNC_GPIO_NUM 25
|
|
|
137 |
#define HREF_GPIO_NUM 26
|
|
|
138 |
#define PCLK_GPIO_NUM 21
|
|
|
139 |
// M5 Stack status/illumination LED details unknown/unclear
|
|
|
140 |
// #define LED_PIN x // Status led
|
|
|
141 |
// #define LED_ON HIGH //
|
|
|
142 |
// #define LED_OFF LOW //
|
|
|
143 |
// #define LAMP_PIN x // LED FloodLamp.
|
|
|
144 |
|
|
|
145 |
#elif defined(CAMERA_MODEL_M5STACK_ESP32CAM)
|
|
|
146 |
//
|
|
|
147 |
// Common M5 Stack without PSRAM
|
|
|
148 |
//
|
|
|
149 |
#define PWDN_GPIO_NUM -1
|
|
|
150 |
#define RESET_GPIO_NUM 15
|
|
|
151 |
#define XCLK_GPIO_NUM 27
|
|
|
152 |
#define SIOD_GPIO_NUM 25
|
|
|
153 |
#define SIOC_GPIO_NUM 23
|
|
|
154 |
#define Y9_GPIO_NUM 19
|
|
|
155 |
#define Y8_GPIO_NUM 36
|
|
|
156 |
#define Y7_GPIO_NUM 18
|
|
|
157 |
#define Y6_GPIO_NUM 39
|
|
|
158 |
#define Y5_GPIO_NUM 5
|
|
|
159 |
#define Y4_GPIO_NUM 34
|
|
|
160 |
#define Y3_GPIO_NUM 35
|
|
|
161 |
#define Y2_GPIO_NUM 17
|
|
|
162 |
#define VSYNC_GPIO_NUM 22
|
|
|
163 |
#define HREF_GPIO_NUM 26
|
|
|
164 |
#define PCLK_GPIO_NUM 21
|
|
|
165 |
// Note NO PSRAM,; so maximum working resolution is XGA 1024×768
|
|
|
166 |
// M5 Stack status/illumination LED details unknown/unclear
|
|
|
167 |
// #define LED_PIN x // Status led
|
|
|
168 |
// #define LED_ON HIGH //
|
|
|
169 |
// #define LED_OFF LOW //
|
|
|
170 |
// #define LAMP_PIN x // LED FloodLamp.
|
|
|
171 |
|
|
|
172 |
#elif defined(CAMERA_MODEL_AI_THINKER)
|
|
|
173 |
//
|
|
|
174 |
// AI Thinker
|
|
|
175 |
// https://github.com/SeeedDocument/forum_doc/raw/master/reg/ESP32_CAM_V1.6.pdf
|
|
|
176 |
//
|
|
|
177 |
#define PWDN_GPIO_NUM 32
|
|
|
178 |
#define RESET_GPIO_NUM -1
|
|
|
179 |
#define XCLK_GPIO_NUM 0
|
|
|
180 |
#define SIOD_GPIO_NUM 26
|
|
|
181 |
#define SIOC_GPIO_NUM 27
|
|
|
182 |
#define Y9_GPIO_NUM 35
|
|
|
183 |
#define Y8_GPIO_NUM 34
|
|
|
184 |
#define Y7_GPIO_NUM 39
|
|
|
185 |
#define Y6_GPIO_NUM 36
|
|
|
186 |
#define Y5_GPIO_NUM 21
|
|
|
187 |
#define Y4_GPIO_NUM 19
|
|
|
188 |
#define Y3_GPIO_NUM 18
|
|
|
189 |
#define Y2_GPIO_NUM 5
|
|
|
190 |
#define VSYNC_GPIO_NUM 25
|
|
|
191 |
#define HREF_GPIO_NUM 23
|
|
|
192 |
#define PCLK_GPIO_NUM 22
|
|
|
193 |
#define LED_PIN 33 // Status led
|
|
|
194 |
#define LED_ON LOW // - Pin is inverted.
|
|
|
195 |
#define LED_OFF HIGH //
|
|
|
196 |
#define LAMP_PIN 4 // LED FloodLamp.
|
|
|
197 |
|
|
|
198 |
#elif defined(CAMERA_MODEL_TTGO_T_JOURNAL)
|
|
|
199 |
//
|
|
|
200 |
// LilyGO TTGO T-Journal ESP32; with OLED! but not used here.. :-(
|
|
|
201 |
#define PWDN_GPIO_NUM 0
|
|
|
202 |
#define RESET_GPIO_NUM 15
|
|
|
203 |
#define XCLK_GPIO_NUM 27
|
|
|
204 |
#define SIOD_GPIO_NUM 25
|
|
|
205 |
#define SIOC_GPIO_NUM 23
|
|
|
206 |
#define Y9_GPIO_NUM 19
|
|
|
207 |
#define Y8_GPIO_NUM 36
|
|
|
208 |
#define Y7_GPIO_NUM 18
|
|
|
209 |
#define Y6_GPIO_NUM 39
|
|
|
210 |
#define Y5_GPIO_NUM 5
|
|
|
211 |
#define Y4_GPIO_NUM 34
|
|
|
212 |
#define Y3_GPIO_NUM 35
|
|
|
213 |
#define Y2_GPIO_NUM 17
|
|
|
214 |
#define VSYNC_GPIO_NUM 22
|
|
|
215 |
#define HREF_GPIO_NUM 26
|
|
|
216 |
#define PCLK_GPIO_NUM 21
|
|
|
217 |
// TTGO T Journal status/illumination LED details unknown/unclear
|
|
|
218 |
// #define LED_PIN 33 // Status led
|
|
|
219 |
// #define LED_ON LOW // - Pin is inverted.
|
|
|
220 |
// #define LED_OFF HIGH //
|
|
|
221 |
// #define LAMP_PIN 4 // LED FloodLamp.
|
|
|
222 |
|
|
|
223 |
#else
|
|
|
224 |
// Well.
|
|
|
225 |
// that went badly...
|
|
|
226 |
#error "Camera model not selected, did you forget to uncomment it in myconfig?"
|
|
|
227 |
#endif
|
|
|
228 |
|
|
|
229 |
camera_config_t camera_config = {
|
|
|
230 |
.pin_pwdn = PWDN_GPIO_NUM,
|
|
|
231 |
.pin_reset = RESET_GPIO_NUM,
|
|
|
232 |
.pin_xclk = XCLK_GPIO_NUM,
|
|
|
233 |
.pin_sscb_sda = SIOD_GPIO_NUM,
|
|
|
234 |
.pin_sscb_scl = SIOC_GPIO_NUM,
|
|
|
235 |
.pin_d7 = Y9_GPIO_NUM,
|
|
|
236 |
.pin_d6 = Y8_GPIO_NUM,
|
|
|
237 |
.pin_d5 = Y7_GPIO_NUM,
|
|
|
238 |
.pin_d4 = Y6_GPIO_NUM,
|
|
|
239 |
.pin_d3 = Y5_GPIO_NUM,
|
|
|
240 |
.pin_d2 = Y4_GPIO_NUM,
|
|
|
241 |
.pin_d1 = Y3_GPIO_NUM,
|
|
|
242 |
.pin_d0 = Y2_GPIO_NUM,
|
|
|
243 |
.pin_vsync = VSYNC_GPIO_NUM,
|
|
|
244 |
.pin_href = HREF_GPIO_NUM,
|
|
|
245 |
.pin_pclk = PCLK_GPIO_NUM,
|
|
|
246 |
.xclk_freq_hz = 20000000, //XCLK 20MHz or 10MHz
|
|
|
247 |
.ledc_timer = LEDC_TIMER_0,
|
|
|
248 |
.ledc_channel = LEDC_CHANNEL_0,
|
|
|
249 |
.pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
|
|
|
250 |
.frame_size = FRAMESIZE_SXGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
|
|
|
251 |
.jpeg_quality = 8, //0-63 lower number means higher quality
|
|
|
252 |
.fb_count = 2, //if more than one, i2s runs in continuous mode. Use only with JPEG
|
|
|
253 |
.fb_location = CAMERA_FB_IN_PSRAM,
|
|
|
254 |
.grab_mode = CAMERA_GRAB_LATEST
|
|
|
255 |
};
|
|
|
256 |
|
|
|
257 |
|
|
|
258 |
int lampChannel = 7; // a free PWM channel (some channels used by camera)
|
|
|
259 |
const int pwmfreq = 50000; // 50K pwm frequency
|
|
|
260 |
const int pwmresolution = 9; // duty cycle bit range
|
|
|
261 |
const int pwmMax = pow(2,pwmresolution)-1;
|
|
|
262 |
|
|
|
263 |
inline esp_err_t init_camera() {
|
|
|
264 |
//initialize the camera
|
|
|
265 |
Serial.print("Camera init... ");
|
|
|
266 |
esp_err_t err = esp_camera_init(&camera_config);
|
|
|
267 |
|
|
|
268 |
if (err != ESP_OK) {
|
|
|
269 |
delay(100); // need a delay here or the next serial o/p gets missed
|
|
|
270 |
Serial.printf("\n\nCRITICAL FAILURE: Camera sensor failed to initialise.\n\n");
|
|
|
271 |
Serial.printf("A full (hard, power off/on) reboot will probably be needed to recover from this.\n");
|
|
|
272 |
return err;
|
|
|
273 |
} else {
|
|
|
274 |
Serial.println("succeeded");
|
|
|
275 |
|
|
|
276 |
// Get a reference to the sensor
|
|
|
277 |
sensor_t* s = esp_camera_sensor_get();
|
|
|
278 |
|
|
|
279 |
// Dump camera module, warn for unsupported modules.
|
|
|
280 |
switch (s->id.PID) {
|
|
|
281 |
case OV9650_PID: Serial.println("WARNING: OV9650 camera module is not properly supported, will fallback to OV2640 operation"); break;
|
|
|
282 |
case OV7725_PID: Serial.println("WARNING: OV7725 camera module is not properly supported, will fallback to OV2640 operation"); break;
|
|
|
283 |
case OV2640_PID: Serial.println("OV2640 camera module detected"); break;
|
|
|
284 |
case OV3660_PID: Serial.println("OV3660 camera module detected"); break;
|
|
|
285 |
default: Serial.println("WARNING: Camera module is unknown and not properly supported, will fallback to OV2640 operation");
|
|
|
286 |
}
|
|
|
287 |
s->set_sharpness(s, 1);
|
|
|
288 |
s->set_vflip(s, 1);
|
|
|
289 |
//s->set_hmirror(s, 1);
|
|
|
290 |
|
|
|
291 |
}
|
|
|
292 |
return ESP_OK;
|
|
|
293 |
}
|