This commit is contained in:
puffaboo 2022-07-13 22:24:17 +01:00
parent d632f29dee
commit 64bef48d36
3 changed files with 29 additions and 10 deletions

View File

@ -21,6 +21,7 @@ embedded-hal = "1.0.0-alpha.8"
esp-idf-hal = "0.38.0" esp-idf-hal = "0.38.0"
esp-idf-svc = "0.42.0" esp-idf-svc = "0.42.0"
esp-idf-sys = { version = "0.31.6", features = ["binstart"] } esp-idf-sys = { version = "0.31.6", features = ["binstart"] }
rand = "0.8.5"
ssd1331 = "0.3.0" ssd1331 = "0.3.0"
tinybmp = "0.3.3" tinybmp = "0.3.3"

BIN
src/eye_closed.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

View File

@ -4,9 +4,11 @@ use embedded_graphics::{image::Image, pixelcolor::Rgb565, prelude::*};
use esp_idf_hal::{ use esp_idf_hal::{
gpio::{Gpio16, Gpio17, Gpio18, Gpio23, Gpio5, Output}, gpio::{Gpio16, Gpio17, Gpio18, Gpio23, Gpio5, Output},
prelude::{Hertz, Peripherals}, 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 ssd1331::{DisplayRotation::Rotate0, Ssd1331};
use tinybmp::Bmp; use tinybmp::Bmp;
@ -45,26 +47,42 @@ fn secondary() -> Result<(), Box<dyn Error>> {
)?; )?;
let mut display = Ssd1331::new(spiman, dc, Rotate0); let mut display = Ssd1331::new(spiman, dc, Rotate0);
display.init().unwrap(); display.init().unwrap();
display.flush().unwrap(); display.flush().unwrap();
let (w, h) = display.dimensions(); let (w, h) = display.dimensions();
println!("got resolution {} x {}", w.clone(), h.clone()); 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 eye = Bmp::from_slice(include_bytes!("./eye.bmp")).expect("Failed to load BMP image");
let eye_closed =
let im: Image<Bmp<Rgb565>> = Image::new(&bmp, Point::zero()); 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<Rgb565>,
display: &mut Ssd1331<
Master<SPI3, Gpio18<Output>, Gpio23<Output>, Gpio5<Output>, Gpio17<Output>>,
Gpio16<Output>,
>,
h: u32,
w: u32,
) {
let im: Image<Bmp<Rgb565>> = Image::new(&img, Point::zero());
// Position image in the center of the display // Position image in the center of the display
let moved = im.translate(Point::new( let moved = im.translate(Point::new(
(w as u32 - bmp.size().width) as i32 / 2, (w - img.size().width) as i32 / 2,
(h as u32 - bmp.size().height) as i32 / 2, (h - img.size().height) as i32 / 2,
)); ));
moved.draw(&mut display).unwrap(); moved.draw(display).unwrap();
display.flush().unwrap(); display.flush().unwrap();
Ok(())
} }
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {