escpos_rs/
command.rs

1use serde::{Serialize, Deserialize};
2
3pub use self::charset::Charset;
4pub use self::font::Font;
5pub use self::code_table::CodeTable;
6pub use self::image_mode::ImageMode;
7
8mod charset;
9mod code_table;
10mod font;
11mod image_mode;
12
13/// Common commands usefull for the printer
14#[derive(Serialize, Deserialize, Clone, Debug)]
15pub enum Command {
16    /// Cuts the paper after 0x96 vertical spaces
17    Cut,
18    /// Equivalent to ESC @
19    Reset,
20    /// Print mode selected to reset the fonts. Equivalent to ESC ! 0
21    PrintModeDefault,
22    /// Set an international character set, Equivalent to ESC R
23    SelectCharset {
24        /// Character set to be set
25        charset: Charset
26    },
27    /// Selects a different code table, Equivalent to ESC t
28    SelectCodeTable {
29        code_table: CodeTable
30    },
31    /// Sets up a font. Equivalent to ESC M
32    SelectFont {
33        font: Font
34    },
35    UnderlineOff,
36    Underline1Dot,
37    Underline2Dot,
38    /// Equivalent to ESC * m = 0
39    BoldOn,
40    BoldOff,
41    /// Prints whats on the buffer, and reverses n lines
42    PrintReverse {
43        n: u8
44    },
45    PrintMode {
46        print_mode: u8
47    },
48    /// Prints in reverse mode (white over black), or disables it
49    ReverseMode {
50        active: bool
51    },
52    /// Equivalent to ESC * m
53    Bitmap {
54        image_mode: ImageMode
55    },
56    /// Change line size
57    NoLine,
58    ResetLine
59}
60
61impl Command {
62    /// Returns the byte-array representation of each command
63    pub fn as_bytes(&self) -> Vec<u8> {
64        match self {
65            Command::Cut => vec![0x1d, 0x56, 0x41, 0x96],
66            Command::Reset => vec![0x1d, 0x40],
67            Command::PrintModeDefault => vec![0x01b, 0x21, 0x00],
68            Command::SelectCharset{charset} => {
69                let mut res = vec![0x1b, 0x52];
70                res.append(&mut charset.as_bytes());
71                res
72            },
73            Command::SelectCodeTable{code_table} => {
74                let mut res = vec![0x1b, 0x74];
75                res.append(&mut code_table.as_bytes());
76                res
77            },
78            Command::SelectFont{font} => {
79                let mut res = vec![0x1b, 0x4d];
80                res.append(&mut font.as_bytes());
81                res
82            },
83            Command::UnderlineOff => vec![0x1b, 0x2d, 0x00],
84            Command::Underline1Dot => vec![0x1b, 0x2d, 0x01],
85            Command::Underline2Dot => vec![0x1b, 0x2d, 0x02],
86            Command::BoldOn => vec![0x1b, 0x45, 0x01],
87            Command::BoldOff => vec![0x1b, 0x45, 0x00],
88            Command::PrintReverse{n} => vec![0x1b, 0x4b, *n],
89            Command::PrintMode{print_mode} => vec![0x1b, 0x21, *print_mode],
90            Command::ReverseMode{active} => vec![0x1d, 0x42, if *active {0x01} else {0x00}],
91            Command::Bitmap{image_mode} => vec![0x1b, 0x2a, image_mode.as_byte()],
92            Command::NoLine => vec![0x1b, 0x33, 0x00],
93            Command::ResetLine => vec![0x1b, 0x32]
94        }
95    }
96}