This is the Adafruit thermal printer, whose Arduino library is published here: http://www.ladyada.net/products/thermalprinter/. This is a basic port to mbed that needs proper testing. The first printBitmap function is implemented but not fully tested, the stream versions are not ported yet.

Dependents:   SMS_LEDMatrixPrinter

Fork of AdafruitThermalPrinter by Ashley Mills

Committer:
SomeRandomBloke
Date:
Wed Dec 05 10:00:33 2012 +0000
Revision:
3:0e12bed6b295
Parent:
2:6c38e763c285
Child:
4:871ca02007be
updates to remove dependancy on putc - hack to use uart2.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 3:0e12bed6b295 1 /***************************************************
ashleymills 0:c4a48036e46f 2 This is a library for the Adafruit Thermal Printer
SomeRandomBloke 3:0e12bed6b295 3
ashleymills 0:c4a48036e46f 4 Pick one up at --> http://www.adafruit.com/products/597
ashleymills 0:c4a48036e46f 5 These printers use TTL serial to communicate, 2 pins are required
ashleymills 0:c4a48036e46f 6
SomeRandomBloke 3:0e12bed6b295 7 Adafruit invests time and resources providing this open source code,
SomeRandomBloke 3:0e12bed6b295 8 please support Adafruit and open-source hardware by purchasing
ashleymills 0:c4a48036e46f 9 products from Adafruit!
ashleymills 0:c4a48036e46f 10
SomeRandomBloke 3:0e12bed6b295 11 Written by Limor Fried/Ladyada for Adafruit Industries.
ashleymills 0:c4a48036e46f 12 MIT license, all text above must be included in any redistribution
ashleymills 0:c4a48036e46f 13 ****************************************************/
SomeRandomBloke 3:0e12bed6b295 14
ashleymills 1:315c49946ded 15 /** Ported hastily to mbed by Ashley Mills - NOTE: printBitmap not ported, nothing tested **/
SomeRandomBloke 3:0e12bed6b295 16 #include "mbed.h"
ashleymills 0:c4a48036e46f 17 #include "AdafruitThermal.h"
SomeRandomBloke 3:0e12bed6b295 18 /*
SomeRandomBloke 3:0e12bed6b295 19 #define LPC_UART0 ((LPC_UART0_TypeDef *) LPC_UART0_BASE )
SomeRandomBloke 3:0e12bed6b295 20 #define LPC_UART1 ((LPC_UART1_TypeDef *) LPC_UART1_BASE )
SomeRandomBloke 3:0e12bed6b295 21 #define LPC_UART2 ((LPC_UART_TypeDef *) LPC_UART2_BASE )
SomeRandomBloke 3:0e12bed6b295 22 #define LPC_UART3 ((LPC_UART_TypeDef *) LPC_UART3_BASE )
SomeRandomBloke 3:0e12bed6b295 23
SomeRandomBloke 3:0e12bed6b295 24 Set correct UART
SomeRandomBloke 3:0e12bed6b295 25
SomeRandomBloke 3:0e12bed6b295 26 UART3
SomeRandomBloke 3:0e12bed6b295 27 p9 = P0_0 (tx)
SomeRandomBloke 3:0e12bed6b295 28 p10 = P0_1 (rx)
SomeRandomBloke 3:0e12bed6b295 29
SomeRandomBloke 3:0e12bed6b295 30 UART1
SomeRandomBloke 3:0e12bed6b295 31 p13 = P0_15 (tx)
SomeRandomBloke 3:0e12bed6b295 32 p14 = P0_16 (rx)
SomeRandomBloke 3:0e12bed6b295 33
SomeRandomBloke 3:0e12bed6b295 34 UART2
SomeRandomBloke 3:0e12bed6b295 35 p28 = P0_10 (tx)
SomeRandomBloke 3:0e12bed6b295 36 p27 = P0_11 (rx)
SomeRandomBloke 3:0e12bed6b295 37
SomeRandomBloke 3:0e12bed6b295 38 UART0
SomeRandomBloke 3:0e12bed6b295 39 USBTX = P0_2
SomeRandomBloke 3:0e12bed6b295 40 USBRX = P0_3
SomeRandomBloke 3:0e12bed6b295 41 */
SomeRandomBloke 3:0e12bed6b295 42
SomeRandomBloke 3:0e12bed6b295 43 LPC_UART_TypeDef *lpcUart;
ashleymills 0:c4a48036e46f 44
ashleymills 0:c4a48036e46f 45
SomeRandomBloke 3:0e12bed6b295 46 AdafruitThermal::AdafruitThermal(PinName RX_Pin, PinName TX_Pin)
SomeRandomBloke 3:0e12bed6b295 47 {
SomeRandomBloke 3:0e12bed6b295 48 _RX_Pin = RX_Pin;
SomeRandomBloke 3:0e12bed6b295 49 _TX_Pin = TX_Pin;
SomeRandomBloke 3:0e12bed6b295 50 // if( TX_Pin == p28 )
SomeRandomBloke 3:0e12bed6b295 51 // lpcUart = ((LPC_UART_TypeDef *)LPC_UART2_BASE);
SomeRandomBloke 3:0e12bed6b295 52 // else if( TX_Pin == p9 )
SomeRandomBloke 3:0e12bed6b295 53 // lpcUart = ((LPC_UART_TypeDef *)LPC_UART3_BASE);
ashleymills 0:c4a48036e46f 54 }
ashleymills 0:c4a48036e46f 55
SomeRandomBloke 3:0e12bed6b295 56 void AdafruitThermal::begin(int heatTime)
SomeRandomBloke 3:0e12bed6b295 57 {
SomeRandomBloke 3:0e12bed6b295 58 _printer = new Serial(_RX_Pin, _TX_Pin);
SomeRandomBloke 3:0e12bed6b295 59 _printer->baud(19200);
ashleymills 0:c4a48036e46f 60
SomeRandomBloke 3:0e12bed6b295 61 // The printer can't start receiving data immediately
SomeRandomBloke 3:0e12bed6b295 62 // upon power up -- needs a moment to initialize. If
SomeRandomBloke 3:0e12bed6b295 63 // Arduino & printer are powered from the same supply,
SomeRandomBloke 3:0e12bed6b295 64 // they're starting simultaneously. Need to pause for
SomeRandomBloke 3:0e12bed6b295 65 // a moment so the printer is ready for commands.
SomeRandomBloke 3:0e12bed6b295 66 // (A more robust approach might be to wait in a loop
SomeRandomBloke 3:0e12bed6b295 67 // issuing status commands until valid response.)
SomeRandomBloke 3:0e12bed6b295 68 wait(0.5);
SomeRandomBloke 3:0e12bed6b295 69
SomeRandomBloke 3:0e12bed6b295 70 reset();
ashleymills 0:c4a48036e46f 71
SomeRandomBloke 3:0e12bed6b295 72 // Description of print settings from page 23 of the manual:
SomeRandomBloke 3:0e12bed6b295 73 // ESC 7 n1 n2 n3 Setting Control Parameter Command
SomeRandomBloke 3:0e12bed6b295 74 // Decimal: 27 55 n1 n2 n3
SomeRandomBloke 3:0e12bed6b295 75 // Set "max heating dots", "heating time", "heating interval"
SomeRandomBloke 3:0e12bed6b295 76 // n1 = 0-255 Max printing dots, Unit (8dots), Default: 7 (64 dots)
SomeRandomBloke 3:0e12bed6b295 77 // n2 = 3-255 Heating time, Unit (10us), Default: 80 (800us)
SomeRandomBloke 3:0e12bed6b295 78 // n3 = 0-255 Heating interval, Unit (10us), Default: 2 (20us)
SomeRandomBloke 3:0e12bed6b295 79 // The more max heating dots, the more peak current will cost
SomeRandomBloke 3:0e12bed6b295 80 // when printing, the faster printing speed. The max heating
SomeRandomBloke 3:0e12bed6b295 81 // dots is 8*(n1+1). The more heating time, the more density,
SomeRandomBloke 3:0e12bed6b295 82 // but the slower printing speed. If heating time is too short,
SomeRandomBloke 3:0e12bed6b295 83 // blank page may occur. The more heating interval, the more
SomeRandomBloke 3:0e12bed6b295 84 // clear, but the slower printing speed.
ashleymills 0:c4a48036e46f 85
SomeRandomBloke 3:0e12bed6b295 86 writeBytes(27, 55); // Esc 7 (print settings)
SomeRandomBloke 3:0e12bed6b295 87 writeBytes(20); // Heating dots (20=balance of darkness vs no jams)
SomeRandomBloke 3:0e12bed6b295 88 writeBytes(heatTime); // Library default = 255 (max)
SomeRandomBloke 3:0e12bed6b295 89 writeBytes(250); // Heat interval (500 uS = slower, but darker)
ashleymills 0:c4a48036e46f 90
SomeRandomBloke 3:0e12bed6b295 91 // Description of print density from page 23 of the manual:
SomeRandomBloke 3:0e12bed6b295 92 // DC2 # n Set printing density
SomeRandomBloke 3:0e12bed6b295 93 // Decimal: 18 35 n
SomeRandomBloke 3:0e12bed6b295 94 // D4..D0 of n is used to set the printing density. Density is
SomeRandomBloke 3:0e12bed6b295 95 // 50% + 5% * n(D4-D0) printing density.
SomeRandomBloke 3:0e12bed6b295 96 // D7..D5 of n is used to set the printing break time. Break time
SomeRandomBloke 3:0e12bed6b295 97 // is n(D7-D5)*250us.
SomeRandomBloke 3:0e12bed6b295 98 // (Unsure of the default value for either -- not documented)
ashleymills 0:c4a48036e46f 99
SomeRandomBloke 3:0e12bed6b295 100 const int
ashleymills 0:c4a48036e46f 101 printDensity = 14, // 120% (? can go higher, text is darker but fuzzy)
ashleymills 0:c4a48036e46f 102 printBreakTime = 4; // 500 uS
SomeRandomBloke 3:0e12bed6b295 103 writeBytes(18, 35); // DC2 # (print density)
SomeRandomBloke 3:0e12bed6b295 104 writeBytes((printBreakTime << 5) | printDensity);
ashleymills 0:c4a48036e46f 105 }
ashleymills 0:c4a48036e46f 106
ashleymills 0:c4a48036e46f 107 // reset printer
SomeRandomBloke 3:0e12bed6b295 108 void AdafruitThermal::reset()
SomeRandomBloke 3:0e12bed6b295 109 {
SomeRandomBloke 3:0e12bed6b295 110 writeBytes(27, 64);
ashleymills 0:c4a48036e46f 111 }
ashleymills 0:c4a48036e46f 112
ashleymills 0:c4a48036e46f 113 // reset formatting
SomeRandomBloke 3:0e12bed6b295 114 void AdafruitThermal::setDefault()
SomeRandomBloke 3:0e12bed6b295 115 {
SomeRandomBloke 3:0e12bed6b295 116 online();
SomeRandomBloke 3:0e12bed6b295 117 justify('L');
SomeRandomBloke 3:0e12bed6b295 118 inverseOff();
SomeRandomBloke 3:0e12bed6b295 119 doubleHeightOff();
SomeRandomBloke 3:0e12bed6b295 120 setLineHeight(32);
SomeRandomBloke 3:0e12bed6b295 121 boldOff();
SomeRandomBloke 3:0e12bed6b295 122 underlineOff();
SomeRandomBloke 3:0e12bed6b295 123 setBarcodeHeight(50);
SomeRandomBloke 3:0e12bed6b295 124 setSize('s');
ashleymills 0:c4a48036e46f 125 }
ashleymills 0:c4a48036e46f 126
SomeRandomBloke 3:0e12bed6b295 127 //void AdafruitThermal::printNothing( char ch ) { }
SomeRandomBloke 3:0e12bed6b295 128
SomeRandomBloke 3:0e12bed6b295 129 void AdafruitThermal::test()
SomeRandomBloke 3:0e12bed6b295 130 {
SomeRandomBloke 3:0e12bed6b295 131 write('h');
SomeRandomBloke 3:0e12bed6b295 132 write('e');
SomeRandomBloke 3:0e12bed6b295 133 write('l');
SomeRandomBloke 3:0e12bed6b295 134 write('l');
SomeRandomBloke 3:0e12bed6b295 135 write('o');
SomeRandomBloke 3:0e12bed6b295 136 write('!');
SomeRandomBloke 3:0e12bed6b295 137 write('\n');
SomeRandomBloke 3:0e12bed6b295 138 feed(2);
ashleymills 0:c4a48036e46f 139 }
ashleymills 0:c4a48036e46f 140
SomeRandomBloke 3:0e12bed6b295 141 void AdafruitThermal::print(char *string)
SomeRandomBloke 3:0e12bed6b295 142 {
ashleymills 0:c4a48036e46f 143 while(*string!=0) {
ashleymills 0:c4a48036e46f 144 write(*string);
ashleymills 0:c4a48036e46f 145 string++;
ashleymills 0:c4a48036e46f 146 }
ashleymills 0:c4a48036e46f 147 }
ashleymills 0:c4a48036e46f 148
SomeRandomBloke 3:0e12bed6b295 149 void AdafruitThermal::testPage()
SomeRandomBloke 3:0e12bed6b295 150 {
SomeRandomBloke 3:0e12bed6b295 151 writeBytes(18, 84);
ashleymills 0:c4a48036e46f 152 }
ashleymills 0:c4a48036e46f 153
SomeRandomBloke 3:0e12bed6b295 154
SomeRandomBloke 3:0e12bed6b295 155 void AdafruitThermal::directPutc( uint8_t c ) {
SomeRandomBloke 3:0e12bed6b295 156
SomeRandomBloke 3:0e12bed6b295 157 do {
SomeRandomBloke 3:0e12bed6b295 158 } while ((LPC_UART2->LSR & 0x20)==0) ;
SomeRandomBloke 3:0e12bed6b295 159 //write character to UART
SomeRandomBloke 3:0e12bed6b295 160 LPC_UART2->THR = c ;
SomeRandomBloke 3:0e12bed6b295 161 /*
SomeRandomBloke 3:0e12bed6b295 162 do {
SomeRandomBloke 3:0e12bed6b295 163 } while ((lpcUart->LSR & 0x20)==0) ;
SomeRandomBloke 3:0e12bed6b295 164 //write character to UART
SomeRandomBloke 3:0e12bed6b295 165 lpcUart->THR = c ;
SomeRandomBloke 3:0e12bed6b295 166 */
SomeRandomBloke 3:0e12bed6b295 167 }
SomeRandomBloke 3:0e12bed6b295 168
SomeRandomBloke 3:0e12bed6b295 169
ashleymills 0:c4a48036e46f 170 // this is the basic function for all printing, the rest is taken care of by the
ashleymills 0:c4a48036e46f 171 // inherited Print class!
SomeRandomBloke 3:0e12bed6b295 172 size_t AdafruitThermal::write(uint8_t c)
SomeRandomBloke 3:0e12bed6b295 173 {
SomeRandomBloke 3:0e12bed6b295 174 if (c == 0x13) return 0;
ashleymills 0:c4a48036e46f 175
SomeRandomBloke 3:0e12bed6b295 176 if (c != 0xA)
SomeRandomBloke 3:0e12bed6b295 177 linefeedneeded = true;
SomeRandomBloke 3:0e12bed6b295 178 else
SomeRandomBloke 3:0e12bed6b295 179 linefeedneeded = false;
ashleymills 0:c4a48036e46f 180
SomeRandomBloke 3:0e12bed6b295 181 //DBG(" 0x");
SomeRandomBloke 3:0e12bed6b295 182 //DBG(c, HEX);
SomeRandomBloke 3:0e12bed6b295 183 //DBG(" (");
SomeRandomBloke 3:0e12bed6b295 184
SomeRandomBloke 3:0e12bed6b295 185 PRINTER_PRINT(c);
SomeRandomBloke 3:0e12bed6b295 186
SomeRandomBloke 3:0e12bed6b295 187 return 1;
ashleymills 0:c4a48036e46f 188
ashleymills 0:c4a48036e46f 189 }
ashleymills 0:c4a48036e46f 190
SomeRandomBloke 3:0e12bed6b295 191 void AdafruitThermal::setBarcodeHeight(int val)
SomeRandomBloke 3:0e12bed6b295 192 {
SomeRandomBloke 3:0e12bed6b295 193 //default is 50
SomeRandomBloke 3:0e12bed6b295 194 writeBytes(29, 104, val);
ashleymills 0:c4a48036e46f 195 }
ashleymills 0:c4a48036e46f 196
SomeRandomBloke 3:0e12bed6b295 197 void AdafruitThermal::printBarcode(char * text, uint8_t type)
SomeRandomBloke 3:0e12bed6b295 198 {
SomeRandomBloke 3:0e12bed6b295 199 int i;
SomeRandomBloke 3:0e12bed6b295 200 uint8_t c;
ashleymills 0:c4a48036e46f 201
SomeRandomBloke 3:0e12bed6b295 202 delay(1000); // Need these delays else barcode doesn't always print. ???
SomeRandomBloke 3:0e12bed6b295 203 writeBytes(29, 107, type); // set the type first
SomeRandomBloke 3:0e12bed6b295 204 delay(500);
SomeRandomBloke 3:0e12bed6b295 205 // Copy string, not including NUL terminator
SomeRandomBloke 3:0e12bed6b295 206 for(i=0; (c = text[i]); i++) PRINTER_PRINT(c);
SomeRandomBloke 3:0e12bed6b295 207 delay(500);
SomeRandomBloke 3:0e12bed6b295 208 PRINTER_PRINT(c); // Terminator must follow delay. ???
ashleymills 0:c4a48036e46f 209
SomeRandomBloke 3:0e12bed6b295 210 delay(3000); // For some reason we can't immediately have line feeds here
SomeRandomBloke 3:0e12bed6b295 211 feed(2);
ashleymills 0:c4a48036e46f 212 }
ashleymills 0:c4a48036e46f 213
SomeRandomBloke 3:0e12bed6b295 214 void AdafruitThermal::writeBytes(uint8_t a)
SomeRandomBloke 3:0e12bed6b295 215 {
SomeRandomBloke 3:0e12bed6b295 216 PRINTER_PRINT(a);
ashleymills 0:c4a48036e46f 217 }
ashleymills 0:c4a48036e46f 218
SomeRandomBloke 3:0e12bed6b295 219 void AdafruitThermal::writeBytes(uint8_t a, uint8_t b)
SomeRandomBloke 3:0e12bed6b295 220 {
SomeRandomBloke 3:0e12bed6b295 221 PRINTER_PRINT(a);
SomeRandomBloke 3:0e12bed6b295 222 PRINTER_PRINT(b);
ashleymills 0:c4a48036e46f 223 }
ashleymills 0:c4a48036e46f 224
SomeRandomBloke 3:0e12bed6b295 225 void AdafruitThermal::writeBytes(uint8_t a, uint8_t b, uint8_t c)
SomeRandomBloke 3:0e12bed6b295 226 {
SomeRandomBloke 3:0e12bed6b295 227 PRINTER_PRINT(a);
SomeRandomBloke 3:0e12bed6b295 228 PRINTER_PRINT(b);
SomeRandomBloke 3:0e12bed6b295 229 PRINTER_PRINT(c);
ashleymills 0:c4a48036e46f 230 }
ashleymills 0:c4a48036e46f 231
SomeRandomBloke 3:0e12bed6b295 232 void AdafruitThermal::writeBytes(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
SomeRandomBloke 3:0e12bed6b295 233 {
SomeRandomBloke 3:0e12bed6b295 234 PRINTER_PRINT(a);
SomeRandomBloke 3:0e12bed6b295 235 PRINTER_PRINT(b);
SomeRandomBloke 3:0e12bed6b295 236 PRINTER_PRINT(c);
SomeRandomBloke 3:0e12bed6b295 237 PRINTER_PRINT(d);
ashleymills 0:c4a48036e46f 238 }
ashleymills 0:c4a48036e46f 239
ashleymills 0:c4a48036e46f 240 // === Character commands ===
ashleymills 0:c4a48036e46f 241
ashleymills 0:c4a48036e46f 242 #define INVERSE_MASK (1 << 1)
ashleymills 0:c4a48036e46f 243 #define UPDOWN_MASK (1 << 2)
ashleymills 0:c4a48036e46f 244 #define BOLD_MASK (1 << 3)
ashleymills 0:c4a48036e46f 245 #define DOUBLE_HEIGHT_MASK (1 << 4)
ashleymills 0:c4a48036e46f 246 #define DOUBLE_WIDTH_MASK (1 << 5)
ashleymills 0:c4a48036e46f 247 #define STRIKE_MASK (1 << 6)
ashleymills 0:c4a48036e46f 248
SomeRandomBloke 3:0e12bed6b295 249 void AdafruitThermal::setPrintMode(uint8_t mask)
SomeRandomBloke 3:0e12bed6b295 250 {
SomeRandomBloke 3:0e12bed6b295 251 printMode |= mask;
SomeRandomBloke 3:0e12bed6b295 252 writePrintMode();
ashleymills 0:c4a48036e46f 253 }
SomeRandomBloke 3:0e12bed6b295 254 void AdafruitThermal::unsetPrintMode(uint8_t mask)
SomeRandomBloke 3:0e12bed6b295 255 {
SomeRandomBloke 3:0e12bed6b295 256 printMode &= ~mask;
SomeRandomBloke 3:0e12bed6b295 257 writePrintMode();
ashleymills 0:c4a48036e46f 258 }
ashleymills 0:c4a48036e46f 259
SomeRandomBloke 3:0e12bed6b295 260 void AdafruitThermal::writePrintMode()
SomeRandomBloke 3:0e12bed6b295 261 {
SomeRandomBloke 3:0e12bed6b295 262 writeBytes(27, 33, printMode);
ashleymills 0:c4a48036e46f 263 }
ashleymills 0:c4a48036e46f 264
SomeRandomBloke 3:0e12bed6b295 265 void AdafruitThermal::normal()
SomeRandomBloke 3:0e12bed6b295 266 {
SomeRandomBloke 3:0e12bed6b295 267 printMode = 0;
SomeRandomBloke 3:0e12bed6b295 268 writePrintMode();
ashleymills 0:c4a48036e46f 269 }
ashleymills 0:c4a48036e46f 270
SomeRandomBloke 3:0e12bed6b295 271 void AdafruitThermal::inverseOn()
SomeRandomBloke 3:0e12bed6b295 272 {
SomeRandomBloke 3:0e12bed6b295 273 setPrintMode(INVERSE_MASK);
ashleymills 0:c4a48036e46f 274 }
ashleymills 0:c4a48036e46f 275
SomeRandomBloke 3:0e12bed6b295 276 void AdafruitThermal::inverseOff()
SomeRandomBloke 3:0e12bed6b295 277 {
SomeRandomBloke 3:0e12bed6b295 278 unsetPrintMode(INVERSE_MASK);
ashleymills 0:c4a48036e46f 279 }
ashleymills 0:c4a48036e46f 280
SomeRandomBloke 3:0e12bed6b295 281 void AdafruitThermal::upsideDownOn()
SomeRandomBloke 3:0e12bed6b295 282 {
SomeRandomBloke 3:0e12bed6b295 283 setPrintMode(UPDOWN_MASK);
ashleymills 0:c4a48036e46f 284 }
ashleymills 0:c4a48036e46f 285
SomeRandomBloke 3:0e12bed6b295 286 void AdafruitThermal::upsideDownOff()
SomeRandomBloke 3:0e12bed6b295 287 {
SomeRandomBloke 3:0e12bed6b295 288 unsetPrintMode(UPDOWN_MASK);
ashleymills 0:c4a48036e46f 289 }
ashleymills 0:c4a48036e46f 290
SomeRandomBloke 3:0e12bed6b295 291 void AdafruitThermal::doubleHeightOn()
SomeRandomBloke 3:0e12bed6b295 292 {
SomeRandomBloke 3:0e12bed6b295 293 setPrintMode(DOUBLE_HEIGHT_MASK);
ashleymills 0:c4a48036e46f 294 }
ashleymills 0:c4a48036e46f 295
SomeRandomBloke 3:0e12bed6b295 296 void AdafruitThermal::doubleHeightOff()
SomeRandomBloke 3:0e12bed6b295 297 {
SomeRandomBloke 3:0e12bed6b295 298 unsetPrintMode(DOUBLE_HEIGHT_MASK);
ashleymills 0:c4a48036e46f 299 }
ashleymills 0:c4a48036e46f 300
SomeRandomBloke 3:0e12bed6b295 301 void AdafruitThermal::doubleWidthOn()
SomeRandomBloke 3:0e12bed6b295 302 {
SomeRandomBloke 3:0e12bed6b295 303 setPrintMode(DOUBLE_WIDTH_MASK);
ashleymills 0:c4a48036e46f 304 }
ashleymills 0:c4a48036e46f 305
SomeRandomBloke 3:0e12bed6b295 306 void AdafruitThermal::doubleWidthOff()
SomeRandomBloke 3:0e12bed6b295 307 {
SomeRandomBloke 3:0e12bed6b295 308 unsetPrintMode(DOUBLE_WIDTH_MASK);
ashleymills 0:c4a48036e46f 309 }
ashleymills 0:c4a48036e46f 310
SomeRandomBloke 3:0e12bed6b295 311 void AdafruitThermal::strikeOn()
SomeRandomBloke 3:0e12bed6b295 312 {
SomeRandomBloke 3:0e12bed6b295 313 setPrintMode(STRIKE_MASK);
ashleymills 0:c4a48036e46f 314 }
ashleymills 0:c4a48036e46f 315
SomeRandomBloke 3:0e12bed6b295 316 void AdafruitThermal::strikeOff()
SomeRandomBloke 3:0e12bed6b295 317 {
SomeRandomBloke 3:0e12bed6b295 318 unsetPrintMode(STRIKE_MASK);
ashleymills 0:c4a48036e46f 319 }
ashleymills 0:c4a48036e46f 320
SomeRandomBloke 3:0e12bed6b295 321 void AdafruitThermal::boldOn()
SomeRandomBloke 3:0e12bed6b295 322 {
SomeRandomBloke 3:0e12bed6b295 323 setPrintMode(BOLD_MASK);
ashleymills 0:c4a48036e46f 324 }
ashleymills 0:c4a48036e46f 325
SomeRandomBloke 3:0e12bed6b295 326 void AdafruitThermal::boldOff()
SomeRandomBloke 3:0e12bed6b295 327 {
SomeRandomBloke 3:0e12bed6b295 328 unsetPrintMode(BOLD_MASK);
ashleymills 0:c4a48036e46f 329 }
ashleymills 0:c4a48036e46f 330
SomeRandomBloke 3:0e12bed6b295 331 void AdafruitThermal::justify(char value)
SomeRandomBloke 3:0e12bed6b295 332 {
SomeRandomBloke 3:0e12bed6b295 333 uint8_t pos = 0;
ashleymills 0:c4a48036e46f 334
SomeRandomBloke 3:0e12bed6b295 335 if(value == 'l' || value == 'L') pos = 0;
SomeRandomBloke 3:0e12bed6b295 336 if(value == 'c' || value == 'C') pos = 1;
SomeRandomBloke 3:0e12bed6b295 337 if(value == 'r' || value == 'R') pos = 2;
ashleymills 0:c4a48036e46f 338
SomeRandomBloke 3:0e12bed6b295 339 writeBytes(0x1B, 0x61, pos);
ashleymills 0:c4a48036e46f 340 }
ashleymills 0:c4a48036e46f 341
ashleymills 0:c4a48036e46f 342 // Feeds by the specified number of lines
SomeRandomBloke 3:0e12bed6b295 343 void AdafruitThermal::feed(uint8_t x)
SomeRandomBloke 3:0e12bed6b295 344 {
SomeRandomBloke 3:0e12bed6b295 345 // The datasheet claims sending bytes 27, 100, <x> will work
SomeRandomBloke 3:0e12bed6b295 346 // but it feeds much much more.
SomeRandomBloke 3:0e12bed6b295 347 while (x--)
SomeRandomBloke 3:0e12bed6b295 348 write('\n');
ashleymills 0:c4a48036e46f 349 }
ashleymills 0:c4a48036e46f 350
ashleymills 0:c4a48036e46f 351 // Feeds by the specified number of rows of pixels
SomeRandomBloke 3:0e12bed6b295 352 void AdafruitThermal::feedRows(uint8_t rows)
SomeRandomBloke 3:0e12bed6b295 353 {
SomeRandomBloke 3:0e12bed6b295 354 writeBytes(27, 74, rows);
ashleymills 0:c4a48036e46f 355 }
ashleymills 0:c4a48036e46f 356
SomeRandomBloke 3:0e12bed6b295 357 void AdafruitThermal::flush()
SomeRandomBloke 3:0e12bed6b295 358 {
SomeRandomBloke 3:0e12bed6b295 359 writeBytes(12);
ashleymills 0:c4a48036e46f 360 }
ashleymills 0:c4a48036e46f 361
SomeRandomBloke 3:0e12bed6b295 362 void AdafruitThermal::setSize(char value)
SomeRandomBloke 3:0e12bed6b295 363 {
SomeRandomBloke 3:0e12bed6b295 364 int size = 0;
ashleymills 0:c4a48036e46f 365
SomeRandomBloke 3:0e12bed6b295 366 if(value == 's' || value == 'S') size = 0;
SomeRandomBloke 3:0e12bed6b295 367 if(value == 'm' || value == 'M') size = 10;
SomeRandomBloke 3:0e12bed6b295 368 if(value == 'l' || value == 'L') size = 25;
ashleymills 0:c4a48036e46f 369
SomeRandomBloke 3:0e12bed6b295 370 writeBytes(29, 33, size, 10);
SomeRandomBloke 3:0e12bed6b295 371 // if (linefeedneeded)
SomeRandomBloke 3:0e12bed6b295 372 // println("lfn"); //feed();
SomeRandomBloke 3:0e12bed6b295 373 //linefeedneeded = false;
ashleymills 0:c4a48036e46f 374 }
ashleymills 0:c4a48036e46f 375
ashleymills 0:c4a48036e46f 376 // Underlines of different weights can be produced:
ashleymills 0:c4a48036e46f 377 // 0 - no underline
ashleymills 0:c4a48036e46f 378 // 1 - normal underline
ashleymills 0:c4a48036e46f 379 // 2 - thick underline
SomeRandomBloke 3:0e12bed6b295 380 void AdafruitThermal::underlineOn(uint8_t weight)
SomeRandomBloke 3:0e12bed6b295 381 {
SomeRandomBloke 3:0e12bed6b295 382 writeBytes(27, 45, weight);
ashleymills 0:c4a48036e46f 383 }
ashleymills 0:c4a48036e46f 384
SomeRandomBloke 3:0e12bed6b295 385 void AdafruitThermal::underlineOff()
SomeRandomBloke 3:0e12bed6b295 386 {
SomeRandomBloke 3:0e12bed6b295 387 underlineOn(0);
ashleymills 0:c4a48036e46f 388 }
ashleymills 0:c4a48036e46f 389
SomeRandomBloke 2:6c38e763c285 390
SomeRandomBloke 3:0e12bed6b295 391 void AdafruitThermal::printBitmap(int w, int h, const uint8_t *bitmap)
SomeRandomBloke 3:0e12bed6b295 392 {
SomeRandomBloke 3:0e12bed6b295 393 if (w > 384) return; // maximum width of the printer
SomeRandomBloke 3:0e12bed6b295 394 for (int rowStart=0; rowStart < h; rowStart += 256) {
SomeRandomBloke 3:0e12bed6b295 395 int chunkHeight = ((h - rowStart) > 255) ? 255 : (h - rowStart);
SomeRandomBloke 3:0e12bed6b295 396 delay(500); // Need these delays else bitmap doesn't always print. ???
SomeRandomBloke 3:0e12bed6b295 397 writeBytes(18, 42);
SomeRandomBloke 3:0e12bed6b295 398 writeBytes(chunkHeight, w/8);
SomeRandomBloke 3:0e12bed6b295 399 delay(500);
SomeRandomBloke 3:0e12bed6b295 400 for (int i=0; i<((w/8)*chunkHeight); i++) {
SomeRandomBloke 3:0e12bed6b295 401 PRINTER_PRINT(bitmap[(rowStart*(w/8)) + i]);
SomeRandomBloke 2:6c38e763c285 402 // PRINTER_PRINT(pgm_read_byte(bitmap + (rowStart*(w/8)) + i));
SomeRandomBloke 3:0e12bed6b295 403 }
SomeRandomBloke 3:0e12bed6b295 404 delay(500);
ashleymills 0:c4a48036e46f 405 }
ashleymills 0:c4a48036e46f 406 }
ashleymills 0:c4a48036e46f 407
SomeRandomBloke 2:6c38e763c285 408 /*
ashleymills 0:c4a48036e46f 409 void AdafruitThermal::printBitmap(int w, int h, Stream *stream) {
ashleymills 0:c4a48036e46f 410 if (w > 384) return; // maximum width of the printer
ashleymills 0:c4a48036e46f 411 for (int rowStart=0; rowStart < h; rowStart += 256) {
ashleymills 0:c4a48036e46f 412 int chunkHeight = ((h - rowStart) > 255) ? 255 : (h - rowStart);
ashleymills 0:c4a48036e46f 413 delay(500); // Need these delays else bitmap doesn't always print. ???
ashleymills 0:c4a48036e46f 414 writeBytes(18, 42);
ashleymills 0:c4a48036e46f 415 writeBytes(chunkHeight, w/8);
ashleymills 0:c4a48036e46f 416 delay(500);
ashleymills 0:c4a48036e46f 417 for (int i=0; i<((w/8)*chunkHeight); i++) {
ashleymills 0:c4a48036e46f 418 PRINTER_PRINT((uint8_t)stream->read());
ashleymills 0:c4a48036e46f 419 }
ashleymills 0:c4a48036e46f 420 delay(500);
ashleymills 0:c4a48036e46f 421 }
ashleymills 0:c4a48036e46f 422 };
ashleymills 0:c4a48036e46f 423
ashleymills 0:c4a48036e46f 424 void AdafruitThermal::printBitmap(Stream *stream) {
ashleymills 0:c4a48036e46f 425 uint8_t tmp;
ashleymills 0:c4a48036e46f 426 uint16_t width, height;
ashleymills 0:c4a48036e46f 427
ashleymills 0:c4a48036e46f 428 tmp = stream->read();
ashleymills 0:c4a48036e46f 429 width = (stream->read() << 8) + tmp;
ashleymills 0:c4a48036e46f 430
ashleymills 0:c4a48036e46f 431 tmp = stream->read();
ashleymills 0:c4a48036e46f 432 height = (stream->read() << 8) + tmp;
ashleymills 0:c4a48036e46f 433
ashleymills 0:c4a48036e46f 434 printBitmap(width, height, stream);
ashleymills 0:c4a48036e46f 435 };
ashleymills 0:c4a48036e46f 436 */
ashleymills 0:c4a48036e46f 437
ashleymills 0:c4a48036e46f 438 // Take the printer offline. Print commands sent after this will be
ashleymills 0:c4a48036e46f 439 // ignored until `online` is called
SomeRandomBloke 3:0e12bed6b295 440 void AdafruitThermal::offline()
SomeRandomBloke 3:0e12bed6b295 441 {
SomeRandomBloke 3:0e12bed6b295 442 writeBytes(27, 61, 0);
ashleymills 0:c4a48036e46f 443 }
ashleymills 0:c4a48036e46f 444
ashleymills 0:c4a48036e46f 445 // Take the printer back online. Subsequent print commands will be
ashleymills 0:c4a48036e46f 446 // obeyed.
SomeRandomBloke 3:0e12bed6b295 447 void AdafruitThermal::online()
SomeRandomBloke 3:0e12bed6b295 448 {
SomeRandomBloke 3:0e12bed6b295 449 writeBytes(27, 61, 1);
ashleymills 0:c4a48036e46f 450 }
ashleymills 0:c4a48036e46f 451
ashleymills 0:c4a48036e46f 452 // Put the printer into a low-energy state immediately
SomeRandomBloke 3:0e12bed6b295 453 void AdafruitThermal::sleep()
SomeRandomBloke 3:0e12bed6b295 454 {
SomeRandomBloke 3:0e12bed6b295 455 sleepAfter(0);
ashleymills 0:c4a48036e46f 456 }
ashleymills 0:c4a48036e46f 457
ashleymills 0:c4a48036e46f 458 // Put the printer into a low-energy state after the given number
ashleymills 0:c4a48036e46f 459 // of seconds
SomeRandomBloke 3:0e12bed6b295 460 void AdafruitThermal::sleepAfter(uint8_t seconds)
SomeRandomBloke 3:0e12bed6b295 461 {
SomeRandomBloke 3:0e12bed6b295 462 writeBytes(27, 56, seconds);
ashleymills 0:c4a48036e46f 463 }
ashleymills 0:c4a48036e46f 464
ashleymills 0:c4a48036e46f 465 // Wake the printer from a low-energy state. This command will wait
ashleymills 0:c4a48036e46f 466 // for 50ms (as directed by the datasheet) before allowing further
ashleymills 0:c4a48036e46f 467 // commands to be send.
SomeRandomBloke 3:0e12bed6b295 468 void AdafruitThermal::wake()
SomeRandomBloke 3:0e12bed6b295 469 {
SomeRandomBloke 3:0e12bed6b295 470 writeBytes(255);
SomeRandomBloke 3:0e12bed6b295 471 delay(50);
ashleymills 0:c4a48036e46f 472 }
ashleymills 0:c4a48036e46f 473
ashleymills 0:c4a48036e46f 474 ////////////////////// not working?
SomeRandomBloke 3:0e12bed6b295 475 void AdafruitThermal::tab()
SomeRandomBloke 3:0e12bed6b295 476 {
SomeRandomBloke 3:0e12bed6b295 477 PRINTER_PRINT(9);
ashleymills 0:c4a48036e46f 478 }
SomeRandomBloke 3:0e12bed6b295 479 void AdafruitThermal::setCharSpacing(int spacing)
SomeRandomBloke 3:0e12bed6b295 480 {
SomeRandomBloke 3:0e12bed6b295 481 writeBytes(27, 32, 0, 10);
ashleymills 0:c4a48036e46f 482 }
SomeRandomBloke 3:0e12bed6b295 483 void AdafruitThermal::setLineHeight(int val)
SomeRandomBloke 3:0e12bed6b295 484 {
SomeRandomBloke 3:0e12bed6b295 485 writeBytes(27, 51, val); // default is 32
ashleymills 0:c4a48036e46f 486 }