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