diff --git a/Cargo.toml b/Cargo.toml index 0899395..dbb1944 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ embedded-hal = "1.0.0-alpha.8" esp-idf-hal = "0.38.0" esp-idf-svc = "0.42.0" esp-idf-sys = { version = "0.31.6", features = ["binstart"] } +rand = "0.8.5" ssd1331 = "0.3.0" tinybmp = "0.3.3" diff --git a/src/eye_closed.bmp b/src/eye_closed.bmp new file mode 100644 index 0000000..8c6a1f1 Binary files /dev/null and b/src/eye_closed.bmp differ diff --git a/src/main.rs b/src/main.rs index 4a43557..b0691a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,9 +4,11 @@ use embedded_graphics::{image::Image, pixelcolor::Rgb565, prelude::*}; use esp_idf_hal::{ gpio::{Gpio16, Gpio17, Gpio18, Gpio23, Gpio5, Output}, prelude::{Hertz, Peripherals}, - spi::{self, Pins, SPI3}, + spi::{self, Master, Pins, SPI3}, }; -use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported +use esp_idf_sys as _; +use rand::Rng; +// If using the `binstart` feature of `esp-idf-sys`, always keep this module imported use ssd1331::{DisplayRotation::Rotate0, Ssd1331}; use tinybmp::Bmp; @@ -45,26 +47,42 @@ fn secondary() -> Result<(), Box> { )?; let mut display = Ssd1331::new(spiman, dc, Rotate0); display.init().unwrap(); - display.flush().unwrap(); let (w, h) = display.dimensions(); println!("got resolution {} x {}", w.clone(), h.clone()); + let mut rng = rand::thread_rng(); - let bmp = Bmp::from_slice(include_bytes!("./eye.bmp")).expect("Failed to load BMP image"); - - let im: Image> = Image::new(&bmp, Point::zero()); + let eye = Bmp::from_slice(include_bytes!("./eye.bmp")).expect("Failed to load BMP image"); + let eye_closed = + Bmp::from_slice(include_bytes!("./eye_closed.bmp")).expect("Failed to load BMP image"); + loop { + print_image(eye, &mut display, h as u32, w as u32); + sleep(Duration::from_secs(rng.gen_range(1..10))); + print_image(eye_closed, &mut display, h as u32, w as u32); + sleep(Duration::from_millis(400)) + } +} +fn print_image( + img: Bmp, + display: &mut Ssd1331< + Master, Gpio23, Gpio5, Gpio17>, + Gpio16, + >, + h: u32, + w: u32, +) { + let im: Image> = Image::new(&img, Point::zero()); // Position image in the center of the display let moved = im.translate(Point::new( - (w as u32 - bmp.size().width) as i32 / 2, - (h as u32 - bmp.size().height) as i32 / 2, + (w - img.size().width) as i32 / 2, + (h - img.size().height) as i32 / 2, )); - moved.draw(&mut display).unwrap(); + moved.draw(display).unwrap(); display.flush().unwrap(); - Ok(()) } fn main() -> Result<(), Box> {