make framebuffer crate and call it

This commit is contained in:
Cooper Quintin
2024-06-07 13:30:33 -07:00
parent dfd8138e21
commit 2ab48875ba
5 changed files with 760 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ mod server;
mod stats;
mod qmdl_store;
mod diag;
mod framebuffer;
use crate::config::{parse_config, parse_args};
use crate::diag::run_diag_read_thread;
@@ -13,6 +14,7 @@ use crate::server::{ServerState, get_qmdl, serve_static};
use crate::pcap::get_pcap;
use crate::stats::get_system_stats;
use crate::error::RayhunterError;
use crate::framebuffer::Framebuffer;
use axum::response::Redirect;
use diag::{get_analysis_report, start_recording, stop_recording, DiagDeviceCtrlMessage};
@@ -113,6 +115,13 @@ fn run_ctrl_c_thread(
})
}
fn update_ui(task_tracker: &TaskTracker){
task_tracker.spawn(async move {
let mut fb: Framebuffer = Framebuffer::new();
fb.draw_img("orca.gif")
});
}
#[tokio::main]
async fn main() -> Result<(), RayhunterError> {
env_logger::init();
@@ -137,6 +146,7 @@ async fn main() -> Result<(), RayhunterError> {
let (server_shutdown_tx, server_shutdown_rx) = oneshot::channel::<()>();
run_ctrl_c_thread(&task_tracker, tx.clone(), server_shutdown_tx, qmdl_store_lock.clone());
update_ui(&task_tracker);
run_server(&task_tracker, &config, qmdl_store_lock.clone(), server_shutdown_rx, tx).await;
task_tracker.close();

71
bin/src/framebuffer.rs Normal file
View File

@@ -0,0 +1,71 @@
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};
const FB_PATH:&str = "/dev/fb0";
static IMAGE_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/static/images/");
#[derive(Copy, Clone)]
// TODO actually poll for this, maybe w/ fbset?
struct Dimensions {
height: u32,
width: u32,
}
#[derive(Copy, Clone)]
pub struct Framebuffer<'a> {
dimensions: Dimensions,
path: &'a str,
}
impl Framebuffer<'_>{
pub const fn new() -> Self {
Framebuffer{
dimensions: Dimensions{height: 128, width: 128},
path: FB_PATH,
}
}
fn write(&mut self, img: DynamicImage) {
let resized_img = img.resize( self.dimensions.width, self.dimensions.height, FilterType::CatmullRom);
let width = self.dimensions.width.min(resized_img.width());
let height = self.dimensions.height.min(resized_img.height());
let img_rgba8 = resized_img.as_rgba8().unwrap();
let mut buf = Vec::new();
for y in 0..height {
for x in 0..width {
let px = img_rgba8.get_pixel(x, y);
let mut rgb565: u16 = (px[0] as u16 & 0b11111000) << 8;
rgb565 |= (px[1] as u16 & 0b11111100) << 3;
rgb565 |= (px[2] as u16) >> 3;
buf.extend(rgb565.to_le_bytes());
}
}
std::fs::write(self.path, &buf).unwrap();
}
pub fn draw_img(&mut self, img_name: &str) {
let img_path = IMAGE_DIR.get_file(img_name).unwrap().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));
}
}
} else {
let img_reader = ImageReader::open(img_path).unwrap();
let img = img_reader.decode().unwrap();
self.write(img);
}
}
}