Skip to content

Commit aead0df

Browse files
committed
Return a Bitmap from take() when appropriate
1 parent 54fe753 commit aead0df

File tree

1 file changed

+20
-3
lines changed
  • ports/espressif/bindings/esp32_camera

1 file changed

+20
-3
lines changed

ports/espressif/bindings/esp32_camera/Camera.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "bindings/esp32_camera/Camera.h"
3636
#include "common-hal/esp32_camera/Camera.h"
3737

38+
#include "shared-bindings/displayio/Bitmap.h"
3839
#include "shared-bindings/microcontroller/Pin.h"
3940
#include "shared-bindings/util.h"
4041
#include "esp_camera.h"
@@ -190,8 +191,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_camera_camera_frame_available_get_obj, es
190191
MP_PROPERTY_GETTER(esp32_camera_camera_frame_available_obj,
191192
(mp_obj_t)&esp32_camera_camera_frame_available_get_obj);
192193

193-
//| def take(timeout: Optional[float]=0.25) -> Optional[ReadableBuffer]:
194-
//| """Record a frame. Wait up to 'timeout' seconds for a frame to be captured."""
194+
//| def take(timeout: Optional[float]=0.25) -> Optional[displayio.Bitmap | ReadableBuffer]:
195+
//| """Record a frame. Wait up to 'timeout' seconds for a frame to be captured.
196+
//|
197+
//| In the case of timeout, `None` is returned.
198+
//| If `pixel_format` is `PixelFormat.JPEG`, the returned value is a `ReadableBuffer`.
199+
//| Otherwise, the returned value is a `displayio.Bitmap`.
200+
//| """
195201
//|
196202
STATIC mp_obj_t esp32_camera_camera_take(size_t n_args, const mp_obj_t *args) {
197203
esp32_camera_camera_obj_t *self = MP_OBJ_TO_PTR(args[0]);
@@ -201,7 +207,18 @@ STATIC mp_obj_t esp32_camera_camera_take(size_t n_args, const mp_obj_t *args) {
201207
if (!result) {
202208
return mp_const_none;
203209
}
204-
return mp_obj_new_memoryview('b', result->len, result->buf);
210+
pixformat_t format = common_hal_esp32_camera_camera_get_pixel_format(self);
211+
if (format == PIXFORMAT_JPEG) {
212+
return mp_obj_new_memoryview('b', result->len, result->buf);
213+
} else {
214+
int width = common_hal_esp32_camera_camera_get_width(self);
215+
int height = common_hal_esp32_camera_camera_get_height(self);
216+
displayio_bitmap_t *bitmap = m_new_obj(displayio_bitmap_t);
217+
bitmap->base.type = &displayio_bitmap_type;
218+
mp_printf(&mp_plat_print, "construct bitmap %dx%d @%p\n", width, height, result->buf);
219+
common_hal_displayio_bitmap_construct_from_buffer(bitmap, width, height, (format == PIXFORMAT_RGB565) ? 16 : 8, (uint32_t *)(void *)result->buf, true);
220+
return bitmap;
221+
}
205222
}
206223
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp32_camera_camera_take_obj, 1, 2, esp32_camera_camera_take);
207224

0 commit comments

Comments
 (0)