From d47a08587cadff4936f25c1501f75ca3cce47df3 Mon Sep 17 00:00:00 2001 From: saji Date: Fri, 2 Aug 2024 00:04:26 -0500 Subject: [PATCH] move display fn to display file, add convert args --- src/display.rs | 18 ++++++++++++++++-- src/eink.rs | 2 +- src/main.rs | 20 ++++++-------------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/display.rs b/src/display.rs index e8e0dc5..0546688 100644 --- a/src/display.rs +++ b/src/display.rs @@ -18,7 +18,6 @@ pub trait EInkPanel { pub struct Wrapper { spi: SpidevDevice, - gpiochip: Chip, delay: Delay, panel: Epd7in3f, } @@ -54,7 +53,6 @@ impl Wrapper { Ok(Self { spi, - gpiochip, delay, panel, }) @@ -91,9 +89,25 @@ impl EInkPanel for FakeEInk { } } +#[instrument] +pub fn get_display() -> Box { + match Wrapper::new() { + Ok(w) => { + info!("Found real hardware, using it"); + Box::new(w) + } + Err(e) => { + warn!("Error opening display SPI interface: {e}"); + warn!("Falling back to fake display"); + Box::new(FakeEInk {}) + } + } +} + /// Create a thread that can take dithered images and display them. This allows the display to be /// used in an async context since updating the display can take ~30 seconds. #[must_use] +#[instrument(skip_all)] pub fn create_display_thread( mut display: Box, ) -> (thread::JoinHandle<()>, mpsc::Sender>) { diff --git a/src/eink.rs b/src/eink.rs index 87df5f6..fbaa1d8 100644 --- a/src/eink.rs +++ b/src/eink.rs @@ -30,7 +30,7 @@ const SIMPLE_PALETTE: [Srgb; 7] = [ Srgb::new(0.757, 0.443, 0.165), // Orange ]; -#[derive(strum::EnumString, Serialize, Deserialize, PartialEq, Eq, Debug)] +#[derive(strum::EnumString, strum::Display, Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Copy)] pub enum Palette { Default, Simple, diff --git a/src/main.rs b/src/main.rs index cbc5edb..ca29fed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,12 +7,12 @@ use serde::Deserialize; use std::path::PathBuf; use toml; -use crate::display::{EInkPanel, FakeEInk, Wrapper}; +use crate::display::get_display; use crate::dither::{DitherMethod, DitheredImage}; use crate::eink::Palette; use clap::{Args, Parser, Subcommand}; use image::RgbImage; -use tracing::{error, event, info, warn, Level}; +use tracing::{error, event, info, instrument, warn, Level}; /// Application config, including sqlite db path, scan folders, and scheduling. #[derive(Deserialize, Debug)] @@ -43,6 +43,8 @@ enum Command { struct ConvertArgs { #[arg(short, long, default_value_t = DitherMethod::NearestNeighbor)] dither_method: DitherMethod, + #[arg(short, long, default_value_t = Palette::Default)] + palette: Palette, input_file: PathBuf, output_file: PathBuf, } @@ -52,18 +54,6 @@ async fn main() -> anyhow::Result<()> { tracing_subscriber::fmt::init(); let cli = Cli::parse(); - let mut display: Box = match Wrapper::new() { - Ok(w) => { - info!("Found real hardware, using it"); - Box::new(w) - } - Err(e) => { - event!(Level::WARN, "Error opening display SPI interface: {e}"); - warn!("Falling back to fake display"); - Box::new(FakeEInk {}) - } - }; - match cli.command { Command::Convert(a) => { let input = image::ImageReader::open(a.input_file)? @@ -81,6 +71,7 @@ async fn main() -> anyhow::Result<()> { result.into_rgbimage().save(a.output_file)?; } Command::Show => { + let mut display = get_display(); let img: RgbImage = image::ImageReader::open("image.png")?.decode()?.into(); error!("HI"); @@ -91,6 +82,7 @@ async fn main() -> anyhow::Result<()> { display.display(&eink_buf)?; } Command::Serve => { + let display = get_display(); let ctx = api::AppState::new(display); let app = api::router().with_state(ctx);