Skip to content

Instantly share code, notes, and snippets.

@adahl5
Created July 25, 2020 02:54
Show Gist options
  • Save adahl5/3726fe8e2abb2ebc7d8756d6f25109a7 to your computer and use it in GitHub Desktop.
Save adahl5/3726fe8e2abb2ebc7d8756d6f25109a7 to your computer and use it in GitHub Desktop.
// Include the header file to get access to the MicroPython API
#include "py/dynruntime.h"
// Code adapted from https://github.com/espressif/esp32-camera
#include "esp_camera.h"
#include "esp_log.h"
#include "py/nlr.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "py/binary.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
STATIC mp_obj_t init()
{
static camera_config_t camera_config = {
.pin_pwdn = 32,
.pin_reset = -1,
.pin_xclk = 0,
.pin_sscb_sda = 26,
.pin_sscb_scl = 27,
.pin_d7 = 35,
.pin_d6 = 34,
.pin_d5 = 39,
.pin_d4 = 36,
.pin_d3 = 21,
.pin_d2 = 19,
.pin_d1 = 18,
.pin_d0 = 5,
.pin_vsync = 25,
.pin_href = 23,
.pin_pclk = 22,
//XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
.xclk_freq_hz = 20000000,
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,
.pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
.frame_size = FRAMESIZE_UXGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
.jpeg_quality = 12, //0-63 lower number means higher quality
.fb_count = 1 //if more than one, i2s runs in continuous mode. Use only with JPEG
};
esp_err_t err = esp_camera_init(&camera_config);
if (err != ESP_OK)
{
ESP_LOGE("camera", "Camera Init Failed");
return mp_const_false;
}
return mp_const_true;
}
MP_DEFINE_CONST_FUN_OBJ_0(init_obj, init);
STATIC mp_obj_t deinit()
{
esp_err_t err = esp_camera_deinit();
if (err != ESP_OK)
{
ESP_LOGE("camera", "Camera deinit Failed");
return mp_const_false;
}
return mp_const_true;
}
MP_DEFINE_CONST_FUN_OBJ_0(deinit_obj, deinit);
STATIC mp_obj_t capture()
{
//acquire a frame
camera_fb_t *fb = esp_camera_fb_get();
if (!fb)
{
ESP_LOGE("camera", "Camera Capture Failed");
return mp_const_false;
}
mp_obj_t image = mp_obj_new_bytes(fb->buf, fb->len);
//return the frame buffer back to the driver for reuse
esp_camera_fb_return(fb);
return image;
}
MP_DEFINE_CONST_FUN_OBJ_0(capture_obj, capture);
// This is the entry point and is called when the module is imported
mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args)
{
// This must be first, it sets up the globals dict and other things
MP_DYNRUNTIME_INIT_ENTRY
// Make the function available in the module's namespace
mp_store_global(MP_QSTR_init, MP_OBJ_FROM_PTR(&init_obj));
mp_store_global(MP_QSTR_deinit, MP_OBJ_FROM_PTR(&deinit_obj));
mp_store_global(MP_QSTR_capture, MP_OBJ_FROM_PTR(&capture_obj));
// This must be last, it restores the globals dict
MP_DYNRUNTIME_INIT_EXIT
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment