-
Notifications
You must be signed in to change notification settings - Fork 4
added autodetect tape size #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
3cef88d
782d4c6
ba38b27
18937bd
9fe6596
f2c94fd
463f7b4
e55d7f5
95101b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -119,6 +119,50 @@ pub struct PrinterStatus { | |
| phase_state: PhaseState, | ||
| } | ||
|
|
||
| impl PrinterStatus { | ||
| /// Get the pixel width (print area width in dots) for the loaded media | ||
| pub fn pixel_width(&self) -> Option<u16> { | ||
| match (self.media_width, self.media_length) { | ||
| // Endless tapes (length = 0) - dots_total - offset_r | ||
| (12, 0) => Some(142 - 29), // 113 | ||
| (18, 0) => Some(256 - 171), // 85 | ||
| (29, 0) => Some(342 - 6), // 336 | ||
| (38, 0) => Some(449 - 12), // 437 | ||
| (50, 0) => Some(590 - 12), // 578 | ||
| (54, 0) => Some(636 - 0), // 636 | ||
| (62, 0) => Some(732 - 12), // 720 | ||
| (102, 0) => Some(1200 - 12), // 1188 | ||
| (104, 0) => Some(1224 - 12), // 1212 | ||
|
|
||
| // Die-cut labels | ||
| (17, 54) => Some(201 - 0), // 201 | ||
| (17, 87) => Some(201 - 0), // 201 | ||
| (23, 23) => Some(272 - 42), // 230 | ||
| (29, 42) => Some(342 - 6), // 336 | ||
| (29, 90) => Some(342 - 6), // 336 | ||
| (38, 90) => Some(449 - 12), // 437 | ||
| (39, 48) => Some(461 - 6), // 455 | ||
| (52, 29) => Some(614 - 0), // 614 | ||
| (54, 29) => Some(630 - 60), // 570 | ||
| (60, 87) => Some(708 - 18), // 690 | ||
| (62, 29) => Some(732 - 12), // 720 | ||
| (62, 100) => Some(732 - 12), // 720 | ||
| (102, 51) => Some(1200 - 12), // 1188 | ||
| (102, 153) => Some(1200 - 12), // 1188 | ||
| (104, 164) => Some(1224 - 12), // 1212 | ||
|
|
||
| // Round die-cut labels | ||
| // This can't be right | ||
| //(12, 12) => Some(142 - 113), // 29 | ||
| (24, 24) => Some(284 - 42), // 242 | ||
| (58, 58) => Some(688 - 51), // 637 | ||
|
|
||
| // Unknown media | ||
| _ => None, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be able to capture width and height here and at least log an error or a warning, that we were unable to determine the width. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| pub enum PrinterCommandMode { | ||
| /// ESC/P mode (normal) | ||
|
|
@@ -255,6 +299,7 @@ impl PrinterCommander { | |
| } | ||
|
|
||
| pub fn read_status(&mut self) -> Result<PrinterStatus, std::io::Error> { | ||
| self.send_command(PrinterCommand::StatusInfoRequest)?; | ||
| let res = self.printer.read(32)?; | ||
| assert!(res[0] == 0x80); | ||
| assert!(res[1] == 0x20); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| use std::env; | ||
|
|
||
|
Comment on lines
+1
to
+2
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the unused import |
||
| use crate::driver::{PrinterCommand, PrinterCommandMode, PrinterExpandedMode, PrinterMode}; | ||
| use crate::error::BrotherQlError; | ||
| use crate::{driver, Settings}; | ||
|
|
@@ -58,7 +60,10 @@ fn apply_threshold( | |
| Ok(img) | ||
| } | ||
|
|
||
| fn img_to_lines(img: ImageBuffer<Rgba<u8>, Vec<u8>>) -> Result<Vec<[u8; 90]>, BrotherQlError> { | ||
| fn img_to_lines( | ||
| img: ImageBuffer<Rgba<u8>, Vec<u8>>, | ||
| image_width: u32, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not need the image width here, as |
||
| ) -> Result<Vec<[u8; 90]>, BrotherQlError> { | ||
| // convert to vec of line bits | ||
| /* | ||
| let mut lines = Vec::new(); | ||
|
|
@@ -83,13 +88,14 @@ fn img_to_lines(img: ImageBuffer<Rgba<u8>, Vec<u8>>) -> Result<Vec<[u8; 90]>, Br | |
| */ | ||
|
|
||
| let mut lines = Vec::new(); | ||
| let padding = 720 - image_width; | ||
|
|
||
| for y in 0..img.height() { | ||
| let mut line = [0u8; 90]; | ||
|
|
||
| for x in 0..img.width() { | ||
| let i = img.get_pixel(x, y).0[0]; | ||
|
|
||
| let x = x + padding; | ||
|
Comment on lines
96
to
+98
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified by using |
||
| let byte = x / 8; | ||
| let bit = x % 8; | ||
|
|
||
|
|
@@ -133,8 +139,10 @@ pub fn render_image(file_path: &str, settings: &Settings) -> Result<Vec<[u8; 90] | |
|
|
||
| // resize | ||
|
|
||
| let new_width = 720; //630 per la carta piccola | ||
|
|
||
| // let new_width = 720; //630 per la carta piccola | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this line instead of commenting out |
||
| let mut printer = driver::PrinterCommander::main("/dev/usb/lp0")?; | ||
| let status = printer.read_status()?; | ||
| let new_width = status.pixel_width().unwrap_or(720) as u32; | ||
| let new_height = new_width * img.height() / img.width() * if settings.dpi_600 { 2 } else { 1 }; | ||
|
|
||
| let mut img = image::imageops::resize( | ||
|
|
@@ -152,7 +160,8 @@ pub fn render_image(file_path: &str, settings: &Settings) -> Result<Vec<[u8; 90] | |
|
|
||
| dithered_img.save("/tmp/out_processed.png")?; | ||
|
|
||
| let lines = img_to_lines(dithered_img)?; | ||
| // if the paper format is not known, assume the biggest one | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this comment not be part of line 145? |
||
| let lines = img_to_lines(dithered_img, new_width)?; | ||
| Ok(lines) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| BOT_TOKEN=999999999:xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxx | ||
| OWNER_ID=222222222 | ||
| RUST_LOG=printer_bot_rs=DEBUG | ||
| RUST_LOG=printer_bot_rs=DEBUG |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,4 @@ log = "0.4.20" | |
| teloxide-core = "0.13" | ||
| thiserror = "2.0.16" | ||
| tokio = { version = "1.34.0", features = ["full"] } | ||
| openssl = { version = "0.10", features = ["vendored"] } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is there a new dependency for openssl, when it is never used by us directly?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's used by the telegram bot
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then we probably want to put it with the vendored feature behind a feature, as not everyone would want the statically linked openssl |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is not only the total width, but there is also an offset, would it make sense to include the offset in the return value?