Skip to content

Instantly share code, notes, and snippets.

@shownb
Created February 12, 2020 13:59
Show Gist options
  • Save shownb/50610a081dc89657d2ebc47c8ae1fdd7 to your computer and use it in GitHub Desktop.
Save shownb/50610a081dc89657d2ebc47c8ae1fdd7 to your computer and use it in GitHub Desktop.
esp32
// 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"
#define TAG "camera"
//WROVER-KIT PIN Map
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 4
#define SIOD_GPIO_NUM 18
#define SIOC_GPIO_NUM 23
#define Y9_GPIO_NUM 36
#define Y8_GPIO_NUM 37
#define Y7_GPIO_NUM 38
#define Y6_GPIO_NUM 39
#define Y5_GPIO_NUM 35
#define Y4_GPIO_NUM 26
#define Y3_GPIO_NUM 13
#define Y2_GPIO_NUM 34
#define VSYNC_GPIO_NUM 5
#define HREF_GPIO_NUM 27
#define PCLK_GPIO_NUM 25
#define XCLK_FREQ 20000000
static camera_config_t camera_config = {
.ledc_channel = LEDC_CHANNEL_0,
.ledc_timer = LEDC_TIMER_0,
.pin_d0 = Y2_GPIO_NUM,
.pin_d1 = Y3_GPIO_NUM,
.pin_d2 = Y4_GPIO_NUM,
.pin_d3 = Y5_GPIO_NUM,
.pin_d4 = Y6_GPIO_NUM,
.pin_d5 = Y7_GPIO_NUM,
.pin_d6 = Y8_GPIO_NUM,
.pin_d7 = Y9_GPIO_NUM,
.pin_xclk = XCLK_GPIO_NUM,
.pin_pclk = PCLK_GPIO_NUM,
.pin_vsync = VSYNC_GPIO_NUM,
.pin_href = HREF_GPIO_NUM,
.pin_sscb_sda = SIOD_GPIO_NUM,
.pin_sscb_scl = SIOC_GPIO_NUM,
.pin_reset = RESET_GPIO_NUM,
.pin_pwdn = PWDN_GPIO_NUM,
.xclk_freq_hz = XCLK_FREQ,
.pixel_format = PIXFORMAT_JPEG,
.frame_size = FRAMESIZE_UXGA,
.jpeg_quality = 12,
.fb_count = 2
};
#include "esp_system.h"
#include "esp_spi_flash.h"
STATIC mp_obj_t camera_init(){
esp_err_t err = esp_camera_init(&camera_config);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Camera Init Failed");
return mp_const_false;
}
return mp_const_true;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(camera_init_obj, camera_init);
STATIC mp_obj_t camera_deinit(){
esp_err_t err = esp_camera_deinit();
if (err != ESP_OK) {
ESP_LOGE(TAG, "Camera deinit Failed");
return mp_const_false;
}
return mp_const_true;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(camera_deinit_obj, camera_deinit);
STATIC mp_obj_t camera_capture(){
//acquire a frame
camera_fb_t * fb = esp_camera_fb_get();
if (!fb) {
ESP_LOGE(TAG, "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;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(camera_capture_obj, camera_capture);
STATIC const mp_rom_map_elem_t camera_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_camera) },
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&camera_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&camera_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR_capture), MP_ROM_PTR(&camera_capture_obj) },
};
STATIC MP_DEFINE_CONST_DICT(camera_module_globals, camera_module_globals_table);
const mp_obj_module_t mp_module_camera = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&camera_module_globals,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment