Skip to content

Commit ccecc1f

Browse files
committed
Major improvements
1 parent 51fcfc2 commit ccecc1f

File tree

4 files changed

+105
-22
lines changed

4 files changed

+105
-22
lines changed

examples/ESP32cam.html

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,40 @@
9696
});
9797
}
9898

99-
document.addEventListener("DOMContentLoaded", setupEventListeners);
99+
// New function to populate the frame size dropdown based on max frame size
100+
function populateFrameSizeDropdown() {
101+
const frameSizes = [
102+
"R96x96", "QQVGA", "CIF", "HQVGA", "R240x240", "QVGA",
103+
"CIF", "HVGA", "VGA", "SVGA", "XGA", "HD", "SXGA",
104+
"UXGA", "FHD", "P_HD", "P_3MP", "QXGA", "QHD", "WQXGA", "P_FHD", "QSXGA"
105+
];
106+
107+
const frameSizeDropdown = document.getElementById('frame_size');
108+
109+
fetch('/get_max_frame_size')
110+
.then(response => response.text())
111+
.then(maxFrameSize => {
112+
const maxSizeIndex = parseInt(maxFrameSize, 10);
113+
frameSizeDropdown.innerHTML = '';
114+
115+
frameSizes.forEach((size, index) => {
116+
if (index <= maxSizeIndex) {
117+
const option = document.createElement('option');
118+
option.value = index;
119+
option.textContent = size;
120+
frameSizeDropdown.appendChild(option);
121+
}
122+
});
123+
})
124+
.catch(error => {
125+
console.error('Error fetching max frame size:', error);
126+
});
127+
}
128+
129+
document.addEventListener("DOMContentLoaded", () => {
130+
setupEventListeners();
131+
populateFrameSizeDropdown(); // Call this to populate the frame size options dynamically
132+
});
100133
</script>
101134
</head>
102135
<body>
@@ -105,6 +138,33 @@ <h1>ESP32 Camera Stream</h1>
105138
</div>
106139
<div class="container">
107140
<div class="settings-container">
141+
<div class="setting">
142+
<label for="frame_size">Frame Size:</label>
143+
<select id="frame_size">
144+
<option value="0">R96x96</option>
145+
<option value="1">QQVGA</option>
146+
<option value="2">CIF</option>
147+
<option value="3">HQVGA</option>
148+
<option value="4">R240x240</option>
149+
<option value="5">QVGA</option>
150+
<option value="6">CIF</option>
151+
<option value="7">HVGA</option>
152+
<option value="8">VGA</option>
153+
<option value="9">SVGA</option>
154+
<option value="10">XGA</option>
155+
<option value="11">HD</option>
156+
<option value="12">SXGA</option>
157+
<option value="13">UXGA</option>
158+
<option value="14">FHD</option>
159+
<option value="15">P_HD</option>
160+
<option value="16">P_3MP</option>
161+
<option value="17">QXGA</option>
162+
<option value="18">QHD</option>
163+
<option value="19">WQXGA</option>
164+
<option value="20">P_FHD</option>
165+
<option value="21">QSXGA</option>
166+
</select>
167+
</div>
108168
<div class="setting">
109169
<label for="quality">JPEG quality:</label>
110170
<input type="range" id="quality" min="0" max="100">
@@ -119,7 +179,6 @@ <h1>ESP32 Camera Stream</h1>
119179
<option value="4">Home</option>
120180
</select>
121181
</div>
122-
<!-- Neue Elemente -->
123182
<div class="setting">
124183
<label for="contrast" title="Regelt den Kontrast des Sensors. Positive Werte erhöhen den Kontrast, negative verringern ihn. Bereich: -2 bis +2.">Contrast:</label>
125184
<input type="range" id="contrast" min="-2" max="2">

src/modcamera.c

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void mp_camera_hal_construct(
100100
// configure camera based on arguments
101101
self->camera_config.pixel_format = pixel_format;
102102
self->camera_config.frame_size = frame_size;
103-
self->camera_config.jpeg_quality = (int8_t)map(jpeg_quality,0,100,63,0); //0-63 lower number means higher quality. TODO: Harmonization in API and Validation
103+
self->camera_config.jpeg_quality = (int8_t)map(jpeg_quality,0,100,63,0); //0-63 lower number means higher quality.
104104
self->camera_config.pin_d0 = data_pins[0];
105105
self->camera_config.pin_d1 = data_pins[1];
106106
self->camera_config.pin_d2 = data_pins[2];
@@ -128,7 +128,7 @@ void mp_camera_hal_construct(
128128
mp_warning(NULL, "Frame buffer size limited to 2");
129129
} else if (fb_count < 1) {
130130
self->camera_config.fb_count = 1;
131-
mp_warning(NULL, "Frame buffer size must be >0. Set to 1");
131+
mp_warning(NULL, "Frame buffer size must be >0. Setting it to 1");
132132
}
133133
else {
134134
self->camera_config.fb_count = fb_count; //if more than one, i2s runs in continuous mode. TODO: Test with others than JPEG
@@ -327,22 +327,19 @@ const mp_rom_map_elem_t mp_camera_hal_gainceiling_table[] = {
327327

328328
// For subsequent modules using this as example, you will probably only need the makros below.
329329
#define SENSOR_GETSET(type, name, field_name, setter_function_name) \
330-
SENSOR_GET(type, name, field_name, setter_function_name) \
330+
SENSOR_GET(type, name, field_name) \
331331
SENSOR_SET(type, name, setter_function_name)
332332

333333
#define SENSOR_GETSET_IN_RANGE(type, name, field_name, setter_function_name, min_val, max_val) \
334-
SENSOR_GET(type, name, field_name, setter_function_name) \
334+
SENSOR_GET(type, name, field_name) \
335335
SENSOR_SET_IN_RANGE(type, name, setter_function_name, min_val, max_val)
336336

337-
#define SENSOR_GET(type, name, status_field_name, getter_function_name) \
337+
#define SENSOR_GET(type, name, status_field_name) \
338338
type mp_camera_hal_get_##name(mp_camera_obj_t * self) { \
339339
if (!self->initialized) { \
340340
mp_raise_ValueError(MP_ERROR_TEXT("Camera not initialized")); \
341341
} \
342342
sensor_t *sensor = esp_camera_sensor_get(); \
343-
if (!sensor->getter_function_name) { \
344-
mp_raise_ValueError(MP_ERROR_TEXT("No attribute " #name)); \
345-
} \
346343
return sensor->status_field_name; \
347344
}
348345

@@ -377,13 +374,13 @@ const mp_rom_map_elem_t mp_camera_hal_gainceiling_table[] = {
377374
} \
378375
}
379376

377+
SENSOR_GET(framesize_t, frame_size, status.framesize);
380378
SENSOR_STATUS_GETSET_IN_RANGE(int, contrast, contrast, set_contrast, -2, 2);
381379
SENSOR_STATUS_GETSET_IN_RANGE(int, brightness, brightness, set_brightness, -2, 2);
382380
SENSOR_STATUS_GETSET_IN_RANGE(int, saturation, saturation, set_saturation, -2, 2);
383381
SENSOR_STATUS_GETSET_IN_RANGE(int, sharpness, sharpness, set_sharpness, -2, 2);
384382
SENSOR_STATUS_GETSET(int, denoise, denoise, set_denoise);
385383
SENSOR_STATUS_GETSET(mp_camera_gainceiling_t, gainceiling, gainceiling, set_gainceiling);
386-
static SENSOR_STATUS_GETSET(int, quality_raw, quality, set_quality); //in_Range not needed since driver limits value. Raw to be used in wrapper funtion
387384
SENSOR_STATUS_GETSET(bool, colorbar, colorbar, set_colorbar);
388385
SENSOR_STATUS_GETSET(bool, whitebal, awb, set_whitebal);
389386
SENSOR_STATUS_GETSET(bool, gain_ctrl, agc, set_gain_ctrl);
@@ -403,22 +400,47 @@ SENSOR_STATUS_GETSET(bool, wpc, wpc, set_wpc);
403400
SENSOR_STATUS_GETSET(bool, raw_gma, raw_gma, set_raw_gma);
404401
SENSOR_STATUS_GETSET(bool, lenc, lenc, set_lenc);
405402

406-
int mp_camera_hal_get_quality(mp_camera_obj_t *self){
407-
return map(mp_camera_hal_get_quality_raw(self),63,0,0,100);
403+
void mp_camera_hal_set_frame_size(mp_camera_obj_t * self, framesize_t value) {
404+
if (!self->initialized) {
405+
mp_raise_ValueError(MP_ERROR_TEXT("Camera not initialized"));
406+
}
407+
sensor_t *sensor = esp_camera_sensor_get();
408+
if (!sensor->set_framesize) {
409+
mp_raise_ValueError(MP_ERROR_TEXT("No attribute frame_size"));
410+
}
411+
if (sensor->set_framesize(sensor, value) < 0) {
412+
mp_raise_ValueError(MP_ERROR_TEXT("Invalid setting for frame_size"));
413+
} else {
414+
self->camera_config.frame_size = value;
415+
}
416+
}
417+
int mp_camera_hal_get_quality(mp_camera_obj_t * self) {
418+
if (!self->initialized) {
419+
mp_raise_ValueError(MP_ERROR_TEXT("Camera not initialized"));
420+
}
421+
sensor_t *sensor = esp_camera_sensor_get();
422+
return map(sensor->status.quality,63,0,0,100);
408423
}
409424

410-
void mp_camera_hal_set_quality(mp_camera_obj_t *self,int quality){
411-
mp_camera_hal_set_quality_raw(self,map(quality,0,100,63,0));
425+
void mp_camera_hal_set_quality(mp_camera_obj_t * self, int value) {
426+
if (!self->initialized) {
427+
mp_raise_ValueError(MP_ERROR_TEXT("Camera not initialized"));
428+
}
429+
sensor_t *sensor = esp_camera_sensor_get();
430+
if (!sensor->set_quality) {
431+
mp_raise_ValueError(MP_ERROR_TEXT("No attribute quality"));
432+
}
433+
if (sensor->set_quality(sensor, map(value,0,100,63,0)) < 0) {
434+
mp_raise_ValueError(MP_ERROR_TEXT("Invalid setting for quality"));
435+
} else {
436+
self->camera_config.jpeg_quality = map(value,0,100,63,0);
437+
}
412438
}
413439

414440
mp_camera_pixformat_t mp_camera_hal_get_pixel_format(mp_camera_obj_t *self) {
415441
return self->camera_config.pixel_format;
416442
}
417443

418-
mp_camera_framesize_t mp_camera_hal_get_frame_size(mp_camera_obj_t *self) {
419-
return self->camera_config.frame_size;
420-
}
421-
422444
camera_grab_mode_t mp_camera_hal_get_grab_mode(mp_camera_obj_t *self) {
423445
return self->camera_config.grab_mode;
424446
}

src/modcamera.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ DECLARE_CAMERA_HAL_GETSET(bool, wpc)
267267

268268
DECLARE_CAMERA_HAL_GET(int, address)
269269
DECLARE_CAMERA_HAL_GET(int, fb_count)
270-
DECLARE_CAMERA_HAL_GET(mp_camera_framesize_t, frame_size)
270+
DECLARE_CAMERA_HAL_GETSET(mp_camera_framesize_t, frame_size)
271271
DECLARE_CAMERA_HAL_GET(camera_grab_mode_t, grab_mode)
272272
DECLARE_CAMERA_HAL_GET(mp_camera_framesize_t, max_frame_size)
273273
DECLARE_CAMERA_HAL_GET(mp_camera_pixformat_t, pixel_format)

src/modcamera_api.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,13 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_camera___exit___obj, 4, 4, mp_came
240240
{ MP_ROM_QSTR(MP_QSTR_get_##property), MP_ROM_PTR(&camera_get_##property##_obj) }, \
241241
{ MP_ROM_QSTR(MP_QSTR_set_##property), MP_ROM_PTR(&camera_set_##property##_obj) }
242242

243-
CREATE_GETTER(frame_size, mp_obj_new_int)
243+
CREATE_GETSET_FUNCTIONS(frame_size, MP_OBJ_NEW_SMALL_INT, mp_obj_get_int)
244244
CREATE_GETTER(pixel_format, mp_obj_new_int)
245245
CREATE_GETTER(grab_mode, mp_obj_new_int)
246246
CREATE_GETTER(fb_count, mp_obj_new_int)
247247
CREATE_GETTER(pixel_width, mp_obj_new_int)
248248
CREATE_GETTER(pixel_height, mp_obj_new_int)
249+
CREATE_GETTER(max_frame_size, mp_obj_new_int)
249250
CREATE_GETSET_FUNCTIONS(contrast, MP_OBJ_NEW_SMALL_INT, mp_obj_get_int);
250251
CREATE_GETSET_FUNCTIONS(brightness, MP_OBJ_NEW_SMALL_INT, mp_obj_get_int);
251252
CREATE_GETSET_FUNCTIONS(saturation, MP_OBJ_NEW_SMALL_INT, mp_obj_get_int);
@@ -281,12 +282,13 @@ static const mp_rom_map_elem_t camera_camera_locals_table[] = {
281282
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_camera_deinit_obj) },
282283
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
283284
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&mp_camera___exit___obj) },
284-
{ MP_ROM_QSTR(MP_QSTR_get_framesize), MP_ROM_PTR(&camera_get_frame_size_obj) },
285285
{ MP_ROM_QSTR(MP_QSTR_get_pixel_format), MP_ROM_PTR(&camera_get_pixel_format_obj) },
286286
{ MP_ROM_QSTR(MP_QSTR_get_grab_mode), MP_ROM_PTR(&camera_get_grab_mode_obj) },
287287
{ MP_ROM_QSTR(MP_QSTR_get_fb_count), MP_ROM_PTR(&camera_get_fb_count_obj) },
288288
{ MP_ROM_QSTR(MP_QSTR_get_pixel_width), MP_ROM_PTR(&camera_get_pixel_width_obj) },
289289
{ MP_ROM_QSTR(MP_QSTR_get_pixel_height), MP_ROM_PTR(&camera_get_pixel_height_obj) },
290+
{ MP_ROM_QSTR(MP_QSTR_get_max_frame_size), MP_ROM_PTR(&camera_get_max_frame_size_obj) },
291+
ADD_PROPERTY_TO_TABLE(frame_size),
290292
ADD_PROPERTY_TO_TABLE(contrast),
291293
ADD_PROPERTY_TO_TABLE(brightness),
292294
ADD_PROPERTY_TO_TABLE(saturation),

0 commit comments

Comments
 (0)