From 64bef48d366f4325282edf485f9b02bd4663e47e Mon Sep 17 00:00:00 2001 From: puffaboo Date: Wed, 13 Jul 2022 22:24:17 +0100 Subject: [PATCH] blinky --- Cargo.toml | 1 + src/eye_closed.bmp | Bin 0 -> 8330 bytes src/main.rs | 38 ++++++++++++++++++++++++++++---------- 3 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 src/eye_closed.bmp 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 0000000000000000000000000000000000000000..8c6a1f1b196aeebbb6254285ac3a39681b5d98d2 GIT binary patch literal 8330 zcmeHLF;c@Y5In#cRI0QqtYOsh29%jVMZ*(Z@Bvz?lzhWaNghI>M^K`(tXQ&TM-H|a zW)zQO$-dm~?VZk=Y+pW0Xg;6n`$L_N?RfgZelE*DuLPdPq^?6R_XT&9DK?fUl5o(8&f93-8mgI#NWw9va9hj0}- z%|lV9?FD1i8z!sZrdjJVhdYC3#(#$fQKdV%gl{12kvu}xw6VEpPs zeGhH)00Q`6_p){dIP2Cwz#;njt54DEEM!q@AUoyW)BuBcRmcn99zc*d3^?4&B6ted~kmXe1 zY(G(RJz#6PU*NsWSyoI{yaneOGWB-8yen`fbGyrX00M56ibGb+s|Sp__U!Tl;6_X; zg1e*;78vzq`5u9`ZVOVYeJk>?nc$48{e;YGDoAPBuQg;)k&5iwqauQg_4KtEB)>_4 Nq(D+2De%7)_yOMAG64Vp literal 0 HcmV?d00001 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> {