From 389aa9e96c5cb435d1864b63343c0f41f74f2e01 Mon Sep 17 00:00:00 2001 From: Shadowbee Date: Tue, 25 Nov 2025 17:15:50 +0100 Subject: [PATCH] Fixed the code --- examples/camera-example/Cargo.toml | 2 +- examples/camera-example/src/main.rs | 131 +++++++++++++++------------- 2 files changed, 71 insertions(+), 62 deletions(-) diff --git a/examples/camera-example/Cargo.toml b/examples/camera-example/Cargo.toml index 3686686..ef0a583 100644 --- a/examples/camera-example/Cargo.toml +++ b/examples/camera-example/Cargo.toml @@ -14,7 +14,7 @@ opt-level = "z" [dependencies] esp-camera-rs = { path = "../../", version = "*" } - +#esp-hal = { version = "1.0.0", features = ["unstable", "esp32s3"] } log = { version = "0.4", default-features = false } anyhow = "1.0.100" esp-idf-svc = { version = "0.51.0", default-features = false, features = [ diff --git a/examples/camera-example/src/main.rs b/examples/camera-example/src/main.rs index a5b9267..68b18cb 100644 --- a/examples/camera-example/src/main.rs +++ b/examples/camera-example/src/main.rs @@ -2,75 +2,84 @@ use esp_idf_svc::hal::{gpio::PinDriver, peripherals::Peripherals}; use esp_idf_sys::esp_deep_sleep; use log::info; use std::time::Duration; +use std::time::Instant; fn main() -> anyhow::Result<()> { - // It is necessary to call this function once. Otherwise some patches to the runtime - // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 - esp_idf_svc::sys::link_patches(); + // It is necessary to call this function once. Otherwise some patches to the runtime + // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 + esp_idf_svc::sys::link_patches(); - // Bind the log crate to the ESP Logging facilities - esp_idf_svc::log::EspLogger::initialize_default(); + // Bind the log crate to the ESP Logging facilities + esp_idf_svc::log::EspLogger::initialize_default(); - info!("initializing camera and flash"); - let peripherals = Peripherals::take().unwrap(); + info!("initializing camera and flash"); + let peripherals = Peripherals::take().unwrap(); - // ESP32-CAM has an onboard Flash LED addressed at GPIO-4 - let mut flash = PinDriver::output(peripherals.pins.gpio4)?; - flash.set_low()?; + // ESP32-CAM has an onboard Flash LED addressed at GPIO-4 + let mut flash = PinDriver::output(peripherals.pins.gpio4)?; + flash.set_low()?; + flash.set_high()?; + blocking_delay(Duration::from_millis(5000)); + flash.set_low()?; + info!("setting up the gio pins"); + // Initialize the camera + let camera = esp_camera_rs::Camera::new( + peripherals.pins.gpio32, + peripherals.pins.gpio0, + peripherals.pins.gpio5, + peripherals.pins.gpio18, + peripherals.pins.gpio19, + peripherals.pins.gpio21, + peripherals.pins.gpio36, + peripherals.pins.gpio39, + peripherals.pins.gpio34, + peripherals.pins.gpio35, + peripherals.pins.gpio25, + peripherals.pins.gpio23, + peripherals.pins.gpio22, + peripherals.pins.gpio26, + peripherals.pins.gpio27, + esp_idf_sys::camera::pixformat_t_PIXFORMAT_JPEG, + esp_idf_sys::camera::framesize_t_FRAMESIZE_UXGA, + esp_idf_sys::camera::camera_fb_location_t_CAMERA_FB_IN_PSRAM, + )?; - // Initialize the camera - let camera = esp_camera_rs::Camera::new( - peripherals.pins.gpio32, - peripherals.pins.gpio0, - peripherals.pins.gpio5, - peripherals.pins.gpio18, - peripherals.pins.gpio19, - peripherals.pins.gpio21, - peripherals.pins.gpio36, - peripherals.pins.gpio39, - peripherals.pins.gpio34, - peripherals.pins.gpio35, - peripherals.pins.gpio25, - peripherals.pins.gpio23, - peripherals.pins.gpio22, - peripherals.pins.gpio26, - peripherals.pins.gpio27, - esp_idf_sys::camera::pixformat_t_PIXFORMAT_JPEG, - esp_idf_sys::camera::framesize_t_FRAMESIZE_UXGA, - esp_idf_sys::camera::camera_fb_location_t_CAMERA_FB_IN_PSRAM, - )?; + info!("initialization complete!"); - info!("initialization complete!"); + // Turn on the flash and take a picture. + // You probably want to keep it on for a while and adjust + // the camera exposure to get a good image, but this is + // mainly here to show that the program is working. + info!("taking a picture"); + flash.set_high()?; + let framebuffer = camera.get_framebuffer(); + flash.set_low()?; - // Turn on the flash and take a picture. - // You probably want to keep it on for a while and adjust - // the camera exposure to get a good image, but this is - // mainly here to show that the program is working. - info!("taking a picture"); - flash.set_high()?; - let framebuffer = camera.get_framebuffer(); - flash.set_low()?; + if let Some(framebuffer) = framebuffer { + info!( + "took picture: {width}x{height} {size} bytes", + width = framebuffer.width(), + height = framebuffer.height(), + size = framebuffer.data().len(), + ); - if let Some(framebuffer) = framebuffer { - info!( - "took picture: {width}x{height} {size} bytes", - width = framebuffer.width(), - height = framebuffer.height(), - size = framebuffer.data().len(), - ); + // TODO: Do something with the framebuffer. + // the JPEG-encoded byte data is in framebuffer.data() + // so you can just dump this to a file or send it over + // the network as "image.jpeg" and it should work. + } else { + panic!("failed to take image"); + }; - // TODO: Do something with the framebuffer. - // the JPEG-encoded byte data is in framebuffer.data() - // so you can just dump this to a file or send it over - // the network as "image.jpeg" and it should work. - } else { - panic!("failed to take image"); - }; - - // Send the board into deep sleep. This will basically turn off everything - // and consume very little power, and then "reboot" the device, restarting - // the pin initialization and taking another picture. - let delay = Duration::from_secs(60); - info!("finished, entering deep sleep for {delay:#?}"); - unsafe { esp_deep_sleep(delay.as_micros() as u64) } + // Send the board into deep sleep. This will basically turn off everything + // and consume very little power, and then "reboot" the device, restarting + // the pin initialization and taking another picture. + let delay = Duration::from_secs(60); + info!("finished, entering deep sleep for {delay:#?}"); + unsafe { esp_deep_sleep(delay.as_micros() as u64) } +} + +fn blocking_delay(duration: Duration) { + let delay_start = Instant::now(); + while delay_start.elapsed() < duration {} }