1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use std::{
fmt::{self, Debug, Display, Formatter},
time::Duration,
};
use anyhow::{bail, Context, Result};
macro_rules! backends {
[$($(# [$($m:tt)*])? $mod:ident :: $name:ident),* $(,)?] => {
$(
$(# [$($m)*])*
mod $mod;
$(# [$($m)*])*
pub use crate::$mod::$name;
)*
};
}
backends![
#[cfg(feature = "usb")]
usb::UsbBackend,
#[cfg(feature = "file")]
file::FileBackend,
];
/// Printing backend.
pub trait Backend {
/// Send data to the printer.
/// TODO: return number of bytes sent
fn send(&mut self, buf: &[u8], timeout: Duration) -> Result<()>;
/// Receive at most `buf.len()` bytes of data from the printer.
///
/// # Return value
/// This functions the number of bytes received from the printer.
fn recv(&mut self, buf: &mut [u8], timeout: Duration) -> Result<usize>;
}
/// MAC Address, see [`Printer::get_mac()`].
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct MacAddr(pub [u8; 6]);
/// PeriPage A6 printer.
pub struct Printer {
backend: Box<dyn Backend>,
}
impl Printer {
/// Construct a new printer using `backend` as it's printing [`Backend`].
pub fn new(backend: impl Backend + 'static) -> Self {
Self {
backend: Box::new(backend),
}
}
/// Find any printer, connected using any backend.
pub fn find() -> Result<Self> {
#[cfg(feature = "usb")]
{
match crate::usb::UsbBackend::list() {
Ok(devs) => {
if let Some(dev) = devs.first() {
let backend = UsbBackend::open(dev)?;
return Ok(Self::new(backend));
}
}
Err(e) => log::error!("cannot get list of usb devices: {e}"),
}
}
bail!("no printer found");
}
fn send(&mut self, buf: &[u8], timeout: u64) -> Result<()> {
log::trace!("send({}{buf:x?}, {timeout}s);", buf.len());
self.backend.send(buf, Duration::from_secs(timeout))
}
fn recv(&mut self, buf: &mut [u8], timeout: u64) -> Result<usize> {
let n = self.backend.recv(buf, Duration::from_secs(timeout))?;
log::trace!("recv({}, {timeout}s): {n}{:x?}", buf.len(), &buf[0..n]);
Ok(n)
}
fn query(&mut self, cmd: &[u8]) -> Result<Vec<u8>> {
self.send(cmd, 3).context("failed to send request")?;
let mut buf = vec![0u8; 1024];
let n = self.recv(&mut buf, 3).context("failed receive response")?;
buf.truncate(n);
Ok(buf)
}
fn query_string(&mut self, cmd: &[u8]) -> Result<String> {
let buf = self.query(cmd)?;
let s = String::from_utf8_lossy(&buf);
Ok(s.into_owned())
}
/// Get printer's "IP" string.
pub fn get_ip(&mut self) -> Result<String> {
self.query_string(&[0x10, 0xff, 0x20, 0xf0])
}
/// Get printer's firmware version.
pub fn get_firmware_ver(&mut self) -> Result<String> {
self.query_string(&[0x10, 0xff, 0x20, 0xf1])
}
/// Get printer's serial number.
pub fn get_serial(&mut self) -> Result<String> {
self.query_string(&[0x10, 0xff, 0x20, 0xf2])
}
/// Get printer's hardware version.
pub fn get_hardware_ver(&mut self) -> Result<String> {
self.query_string(&[0x10, 0xff, 0x30, 0x10])
}
/// Get printer's name.
pub fn get_name(&mut self) -> Result<String> {
self.query_string(&[0x10, 0xff, 0x30, 0x11])
}
/// Get printer's MAC address.
/// TODO: Return a MacAddr struct i
pub fn get_mac(&mut self) -> Result<MacAddr> {
let buf = self.query(&[0x10, 0xff, 0x30, 0x12])?;
// for some reason the printer sends the MAC address twice
if buf.len() < 6 {
bail!(
"invalid MAC address response, got {} bytes: {:x?}",
buf.len(),
&buf
);
}
let mut mac = [0u8; 6];
mac.copy_from_slice(&buf[0..6]);
Ok(MacAddr(mac))
}
/// Get printer's battery state.
pub fn get_battery(&mut self) -> Result<u8> {
let buf = self.query(&[0x10, 0xff, 0x50, 0xf1])?;
if buf.len() != 2 {
bail!("invalid battery response");
}
Ok(buf[1])
}
/// Set printing concentration, valid values are between `0..=2`.
pub fn set_concentration(&mut self, c: u8) -> Result<()> {
if c > 2 {
bail!("invalid concentration: {c}");
}
self.send(&[0x10, 0xff, 0x10, 0x00, c], 1)
}
/// Reset the printer.
/// This command has to be sent, before printing can be done.
pub fn reset(&mut self) -> Result<()> {
let buf = [
0x10, 0xff, 0xfe, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
];
self.send(&buf, 3)?;
let mut buf = [0u8; 128];
let _ = self.backend.recv(&mut buf, Duration::from_secs(1));
Ok(())
}
/// Print ASCII text.
/// Please don't use this, better use a font rasterizer, like [cosmic-text](https://docs.rs/cosmic-text).
///
/// # Printer Bugs (PeriPage A6)
/// - Only ASCII, no Unicode
/// - No ASCII escape sequences, except '\n' (line feed)
/// - Line wrapping is very buggy, sometimes it works, sometimes it discards the rest of the line.
/// - No font size/weight settings
pub fn print_text(&mut self, text: &str) -> Result<()> {
let text: Vec<u8> = text
.chars()
.filter(|ch| matches!(ch, '\n' | '\x20'..='\x7f'))
.map(|ch| ch as u8)
.collect();
self.send(&text, 30)?;
Ok(())
}
/// Print raw pixels.
///
/// # Overheating
/// The printer can overheat, if too much black is being printed at once,
/// therefore it's better to use the [`Printer::print_image_chunked()`] function instead.
///
/// # Printing Limitations
/// While the printer has a density of 203dpi,
/// printing very small things and thin lines should be avoided,
/// as the printer is simply not precise enough.
///
/// # "Concentration"
/// The printing concentration can be adjusted with [`Printer::set_concentration()`],
/// to make the output brighter or darker.
///
/// # Format
/// TODO: describe pixel format:
/// - monochrome
/// - 0=white, 1=black
/// - MSB: left, LSB: right
/// - must be multiples of `width/8` bytes
/// - must not be longer than `65535` rows
/// - due to accuracy constraints, printing single pixels should be avoided
///
/// # Notes
/// Printing gray scale pictures is possible,
/// by using [dithering](https://en.wikipedia.org/wiki/Dithering) to convert them to monochrome first.
/// Similarly, color images must be first converted to gray scale.
/// The [image](https://docs.rs/image/latest/image/) crate can be used, to do the conversions.
pub fn print_image(&mut self, pixels: &[u8], width: u16) -> Result<()> {
if width == 0 || width % 8 != 0 {
bail!("width must be non-zero and divisible by 8");
}
let n = pixels.len() * 8;
let w = width as usize;
let h = n / w;
if h > 0xff {
bail!("document too long");
}
if pixels.len() != (w * h / 8) {
bail!("invalid length of pixels: {}", pixels.len());
}
let rs = w / 8;
let mut packet = vec![
0x1d,
0x76,
0x30,
(rs >> 8) as u8,
(rs & 0xff) as u8,
0x00,
h as u8,
0x00,
];
packet.extend_from_slice(pixels);
self.send(&packet, 60)?;
// no idea what this does, but the Windows driver sends this after every print.
self.send(&[0x10, 0xff, 0xfe, 0x45], 1)?;
Ok(())
}
/// Just like [`Printer::print_image()`], but breaks the pixels into rows of `chunk_height`.
/// This may be needed, to prevent the printer from overheating, while printing a long document.
pub fn print_image_chunked_ext(
&mut self,
pixels: &[u8],
width: u16,
chunk_height: u16,
delay: Duration,
) -> Result<()> {
pixels
.chunks(width as usize * chunk_height as usize / 8)
.try_for_each(|chunk| {
self.print_image(chunk, width)?;
std::thread::sleep(delay);
Ok(())
})
}
pub fn print_image_chunked(&mut self, pixels: &[u8], width: u16) -> Result<()> {
self.print_image_chunked_ext(pixels, width, 24, Duration::from_millis(50))
}
/// Push out `num` rows of paper.
pub fn push(&mut self, num: u8) -> Result<()> {
self.send(&[0x1b, 0x4a, num], 5)?;
Ok(())
}
}
impl Display for MacAddr {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let [x0, x1, x2, x3, x4, x5] = self.0;
write!(f, "{x0:02x}:{x1:02x}:{x2:02x}:{x3:02x}:{x4:02x}:{x5:02x}")
}
}
impl Debug for MacAddr {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
<Self as Display>::fmt(self, f)
}
}