Fixed the code

This commit is contained in:
Robin Löhn 2025-11-25 17:15:50 +01:00
parent 0334c851f8
commit 389aa9e96c
Signed by: robin
GPG key ID: 4F5CDA3F9635EB10
2 changed files with 71 additions and 62 deletions

View file

@ -14,7 +14,7 @@ opt-level = "z"
[dependencies] [dependencies]
esp-camera-rs = { path = "../../", version = "*" } esp-camera-rs = { path = "../../", version = "*" }
#esp-hal = { version = "1.0.0", features = ["unstable", "esp32s3"] }
log = { version = "0.4", default-features = false } log = { version = "0.4", default-features = false }
anyhow = "1.0.100" anyhow = "1.0.100"
esp-idf-svc = { version = "0.51.0", default-features = false, features = [ esp-idf-svc = { version = "0.51.0", default-features = false, features = [

View file

@ -2,75 +2,84 @@ use esp_idf_svc::hal::{gpio::PinDriver, peripherals::Peripherals};
use esp_idf_sys::esp_deep_sleep; use esp_idf_sys::esp_deep_sleep;
use log::info; use log::info;
use std::time::Duration; use std::time::Duration;
use std::time::Instant;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
// It is necessary to call this function once. Otherwise some patches to the runtime // 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 // 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(); esp_idf_svc::sys::link_patches();
// Bind the log crate to the ESP Logging facilities // Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default(); esp_idf_svc::log::EspLogger::initialize_default();
info!("initializing camera and flash"); info!("initializing camera and flash");
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take().unwrap();
// ESP32-CAM has an onboard Flash LED addressed at GPIO-4 // ESP32-CAM has an onboard Flash LED addressed at GPIO-4
let mut flash = PinDriver::output(peripherals.pins.gpio4)?; let mut flash = PinDriver::output(peripherals.pins.gpio4)?;
flash.set_low()?; 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 info!("initialization complete!");
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!"); // 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. if let Some(framebuffer) = framebuffer {
// You probably want to keep it on for a while and adjust info!(
// the camera exposure to get a good image, but this is "took picture: {width}x{height} {size} bytes",
// mainly here to show that the program is working. width = framebuffer.width(),
info!("taking a picture"); height = framebuffer.height(),
flash.set_high()?; size = framebuffer.data().len(),
let framebuffer = camera.get_framebuffer(); );
flash.set_low()?;
if let Some(framebuffer) = framebuffer { // TODO: Do something with the framebuffer.
info!( // the JPEG-encoded byte data is in framebuffer.data()
"took picture: {width}x{height} {size} bytes", // so you can just dump this to a file or send it over
width = framebuffer.width(), // the network as "image.jpeg" and it should work.
height = framebuffer.height(), } else {
size = framebuffer.data().len(), panic!("failed to take image");
); };
// TODO: Do something with the framebuffer. // Send the board into deep sleep. This will basically turn off everything
// the JPEG-encoded byte data is in framebuffer.data() // and consume very little power, and then "reboot" the device, restarting
// so you can just dump this to a file or send it over // the pin initialization and taking another picture.
// the network as "image.jpeg" and it should work. let delay = Duration::from_secs(60);
} else { info!("finished, entering deep sleep for {delay:#?}");
panic!("failed to take image"); unsafe { esp_deep_sleep(delay.as_micros() as u64) }
}; }
// Send the board into deep sleep. This will basically turn off everything fn blocking_delay(duration: Duration) {
// and consume very little power, and then "reboot" the device, restarting let delay_start = Instant::now();
// the pin initialization and taking another picture. while delay_start.elapsed() < duration {}
let delay = Duration::from_secs(60);
info!("finished, entering deep sleep for {delay:#?}");
unsafe { esp_deep_sleep(delay.as_micros() as u64) }
} }