-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.cpp
293 lines (248 loc) · 7.78 KB
/
camera.cpp
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
/**
* @package Wildlife Camera
* Camera handling
* @author WizLab.it
* @board AI-Thinker ESP32-CAM
* @version 20240811.044
*/
#include "camera.h"
/**
* Camera
* Class constructor
* @param frameSize Size of the photo (see framesize_t)
* @param jpegQuality JPG quality (1-100, low value is better quality)
* @param sdCardEnabled Use SD Card to save photos
*/
Camera::Camera(framesize_t frameSize, int jpegQuality, bool sdCardEnabled) {
_frameSize = frameSize;
_jpegQuality = jpegQuality;
_sdCardEnabled = sdCardEnabled;
_sdIsOpen = false;
}
/**
* Camera::init
* Camera Configuration
* @return true if camera is successfully initialized; false otherwise
*/
bool Camera::init() {
//Configure camera
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = _CAMERA_Y2_GPIO_NUM;
config.pin_d1 = _CAMERA_Y3_GPIO_NUM;
config.pin_d2 = _CAMERA_Y4_GPIO_NUM;
config.pin_d3 = _CAMERA_Y5_GPIO_NUM;
config.pin_d4 = _CAMERA_Y6_GPIO_NUM;
config.pin_d5 = _CAMERA_Y7_GPIO_NUM;
config.pin_d6 = _CAMERA_Y8_GPIO_NUM;
config.pin_d7 = _CAMERA_Y9_GPIO_NUM;
config.pin_xclk = _CAMERA_XCLK_GPIO_NUM;
config.pin_pclk = _CAMERA_PCLK_GPIO_NUM;
config.pin_vsync = _CAMERA_VSYNC_GPIO_NUM;
config.pin_href = _CAMERA_HREF_GPIO_NUM;
config.pin_sscb_sda = _CAMERA_SIOD_GPIO_NUM;
config.pin_sscb_scl = _CAMERA_SIOC_GPIO_NUM;
config.pin_pwdn = _CAMERA_PWDN_GPIO_NUM;
config.pin_reset = _CAMERA_RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
config.grab_mode = CAMERA_GRAB_LATEST;
config.frame_size = _frameSize;
config.jpeg_quality = _jpegQuality;
config.fb_count = 1;
//Initialize camera
esp_err_t err = esp_camera_init(&config);
if(err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return false;
}
//Flash
pinMode(_CAMERA_FLASH_PIN, OUTPUT);
digitalWrite(_CAMERA_FLASH_PIN, LOW);
//If here, all good
return true;
}
/**
* Camera::takePhoto
* Takes a photo, optionally using the built-in flash
* @param image pointer of a pointer that will be used to store the image data (original variable should be NULL)
* @param useFlash if true, the flash is activated
* @return size of the *image* memory block, or negative value in the case of failure (-1: photo capture failed; -2: photo size is 0)
*/
long Camera::takePhoto(uint8_t **image, bool useFlash) {
//Check if to activate flash
if(useFlash == true) {
digitalWrite(_CAMERA_FLASH_PIN, HIGH);
delay(50);
}
//Dispose first picture because of bad quality
camera_fb_t *fb = NULL;
fb = esp_camera_fb_get();
esp_camera_fb_return(fb);
//Takes a new photo
fb = NULL;
fb = esp_camera_fb_get();
//Deactivate flash
digitalWrite(_CAMERA_FLASH_PIN, LOW);
//Check if photo capture failed
if(!fb) return -1;
if(fb->len == 0) return -2;
//Save photo on SD Card
if(sdOpen()) {
//Build path and file name based on datetime info
String path = _CAMERA_SD_BASE_PATH;
String filename = "/WCP-";
if(getDateFormat("%Y") == "") {
//No datetime info
path += "/UnknownDate";
filename += String(random(100000000, 999999999)) + ".jpg";
} else {
//Datetime info available
path += "/" + getDateFormat("%F");
filename += getDateFormat("%Y%m%d-%H%M%S") + ".jpg";
}
String pathfilename = path + filename;
//Save photo on SD Card
if(SD_MMC.mkdir(path.c_str())) {
fs::FS &fs = SD_MMC;
File file = fs.open(pathfilename.c_str(), FILE_WRITE);
if(file) {
size_t wb = file.write(fb->buf, fb->len);
Serial.printf(" [+] Photo saved on SD Card: %s (%d bytes)\n", pathfilename.c_str(), wb);
}
file.close();
//Update Photo DB
photoDBPack.photoDB.photoCounter++;
memset(photoDBPack.photoDB.lastPhotoFilename, 0x00, _CAMERA_PHOTODB_FILENAME_MAX_LENGTH);
strncpy(photoDBPack.photoDB.lastPhotoFilename, pathfilename.c_str(), (_CAMERA_PHOTODB_FILENAME_MAX_LENGTH - 1));
photoDBPack.photoDB.lastPhotoTimestamp = getTimestamp();
_sdPhotoDbSave();
}
}
sdClose();
//Copy image data in memory space
long imageSize = fb->len;
*image = (uint8_t *)malloc(imageSize);
memcpy(*image, fb->buf, imageSize);
esp_camera_fb_return(fb);
return imageSize;
}
/**
* Camera::flashBlink
* Blink the flash
* @param duration Blink duration in milliseconds
*/
void Camera::flashBlink(uint16_t duration) {
digitalWrite(_CAMERA_FLASH_PIN, 1);
delay(duration);
digitalWrite(_CAMERA_FLASH_PIN, 0);
}
/**
* Camera::flashGpioHold
* Set hold status for flash GPIO pin
* @param status true: enable hold; false: disable hold
*/
void Camera::flashGpioHold(bool status) {
if(status) {
gpio_hold_en(_CAMERA_FLASH_PIN);
} else {
gpio_hold_dis(_CAMERA_FLASH_PIN);
}
}
/**
* Camera::sdOpen
* Open SD Card, check if base path exists
* @return true if SD Card is successfully opened; false otherwise
*/
bool Camera::sdOpen() {
//Check if to use SD Card
if(!_sdCardEnabled) return false;
//Check if SD is already open
if(_sdIsOpen) return true;
//Try to open SD Card and base path
if(!SD_MMC.begin("/sdcard", true) || (SD_MMC.cardType() == CARD_NONE) || !SD_MMC.mkdir(_CAMERA_SD_BASE_PATH)) {
Serial.println(" [-] Error opening SD Card");
sdClose();
return false;
}
//Check system Photo DB CRC: if not valid, then try to load Photo DB from SD Card
uint32_t crc = CRC32::calculate((const uint8_t *)&photoDBPack.photoDB, sizeof(_PhotoDB));
if(photoDBPack.crc != crc) {
//Try to load Photo DB from SD Card
if(SD_MMC.exists(_CAMERA_PHOTODB)) {
fs::FS &fs = SD_MMC;
uint32_t crcTmp;
//Read Photo DB on SD Card
struct _PhotoDBPackage photoDBTmp;
File file = fs.open(_CAMERA_PHOTODB, FILE_READ);
if(file) file.read((uint8_t *)&photoDBTmp, sizeof(_PhotoDBPackage));
file.close();
//Check CRC of Photo DB from SD Card and if last photo file exists: if OK, then load it in system Photo DB
crcTmp = CRC32::calculate((const uint8_t *)&photoDBTmp.photoDB, sizeof(_PhotoDB));
if((photoDBTmp.crc == crcTmp) && SD_MMC.exists(photoDBTmp.photoDB.lastPhotoFilename)) {
memcpy(&photoDBPack, &photoDBTmp, sizeof(_PhotoDBPackage));
} else {
//Photo DB on SD Card is invalid, delete it
Serial.println(" [-] Removed invalid Photo DB on SD Card");
SD_MMC.remove(_CAMERA_PHOTODB);
}
}
}
_sdIsOpen = true;
return true;
}
/**
* Camera::sdClose
* Close SD Card
*/
void Camera::sdClose() {
_sdIsOpen = false;
SD_MMC.end();
//Free pins 12 and 13
pinMode(12, INPUT);
pinMode(13, INPUT);
}
/**
* Camera::sdGetUsedSpace
* Get percentage of used space on SD Card
* @return SD card usage percentage
*/
float Camera::sdGetUsedSpace() {
float usedSpace = -1.0;
if(sdOpen()) {
usedSpace = SD_MMC.usedBytes() * 100.00 / SD_MMC.totalBytes();
}
sdClose();
return usedSpace;
}
/**
* Camera::sdGetPhotoCounter
* Get number of photo on SD Card
* @return Number of photo in Photo DB
*/
uint16_t Camera::sdGetPhotoCounter() {
return photoDBPack.photoDB.photoCounter;
}
/**
* Camera::sdGetLastPhotoTimestamp
* Get timestamp of the last photo
* @return Timestamp of the last photo
*/
unsigned long Camera::sdGetLastPhotoTimestamp() {
return photoDBPack.photoDB.lastPhotoTimestamp;
}
/**
* Camera::_photoDbSave
* Save system Photo DB on SD Card
* Also check the used space on SD Card
*/
void Camera::_sdPhotoDbSave() {
//Calculate Photo DB CRC
photoDBPack.crc = CRC32::calculate((const uint8_t *)&photoDBPack.photoDB, sizeof(_PhotoDB));
//Save on SD Card
fs::FS &fs = SD_MMC;
File file = fs.open(_CAMERA_PHOTODB, FILE_WRITE);
if(file) file.write((uint8_t *)&photoDBPack, sizeof(_PhotoDBPackage));
file.close();
}