#include "camera_mgr.h" #include // strcasecmp #include "app_config.h" #include "board_pins.h" static bool s_ready = false; // Built from the enum symbols rather than literal indices, so this table // cannot drift when esp32-camera renumbers framesize_t. struct FsName { framesize_t fs; const char* name; }; static const FsName kFsNames[] = { {FRAMESIZE_96X96, "96X96"}, {FRAMESIZE_QQVGA, "QQVGA"}, {FRAMESIZE_128X128, "128X128"}, {FRAMESIZE_QCIF, "QCIF"}, {FRAMESIZE_HQVGA, "HQVGA"}, {FRAMESIZE_240X240, "240X240"}, {FRAMESIZE_QVGA, "QVGA"}, {FRAMESIZE_320X320, "320X320"}, {FRAMESIZE_CIF, "CIF"}, {FRAMESIZE_HVGA, "HVGA"}, {FRAMESIZE_VGA, "VGA"}, {FRAMESIZE_SVGA, "SVGA"}, {FRAMESIZE_XGA, "XGA"}, {FRAMESIZE_HD, "HD"}, {FRAMESIZE_SXGA, "SXGA"}, {FRAMESIZE_UXGA, "UXGA"}, {FRAMESIZE_FHD, "FHD"}, {FRAMESIZE_QXGA, "QXGA"}, {FRAMESIZE_QHD, "QHD"}, {FRAMESIZE_WQXGA, "WQXGA"}, {FRAMESIZE_QSXGA, "QSXGA"}, {FRAMESIZE_5MP, "5MP"}, }; uint8_t framesizeFromName(const char* name, uint8_t fallback) { if (!name || !name[0]) return fallback; for (const FsName& e : kFsNames) { if (strcasecmp(name, e.name) == 0) return (uint8_t)e.fs; } log_w("camera: unknown frame size '%s', keeping %s", name, framesizeName(fallback)); return fallback; } const char* framesizeName(uint8_t fs) { for (const FsName& e : kFsNames) { if ((uint8_t)e.fs == fs) return e.name; } return "?"; } void framesizeDims(uint8_t fs, uint16_t* w, uint16_t* h) { if (w) *w = 0; if (h) *h = 0; if (fs >= FRAMESIZE_INVALID) return; // resolution[] is the driver's own table, so these always match reality. if (w) *w = resolution[fs].width; if (h) *h = resolution[fs].height; } static camera_config_t buildConfig() { camera_config_t c = {}; c.ledc_channel = LEDC_CHANNEL_0; c.ledc_timer = LEDC_TIMER_0; c.pin_d0 = CAM_PIN_D0; c.pin_d1 = CAM_PIN_D1; c.pin_d2 = CAM_PIN_D2; c.pin_d3 = CAM_PIN_D3; c.pin_d4 = CAM_PIN_D4; c.pin_d5 = CAM_PIN_D5; c.pin_d6 = CAM_PIN_D6; c.pin_d7 = CAM_PIN_D7; c.pin_xclk = CAM_PIN_XCLK; c.pin_pclk = CAM_PIN_PCLK; c.pin_vsync = CAM_PIN_VSYNC; c.pin_href = CAM_PIN_HREF; c.pin_sccb_sda = CAM_PIN_SIOD; c.pin_sccb_scl = CAM_PIN_SIOC; c.pin_pwdn = CAM_PIN_PWDN; c.pin_reset = CAM_PIN_RESET; c.xclk_freq_hz = 20000000; c.pixel_format = PIXFORMAT_JPEG; c.frame_size = (framesize_t)g_cfg.frame_size; c.jpeg_quality = g_cfg.jpeg_quality; // Two framebuffers in PSRAM with CAM_GRAB_LATEST: the driver keeps // capturing while we are busy copying the previous frame into the ring, // and a grab always returns the newest frame rather than a queued stale // one. On a trigger, "newest" is what you want. c.fb_location = CAMERA_FB_IN_PSRAM; c.fb_count = 2; c.grab_mode = CAMERA_GRAB_LATEST; return c; } bool cameraBegin() { if (s_ready) return true; if (!psramFound()) { // Without PSRAM there is no ring buffer and no SVGA framebuffer. // Fail loudly rather than silently running a crippled node. log_e("PSRAM not found — check board_build.arduino.memory_type=qio_opi"); return false; } camera_config_t c = buildConfig(); esp_err_t err = esp_camera_init(&c); if (err != ESP_OK) { log_e("esp_camera_init failed: 0x%x", err); return false; } s_ready = true; cameraApplyConfig(); cameraDiscardFrames(3); uint16_t w = 0, h = 0; framesizeDims(g_cfg.frame_size, &w, &h); sensor_t* s = esp_camera_sensor_get(); log_i("camera ready: sensor PID 0x%04x, %s (%ux%u) quality=%u psram_free=%uKB", s ? s->id.PID : 0, framesizeName(g_cfg.frame_size), w, h, g_cfg.jpeg_quality, (unsigned)(heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024)); return true; } void cameraEnd() { if (!s_ready) return; esp_camera_deinit(); s_ready = false; } bool cameraRestart() { cameraEnd(); delay(50); return cameraBegin(); } static bool s_night_profile = false; bool cameraNightProfileActive() { return s_night_profile; } // Everything the OV2640 exposes that is worth touching on a bird camera. // Notes on the ones that actually matter here: // // gainceiling The single most important setting for small fast birds. // High gain lets the sensor pick a shorter exposure in poor // light, but amplifies noise; low gain forces a longer // exposure and smears a moving bird into a streak. 4x by day // is a good compromise; the night profile raises it because a // noisy photo beats a black one. // exposure_ctrl Leave auto by day. Set false + aec_value to force a fixed // short shutter if you find motion blur is the limiting // factor at your feeder — that is the tuning lever. // ae_level Exposure bias. Birds are usually backlit against bright sky, // which fools metering into underexposing the bird; +1 helps. // lenc Lens shading correction. On, unless you are stitching. // colorbar Test pattern. Set true to prove the data path works before // you have optics or light. void cameraApplyTuning(bool night) { sensor_t* s = esp_camera_sensor_get(); if (!s) return; const CamTuning& t = night ? g_cfg.cam_night : g_cfg.cam_day; s_night_profile = night; s->set_brightness(s, t.brightness); s->set_contrast(s, t.contrast); s->set_saturation(s, t.saturation); s->set_sharpness(s, t.sharpness); s->set_special_effect(s, t.special_effect); s->set_whitebal(s, t.whitebal ? 1 : 0); s->set_awb_gain(s, t.awb_gain ? 1 : 0); s->set_wb_mode(s, t.wb_mode); s->set_exposure_ctrl(s, t.exposure_ctrl ? 1 : 0); s->set_aec2(s, t.aec2 ? 1 : 0); s->set_ae_level(s, t.ae_level); if (!t.exposure_ctrl) s->set_aec_value(s, t.aec_value); s->set_gain_ctrl(s, t.gain_ctrl ? 1 : 0); if (!t.gain_ctrl) s->set_agc_gain(s, t.agc_gain); s->set_gainceiling(s, (gainceiling_t)t.gainceiling); s->set_bpc(s, t.bpc ? 1 : 0); s->set_wpc(s, t.wpc ? 1 : 0); s->set_raw_gma(s, t.raw_gma ? 1 : 0); s->set_lenc(s, t.lenc ? 1 : 0); s->set_dcw(s, t.dcw ? 1 : 0); s->set_colorbar(s, t.colorbar ? 1 : 0); // The sensor needs a couple of frames to act on an exposure change. cameraDiscardFrames(2); log_i("camera: %s profile applied (gainceiling=%u ae_level=%d aec=%s)", night ? "night" : "day", t.gainceiling, t.ae_level, t.exposure_ctrl ? "auto" : "manual"); } void cameraApplyConfig() { sensor_t* s = esp_camera_sensor_get(); if (!s) return; s->set_quality(s, g_cfg.jpeg_quality); s->set_vflip(s, g_cfg.vflip ? 1 : 0); s->set_hmirror(s, g_cfg.hmirror ? 1 : 0); cameraApplyTuning(s_night_profile); } bool cameraSetFrameSize(uint8_t framesize) { sensor_t* s = esp_camera_sensor_get(); if (!s) return false; // set_framesize refuses anything above the sensor's own maximum, which is // how an OV2640 (max UXGA) tells you it cannot do the QXGA an OV5640 // would manage. Report it rather than silently keeping the old size. if (s->set_framesize(s, (framesize_t)framesize) != 0) { log_w("camera: sensor rejected frame size %s — above its maximum?", framesizeName(framesize)); return false; } cameraDiscardFrames(2); return true; } camera_fb_t* cameraGrab() { if (!s_ready) return nullptr; camera_fb_t* fb = esp_camera_fb_get(); if (!fb) log_w("camera grab failed"); return fb; } void cameraDiscardFrames(uint8_t n) { for (uint8_t i = 0; i < n; i++) { camera_fb_t* fb = esp_camera_fb_get(); if (fb) esp_camera_fb_return(fb); } } bool cameraReady() { return s_ready; }