From d04a697d5139ddf740f308070bff2b1242c2967c Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 30 Mar 2024 12:21:38 +0100 Subject: [PATCH] Turn Calendar into drawing --- showbits-thermal-printer/src/drawer.rs | 4 +-- .../src/drawer/calendar.rs | 30 ++++++++++++------- showbits-thermal-printer/src/printer.rs | 2 +- showbits-thermal-printer/src/server.rs | 6 ++-- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/showbits-thermal-printer/src/drawer.rs b/showbits-thermal-printer/src/drawer.rs index af3c93a..409aaf9 100644 --- a/showbits-thermal-printer/src/drawer.rs +++ b/showbits-thermal-printer/src/drawer.rs @@ -16,6 +16,8 @@ use tokio::sync::mpsc; use crate::printer::Printer; +pub use calendar::CalendarDrawing; + #[derive(Default)] pub struct Context { font_stuff: FontStuff, @@ -35,7 +37,6 @@ pub enum Command { Image { image: RgbaImage, bright: bool }, Photo { image: RgbaImage, title: String }, ChatMessage { username: String, content: String }, - Calendar { year: i32, month: u8 }, Cells { rule: u8, rows: u32, scale: u32 }, } @@ -86,7 +87,6 @@ impl Drawer { Command::ChatMessage { username, content } => { self.on_chat_message(username, content)? } - Command::Calendar { year, month } => self.draw_calendar(year, month)?, Command::Cells { rule, rows, scale } => self.draw_cells(rule, rows, scale)?, } Ok(()) diff --git a/showbits-thermal-printer/src/drawer/calendar.rs b/showbits-thermal-printer/src/drawer/calendar.rs index 07b80bc..1dbbd0e 100644 --- a/showbits-thermal-printer/src/drawer/calendar.rs +++ b/showbits-thermal-printer/src/drawer/calendar.rs @@ -9,11 +9,18 @@ use taffy::{ }; use time::Date; -use super::{Context, Drawer}; +use crate::printer::Printer; -impl Drawer { - pub fn draw_calendar(&mut self, year: i32, month: u8) -> anyhow::Result<()> { - let mut date = Date::from_calendar_date(year, month.try_into()?, 1)?; +use super::{Context, Drawing}; + +pub struct CalendarDrawing { + pub year: i32, + pub month: u8, +} + +impl Drawing for CalendarDrawing { + fn draw(&self, printer: &mut Printer, ctx: &mut Context) -> anyhow::Result<()> { + let mut date = Date::from_calendar_date(self.year, self.month.try_into()?, 1)?; let mut tree = Tree::::new(WHITE); @@ -27,7 +34,7 @@ impl Drawer { let text = Text::new() .with_metrics(Text::default_metrics().scale(2.0)) .and_plain(weekday) - .widget(&mut self.ctx.font_stuff) + .widget(&mut ctx.font_stuff) .node() .with_justify_self(Some(AlignItems::Center)) .with_align_self(Some(AlignItems::Center)) @@ -45,7 +52,7 @@ impl Drawer { loop { let day = Text::new() .and_plain(date.day().to_string()) - .widget(&mut self.ctx.font_stuff) + .widget(&mut ctx.font_stuff) .node() .register(&mut tree)?; @@ -69,14 +76,16 @@ impl Drawer { } let title = Text::new() - .and_plain(format!("Ankreuzkalender {year:04}-{month:02}")) - .widget(&mut self.ctx.font_stuff) + .and_plain(format!( + "Ankreuzkalender {:04}-{:02}", + self.year, self.month + )) + .widget(&mut ctx.font_stuff) .node() .register(&mut tree)?; let root = Node::empty() .with_size_width(percent(1.0)) - .with_padding_bottom(length(Self::FEED)) .with_display(Display::Flex) .with_flex_direction(FlexDirection::Column) .with_align_items(Some(AlignItems::Center)) @@ -84,7 +93,8 @@ impl Drawer { .and_child(grid.register(&mut tree)?) .register(&mut tree)?; - self.printer.print_tree(&mut tree, &mut self.ctx, root)?; + printer.print_tree(&mut tree, ctx, root)?; + printer.feed()?; Ok(()) } } diff --git a/showbits-thermal-printer/src/printer.rs b/showbits-thermal-printer/src/printer.rs index 93b9575..95edb22 100644 --- a/showbits-thermal-printer/src/printer.rs +++ b/showbits-thermal-printer/src/printer.rs @@ -56,7 +56,7 @@ impl Printer { }) } - pub fn rip(&mut self) -> anyhow::Result<()> { + pub fn feed(&mut self) -> anyhow::Result<()> { if let Some(printer) = &mut self.printer { printer.init()?.feeds(4)?.print()?; } diff --git a/showbits-thermal-printer/src/server.rs b/showbits-thermal-printer/src/server.rs index ec5b40d..57fc872 100644 --- a/showbits-thermal-printer/src/server.rs +++ b/showbits-thermal-printer/src/server.rs @@ -12,7 +12,7 @@ use axum::{ use serde::Deserialize; use tokio::{net::TcpListener, sync::mpsc}; -use crate::drawer::Command; +use crate::drawer::{CalendarDrawing, Command}; use self::{r#static::get_static_file, statuscode::status_code}; @@ -133,10 +133,10 @@ struct PostCalendarForm { async fn post_calendar(server: State, request: Form) { let _ = server .tx - .send(Command::Calendar { + .send(Command::draw(CalendarDrawing { year: request.0.year, month: request.0.month, - }) + })) .await; }