From 8365cb5706b6ddcd6b304054243da991151fa28e Mon Sep 17 00:00:00 2001 From: Cooper Quintin Date: Wed, 12 Jun 2024 17:04:47 -0700 Subject: [PATCH] send bytes to framebuffer instead of image --- bin/src/daemon.rs | 15 +++++++++++---- bin/src/framebuffer.rs | 31 ++++++++++++++----------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/bin/src/daemon.rs b/bin/src/daemon.rs index 38d89fb..e27c396 100644 --- a/bin/src/daemon.rs +++ b/bin/src/daemon.rs @@ -33,6 +33,7 @@ use std::time::Duration; use tokio::net::TcpListener; use tokio::sync::{RwLock, oneshot}; use std::sync::Arc; +use include_dir::{include_dir, Dir}; // Runs the axum server, taking all the elements needed to build up our // ServerState and a oneshot Receiver that'll fire when it's time to shutdown @@ -123,14 +124,16 @@ fn run_ctrl_c_thread( } async fn update_ui(task_tracker: &TaskTracker, config: &config::Config, mut ui_shutdown_rx: oneshot::Receiver<()>){ - let image = match config.ui_level { + static IMAGE_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/static/images/"); + let img_name = match config.ui_level { 0 => {info!("silent UI!"); return}, 1 => "subtle.png", 2 => "orca.gif", _ => "orca.gif" }; + let img = IMAGE_DIR.get_file(img_name).unwrap(); - info!("spawning UI with {}", image); + info!("spawning UI with {:?}", img.path()); task_tracker.spawn_blocking(move || { let mut fb: Framebuffer = Framebuffer::new(); loop { @@ -140,10 +143,14 @@ async fn update_ui(task_tracker: &TaskTracker, config: &config::Config, mut ui_ break; }, Err(TryRecvError::Empty) => {}, - Err(e) => panic!("what the fuck {e}") + Err(e) => panic!("error receiving shutdown message: {e}") } - fb.draw_img(image); + if img_name.ends_with(".gif"){ + fb.draw_gif(img.contents()); + } else { + fb.draw_img(img.contents()); + } sleep(Duration::from_millis(100)); } }).await.unwrap(); diff --git a/bin/src/framebuffer.rs b/bin/src/framebuffer.rs index 923fdc8..e1c77e1 100644 --- a/bin/src/framebuffer.rs +++ b/bin/src/framebuffer.rs @@ -1,9 +1,7 @@ use image::{codecs::gif::GifDecoder, imageops::FilterType, AnimationDecoder, DynamicImage}; use std::{io::Cursor, time::Duration}; -use include_dir::{include_dir, Dir}; const FB_PATH:&str = "/dev/fb0"; -static IMAGE_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/static/images/"); #[derive(Copy, Clone)] @@ -55,22 +53,21 @@ impl Framebuffer<'_>{ } - pub fn draw_img(&mut self, img_name: &str) { - let img = IMAGE_DIR.get_file(img_name).unwrap(); - if img_name.ends_with(".gif") { - // this is dumb and i'm sure there's a better way to loop this - let cursor = Cursor::new(img.contents()); - let decoder = GifDecoder::new(cursor).unwrap(); - for maybe_frame in decoder.into_frames() { - let frame = maybe_frame.unwrap(); - let (numerator, _) = frame.delay().numer_denom_ms(); - let img = DynamicImage::from(frame.into_buffer()); - self.write(img); - std::thread::sleep(Duration::from_millis(numerator as u64)); - } - } else { - let img = image::load_from_memory(img.contents()).unwrap(); + pub fn draw_gif(&mut self, img_buffer: &[u8]) { + // this is dumb and i'm sure there's a better way to loop this + let cursor = Cursor::new(img_buffer); + let decoder = GifDecoder::new(cursor).unwrap(); + for maybe_frame in decoder.into_frames() { + let frame = maybe_frame.unwrap(); + let (numerator, _) = frame.delay().numer_denom_ms(); + let img = DynamicImage::from(frame.into_buffer()); self.write(img); + std::thread::sleep(Duration::from_millis(numerator as u64)); } } + + pub fn draw_img(&mut self, img_buffer: &[u8]) { + let img = image::load_from_memory(img_buffer).unwrap(); + self.write(img); + } } \ No newline at end of file