Skip to content

Commit

Permalink
respect ctrl c in image display loop
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperq committed Jun 10, 2024
1 parent 185f9a3 commit 81f545e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
29 changes: 23 additions & 6 deletions bin/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use axum::routing::{get, post};
use axum::Router;
use stats::get_qmdl_manifest;
use tokio::sync::mpsc::{self, Sender};
use tokio::sync::oneshot::error::TryRecvError;
use tokio::task::JoinHandle;
use tokio_util::task::TaskTracker;
use std::net::SocketAddr;
Expand Down Expand Up @@ -90,6 +91,7 @@ fn run_ctrl_c_thread(
task_tracker: &TaskTracker,
diag_device_sender: Sender<DiagDeviceCtrlMessage>,
server_shutdown_tx: oneshot::Sender<()>,
ui_shutdown_tx: oneshot::Sender<()>,
qmdl_store_lock: Arc<RwLock<RecordingStore>>
) -> JoinHandle<Result<(), RayhunterError>> {
task_tracker.spawn(async move {
Expand All @@ -104,6 +106,9 @@ fn run_ctrl_c_thread(

server_shutdown_tx.send(())
.expect("couldn't send server shutdown signal");
info!("sending UI shutdown");
ui_shutdown_tx.send(())
.expect("couldn't send ui shutdown signal");
diag_device_sender.send(DiagDeviceCtrlMessage::Exit).await
.expect("couldn't send Exit message to diag thread");
},
Expand All @@ -115,11 +120,23 @@ fn run_ctrl_c_thread(
})
}

async fn update_ui(task_tracker: &TaskTracker){
task_tracker.spawn_blocking(|| {
async fn update_ui(task_tracker: &TaskTracker, mut ui_shutdown_rx: oneshot::Receiver<()>){
task_tracker.spawn_blocking(move || {
let mut fb: Framebuffer = Framebuffer::new();
fb.draw_img("/data/rayhunter/orca.gif");
loop {
match ui_shutdown_rx.try_recv() {
Ok(_) => {
info!("received UI shutdown");
break;
},
Err(TryRecvError::Empty) => {},
Err(e) => panic!("what the fuck {e}")

}
fb.draw_img("/data/rayhunter/orca.gif");
}
}).await.unwrap();

}

#[tokio::main]
Expand All @@ -143,11 +160,11 @@ async fn main() -> Result<(), RayhunterError> {

run_diag_read_thread(&task_tracker, dev, rx, qmdl_store_lock.clone());
}

let (ui_shutdown_tx, ui_shutdown_rx) = oneshot::channel();
let (server_shutdown_tx, server_shutdown_rx) = oneshot::channel::<()>();
run_ctrl_c_thread(&task_tracker, tx.clone(), server_shutdown_tx, qmdl_store_lock.clone());
run_ctrl_c_thread(&task_tracker, tx.clone(), server_shutdown_tx, ui_shutdown_tx, qmdl_store_lock.clone());
run_server(&task_tracker, &config, qmdl_store_lock.clone(), server_shutdown_rx, tx).await;
update_ui(&task_tracker).await;
update_ui(&task_tracker, ui_shutdown_rx).await;

task_tracker.close();
task_tracker.wait().await;
Expand Down
22 changes: 9 additions & 13 deletions bin/src/framebuffer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use image::{io::Reader as ImageReader, AnimationDecoder, imageops::FilterType, codecs::gif::GifDecoder, DynamicImage};
use std::{io::BufReader, fs::File, time::Duration};
use include_dir::{include_dir, Dir};
use log::{info, error};

const FB_PATH:&str = "/dev/fb0";
static IMAGE_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/static/images/");
Expand Down Expand Up @@ -52,19 +51,16 @@ impl Framebuffer<'_>{
pub fn draw_img(&mut self, img_name: &str) {
//let img_path = IMAGE_DIR.get_file(img_name).unwrap().path();
let img_path = img_name;
info!("img_path: {:?}", img_path);
if img_path.ends_with(".gif") {
loop{
// this is dumb and i'm sure there's a better way to loop this
let stream = BufReader::new(File::open(&img_path).unwrap());
let decoder = GifDecoder::new(stream).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));
}
// this is dumb and i'm sure there's a better way to loop this
let stream = BufReader::new(File::open(&img_path).unwrap());
let decoder = GifDecoder::new(stream).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_reader = ImageReader::open(img_path).unwrap();
Expand Down

0 comments on commit 81f545e

Please sign in to comment.