poc client, refactor embedded

This commit is contained in:
2025-01-15 23:51:08 -08:00
parent 00cd459f34
commit 8135aa75d0
6 changed files with 1516 additions and 78 deletions

1146
client/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

8
client/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "client"
version = "0.1.0"
edition = "2021"
[dependencies]
image = "0.25.5"
serialport = "4.7.0"

88
client/src/main.rs Normal file
View File

@ -0,0 +1,88 @@
use image::ImageReader;
use std::env;
use std::fs::{self, DirEntry};
use std::path::{Path, PathBuf};
use std::thread::sleep;
use std::time::Duration;
#[repr(u8)]
#[derive(Eq, PartialEq, Debug)]
enum Command {
Info = 0,
Frame = 1,
Brightness = 2,
Invalid = 255,
}
fn find_images(path: PathBuf) -> Vec<DirEntry> {
let mut res = Vec::new();
for f in path.read_dir().unwrap() {
if let Ok(entry) = f {
if entry.file_name().as_encoded_bytes().ends_with(b".png") {
res.push(entry);
}
}
}
res
}
fn main() {
let mut args = env::args();
let mut port = serialport::new(args.nth(1).unwrap(), 1_000_000)
.open()
.unwrap();
let frames = find_images("frames/".into());
let src_fps = 50;
port.write(&[Command::Info as u8, 0, 0]).unwrap();
port.set_timeout(Duration::from_secs(1)).unwrap();
let (width, height, tgt_fps, mut cur_frame) = {
let mut info = [0u8; 14];
port.read_exact(&mut info).unwrap();
let width = u16::from_be_bytes(info[0..2].try_into().unwrap());
let height = u16::from_be_bytes(info[2..4].try_into().unwrap());
let tgt_fps = u16::from_be_bytes(info[4..6].try_into().unwrap());
let cur_frame = u64::from_be_bytes(info[6..14].try_into().unwrap());
(width, height, tgt_fps, cur_frame)
};
let src_frametime = 1. / src_fps as f64;
let dst_frametime = 1. / tgt_fps as f64;
let frames_per_frame = (tgt_fps / src_fps) as u64;
let frame_pkt_size = width * height + 8;
let bright = 0;
port.write(&[Command::Brightness as u8]).unwrap();
port.write(&1u16.to_be_bytes()).unwrap();
port.write(&[bright]).unwrap();
{
let mut buf = [0u8; 1];
port.read_exact(&mut buf).unwrap();
}
loop {
for frame in &frames {
let next_frame = cur_frame + frames_per_frame;
let img = ImageReader::open(frame.path()).unwrap().decode().unwrap();
let gs = img.as_luma8().unwrap();
println!(
"writing frame bytes {} for display at {}, cur_frame: {}",
gs.len(),
next_frame, cur_frame
);
port.write(&[Command::Frame as u8]).unwrap();
port.write(&frame_pkt_size.to_be_bytes()).unwrap();
port.write(&next_frame.to_be_bytes()).unwrap();
port.write(&gs).unwrap();
cur_frame = {
let mut buf = [0; 8];
port.read_exact(&mut buf).unwrap();
u64::from_be_bytes(buf)
};
sleep(Duration::from_secs_f64(src_frametime));
}
}
}