escpos_rust/
command.rs

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