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
//! # escpos - A ESCPOS implementation in Rust
//!
//! This crate implements a subset of Epson's ESC/POS protocol for thermal receipt printers.
//! It allows you to generate and print documents with basic text formatting, cutting, barcodes,
//! QR codes and raster images on a compatible printer.
//!
//! ## Examples
//! The `examples` folder contains various examples of how to use `escpos`.
//! The [docs](https://docs.rs/escpos) (will) also provide lots of code snippets and examples.
//! The list of all the examples can be found [here](examples/EXAMPLES.md).
//!
//! ### Simple text formatting
//!
//! ```rust
//! use escpos::printer::Printer;
//! use escpos::printer_options::PrinterOptions;
//! use escpos::utils::*;
//! use escpos::{driver::*, errors::Result};
//!
//! fn main() -> Result<()> {
//! // env_logger::init();
//!
//! // let driver = NetworkDriver::open("192.168.1.248", 9100, None)?;
//! let driver = ConsoleDriver::open(true);
//! Printer::new(driver, Protocol::default(), Some(PrinterOptions::default()))
//! .debug_mode(Some(DebugMode::Dec))
//! .init()?
//! .smoothing(true)?
//! .bold(true)?
//! .underline(UnderlineMode::Single)?
//! .writeln("Bold underline")?
//! .justify(JustifyMode::CENTER)?
//! .reverse(true)?
//! .bold(false)?
//! .writeln("Hello world - Reverse")?
//! .feed()?
//! .justify(JustifyMode::RIGHT)?
//! .reverse(false)?
//! .underline(UnderlineMode::None)?
//! .size(2, 3)?
//! .writeln("Hello world - Normal")?
//! .print_cut()?; // print() or print_cut() is mandatory to send the data to the printer
//!
//! Ok(())
//! }
//! ```
//!
//! ### EAN13 (with `barcode` feature enabled)
//!
//! ```rust
//! use escpos::printer::Printer;
//! use escpos::utils::*;
//! use escpos::{driver::*, errors::Result};
//!
//! fn main() -> Result<()> {
//! // env_logger::init();
//!
//! // let driver = NetworkDriver::open("192.168.1.248", 9100, None)?;
//! let driver = ConsoleDriver::open(true);
//! Printer::new(driver, Protocol::default(), None)
//! .debug_mode(Some(DebugMode::Hex))
//! .init()?
//! .ean13_option(
//! "1234567890265",
//! BarcodeOption::new(
//! BarcodeWidth::M,
//! BarcodeHeight::S,
//! BarcodeFont::A,
//! BarcodePosition::Below,
//! )
//! )?
//! .feed()?
//! .print()?; // print() or print_cut() is mandatory to send the data to the printer
//!
//! Ok(())
//! }
//! ```
//!
//! ### QR Code (with `codes_2d` feature enabled)
//!
//! ```rust
//! use escpos::printer::Printer;
//! use escpos::utils::*;
//! use escpos::{driver::*, errors::Result};
//!
//! fn main() -> Result<()> {
//! // env_logger::init();
//!
//! // let driver = NetworkDriver::open("192.168.1.248", 9100, None)?;
//! let driver = ConsoleDriver::open(true);
//! Printer::new(driver, Protocol::default(), None)
//! .debug_mode(Some(DebugMode::Hex))
//! .init()?
//! .qrcode_option(
//! "https://www.google.com",
//! QRCodeOption::new(QRCodeModel::Model1, 6, QRCodeCorrectionLevel::M),
//! )?
//! .feed()?
//! .print_cut()?; // print() or print_cut() is mandatory to send the data to the printer
//!
//! Ok(())
//! }
//! ```
//!
//! ### Bit image (with `graphics` feature enabled)
//!
//! ```rust
//! use escpos::printer::Printer;
//! use escpos::utils::*;
//! use escpos::{driver::*, errors::Result};
//!
//! fn main() -> Result<()> {
//! // env_logger::init();
//!
//! // let driver = NetworkDriver::open("192.168.1.248", 9100, None)?;
//! let driver = ConsoleDriver::open(true);
//! let mut printer = Printer::new(driver, Protocol::default(), None);
//! printer.debug_mode(Some(DebugMode::Hex))
//! .init()?
//! .bit_image_option(
//! "./resources/images/rust-logo-small.png",
//! BitImageOption::new(Some(128), None, BitImageSize::Normal)?,
//! )?
//! .feed()?
//! .print_cut()?; // print() or print_cut() is mandatory to send the data to the printer
//!
//! Ok(())
//! }
//! ```
//!
//! ### Check printer status
//!
//! ```rust,ignore
//! use escpos::printer::Printer;
//! use escpos::utils::*;
//! use escpos::{driver::*, errors::Result};
//!
//! fn main() -> Result<()> {
//! // env_logger::init();
//!
//! // let driver = NetworkDriver::open("192.168.1.248", 9100, None)?;
//! let driver = ConsoleDriver::open(true);
//! Printer::new(driver.clone(), Protocol::default(), None)
//! .debug_mode(Some(DebugMode::Dec))
//! .real_time_status(RealTimeStatusRequest::Printer)?
//! .real_time_status(RealTimeStatusRequest::RollPaperSensor)?
//! .send_status()?;
//!
//! let mut buf = [0; 1];
//! driver.read(&mut buf)?;
//!
//! let status = RealTimeStatusResponse::parse(RealTimeStatusRequest::Printer, buf[0])?;
//! println!(
//! "Printer online: {}",
//! status.get(&RealTimeStatusResponse::Online).unwrap_or(&false)
//! );
//!
//! Ok(())
//! }
//! ```
//!
//! ## Features list
//!
//! | Name | Description | Default |
//! | ------------- | ---------------------------------------------------------------------- | :-----: |
//! | `std` | Enable `std` support (disable for `no_std` + `alloc` environments) | ✅ |
//! | `barcodes` | Print barcodes (UPC-A, UPC-E, EAN8, EAN13, CODE39, ITF or CODABAR) | ✅ |
//! | `codes_2d` | Print 2D codes (QR Code, PDF417, GS1 DataBar, DataMatrix, Aztec, etc.) | ✅ |
//! | `graphics` | Print raster images (requires `std`) | ❌ |
//! | `usb` | Enable USB feature (requires `std`) | ❌ |
//! | `native_usb` | Enable native USB feature (requires `std`) | ❌ |
//! | `hidapi` | Enable HidApi feature (requires `std`) | ❌ |
//! | `serial_port` | Enable Serial port feature (requires `std`) | ❌ |
//! | `usbprint` | Enable Windows USB print driver (`usbprint.sys` via Win32 API) | ❌ |
//! | `ui` | Enable ui feature (UI components) | ❌ |
//! | `full` | Enable all features | ❌ |
//!
//! ## `no_std` support
//!
//! The crate can be used on bare-metal targets (microcontrollers, kernels…) by disabling default
//! features. Only `alloc` is required:
//!
//! ```toml
//! escpos = { version = "0.18", default-features = false, features = ["barcodes", "codes_2d"] }
//! ```
//!
//! The built-in `Console`, `Network` and `File` drivers as well as the `graphics` feature require
//! `std`. In `no_std` mode you implement the [`Driver`] trait for your peripheral (UART, SPI, USB
//! endpoint, …) and pass it to [`Printer::new`]. [`Printer::driver`] lets you recover the driver
//! from a [`Printer`] (useful to read back status responses).
//!
//! All protocol-level APIs (barcodes, QR Code, PDF417, DataMatrix, Aztec, MaxiCode, page codes,
//! status…) work in `no_std` because the actual code rendering is done by the printer's firmware
//! — this crate only serializes the ESC/POS command bytes.
//!
//! See `examples/no_std_codes.rs` for a minimal end-to-end example with a custom in-memory driver.
//!
//! [`Driver`]: crate::driver::Driver
//! [`Printer`]: crate::printer::Printer
//! [`Printer::new`]: crate::printer::Printer::new
//! [`Printer::driver`]: crate::printer::Printer::driver
//!
//! ## External resources
//!
//! - [Epson documentation](https://download4.epson.biz/sec_pubs/pos/reference_en/escpos)
extern crate alloc;
/// Error module
pub
/// Print document
/// Printer options
/// Utils module contains protocol and all needed constants and enums
/// UI components like lines, tables, etc.
/// Drivers used to send data to the printer (Network or USB)
pub use driver;