From 9b1df0281971b1bc403227fc90db3c5f1860ef02 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 30 Mar 2024 12:24:59 +0100 Subject: [PATCH] Turn Cells into drawing --- showbits-thermal-printer/src/drawer.rs | 4 +-- showbits-thermal-printer/src/drawer/cells.rs | 31 +++++++++++--------- showbits-thermal-printer/src/server.rs | 6 ++-- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/showbits-thermal-printer/src/drawer.rs b/showbits-thermal-printer/src/drawer.rs index 409aaf9..74cf5f2 100644 --- a/showbits-thermal-printer/src/drawer.rs +++ b/showbits-thermal-printer/src/drawer.rs @@ -16,7 +16,7 @@ use tokio::sync::mpsc; use crate::printer::Printer; -pub use calendar::CalendarDrawing; +pub use self::{calendar::CalendarDrawing, cells::CellsDrawing}; #[derive(Default)] pub struct Context { @@ -37,7 +37,6 @@ pub enum Command { Image { image: RgbaImage, bright: bool }, Photo { image: RgbaImage, title: String }, ChatMessage { username: String, content: String }, - Cells { rule: u8, rows: u32, scale: u32 }, } impl Command { @@ -87,7 +86,6 @@ impl Drawer { Command::ChatMessage { username, content } => { self.on_chat_message(username, content)? } - Command::Cells { rule, rows, scale } => self.draw_cells(rule, rows, scale)?, } Ok(()) } diff --git a/showbits-thermal-printer/src/drawer/cells.rs b/showbits-thermal-printer/src/drawer/cells.rs index 8f832d0..980d824 100644 --- a/showbits-thermal-printer/src/drawer/cells.rs +++ b/showbits-thermal-printer/src/drawer/cells.rs @@ -3,14 +3,11 @@ use image::{ Rgba, RgbaImage, }; use showbits_common::{color, widgets::Image, Node, Tree, WidgetExt}; -use taffy::{ - style_helpers::{length, percent}, - AlignItems, Display, FlexDirection, -}; +use taffy::{style_helpers::percent, AlignItems, Display, FlexDirection}; use crate::printer::Printer; -use super::{Context, Drawer}; +use super::{Context, Drawing}; const BLACK: Rgba = Rgba([0, 0, 0, 255]); const WHITE: Rgba = Rgba([255, 255, 255, 255]); @@ -45,9 +42,15 @@ fn apply_rule(rule: u8, neighbors: [bool; 3]) -> bool { rule & (1 << index) != 0 } -impl Drawer { - pub fn draw_cells(&mut self, rule: u8, rows: u32, scale: u32) -> anyhow::Result<()> { - let mut image = RgbaImage::new(Printer::WIDTH / scale, rows); +pub struct CellsDrawing { + pub rule: u8, + pub rows: u32, + pub scale: u32, +} + +impl Drawing for CellsDrawing { + fn draw(&self, printer: &mut Printer, ctx: &mut Context) -> anyhow::Result<()> { + let mut image = RgbaImage::new(Printer::WIDTH / self.scale, self.rows); // Initialize first line randomly for x in 0..image.width() { @@ -55,18 +58,18 @@ impl Drawer { } // Calculate next rows - for y in 1..rows { + for y in 1..self.rows { for x in 0..image.width() { let neighbors = neighbors_at(&image, x, y - 1); - let state = apply_rule(rule, neighbors); + let state = apply_rule(self.rule, neighbors); image.put_pixel(x, y, b2c(state)); } } let image = imageops::resize( &image, - image.width() * scale, - image.height() * scale, + image.width() * self.scale, + image.height() * self.scale, FilterType::Nearest, ); @@ -76,14 +79,14 @@ impl Drawer { 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)) .and_child(image) .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/server.rs b/showbits-thermal-printer/src/server.rs index 57fc872..760665f 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::{CalendarDrawing, Command}; +use crate::drawer::{CalendarDrawing, CellsDrawing, Command}; use self::{r#static::get_static_file, statuscode::status_code}; @@ -150,10 +150,10 @@ struct PostCellsForm { async fn post_cells(server: State, request: Form) { let _ = server .tx - .send(Command::Cells { + .send(Command::draw(CellsDrawing { rule: request.0.rule, rows: request.0.rows.unwrap_or(32).min(512), scale: request.0.scale.unwrap_or(4), - }) + })) .await; }