feat: default to an 80 mm USB thermal printer
Fall back to /dev/usb/lp0 at 576 dots when the printer variables are unset; an empty PHOTOBOOTH_PRINTER still disables printing. Log the resolved printer at startup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
3
.env
3
.env
@@ -1,7 +1,8 @@
|
||||
# Photobooth configuration, loaded from the directory the app is started from.
|
||||
# Variables already set in the environment (e.g. by systemd) take precedence.
|
||||
# Every variable is optional; the values below are the built-in defaults.
|
||||
|
||||
# Thermal printer (ESC/POS). Remove or leave empty to disable printing.
|
||||
# Thermal printer (ESC/POS). Set to an empty value to disable printing.
|
||||
# A device path for USB/serial printers, or tcp://host[:port] for network
|
||||
# printers (port defaults to 9100).
|
||||
PHOTOBOOTH_PRINTER=/dev/usb/lp0
|
||||
|
||||
@@ -80,8 +80,9 @@ impl Worker {
|
||||
// Load and own the model on the worker too.
|
||||
let mut detector = crate::faces::Detector::new();
|
||||
let printer = crate::printer::Printer::from_env();
|
||||
if printer.is_none() {
|
||||
eprintln!("[capture] PHOTOBOOTH_PRINTER not set; printing disabled");
|
||||
match &printer {
|
||||
Some(printer) => eprintln!("[capture INFO] Printing to {printer:?}"),
|
||||
None => eprintln!("[capture INFO] PHOTOBOOTH_PRINTER is empty; printing disabled"),
|
||||
}
|
||||
move |frame| match &mut detector {
|
||||
Ok(detector) => {
|
||||
|
||||
@@ -4,9 +4,10 @@
|
||||
//! startup):
|
||||
//! - `PHOTOBOOTH_PRINTER`: where to send print data. Either a device path such as
|
||||
//! `/dev/usb/lp0` (USB printer class) or a raw network printer as
|
||||
//! `tcp://host[:port]` (port defaults to 9100). Printing is disabled when unset.
|
||||
//! `tcp://host[:port]` (port defaults to 9100). Defaults to `/dev/usb/lp0`;
|
||||
//! set it to an empty value to disable printing.
|
||||
//! - `PHOTOBOOTH_PRINTER_DOTS`: printable width in dots, rounded down to a multiple
|
||||
//! of 8. Defaults to 384 (58 mm paper); use 576 for 80 mm paper.
|
||||
//! of 8. Defaults to 576 (80 mm paper); use 384 for 58 mm paper.
|
||||
//!
|
||||
//! Serial printers must have their baud rate set beforehand (for example with
|
||||
//! `stty -F /dev/ttyUSB0 9600`); this module only writes bytes.
|
||||
@@ -25,7 +26,8 @@ use opencv::{
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
const DEFAULT_DOTS: usize = 384;
|
||||
const DEFAULT_PRINTER: &str = "/dev/usb/lp0";
|
||||
const DEFAULT_DOTS: usize = 576;
|
||||
const DEFAULT_NETWORK_PORT: u16 = 9100;
|
||||
const NETWORK_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
/// Raster rows per `GS v 0` command; small bands fit every printer's buffer.
|
||||
@@ -52,16 +54,24 @@ impl Target {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Printer {
|
||||
target: Target,
|
||||
dots: usize,
|
||||
}
|
||||
|
||||
impl Printer {
|
||||
/// Returns `None` only when `PHOTOBOOTH_PRINTER` is explicitly empty.
|
||||
pub fn from_env() -> Option<Self> {
|
||||
let target = Target::parse(&std::env::var("PHOTOBOOTH_PRINTER").ok()?)?;
|
||||
let dots = std::env::var("PHOTOBOOTH_PRINTER_DOTS")
|
||||
.ok()
|
||||
Self::from_values(
|
||||
std::env::var("PHOTOBOOTH_PRINTER").ok().as_deref(),
|
||||
std::env::var("PHOTOBOOTH_PRINTER_DOTS").ok().as_deref(),
|
||||
)
|
||||
}
|
||||
|
||||
fn from_values(printer: Option<&str>, dots: Option<&str>) -> Option<Self> {
|
||||
let target = Target::parse(printer.unwrap_or(DEFAULT_PRINTER))?;
|
||||
let dots = dots
|
||||
.and_then(|value| value.trim().parse::<usize>().ok())
|
||||
.map_or(DEFAULT_DOTS, |dots| dots / 8 * 8)
|
||||
.max(8);
|
||||
@@ -194,6 +204,18 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unset_variables_fall_back_to_80mm_usb_printer() {
|
||||
let printer = Printer::from_values(None, None).unwrap();
|
||||
assert_eq!(printer.target, Target::Device("/dev/usb/lp0".into()));
|
||||
assert_eq!(printer.dots, 576);
|
||||
// Unparsable widths fall back; valid ones are rounded to whole bytes.
|
||||
assert_eq!(Printer::from_values(None, Some("wide")).unwrap().dots, 576);
|
||||
assert_eq!(Printer::from_values(None, Some(" 390 ")).unwrap().dots, 384);
|
||||
// An explicitly empty printer disables printing.
|
||||
assert!(Printer::from_values(Some(""), None).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dithers_extremes_and_packs_msb_first() {
|
||||
assert_eq!(dither(&[0; 16], 16, 1), vec![0xFF, 0xFF]);
|
||||
|
||||
Reference in New Issue
Block a user