AS-289R2 library test code for mbed 2 (classic)

Dependencies:   AS289R2 mbed

Revision:
2:89363d1cba42
Parent:
1:3a6d4c7a3a73
Child:
4:c86842a92553
--- a/main.cpp	Sat Sep 03 05:22:24 2016 +0000
+++ b/main.cpp	Sun Sep 04 02:27:47 2016 +0000
@@ -1,241 +1,85 @@
-
-
+/* AS289R2 library test program
+ * Copyright (c) 2016, Toyomasa Watarai 
+ *
+ * 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.
+ */
 #include "mbed.h"
-
-
-#define AS289_FNC_ENABLE    0x31
-#define AS289_FNC_DISABLE   0x30
-
-#define AS289_KANJI_24x24   0x30
-#define AS289_KANJI_16x16   0x31
-
-#define AS289_ANK_8x16      0x30
-#define AS289_ANK_12x16     0x31
-#define AS289_ANK_16x16     0x32
-#define AS289_ANK_24x24     0x33
-#define AS289_ANK_DEFAULT   0x31
-
-#define AS289_QR_ERR_LVL_L  0x4C
-#define AS289_QR_ERR_LVL_M  0x4D
-#define AS289_QR_ERR_LVL_Q  0x51
-#define AS289_QR_ERR_LVL_H  0x48
-
-#define AS289_BCODE_UPC_A   0x30
-#define AS289_BCODE_JAN13   0x32
-#define AS289_BCODE_JAN8    0x33
-#define AS289_BCODE_CODE39  0x34
-#define AS289_BCODE_ITF     0x35
-#define AS289_BCODE_CODABAR 0x36
-
-
-class AS289R2 : public Stream {
-public:
-
-    /** Create a AS289R2 instance
-     *  which is connected to specified I2C pins with specified address
-     *
-     * @param tx I2C-bus SDA pin
-     * @param rx I2C-bus SCL pin
-     * @param baud (option) I2C-bus address (default: 0x76)
-     */
-    AS289R2(PinName tx, PinName rx, uint32_t baud = 9600);
-
-    /** Create a BME280 instance
-     *  which is connected to specified I2C pins with specified address
-     *
-     * @param i2c_obj I2C object (instance)
-     * @param slave_adr (option) I2C-bus address (default: 0x76)
-     */
-    AS289R2(Serial &serial_obj, uint32_t baud = 9600);
-
-    /** Destructor of BME280
-     */
-    virtual ~AS289R2();
-
-    /** Initializa BME280 sensor
-     *
-     *  Configure sensor setting and read parameters for calibration
-     *
-     */
-    void initialize(void);
-    void putLineFeed(uint32_t lines);
-    void clearBuffer(void);
-    void DoubleSizeHeight(uint32_t param);
-    void DoubleSizeWidth(uint32_t param);
-    void setLargeFont(uint32_t param);
-    void printQRCode(uint32_t err, char* buf);
-    void printBarCode(uint32_t code, char* param);
-    void setANKFont(uint32_t font);
-    void printBitmapImage(uint32_t cmd, uint16_t lines, char * image);
-
-private:
-    Serial      *_serial_p;
-    Serial      &_serial;
-
-protected:
-    // Stream implementation functions
-    virtual int _putc(int value);
-    virtual int _getc();
-};
-
-AS289R2::AS289R2(PinName tx, PinName rx, uint32_t baud)
-    :
-    _serial_p(new Serial(tx, tx)), 
-    _serial(*_serial_p)
-{
-    _serial.baud(baud);
-    initialize();
-}
-
-AS289R2::AS289R2(Serial &serial_obj, uint32_t baud)
-    :
-    _serial_p(NULL), 
-    _serial(serial_obj)
-{
-    _serial.baud(9600);
-    initialize();
-}
-
-AS289R2::~AS289R2()
-{
-}
-
-void AS289R2::initialize(void)
-{
-    _serial.printf("\x1B\x40");
-}
+#include "AS289R2.h"
 
