ESP_IOT v2.5
IOT ESP Coding
OV2640.cpp
Go to the documentation of this file.
1#include "OV2640.h"
2#include "../../../Defines.h"
3
4#ifdef USE_CAMERA_MODULE
5
6#define TAG "OV2640"
7
8// definitions appropriate for the ESP32-CAM devboard (and most clones)
9camera_config_t esp32cam_config{
10
11 .pin_pwdn = -1, // FIXME: on the TTGO T-Journal I think this is GPIO 0
12 .pin_reset = 15,
13
14 .pin_xclk = 27,
15
16 .pin_sscb_sda = 25,
17 .pin_sscb_scl = 23,
18
19 .pin_d7 = 19,
20 .pin_d6 = 36,
21 .pin_d5 = 18,
22 .pin_d4 = 39,
23 .pin_d3 = 5,
24 .pin_d2 = 34,
25 .pin_d1 = 35,
26 .pin_d0 = 17,
27 .pin_vsync = 22,
28 .pin_href = 26,
29 .pin_pclk = 21,
30 .xclk_freq_hz = 20000000,
31 .ledc_timer = LEDC_TIMER_0,
32 .ledc_channel = LEDC_CHANNEL_0,
33 .pixel_format = PIXFORMAT_JPEG,
34 // .frame_size = FRAMESIZE_UXGA, // needs 234K of framebuffer space
35 // .frame_size = FRAMESIZE_SXGA, // needs 160K for framebuffer
36 // .frame_size = FRAMESIZE_XGA, // needs 96K or even smaller FRAMESIZE_SVGA - can work if using only 1 fb
37 .frame_size = FRAMESIZE_SVGA,
38 .jpeg_quality = 12, //0-63 lower numbers are higher quality
39 .fb_count = 2 // if more than one i2s runs in continous mode. Use only with jpeg
40};
41
42camera_config_t esp32cam_aithinker_config{
43
44 .pin_pwdn = 32,
45 .pin_reset = -1,
46
47 .pin_xclk = 0,
48
49 .pin_sscb_sda = 26,
50 .pin_sscb_scl = 27,
51
52 // Note: LED GPIO is apparently 4 not sure where that goes
53 // per https://github.com/donny681/ESP32_CAMERA_QR/blob/e4ef44549876457cd841f33a0892c82a71f35358/main/led.c
54 .pin_d7 = 35,
55 .pin_d6 = 34,
56 .pin_d5 = 39,
57 .pin_d4 = 36,
58 .pin_d3 = 21,
59 .pin_d2 = 19,
60 .pin_d1 = 18,
61 .pin_d0 = 5,
62 .pin_vsync = 25,
63 .pin_href = 23,
64 .pin_pclk = 22,
65 .xclk_freq_hz = 20000000,
66 .ledc_timer = LEDC_TIMER_1,
67 .ledc_channel = LEDC_CHANNEL_1,
68 .pixel_format = PIXFORMAT_JPEG,
69 // .frame_size = FRAMESIZE_UXGA, // needs 234K of framebuffer space
70 // .frame_size = FRAMESIZE_SXGA, // needs 160K for framebuffer
71 // .frame_size = FRAMESIZE_XGA, // needs 96K or even smaller FRAMESIZE_SVGA - can work if using only 1 fb
72 .frame_size = FRAMESIZE_SVGA,
73 .jpeg_quality = 12, //0-63 lower numbers are higher quality
74 .fb_count = 2 // if more than one i2s runs in continous mode. Use only with jpeg
75};
76
77camera_config_t esp32cam_ttgo_t_config{
78
79 .pin_pwdn = 26,
80 .pin_reset = -1,
81
82 .pin_xclk = 32,
83
84 .pin_sscb_sda = 13,
85 .pin_sscb_scl = 12,
86
87 .pin_d7 = 39,
88 .pin_d6 = 36,
89 .pin_d5 = 23,
90 .pin_d4 = 18,
91 .pin_d3 = 15,
92 .pin_d2 = 4,
93 .pin_d1 = 14,
94 .pin_d0 = 5,
95 .pin_vsync = 27,
96 .pin_href = 25,
97 .pin_pclk = 19,
98 .xclk_freq_hz = 20000000,
99 .ledc_timer = LEDC_TIMER_0,
100 .ledc_channel = LEDC_CHANNEL_0,
101 .pixel_format = PIXFORMAT_JPEG,
102 .frame_size = FRAMESIZE_SVGA,
103 .jpeg_quality = 12, //0-63 lower numbers are higher quality
104 .fb_count = 2 // if more than one i2s runs in continous mode. Use only with jpeg
105};
106
107void OV2640::run(void)
108{
109 if (fb)
110 //return the frame buffer back to the driver for reuse
111 esp_camera_fb_return(fb);
112
113 fb = esp_camera_fb_get();
114}
115
116void OV2640::runIfNeeded(void)
117{
118 if (!fb)
119 run();
120}
121
122int OV2640::getWidth(void)
123{
124 runIfNeeded();
125 return fb->width;
126}
127
128int OV2640::getHeight(void)
129{
130 runIfNeeded();
131 return fb->height;
132}
133
134size_t OV2640::getSize(void)
135{
136 runIfNeeded();
137 if (!fb)
138 return 0; // FIXME - this shouldn't be possible but apparently the new cam board returns null sometimes?
139 return fb->len;
140}
141
142uint8_t *OV2640::getfb(void)
143{
144 runIfNeeded();
145 if (!fb)
146 return NULL; // FIXME - this shouldn't be possible but apparently the new cam board returns null sometimes?
147
148 return fb->buf;
149}
150
151framesize_t OV2640::getFrameSize(void)
152{
153 return _cam_config.frame_size;
154}
155
156void OV2640::setFrameSize(framesize_t size)
157{
158 _cam_config.frame_size = size;
159}
160
161pixformat_t OV2640::getPixelFormat(void)
162{
163 return _cam_config.pixel_format;
164}
165
166void OV2640::setPixelFormat(pixformat_t format)
167{
168 switch (format)
169 {
170 case PIXFORMAT_RGB565:
171 case PIXFORMAT_YUV422:
172 case PIXFORMAT_GRAYSCALE:
173 case PIXFORMAT_JPEG:
174 _cam_config.pixel_format = format;
175 break;
176 default:
177 _cam_config.pixel_format = PIXFORMAT_GRAYSCALE;
178 break;
179 }
180}
181
182esp_err_t OV2640::init(camera_config_t config)
183{
184 memset(&_cam_config, 0, sizeof(_cam_config));
185 memcpy(&_cam_config, &config, sizeof(config));
186
187 esp_err_t err = esp_camera_init(&_cam_config);
188 if (err != ESP_OK)
189 {
190 printf("Camera probe failed with error 0x%x", err);
191 return err;
192 }
193 // ESP_ERROR_CHECK(gpio_install_isr_service(0));
194
195 return ESP_OK;
196}
197
198#endif