This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Thermal.cpp
322 lines (265 loc) · 6.91 KB
/
Thermal.cpp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include "WConstants.h"
#endif
#include "Thermal.h"
#include <avr/pgmspace.h>
Thermal::Thermal(int RX_Pin, int TX_Pin) {
_RX_Pin = RX_Pin;
_TX_Pin = TX_Pin;
}
void Thermal::begin() {
_printer = new SERIAL_IMPL(_RX_Pin, _TX_Pin);
_printer->begin(19200);
reset();
heatTime = 120; //80 is default from page 23 of datasheet. Controls speed of printing and darkness
heatInterval = 50; //2 is default from page 23 of datasheet. Controls speed of printing and darkness
printDensity = 15; //Not sure what the defaut is. Testing shows the max helps darken text. From page 23.
printBreakTime = 15; //Not sure what the defaut is. Testing shows the max helps darken text. From page 23.
writeBytes(27, 55);
writeBytes(7); //Default 64 dots = 8*('7'+1)
writeBytes(heatTime); //Default 80 or 800us
writeBytes(heatInterval); //Default 2 or 20us
//Modify the print density and timeout
writeBytes(18, 35);
int printSetting = (printDensity<<4) | printBreakTime;
writeBytes(printSetting); //Combination of printDensity and printBreakTime
}
// reset printer
void Thermal::reset() {
writeBytes(27, 64);
}
// reset formatting
void Thermal::setDefault(){
online();
justify('L');
inverseOff();
doubleHeightOff();
setLineHeight(32);
boldOff();
underlineOff();
setBarcodeHeight(50);
setSize('s');
}
void Thermal::test(){
println("Hello World!");
feed(2);
}
void Thermal::testPage() {
writeBytes(18, 84);
}
// this is the basic function for all printing, the rest is taken care of by the
// inherited Print class!
#if ARDUINO >= 100
size_t Thermal::write(uint8_t c) {
if (c == 0x13) return 0;
#else
void Thermal::write(uint8_t c) {
if (c == 0x13) return;
#endif
if (c != 0xA)
linefeedneeded = true;
else
linefeedneeded = false;
Serial.print(" 0x");
Serial.print(c, HEX);
Serial.print(" (");
#if ARDUINO >= 100
Serial.write(c);
#else
Serial.print(" (");
#endif
Serial.println(")");
PRINTER_PRINT(c);
delay(1);
#if ARDUINO >= 100
return 1;
#endif
}
void Thermal::setBarcodeHeight(int val){
//default is 50
writeBytes(29, 104, val);
}
void Thermal::printBarcode(char * text, uint8_t type) {
writeBytes(29, 107, type); // set the type first
for(uint16_t i = 0; i < strlen(text); i ++){
write(text[i]); //Data
}
write(0); //Terminator
delay(3000); //For some reason we can't immediately have line feeds here
feed(2);
}
void Thermal::writeBytes(uint8_t a) {
PRINTER_PRINT(a);
}
void Thermal::writeBytes(uint8_t a, uint8_t b) {
PRINTER_PRINT(a);
PRINTER_PRINT(b);
}
void Thermal::writeBytes(uint8_t a, uint8_t b, uint8_t c) {
PRINTER_PRINT(a);
PRINTER_PRINT(b);
PRINTER_PRINT(c);
}
void Thermal::writeBytes(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
PRINTER_PRINT(a);
PRINTER_PRINT(b);
PRINTER_PRINT(c);
PRINTER_PRINT(d);
}
// === Character commands ===
#define INVERSE_MASK (1 << 1)
#define UPDOWN_MASK (1 << 2)
#define BOLD_MASK (1 << 3)
#define DOUBLE_HEIGHT_MASK (1 << 4)
#define DOUBLE_WIDTH_MASK (1 << 5)
#define STRIKE_MASK (1 << 6)
void Thermal::setPrintMode(uint8_t mask) {
printMode |= mask;
writePrintMode();
}
void Thermal::unsetPrintMode(uint8_t mask) {
printMode &= ~mask;
writePrintMode();
}
void Thermal::writePrintMode() {
writeBytes(27, 33, printMode);
}
void Thermal::normal() {
printMode = 0;
writePrintMode();
}
void Thermal::inverseOn(){
setPrintMode(INVERSE_MASK);
}
void Thermal::inverseOff(){
unsetPrintMode(INVERSE_MASK);
}
void Thermal::upsideDownOn(){
setPrintMode(UPDOWN_MASK);
}
void Thermal::upsideDownOff(){
unsetPrintMode(UPDOWN_MASK);
}
void Thermal::doubleHeightOn(){
setPrintMode(DOUBLE_HEIGHT_MASK);
}
void Thermal::doubleHeightOff(){
unsetPrintMode(DOUBLE_HEIGHT_MASK);
}
void Thermal::doubleWidthOn(){
setPrintMode(DOUBLE_WIDTH_MASK);
}
void Thermal::doubleWidthOff(){
unsetPrintMode(DOUBLE_WIDTH_MASK);
}
void Thermal::strikeOn(){
setPrintMode(STRIKE_MASK);
}
void Thermal::strikeOff(){
unsetPrintMode(STRIKE_MASK);
}
void Thermal::boldOn(){
setPrintMode(BOLD_MASK);
}
void Thermal::boldOff(){
unsetPrintMode(BOLD_MASK);
}
void Thermal::justify(char value){
uint8_t pos = 0;
if(value == 'l' || value == 'L') pos = 0;
if(value == 'c' || value == 'C') pos = 1;
if(value == 'r' || value == 'R') pos = 2;
writeBytes(0x1B, 0x61, pos);
}
// Feeds by the specified number of lines
void Thermal::feed(uint8_t x){
// The datasheet claims sending bytes 27, 100, <x> will work
// but it feeds much much more.
while (x--)
write('\n');
}
// Feeds by the specified number of rows of pixels
void Thermal::feedRows(uint8_t rows) {
writeBytes(27, 74, rows);
}
void Thermal::flush() {
writeBytes(12);
}
void Thermal::setSize(char value){
int size = 0;
if(value == 's' || value == 'S') size = 0;
if(value == 'm' || value == 'M') size = 10;
if(value == 'l' || value == 'L') size = 25;
writeBytes(29, 33, size, 10);
// if (linefeedneeded)
// println("lfn"); //feed();
//linefeedneeded = false;
}
// Underlines of different weights can be produced:
// 0 - no underline
// 1 - normal underline
// 2 - thick underline
void Thermal::underlineOn(uint8_t weight) {
writeBytes(27, 45, weight);
}
void Thermal::underlineOff() {
underlineOn(0);
}
void Thermal::printBitmap(int w, int h, const uint8_t *bitmap) {
if (w > 384) return; // maximum width of the printer
for (int rowStart=0; rowStart < h; rowStart += 256) {
int chunkHeight = ((h - rowStart) > 255) ? 255 : (h - rowStart);
printBitmapChunk(w, chunkHeight, rowStart*(w/8), bitmap);
}
}
void Thermal::printBitmapChunk(int w, uint8_t h, int offset, const uint8_t *bitmap) {
writeBytes(18, 42);
writeBytes(h, w/8);
for (int i=0; i<((w/8)*h); i++) {
PRINTER_PRINT(pgm_read_byte(bitmap + offset + i));
}
}
// Take the printer offline. Print commands sent after this will be
// ignored until `online` is called
void Thermal::offline(){
writeBytes(27, 61, 0);
}
// Take the printer back online. Subsequent print commands will be
// obeyed.
void Thermal::online(){
writeBytes(27, 61, 1);
}
// Put the printer into a low-energy state immediately
void Thermal::sleep() {
sleepAfter(0);
}
// Put the printer into a low-energy state after the given number
// of seconds
void Thermal::sleepAfter(uint8_t seconds) {
writeBytes(27, 56, seconds);
}
// Wake the printer from a low-energy state. This command will wait
// for 50ms (as directed by the datasheet) before allowing further
// commands to be send.
void Thermal::wake() {
writeBytes(255);
delay(50);
}
////////////////////// not working?
void Thermal::tab(){
PRINTER_PRINT(9);
}
void Thermal::setCharSpacing(int spacing) {
writeBytes(27, 32, 0, 10);
}
void Thermal::setLineHeight(int val){
writeBytes(27, 51, val); // default is 32
}
/////////////////////////
#if ARDUINO < 100
void *operator new(size_t size_) { return malloc(size_); }
void* operator new(size_t size_,void *ptr_) { return ptr_; }
#endif