esc_pos_lib/
qr.rs

1use super::constants;
2
3
4pub struct Qr {
5    model: u8,
6    error_correction: u8,
7    size: u8,
8    data: Vec<u8>,
9}
10
11impl Qr {
12
13    ///Initialize a new Qr code with the given data.
14    ///The data must be a vector of bytes.
15    pub fn new(data: Vec<u8>) -> Qr {
16        Qr {
17            model: constants::QR_MODEL_1,
18            error_correction: constants::ERROR_L,
19            size: 3,
20            data: data,
21        }
22    }
23    
24    ///Set the model of QR code to print
25    ///Can be either QR_MODEL_1, QR_MODEL_2 or QR_MODEL_MICRO
26    ///However QR_MODEL_MICRO only works on TM-L90 4** models
27    ///Default is QR_MODEL_1
28    pub fn set_model(&mut self, model: u8) {
29        self.model = model;
30    }
31
32    ///Set the error correction level of QR code to print
33    ///Can be either ERROR_L, ERROR_M, ERROR_Q or ERROR_H
34    ///Default is ERROR_L
35    ///L: 7% M: 15% Q: 25% H: 30%
36    pub fn set_error_correction(&mut self, error_correction: u8) {
37        self.error_correction = error_correction;
38    }
39
40    ///Set the size of QR code to print
41    ///Can be between 1 and 16
42    ///Default is 3
43    ///However some models only support 3-16
44    ///If a value is set that is not supported by the model, it will be set to the closest value
45    pub fn set_size(&mut self, size: u8) {
46        if size < 1 {
47            self.size = 1;
48        } else if size > 16 {
49            self.size = 16;
50        } else {
51            self.size = size;
52        }
53    }
54
55    fn get_model(&self, data: &mut Vec<u8>) {
56        data.push(constants::GS);
57        data.push(0x28);
58        data.push(0x6b);
59        data.push(0x04);
60        data.push(0x00);
61        data.push(0x31);
62        data.push(0x41);
63        data.push(self.model);
64        data.push(0x00);
65    }
66
67    fn get_error_correction(&self, data: &mut Vec<u8>) {
68        data.push(constants::GS);
69        data.push(0x28);
70        data.push(0x6b);
71        data.push(0x03);
72        data.push(0x00);
73        data.push(0x31);
74        data.push(0x45);
75        data.push(self.error_correction);
76    }
77
78    fn get_size(&self, data: &mut Vec<u8>) {
79        data.push(constants::GS);
80        data.push(0x28);
81        data.push(0x6b);
82        data.push(0x03);
83        data.push(0x00);
84        data.push(0x31);
85        data.push(0x43);
86        data.push(self.size);
87    }
88
89    fn get_data(&self, data: &mut Vec<u8>) {
90        data.push(constants::GS);
91        data.push(0x28);
92        data.push(0x6b);
93        data.push((self.data.len() + 3)  as u8);
94        data.push(((self.data.len() + 3) >> 8) as u8);
95        data.push(0x31);
96        data.push(0x50);
97        data.push(0x30);
98        data.extend(self.data.clone());
99    }
100
101    fn add_print(&self, data: &mut Vec<u8>) {
102        data.push(constants::GS);
103        data.push(0x28);
104        data.push(0x6b);
105        data.push(0x03);
106        data.push(0x00);
107        data.push(0x31);
108        data.push(0x51);
109        data.push(48);
110    }
111
112    ///Call this to get the bytes to print the QR code
113    pub fn export(&self) -> Vec<u8> {
114        let mut to_return: Vec<u8> = Vec::new();
115        
116        //Set the model
117        self.get_model(&mut to_return);
118
119        //Set the error correction level
120        self.get_error_correction(&mut to_return);
121
122        //Set the size
123        self.get_size(&mut to_return);
124
125        //Add the data
126        self.get_data(&mut to_return);
127
128        //Print the QR code
129        self.add_print(&mut to_return);
130
131        to_return
132    }
133}
134
135