Thermal Printer Basic Print Operations

Dependents:   Thermal_Printer

Files at this revision

API Documentation at this revision

Comitter:
shivanandgowdakr
Date:
Mon May 21 07:27:19 2018 +0000
Child:
1:8372894bfc19
Commit message:
Thermal Printer APSEPM207LV Basic Print Operations ;

Changed in this revision

APSEPM207LV.cpp Show annotated file Show diff for this revision Revisions of this file
APSEPM207LV.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/APSEPM207LV.cpp	Mon May 21 07:27:19 2018 +0000
@@ -0,0 +1,357 @@
+
+
+#include "mbed.h"
+#include "APSEPM207LV.h"
+
+APSEPM207LV::APSEPM207LV(PinName tx, PinName rx,uint32_t baud)
+    :
+    _serial_p(new Serial(tx, rx)),
+    _serial(*_serial_p)
+{
+    _serial.baud(baud);
+    initialize();
+}
+
+APSEPM207LV::APSEPM207LV(Serial &serial_obj, uint32_t baud)
+    :
+    _serial_p(NULL),
+    _serial(serial_obj)
+{
+    _serial.baud(baud);
+    initialize();
+}
+
+APSEPM207LV::~APSEPM207LV()
+{
+}
+
+
+void APSEPM207LV::initialize(void)
+{
+    _serial.putc(0x1B);
+    _serial.putc(0x40);
+}
+
+void APSEPM207LV::printTest(void)
+{
+    _serial.putc(0x1D);
+    _serial.putc(0x28);
+    _serial.putc(0x41);
+}
+
+
+void APSEPM207LV::lineFeed(void)
+{
+    _serial.putc(0x0A);
+  
+}
+
+
+void APSEPM207LV::carriageReturn(void)
+{
+    _serial.putc(0x0D);
+}
+
+
+
+void APSEPM207LV::putLineFeed(uint8_t lines)
+{
+    for(uint32_t i = 0; i < lines; i++) {
+        _serial.putc('\r');
+    }
+}
+
+void APSEPM207LV::printnputLineFeed(uint8_t lines)
+{
+   _serial.putc(0x1B);
+   _serial.putc(0x64);
+  _serial.putc(lines);
+  
+  }
+
+void APSEPM207LV::setdefaultLinespacing(void)
+{
+   _serial.putc(0x1B);
+   _serial.putc(0x32);
+  _serial.putc(0x32);
+  
+  }
+  
+void APSEPM207LV::setLinespacing(uint8_t lines)
+{
+  uint8_t num=32;
+  if(lines==1)
+  num=32;
+  else if(lines==2)
+  num=64;
+  else if(lines==3)
+  num=96;
+  
+  
+   _serial.putc(0x1B);
+   _serial.putc(0x33);
+  _serial.putc(num);
+  
+  }
+
+  void APSEPM207LV::setleftMargin(uint8_t n1, uint8_t n2)
+  {
+   _serial.putc(0x1D);
+   _serial.putc(0x4C);
+  _serial.putc(n1);
+  _serial.putc(n2);
+  }
+
+ void APSEPM207LV::absoluteprintPosition(uint8_t n1, uint8_t n2)
+  {
+   _serial.putc(0x1B);
+   _serial.putc(0x24);
+  _serial.putc(n1);
+  _serial.putc(n2);
+  }
+
+void APSEPM207LV::whiteblack(uint8_t n1)
+  {
+   _serial.putc(0x1D);
+   _serial.putc(0x42);
+  _serial.putc(n1);// n1=0   for OFF n1=1 for ON
+ 
+  }
+
+  void APSEPM207LV::papersensorstatus(void)
+  {
+  _serial.putc(0x1B);
+   _serial.putc(0x76);
+
+  // Poll Rx pin of printer Here
+  //0x00 Paper Present
+  //0x20 Paper Absent
+  }
+  
+
+  
+void APSEPM207LV::clearBuffer(void)
+{
+    _serial.putc(0x18);
+}
+
+void APSEPM207LV::putHorizontaltab(void)
+{
+    _serial.putc(0x09);
+}
+
+void APSEPM207LV::setDoubleSizeHeight(void)
+{
+    _serial.printf("\x1D\x21\x01");
+}
+
+void APSEPM207LV::clearDoubleSizeHeight(void)
+{
+    _serial.printf("\x1D\x21\x00");
+}
+
+void APSEPM207LV::setDoubleSizeWidth(void)
+{
+    _serial.printf("\x1D\x21\x10");
+}
+
+void APSEPM207LV::clearDoubleSizeWidth(void)
+{
+        _serial.printf("\x1D\x21\x00");
+}
+
+void APSEPM207LV::setLargeFont(void)
+{
+    _serial.printf("\x1D\x21\x11");
+}
+
+void APSEPM207LV::clearLargeFont()
+{
+    _serial.printf("\x1D\x21\x00");
+}
+
+void APSEPM207LV::setANKFont(uint32_t font)
+{
+    _serial.putc(0x1B);
+    _serial.putc(0x68);
+    _serial.putc(font);
+}
+
+void APSEPM207LV::setKanjiFont(uint32_t font)
+{
+    _serial.putc(0x12);
+    _serial.putc(0x53);
+    _serial.putc(font);
+}
+
+void APSEPM207LV::printQRCode(uint32_t err, const char* param)
+{
+    uint32_t len = strlen(param);
+    char buf[4] = {0x1D, 0x78};
+    buf[2] = err;
+    buf[3] = len;
+    for (uint32_t i = 0; i < sizeof(buf); i++) {
+        _serial.putc(buf[i]);
+    }
+    for (uint32_t i = 0; i < len; i++) {
+        _serial.putc(param[i]);
+    }
+}
+
+void APSEPM207LV::printBarCode(uint32_t code, const char* param)
+{
+    char buf[3] = {0x1D, 0x6B};
+    buf[2] = code;
+    for (uint32_t i = 0; i < sizeof(buf); i++) {
+        _serial.putc(buf[i]);
+    }
+    for (uint32_t i = 0; i < strlen(param); i++) {
+        _serial.putc(param[i]);
+    }
+    _serial.putc('\0');
+}
+
+void APSEPM207LV::printBitmapImage(uint32_t mode, uint16_t lines, const uint8_t * image)
+{
+    char buf[3] = {0x1C, 0x2A};
+    buf[2] = mode;
+    for (uint32_t i = 0; i < sizeof(buf); i++) {
+        _serial.putc(buf[i]);
+    }
+    _serial.putc((lines >> 8) & 0xFF); // n1
+    _serial.putc((lines >> 0) & 0xFF); // n2
+
+    if (mode == 0x61) {
+        return;
+    }
+
+    for (uint32_t i = 0; i < (48 * lines); i++) {
+        _serial.putc(image[i]);
+    }
+}
+
+void APSEPM207LV::setLineSpaceing(uint32_t space)
+{
+    _serial.putc(0x1B);
+    _serial.putc(0x33);
+    _serial.putc(space);
+}
+
+void APSEPM207LV::defaultLineSpaceing()
+{
+    _serial.printf("\x1B\x33\x04");
+}
+
+void APSEPM207LV::setPrintDirection(uint32_t direction)
+{
+    _serial.putc(0x1B);
+    _serial.putc(0x49);
+    _serial.putc(direction);
+}
+
+void APSEPM207LV::putPaperFeed(uint32_t space)
+{
+    _serial.putc(0x1B);
+    _serial.putc(0x4A);
+    _serial.putc(space);
+}
+
+void APSEPM207LV::setInterCharacterSpace(uint32_t space)
+{
+    _serial.putc(0x1B);
+    _serial.putc(0x20);
+    _serial.putc(space);
+}
+
+void APSEPM207LV::defaultInterCharacterSpace()
+{
+    _serial.printf("\x1B\x20\x01");
+}
+
+void APSEPM207LV::putPrintPosition(uint32_t position)
+{
+    _serial.putc(0x1B);
+    _serial.putc(0x6c);
+    _serial.putc(position);
+}
+
+void APSEPM207LV::setScript(script_mode script)
+{
+    _serial.putc(0x1B);
+    _serial.putc(0x73);
+    _serial.putc(script);
+}
+
+void APSEPM207LV::clearScript()
+{
+    _serial.printf("\x1B\x73\x30");
+}
+
+void APSEPM207LV::setQuadrupleSize()
+{
+    _serial.printf("\x1C\x57\x31");
+}
+
+void APSEPM207LV::clearQuadrupleSize()
+{
+    _serial.printf("\x1C\x57\x30");
+}
+
+void APSEPM207LV::setEnlargement(uint32_t width, uint32_t height)
+{
+    _serial.putc(0x1C);
+    _serial.putc(0x65);
+    _serial.putc(width);
+    _serial.putc(height);
+}
+
+void APSEPM207LV::clearEnlargement()
+{
+    _serial.printf("\x1C\x65\x31\x31");
+}
+
+void APSEPM207LV::setBarCodeHeight(uint32_t height)
+{
+    _serial.putc(0x1D);
+    _serial.putc(0x68);
+    _serial.putc(height);
+}
+
+void APSEPM207LV::defaultBarCodeHeight()
+{
+    _serial.printf("\x1D\x68\x50");
+}
+
+void APSEPM207LV::setBarCodeBarSize(uint32_t narrowbar, uint32_t widebar)
+{
+    _serial.putc(0x1D);
+    _serial.putc(0x77);
+    _serial.putc(narrowbar);
+    _serial.putc(widebar);
+}
+
+void APSEPM207LV::defaultBarCodeBarSize()
+{
+    _serial.printf("\x1D\x77\x02\x05");
+}
+
+int APSEPM207LV::_putc(int value)
+{
+    _serial.putc(value);
+    return value;
+}
+
+int APSEPM207LV::_getc()
+{
+    return -1;
+}
+
+
+//00 = 32 Char Normal                                        08 = 32 Char BOLD
+//01 = 24 Char Normal                                        09 = 24 Char BOLD
+//11 = 24 Char Double Height                                 19 = 24 Char Double Height & BOLD
+//21 = 24 Char Double Width                                  29 = 24 Char Double Width & BOLD
+//31 = 24 Char Double Height & Double Width                  39 = 24 Char Double Height & Double Width & BOLD
+//10 = 32 Char Double Height                                 18 = 32 Char Double Height & BOLD
+//20 = 32 Char Double Width                                  28 = 32 Char Double Width & BOLD
+//30 = 32 Char Double Height & Double Width                  38 = 32 Char Double Height & Double Width & BOLD
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/APSEPM207LV.h	Mon May 21 07:27:19 2018 +0000
@@ -0,0 +1,395 @@
+/**
+ ******************************************************************************
+ * @file    APSEPM207LV.h
+ * @author  Shivanand Gowda 
+ * @version V1.0.0
+ * @date    11 March 2017
+ * @brief   This file contains the class of a APSEPM207LV thermal control component
+ ******************************************************************************
+ * @attention
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MBED_APSEPM207LV_H
+#define MBED_APSEPM207LV_H
+
+#include "mbed.h"
+
+#if defined(__CC_ARM)
+// To avoid "invalid multibyte character sequence" warning
+#pragma diag_suppress 870
+#endif
+
+/**  A printer interface for driving AS-289R2 thermal printer shield of NADA Electronics, Ltd.
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "AS829R2.h"
+ *
+ * Serial pc(USBTX, USBRX);
+ * AS829R2 tp(D1); // tx, 9600bps
+ *
+ * int main()
+ * {
+ *     tp.initialize();
+ *     tp.putLineFeed(2);
+ *
+ *     tp.printf("** Thermal Printer Shield **\r\r");
+ *
+ *     tp.setDoubleSizeWidth();
+ *     tp.printf("  AS-289R2\r\r");
+ *     tp.clearDoubleSizeWidth();
+ *
+ *     tp.printf("日本語文字列の印字テスト:24x24\r");
+ *     tp.setKanjiFont(APSEPM207LV::KANJI_16x16);
+ *     tp.setANKFont(APSEPM207LV::ANK_8x16);
+ *     tp.printf("日本語文字列の印字テスト:16x16\r\r");
+ *
+ *     tp.setKanjiFont(APSEPM207LV::KANJI_DEFAULT);
+ *     tp.setANKFont(APSEPM207LV::ANK_DEFAULT);
+ *     tp.setDoubleSizeWidth();
+ *     tp.printf("ABCDEFG 0123456789\r");
+ *     tp.clearDoubleSizeWidth();
+ *
+ *     tp.setDoubleSizeHeight();
+ *     tp.printf("ABCDEFG 0123456789\r");
+ *     tp.clearDoubleSizeHeight();
+ *
+ *     pc.printf("AS-289R2 thermal printer shield test\n\n");
+ *     while(1) {
+ *         if (pc.readable()) {
+ *             int c = pc.getc();
+ *             pc.putc(c);
+ *             tp.putc(c);
+ *         }
+ *     }
+ * }
+ * 
+ * @endcode
+ */
+class APSEPM207LV : public Stream
+{
+public:
+
+    /**
+     * @enum Kanji_font_size
+     * Value of Japanese Kanji font size
+     */
+    enum Kanji_font_size {
+        //! 24x24 dot font
+        KANJI_24x24 = 0x30,
+        //! 16x16 dot font
+        KANJI_16x16,
+        //! Defalut font size
+        KANJI_DEFAULT = KANJI_24x24
+    };
+
+    /**
+     * @enum ANK_font_size
+     * Value of ANK font size
+     */
+    enum ANK_font_size {
+        //! 8x16 dot font
+        ANK_8x16 = 0x30,
+        //! 12x24 dot font
+        ANK_12x24,
+        //! 16x16 dot font
+        ANK_16x16,
+        //! 24x24 dot fot
+        ANK_24x24,
+        //! Defalut font size
+        ANK_DEFAULT = ANK_12x24
+    };
+
+    /**
+     * @enum QRcode_error_level
+     * Value of CQ code error correction level
+     */
+    enum QRcode_error_level {
+        //! Error correction level L (7%)
+        QR_ERR_LVL_L = 0x4C,
+        //! Error correction level M (15%)
+        QR_ERR_LVL_M = 0x4D,
+        //! Error correction level Q (25%)
+        QR_ERR_LVL_Q = 0x51,
+        //! Error correction level H (30%)
+        QR_ERR_LVL_H = 0x48
+    };
+
+    /**
+     * @enum barcode_mode
+     * Value of barcode mode
+     */
+    enum barcode_mode {
+        //!  UPC-A : 11-digit, d1-d11, C/D
+        BCODE_UPC_A = 0x30,
+        //! JAN13 : 12-digit, d1-d12, C/D
+        BCODE_JAN13 = 0x32,
+        //! JAN8 : 7-digit, d1-d7, C/D
+        BCODE_JAN8,
+        //! CODE39 : variable, d1-d20, C/D
+        BCODE_CODE39,
+        //! ITF : variable, d1-d20
+        BCODE_ITF,
+        //! CODABAR (NW7) : variable, d1-d20
+        BCODE_CODABAR
+    };
+    
+    /**
+     * @enum script_mode
+     * Value of script mode
+     */
+    enum script_mode {
+        //! Cancel script mode
+        SCRIPT_CANCEL = 0,
+        //! Super script
+        SCRIPT_SUPER,
+        //! Sub script
+        SCRIPT_SUB
+    };
+
+    /** Create a APSEPM207LV instance
+     *  which is connected to specified Serial pin with specified baud rate
+     *
+     * @param tx Serial TX pin
+     * @param baud (option) serial baud rate (default: 9600bps)
+     */
+    APSEPM207LV(PinName tx,PinName rx, uint32_t baud = 9600);
+
+    /** Create a APSEPM207LV instance
+     *  which is connected to specified Serial instance with specified baud rate
+     *
+     * @param serial_obj Serial object (instance)
+     * @param baud (option) serial baud rate (default: 9600bps)
+     */
+    APSEPM207LV(Serial &serial_obj, uint32_t baud = 9600);
+
+    /** Destructor of APSEPM207LV
+     */
+    virtual ~APSEPM207LV();
+
+    /** Initializa APSEPM207LV
+     *
+     *  Issues initialize command for AS-289R2
+     *
+     */
+    void initialize(void);
+
+    /** Send line feed code
+     *  which is connected to specified Serial pin with specified baud rate
+     *
+     * @param lines Number of line feed
+     */
+     void printTest(void);
+     void printnputLineFeed(uint8_t lines);
+     void lineFeed(void);
+     void putHorizontaltab(void);
+
+     void setdefaultLinespacing(void);
+     void setLinespacing(uint8_t lines);
+     void setleftMargin(uint8_t n1, uint8_t n2);
+     void absoluteprintPosition(uint8_t n1, uint8_t n2);
+     void whiteblack(uint8_t n1);
+    void papersensorstatus(void);
+    void carriageReturn(void);
+     /** pur Horizontal Tab
+     *
+     */
+    
+    void putLineFeed(uint8_t lines);
+
+    /** Clear image buffer of the AS-289R2
+     *
+     */
+    void clearBuffer(void);
+
+    /** Set double height size font
+     *
+     */
+    void setDoubleSizeHeight(void);
+
+    /** Set normal height size font
+     *
+     */
+    void clearDoubleSizeHeight(void);
+
+    /** Set double width size font
+     *
+     */
+    void setDoubleSizeWidth(void);
+
+    /** Set normal width size font
+     *
+     */
+    void clearDoubleSizeWidth(void);
+
+    /** Set large size font (48x96)
+     *
+     */
+    void setLargeFont(void);
+
+    /** Set normal size font
+     *
+     */
+    void clearLargeFont(void);
+
+    /** Set ANK font
+     *
+     * @param font ANK font e.g. APSEPM207LV::ANK_8x16
+     */
+    void setANKFont(uint32_t font);
+
+    /** Set Kanji font size
+     *
+     * @param font Kanji font e.g. APSEPM207LV::KANJI_16x16
+     */
+    void setKanjiFont(uint32_t font);
+
+    /** Print QR code
+     *
+     * @param err QR code error correction level e.g. APSEPM207LV::QR_ERR_LVL_M
+     * @param buf Data to be printed
+     */
+    void printQRCode(uint32_t err, const char* buf);
+
+    /** Print Bar code
+     *
+     * @param code Type of Bar code e.g. APSEPM207LV::JAN13
+     * @param buf Data to be printed
+     */
+    void printBarCode(uint32_t code, const char* param);
+
+    /** Print bitmap image
+     *
+     * @param cmd Type of operation mode, 0x61: print image buffer, 0x62: register image buffer, 0x63: register -> print, 0x64: print -> register, 0x65: line print
+     * @param lines Number of print line
+     * @param image Data to be printed
+     */
+    void printBitmapImage(uint32_t cmd, uint16_t lines, const uint8_t * image);
+
+    /** Set Line Spaceing
+     *
+     * @param space line spacing
+     */
+    void setLineSpaceing(uint32_t space);
+
+    /** Set as default Line Spaceing
+     *
+     */
+    void defaultLineSpaceing(void);
+
+    /** Set Print Direction
+     *
+     * @param direction Print direction, 0: lister, 1: texter
+     */
+    void setPrintDirection(uint32_t direction);
+
+    /** Send feed code
+     *
+     * @param space Paper feed
+     */
+    void putPaperFeed(uint32_t space);
+
+    /** Set Inter Character Space
+     *
+     * @param space inter-character space
+     */
+    void setInterCharacterSpace(uint32_t space);
+
+    /** Set as default Inter Character Space
+     *
+     */
+    void defaultInterCharacterSpace(void);
+
+    /** Send Print Position
+     *
+     * @param position Print position
+     */
+    void putPrintPosition(uint32_t position);
+
+    /** Set Script
+     *
+     * @param script mode e.g. APSEPM207LV::SCRIPT_SUPER
+     */
+    void setScript(script_mode script);
+
+    /** Clear Script
+     *
+     */
+    void clearScript(void);
+
+    /** Set Quadruple size
+     *
+     */
+    void setQuadrupleSize(void);
+
+    /** Clear Quadruple size
+     *
+     */
+    void clearQuadrupleSize(void);
+
+    /** Set Enlargement size
+     *
+     * @param width enlargement
+     * @param height enlargement
+     */
+    void setEnlargement(uint32_t width, uint32_t height);
+
+    /** Clear Enlargement size
+     *
+     */
+    void clearEnlargement(void);
+
+    /** Set BarCode Height size
+     *
+     * @param height Bar height
+     */
+    void setBarCodeHeight(uint32_t height);
+
+    /** Set as default BarCode Height size
+     *
+     */
+    void defaultBarCodeHeight(void);
+
+    /** Set BarCode Bar size
+     *
+     * @param narrowbar narrow bars size
+     * @param widebar wide bars size
+     */
+    void setBarCodeBarSize(uint32_t narrowbar, uint32_t widebar);
+
+    /** Set as default BarCode Bar size
+     *
+     */
+    void defaultBarCodeBarSize(void);
+
+private:
+    Serial *_serial_p;
+    Serial &_serial;
+
+protected:
+    // Stream implementation functions
+    virtual int _putc(int value);
+    virtual int _getc();
+};
+
+#endif
+
+