Skip to content

Commit

Permalink
send bytes to framebuffer instead of image
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperq committed Jun 13, 2024
1 parent 3123356 commit 8365cb5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
15 changes: 11 additions & 4 deletions bin/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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();
Expand Down
31 changes: 14 additions & 17 deletions bin/src/framebuffer.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 8365cb5

Please sign in to comment.