35
35
#include "bindings/esp32_camera/Camera.h"
36
36
#include "common-hal/esp32_camera/Camera.h"
37
37
38
+ #include "shared-bindings/displayio/Bitmap.h"
38
39
#include "shared-bindings/microcontroller/Pin.h"
39
40
#include "shared-bindings/util.h"
40
41
#include "esp_camera.h"
@@ -190,8 +191,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_camera_camera_frame_available_get_obj, es
190
191
MP_PROPERTY_GETTER (esp32_camera_camera_frame_available_obj ,
191
192
(mp_obj_t )& esp32_camera_camera_frame_available_get_obj );
192
193
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
+ //| """
195
201
//|
196
202
STATIC mp_obj_t esp32_camera_camera_take (size_t n_args , const mp_obj_t * args ) {
197
203
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) {
201
207
if (!result ) {
202
208
return mp_const_none ;
203
209
}
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
+ }
205
222
}
206
223
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (esp32_camera_camera_take_obj , 1 , 2 , esp32_camera_camera_take );
207
224
0 commit comments