use std::{error::Error, thread::sleep, time::Duration}; use embedded_graphics::{image::Image, pixelcolor::Rgb565, prelude::*}; use esp_idf_hal::{ gpio::{Gpio16, Gpio17, Gpio18, Gpio23, Gpio5, Output}, prelude::{Hertz, Peripherals}, spi::{self, Master, Pins, SPI3}, }; 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; fn secondary() -> Result<(), Box> { // Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once, // or else some patches to the runtime implemented by esp-idf-sys might not link properly. esp_idf_sys::link_patches(); // Bind the log crate to the ESP Logging facilities esp_idf_svc::log::EspLogger::initialize_default(); let peripherals = Peripherals::take().unwrap(); let spi3 = peripherals.spi3; let pins = peripherals.pins; let cs: Gpio17 = pins.gpio17.into_output()?; let dc: Gpio16 = pins.gpio16.into_output()?; let mosi: Gpio23 = pins.gpio23.into_output()?; let sclk: Gpio18 = pins.gpio18.into_output()?; let reset: Gpio5 = pins.gpio5.into_output()?; // // Set up SPI interface and digital pin. These are stub implementations used in examples. let spiman = spi::Master::, Gpio23, Gpio5, Gpio17>::new( spi3, Pins { sclk: sclk, sdo: mosi, sdi: Some(reset), cs: Some(cs), }, spi::config::Config { baudrate: Hertz(24000000), data_mode: embedded_hal::spi::MODE_0, write_only: false, dma: spi::Dma::Disabled, }, )?; 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 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 - img.size().width) as i32 / 2, (h - img.size().height) as i32 / 2, )); moved.draw(display).unwrap(); display.flush().unwrap(); } fn main() -> Result<(), Box> { sleep(Duration::from_secs(1)); Ok(secondary().unwrap()) }