use futures::stream::StreamExt; use image::ImageReader; use core::any::Any; use std::{env, io, str}; use tokio_util::codec::{Decoder, Encoder}; use tokio::time::{sleep, Duration}; use image::imageops::FilterType; use tokio::io::AsyncWriteExt; use bytes::BytesMut; use tokio_serial::SerialPortBuilderExt; const DEFAULT_TTY: &str = "/dev/ttyACM1"; #[tokio::main] async fn main() -> tokio_serial::Result<()> { let mut args = env::args(); let path = args.nth(1).unwrap(); let mut img = ImageReader::open(&path).unwrap().decode().unwrap(); let tty_path = args.nth(2).unwrap_or_else(|| DEFAULT_TTY.into()); let mut port = tokio_serial::new(tty_path, 115200).open_native_async()?; const WIDTH: usize = 800; const HEIGHT: usize = 480; const TOTAL_BYTES: usize = WIDTH * HEIGHT * 3; const MAX_WRITE_LEN: usize = 115_200; if (img.width() < img.height()) { img = img.rotate90(); } let ratio = WIDTH as f64 / img.width() as f64; let height = (ratio * img.height() as f64) as u32; let rgb_img = img.resize_exact(WIDTH.try_into().unwrap(), height, FilterType::Nearest).into_rgb8(); let mut raw_img = rgb_img.into_raw(); raw_img.resize(TOTAL_BYTES, 0x00); port.write(&[ 'i' as u8 ] ).await?; port.flush().await?; let mut total: usize = raw_img.len(); let mut pos: usize = 0; while (total > 0) { let mut size = MAX_WRITE_LEN; if (size > total) { size = total; } port.write(&raw_img[pos..(pos+size)]).await?; port.flush().await?; total -= size; pos += size; } Ok(()) }