Library for Adafruit Thermal Printer. Arduino library is published here: http://www.ladyada.net/products/thermalprinter/

Dependents:   Thermal_HelloWorld

Fork of AdafruitThermalPrinter by Ashley Mills

Committer:
aross34
Date:
Wed Mar 16 01:40:10 2016 +0000
Revision:
2:de26317d87a6
Parent:
1:315c49946ded
Child:
3:1f832bfe41d1
Hello world program for Adafruit nano thermal printer. Added library documentation.

Who changed what in which revision?

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