// Wake the printer from a low-energy state. void Adafruit_Thermal::wake() { // Printer may have been idle for a very long time, during which the // micros() counter has rolled over. To avoid shenanigans, reset the // timeout counter before issuing the wake command. timeoutSet(0); writeBytes(255); // Datasheet recomments a 50 mS delay before issuing further commands, // but in practice this alone isn't sufficient (e.g. text size/style // commands may still be misinterpreted on wake). A slightly longer // delay, interspersed with ESC chars (no-ops) seems to help. for(uint8_t i=0; i<10; i++) { writeBytes(27); timeoutSet(10000L); } }
void Adafruit_Thermal::printBitmap(int w, int h, Stream *fromStream) { int rowBytes, rowBytesClipped, rowStart, chunkHeight, chunkHeightLimit, x, y, i, c; rowBytes = (w + 7) / 8; // Round up to next byte boundary rowBytesClipped = (rowBytes >= 48) ? 48 : rowBytes; // 384 pixels max width // Est. max rows to write at once, assuming 256 byte printer buffer. chunkHeightLimit = 256 / rowBytesClipped; if(chunkHeightLimit > maxChunkHeight) chunkHeightLimit = maxChunkHeight; else if(chunkHeightLimit < 1) chunkHeightLimit = 1; for(rowStart=0; rowStart < h; rowStart += chunkHeightLimit) { // Issue up to chunkHeightLimit rows at a time: chunkHeight = h - rowStart; if(chunkHeight > chunkHeightLimit) chunkHeight = chunkHeightLimit; writeBytes(ASCII_DC2, '*', chunkHeight, rowBytesClipped); for(y=0; y < chunkHeight; y++) { for(x=0; x < rowBytesClipped; x++) { while((c = fromStream->read()) < 0); timeoutWait(); stream->write((uint8_t)c); } for(i = rowBytes - rowBytesClipped; i>0; i--) { while((c = fromStream->read()) < 0); } } timeoutSet(chunkHeight * dotPrintTime); } prevByte = '\n'; }
void Adafruit_Thermal::writeBytes(uint8_t a, uint8_t b, uint8_t c) { timeoutWait(); PRINTER_PRINT(a); PRINTER_PRINT(b); PRINTER_PRINT(c); timeoutSet(3 * BYTE_TIME); }
void Adafruit_Thermal::printBitmap(int w, int h, Stream *stream) { int rowBytes, rowBytesClipped, rowStart, chunkHeight, x, y, i, c; rowBytes = (w + 7) / 8; // Round up to next byte boundary rowBytesClipped = (rowBytes >= 48) ? 48 : rowBytes; // 384 pixels max width for(rowStart=0; rowStart < h; rowStart += 255) { // Issue up to 255 rows at a time: chunkHeight = h - rowStart; if(chunkHeight > 255) chunkHeight = 255; writeBytes(18, 42, chunkHeight, rowBytesClipped); for(y=0; y < chunkHeight; y++) { for(x=0; x < rowBytesClipped; x++) { while((c = stream->read()) < 0); PRINTER_PRINT((uint8_t)c); } for(i = rowBytes - rowBytesClipped; i>0; i--) { while((c = stream->read()) < 0); } } timeoutSet(chunkHeight * dotPrintTime); } prevByte = '\n'; }
void Adafruit_Thermal::printBitmap( int w, int h, const uint8_t *bitmap, bool fromProgMem) { int rowBytes, rowBytesClipped, rowStart, chunkHeight, chunkHeightLimit, x, y, i; rowBytes = (w + 7) / 8; // Round up to next byte boundary rowBytesClipped = (rowBytes >= 48) ? 48 : rowBytes; // 384 pixels max width // Est. max rows to write at once, assuming 256 byte printer buffer. chunkHeightLimit = 256 / rowBytesClipped; if(chunkHeightLimit > maxChunkHeight) chunkHeightLimit = maxChunkHeight; else if(chunkHeightLimit < 1) chunkHeightLimit = 1; for(i=rowStart=0; rowStart < h; rowStart += chunkHeightLimit) { // Issue up to chunkHeightLimit rows at a time: chunkHeight = h - rowStart; if(chunkHeight > chunkHeightLimit) chunkHeight = chunkHeightLimit; writeBytes(ASCII_DC2, '*', chunkHeight, rowBytesClipped); for(y=0; y < chunkHeight; y++) { for(x=0; x < rowBytesClipped; x++, i++) { timeoutWait(); stream->write(fromProgMem ? pgm_read_byte(bitmap + i) : *(bitmap+i)); } i += rowBytes - rowBytesClipped; } timeoutSet(chunkHeight * dotPrintTime); } prevByte = '\n'; }
void Adafruit_Thermal::writeBytes(uint8_t a, uint8_t b, uint8_t c) { timeoutWait(); stream->write(a); stream->write(b); stream->write(c); timeoutSet(3 * BYTE_TIME); }
// Feeds by the specified number of lines void Adafruit_Thermal::feed(uint8_t x) { #if PRINTER_FIRMWARE >= 264 writeBytes(ASCII_ESC, 'd', x); timeoutSet(dotFeedTime * charHeight); prevByte = '\n'; column = 0; #else while(x--) write('\n'); // Feed manually; old firmware feeds excess lines #endif }
void Adafruit_Thermal::printBarcode(char * text, uint8_t type) { int i = 0; byte c; writeBytes(29, 72, 2); // Print label below barcode writeBytes(29, 119, 3); // Barcode width writeBytes(29, 107, type); // Barcode type (listed in .h file) do { // Copy string + NUL terminator writeBytes(c = text[i++]); } while(c); timeoutSet((barcodeHeight + 40) * dotPrintTime); prevByte = '\n'; feed(2); }
void Adafruit_Thermal::begin(int heatTime) { // The printer can't start receiving data immediately upon power up -- // it needs a moment to cold boot and initialize. Allow at least 1/2 // sec of uptime before printer can receive data. timeoutSet(500000L); wake(); reset(); // ESC 7 n1 n2 n3 Setting Control Parameter Command // n1 = "max heating dots" 0-255 -- max number of thermal print head // elements that will fire simultaneously. Units = 8 dots (minus 1). // Printer default is 7 (64 dots, or 1/6 of 384-dot width), this code // sets it to 11 (96 dots, or 1/4 of width). // n2 = "heating time" 3-255 -- duration that heating dots are fired. // Units = 10 us. Printer default is 80 (800 us), this code sets it // to value passed (default 120, or 1.2 ms -- a little longer than // the default because we've increased the max heating dots). // n3 = "heating interval" 0-255 -- recovery time between groups of // heating dots on line; possibly a function of power supply. // Units = 10 us. Printer default is 2 (20 us), this code sets it // to 40 (throttled back due to 2A supply). // More heating dots = more peak current, but faster printing speed. // More heating time = darker print, but slower printing speed and // possibly paper 'stiction'. More heating interval = clearer print, // but slower printing speed. writeBytes(ASCII_ESC, '7'); // Esc 7 (print settings) writeBytes(11, heatTime, 40); // Heating dots, heat time, heat interval // Print density description from manual: // DC2 # n Set printing density // D4..D0 of n is used to set the printing density. Density is // 50% + 5% * n(D4-D0) printing density. // D7..D5 of n is used to set the printing break time. Break time // is n(D7-D5)*250us. // (Unsure of the default value for either -- not documented) #define printDensity 10 // 100% (? can go higher, text is darker but fuzzy) #define printBreakTime 2 // 500 uS writeBytes(ASCII_DC2, '#', (printBreakTime << 5) | printDensity); dotPrintTime = 30000; // See comments near top of file for dotFeedTime = 2100; // an explanation of these values. maxChunkHeight = 256; }
void Adafruit_Thermal::begin(SERIAL_IMPL* serial, int heatTime, int maxHeatingDots, int heatingInterval, int printDensity, int printBreakTime) { _printer = serial; // The printer can't start receiving data immediately upon power up -- // it needs a moment to cold boot and initialize. Allow at least 1/2 // sec of uptime before printer can receive data. timeoutSet(500000L); wake(); reset(); // Description of print settings from page 23 of the manual: // ESC 7 n1 n2 n3 Setting Control Parameter Command // Decimal: 27 55 n1 n2 n3 // Set "max heating dots", "heating time", "heating interval" // n1 = 0-255 Max printing dots, Unit (8dots), Default: 7 (64 dots) // n2 = 3-255 Heating time, Unit (10us), Default: 80 (800us) // n3 = 0-255 Heating interval, Unit (10us), Default: 2 (20us) // The more max heating dots, the more peak current will cost // when printing, the faster printing speed. The max heating // dots is 8*(n1+1). The more heating time, the more density, // but the slower printing speed. If heating time is too short, // blank page may occur. The more heating interval, the more // clear, but the slower printing speed. writeBytes(27, 55); // Esc 7 (print settings) writeBytes(maxHeatingDots); // Heating dots (20=balance of darkness vs no jams) writeBytes(heatTime); // Library default = 255 (max) writeBytes(heatingInterval); // Heat interval (500 uS = slower, but darker) // Description of print density from page 23 of the manual: // DC2 # n Set printing density // Decimal: 18 35 n // D4..D0 of n is used to set the printing density. Density is // 50% + 5% * n(D4-D0) printing density. // D7..D5 of n is used to set the printing break time. Break time // is n(D7-D5)*250us. // (Unsure of the default value for either -- not documented) writeBytes(18, 35); // DC2 # (print density) writeBytes((printBreakTime << 5) | printDensity); dotPrintTime = 30000; // See comments near top of file for dotFeedTime = 2100; // an explanation of these values. maxChunkHeight = 1; }
void Adafruit_Thermal::printBarcode(char *text, uint8_t type) { feed(1); // Recent firmware can't print barcode w/o feed first??? writeBytes(ASCII_GS, 'H', 2); // Print label below barcode writeBytes(ASCII_GS, 'w', 3); // Barcode width 3 (0.375/1.0mm thin/thick) writeBytes(ASCII_GS, 'k', type); // Barcode type (listed in .h file) #if PRINTER_FIRMWARE >= 264 int len = strlen(text); if(len > 255) len = 255; writeBytes(len); // Write length byte for(uint8_t i=0; i<len; i++) writeBytes(text[i]); // Write string sans NUL #else uint8_t c, i=0; do { // Copy string + NUL terminator writeBytes(c = text[i++]); } while(c); #endif timeoutSet((barcodeHeight + 40) * dotPrintTime); prevByte = '\n'; }
// The underlying method for all high-level printing (e.g. println()). // The inherited Print class handles the rest! size_t Adafruit_Thermal::write(uint8_t c) { if(c != 0x13) { // Strip carriage returns timeoutWait(); stream->write(c); unsigned long d = BYTE_TIME; if((c == '\n') || (column == maxColumn)) { // If newline or wrap d += (prevByte == '\n') ? ((charHeight+lineSpacing) * dotFeedTime) : // Feed line ((charHeight*dotPrintTime)+(lineSpacing*dotFeedTime)); // Text line column = 0; c = '\n'; // Treat wrap as newline on next pass } else { column++; } timeoutSet(d); prevByte = c; } return 1; }
void Adafruit_Thermal::printBitmap(int w, int h, const uint8_t *bitmap, bool fromProgMem) { int rowBytes, rowBytesClipped, rowStart, chunkHeight, x, y, i; rowBytes = (w + 7) / 8; // Round up to next byte boundary rowBytesClipped = (rowBytes >= 48) ? 48 : rowBytes; // 384 pixels max width for(i=rowStart=0; rowStart < h; rowStart += 255) { // Issue up to 255 rows at a time: chunkHeight = h - rowStart; if(chunkHeight > 255) chunkHeight = 255; writeBytes(18, 42, chunkHeight, rowBytesClipped); for(y=0; y < chunkHeight; y++) { for(x=0; x < rowBytesClipped; x++, i++) { PRINTER_PRINT(fromProgMem ? pgm_read_byte(bitmap + i) : *(bitmap+i)); } i += rowBytes - rowBytesClipped; } timeoutSet(chunkHeight * dotPrintTime); } prevByte = '\n'; }
void Adafruit_Thermal::writeBytes(uint8_t a) { timeoutWait(); PRINTER_PRINT(a); timeoutSet(BYTE_TIME); }
// 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 Adafruit_Thermal::wake() { writeBytes(255); timeoutSet(50000); }
void Adafruit_Thermal::testPage() { writeBytes(ASCII_DC2, 'T'); timeoutSet( dotPrintTime * 24 * 26 + // 26 lines w/text (ea. 24 dots high) dotFeedTime * (6 * 26 + 30)); // 26 text lines (feed 6 dots) + blank line }
// Feeds by the specified number of individual pixel rows void Adafruit_Thermal::feedRows(uint8_t rows) { writeBytes(27, 74, rows); timeoutSet(rows * dotFeedTime); }
void Adafruit_Thermal::testPage() { writeBytes(18, 84); timeoutSet( dotPrintTime * 24 * 26 + // 26 lines w/text (ea. 24 dots high) dotFeedTime * (8 * 26 + 32)); // 26 text lines (feed 8 dots) + blank line }
size_t Adafruit_Thermal::write(uint8_t c) { #else void Adafruit_Thermal::write(uint8_t c) { #endif if(c != 0x13) { // Strip carriage returns timeoutWait(); PRINTER_PRINT(c); unsigned long d = BYTE_TIME; if((c == '\n') || (column == maxColumn)) { // If newline or wrap d += (prevByte == '\n') ? ((charHeight+lineSpacing) * dotFeedTime) : // Feed line ((charHeight*dotPrintTime)+(lineSpacing*dotFeedTime)); // Text line column = 0; c = '\n'; // Treat wrap as newline on next pass } else { column++; } timeoutSet(d); prevByte = c; } #if ARDUINO >= 100 return 1; #endif } void Adafruit_Thermal::begin(int heatTime) { _printer = new SERIAL_IMPL(_RX_Pin, _TX_Pin); _printer->begin(BAUDRATE); // The printer can't start receiving data immediately upon power up -- // it needs a moment to cold boot and initialize. Allow at least 1/2 // sec of uptime before printer can receive data. timeoutSet(500000); wake(); reset(); // Description of print settings from page 23 of the manual: // ESC 7 n1 n2 n3 Setting Control Parameter Command // Decimal: 27 55 n1 n2 n3 // Set "max heating dots", "heating time", "heating interval" // n1 = 0-255 Max printing dots, Unit (8dots), Default: 7 (64 dots) // n2 = 3-255 Heating time, Unit (10us), Default: 80 (800us) // n3 = 0-255 Heating interval, Unit (10us), Default: 2 (20us) // The more max heating dots, the more peak current will cost // when printing, the faster printing speed. The max heating // dots is 8*(n1+1). The more heating time, the more density, // but the slower printing speed. If heating time is too short, // blank page may occur. The more heating interval, the more // clear, but the slower printing speed. writeBytes(27, 55); // Esc 7 (print settings) writeBytes(20); // Heating dots (20=balance of darkness vs no jams) writeBytes(heatTime); // Library default = 255 (max) writeBytes(250); // Heat interval (500 uS = slower, but darker) // Description of print density from page 23 of the manual: // DC2 # n Set printing density // Decimal: 18 35 n // D4..D0 of n is used to set the printing density. Density is // 50% + 5% * n(D4-D0) printing density. // D7..D5 of n is used to set the printing break time. Break time // is n(D7-D5)*250us. // (Unsure of the default value for either -- not documented) #define printDensity 40 // 120% (? can go higher, text is darker but fuzzy) #define printBreakTime 4 // 500 uS writeBytes(18, 35); // DC2 # (print density) writeBytes((printBreakTime << 5) | printDensity); dotPrintTime = 30000; // See comments near top of file for dotFeedTime = 2100; // an explanation of these values. }
// Feeds by the specified number of individual pixel rows void Adafruit_Thermal::feedRows(uint8_t rows) { writeBytes(ASCII_ESC, 'J', rows); timeoutSet(rows * dotFeedTime); prevByte = '\n'; column = 0; }
void Adafruit_Thermal::writeBytes(uint8_t a) { timeoutWait(); stream->write(a); timeoutSet(BYTE_TIME); }