Skip to content
This repository was archived by the owner on May 16, 2023. It is now read-only.

Commit 66cb528

Browse files
authored
Merge pull request #47 from caternuson/fontb
Add FontB support
2 parents f59a3ad + 09cfdb5 commit 66cb528

File tree

4 files changed

+73
-21
lines changed

4 files changed

+73
-21
lines changed

Adafruit_Thermal.cpp

+58-16
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ void Adafruit_Thermal::printBarcode(const char *text, uint8_t type) {
268268
}
269269

270270
// === Character commands ===
271-
271+
#define FONT_MASK (1 << 0) //!< Select character font A or B
272272
#define INVERSE_MASK \
273273
(1 << 1) //!< Turn on/off white/black reverse printing mode. Not in 2.6.8
274274
//!< firmware (see inverseOn())
@@ -278,18 +278,43 @@ void Adafruit_Thermal::printBarcode(const char *text, uint8_t type) {
278278
#define DOUBLE_WIDTH_MASK (1 << 5) //!< Turn on/off double-width printing mode
279279
#define STRIKE_MASK (1 << 6) //!< Turn on/off deleteline mode
280280

281+
void Adafruit_Thermal::adjustCharValues(uint8_t printMode) {
282+
uint8_t charWidth;
283+
if (printMode & FONT_MASK) {
284+
// FontB
285+
charHeight = 17;
286+
charWidth = 9;
287+
} else {
288+
// FontA
289+
charHeight = 24;
290+
charWidth = 12;
291+
}
292+
// Double Width Mode
293+
if (printMode & DOUBLE_WIDTH_MASK) {
294+
maxColumn /= 2;
295+
charWidth *= 2;
296+
}
297+
// Double Height Mode
298+
if (printMode & DOUBLE_HEIGHT_MASK) {
299+
charHeight *= 2;
300+
}
301+
maxColumn = (384 / charWidth);
302+
}
303+
281304
void Adafruit_Thermal::setPrintMode(uint8_t mask) {
282305
printMode |= mask;
283306
writePrintMode();
284-
charHeight = (printMode & DOUBLE_HEIGHT_MASK) ? 48 : 24;
285-
maxColumn = (printMode & DOUBLE_WIDTH_MASK) ? 16 : 32;
307+
adjustCharValues(printMode);
308+
// charHeight = (printMode & DOUBLE_HEIGHT_MASK) ? 48 : 24;
309+
// maxColumn = (printMode & DOUBLE_WIDTH_MASK) ? 16 : 32;
286310
}
287311

288312
void Adafruit_Thermal::unsetPrintMode(uint8_t mask) {
289313
printMode &= ~mask;
290314
writePrintMode();
291-
charHeight = (printMode & DOUBLE_HEIGHT_MASK) ? 48 : 24;
292-
maxColumn = (printMode & DOUBLE_WIDTH_MASK) ? 16 : 32;
315+
adjustCharValues(printMode);
316+
// charHeight = (printMode & DOUBLE_HEIGHT_MASK) ? 48 : 24;
317+
// maxColumn = (printMode & DOUBLE_WIDTH_MASK) ? 16 : 32;
293318
}
294319

295320
void Adafruit_Thermal::writePrintMode() {
@@ -395,24 +420,30 @@ void Adafruit_Thermal::setSize(char value) {
395420

396421
switch (toupper(value)) {
397422
default: // Small: standard width and height
398-
size = 0x00;
399-
charHeight = 24;
400-
maxColumn = 32;
423+
// size = 0x00;
424+
// charHeight = 24;
425+
// maxColumn = 32;
426+
doubleWidthOff();
427+
doubleHeightOff();
401428
break;
402429
case 'M': // Medium: double height
403-
size = 0x01;
404-
charHeight = 48;
405-
maxColumn = 32;
430+
// size = 0x01;
431+
// charHeight = 48;
432+
// maxColumn = 32;
433+
doubleHeightOn();
434+
doubleWidthOff();
406435
break;
407436
case 'L': // Large: double width and height
408-
size = 0x11;
409-
charHeight = 48;
410-
maxColumn = 16;
437+
// size = 0x11;
438+
// charHeight = 48;
439+
// maxColumn = 16;
440+
doubleHeightOn();
441+
doubleWidthOn();
411442
break;
412443
}
413444

414-
writeBytes(ASCII_GS, '!', size);
415-
prevByte = '\n'; // Setting the size adds a linefeed
445+
// writeBytes(ASCII_GS, '!', size);
446+
// prevByte = '\n'; // Setting the size adds a linefeed
416447
}
417448

418449
// ESC 7 n1 n2 n3 Setting Control Parameter Command
@@ -654,6 +685,17 @@ void Adafruit_Thermal::tab() {
654685
column = (column + 4) & 0b11111100;
655686
}
656687

688+
void Adafruit_Thermal::setFont(char font) {
689+
switch (toupper(font)) {
690+
case 'B':
691+
setPrintMode(FONT_MASK);
692+
break;
693+
case 'A':
694+
default:
695+
unsetPrintMode(FONT_MASK);
696+
}
697+
}
698+
657699
void Adafruit_Thermal::setCharSpacing(int spacing) {
658700
writeBytes(ASCII_ESC, ' ', spacing);
659701
}

Adafruit_Thermal.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ class Adafruit_Thermal : public Print {
212212
* @param val Desired height of the barcode
213213
*/
214214
setBarcodeHeight(uint8_t val=50),
215+
/*!
216+
* @brief Sets the font
217+
* @param font Desired font, either A or B
218+
*/
219+
setFont(char font='A'),
215220
/*!
216221
* @brief Sets the character spacing
217222
* @param spacing Desired character spacing
@@ -352,7 +357,7 @@ class Adafruit_Thermal : public Print {
352357
writeBytes(uint8_t a, uint8_t b, uint8_t c),
353358
writeBytes(uint8_t a, uint8_t b, uint8_t c, uint8_t d),
354359
setPrintMode(uint8_t mask), unsetPrintMode(uint8_t mask),
355-
writePrintMode();
360+
writePrintMode(), adjustCharValues(uint8_t printMode);
356361
};
357362

358363
#endif // ADAFRUIT_THERMAL_H

examples/A_printertest/A_printertest.ino

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ void setup() {
4747
// over and over (which would happen if they were in loop() instead).
4848
// Some functions will feed a line when called, this is normal.
4949

50+
// Font options
51+
printer.setFont('B');
52+
printer.println("FontB");
53+
printer.println("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
54+
printer.setFont('A');
55+
printer.println("FontA (default)");
56+
printer.println("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
57+
5058
// Test inverse on & off
5159
printer.inverseOn();
5260
printer.println(F("Inverse ON"));

keywords.txt

+1-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ doubleHeightOff KEYWORD2
3838
inverseOn KEYWORD2
3939
inverseOff KEYWORD2
4040
setDefault KEYWORD2
41-
42-
43-
44-
41+
setFont KEYWORD2
4542

4643
#######################################
4744
# Constants (LITERAL1)

0 commit comments

Comments
 (0)