-
-
-void AS289R2::putLineFeed(uint32_t lines)
-{
-    for(uint32_t i = 0; i < lines; i++) {
-        _serial.putc('\r');
-    }
-}
-
-void AS289R2::clearBuffer(void)
-{
-    _serial.putc(0x18);
-}
-
-void AS289R2::DoubleSizeHeight(uint32_t param)
-{
-    char buf[3] = {0x1B, 0x4E};
-    buf[2] = param;
-    for (uint32_t i = 0; i < sizeof(buf); i++) {
-        _serial.putc(buf[i]);
-    }
-}
-
-void AS289R2::DoubleSizeWidth(uint32_t param)
-{
-    char buf[3] = {0x1B, 0x57};
-    buf[2] = param;
-    for (uint32_t i = 0; i < sizeof(buf); i++) {
-        _serial.putc(buf[i]);
-    }
-}
-
-void AS289R2::setLargeFont(uint32_t param)
-{
-    char buf[3] = {0x1B, 0x4C};
-    buf[2] = param;
-    for (uint32_t i = 0; i < sizeof(buf); i++) {
-        _serial.putc(buf[i]);
-    }
-}
-
-void AS289R2::setANKFont(uint32_t font)
-{
-    char buf[3] = {0x1B, 0x68};
-    buf[2] = font;
-    for (uint32_t i = 0; i < sizeof(buf); i++) {
-        _serial.putc(buf[i]);
-    }
-}
-
-void AS289R2::printQRCode(uint32_t err, 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 AS289R2::printBarCode(uint32_t code, 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 AS289R2::printBitmapImage(uint32_t mode, uint16_t lines, char * 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]);
-    }
-}
-
-int AS289R2::_putc(int value) {
-    _serial.putc(value);
-    return value;
-}
- 
-int AS289R2::_getc() {
-    return -1;
-}
-
-AS289R2 tprn(D1, D0);  // tx, rx
+Serial pc(USBTX, USBRX);
+AS289R2 tp(D1);
 
 int main()
 {
-    tprn.initialize();
-    tprn.putLineFeed(2);
+    tp.initialize();
+    tp.putLineFeed(2);
+
+    tp.printf("** Thermal Printer Shield **\r\r");
+
+    tp.setDoubleSizeWidth();
+    tp.printf("  AS-289R2\r\r");
+    tp.clearDoubleSizeWidth();
 
-    // Text print
-    tprn.printf("** Thermal Printer Shield **\r");
-    tprn.DoubleSizeWidth(AS289_FNC_ENABLE);
-    tprn.printf("AS-289R2\r\r");
-    tprn.DoubleSizeWidth(AS289_FNC_DISABLE);
-    tprn.printf("日本語文字列の印字テストです。\r\r");
-    tprn.DoubleSizeHeight(AS289_FNC_DISABLE);
-    tprn.printf("0123456789\r");
-    tprn.putLineFeed(2);
+    tp.printf("日本語文字列の印字テスト:24x24\r");
+    tp.setKanjiFont(AS289R2::KANJI_16x16);
+    tp.setANKFont(AS289R2::ANK_8x16);
+    tp.printf("日本語文字列の印字テスト:16x16\r\r");
+
+    tp.setKanjiFont(AS289R2::KANJI_DEFAULT);
+    tp.setANKFont(AS289R2::ANK_DEFAULT);
+    tp.setDoubleSizeWidth();
+    tp.printf("ABCDEFG 0123456789\r");
+    tp.clearDoubleSizeWidth();
+
+    tp.setDoubleSizeHeight();
+    tp.printf("ABCDEFG 0123456789\r");
+    tp.clearDoubleSizeHeight();
+
+    tp.putLineFeed(2);
 
-    // Barcode print
-    tprn.setANKFont(AS289_ANK_8x16);
-    tprn.printf("QR\r");
-    tprn.printQRCode(AS289_QR_ERR_LVL_M, "https://developer.mbed.org");
-    tprn.putLineFeed(2);
+    tp.setANKFont(AS289R2::ANK_8x16);
+    tp.printf("8x16: Test 012345 アイウエオ\r\r");
+    tp.setANKFont(AS289R2::ANK_12x24);
+    tp.printf("12x24: Test 012345 アイウエオ\r\r");
+    tp.setANKFont(AS289R2::ANK_16x16);
+    tp.printf("16x16: Test 012345 アイウエオ\r\r");
+    tp.setANKFont(AS289R2::ANK_24x24);
+    tp.printf("24x24: Test 012345 アイウエオ\r\r");
+    tp.putLineFeed(1);
 
-    tprn.printf("UPC-A\r");
-    tprn.printBarCode(AS289_BCODE_UPC_A, "01234567890");
-    tprn.putLineFeed(6);
+    tp.setANKFont(AS289R2::ANK_8x16);
+    tp.printf("QR\r");
+    tp.printQRCode(AS289R2::QR_ERR_LVL_M, "https://developer.mbed.org/components/AS-289R2/");
+    tp.printf("\rhttps://developer.mbed.org/components/AS-289R2\r");
+    tp.putLineFeed(2);
+
+    tp.printf("UPC-A\r");
+    tp.printBarCode(AS289R2::BCODE_UPC_A, "01234567890");
+    tp.putLineFeed(4);
 
-    while(1);
+    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);
+        }
+    }
+
 }