Toyomasa Watarai
/
AS-289R2_Hello-World
AS-289R2 library test code for mbed 2 (classic)
main.cpp@0:c407b8539f80, 2016-09-03 (annotated)
- Committer:
- MACRUM
- Date:
- Sat Sep 03 04:33:46 2016 +0000
- Revision:
- 0:c407b8539f80
- Child:
- 1:3a6d4c7a3a73
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:c407b8539f80 | 1 | |
MACRUM | 0:c407b8539f80 | 2 | |
MACRUM | 0:c407b8539f80 | 3 | #include "mbed.h" |
MACRUM | 0:c407b8539f80 | 4 | |
MACRUM | 0:c407b8539f80 | 5 | Serial _serial(D1, D0); // tx, rx |
MACRUM | 0:c407b8539f80 | 6 | |
MACRUM | 0:c407b8539f80 | 7 | #define AS289_FNC_ENABLE 0x31 |
MACRUM | 0:c407b8539f80 | 8 | #define AS289_FNC_DISABLE 0x30 |
MACRUM | 0:c407b8539f80 | 9 | |
MACRUM | 0:c407b8539f80 | 10 | #define AS289_KANJI_24x24 0x30 |
MACRUM | 0:c407b8539f80 | 11 | #define AS289_KANJI_16x16 0x31 |
MACRUM | 0:c407b8539f80 | 12 | |
MACRUM | 0:c407b8539f80 | 13 | #define AS289_ANK_8x16 0x30 |
MACRUM | 0:c407b8539f80 | 14 | #define AS289_ANK_12x16 0x31 |
MACRUM | 0:c407b8539f80 | 15 | #define AS289_ANK_16x16 0x32 |
MACRUM | 0:c407b8539f80 | 16 | #define AS289_ANK_24x24 0x33 |
MACRUM | 0:c407b8539f80 | 17 | #define AS289_ANK_DEFAULT 0x31 |
MACRUM | 0:c407b8539f80 | 18 | |
MACRUM | 0:c407b8539f80 | 19 | #define AS289_QR_ERR_LVL_L 0x4C |
MACRUM | 0:c407b8539f80 | 20 | #define AS289_QR_ERR_LVL_M 0x4D |
MACRUM | 0:c407b8539f80 | 21 | #define AS289_QR_ERR_LVL_Q 0x51 |
MACRUM | 0:c407b8539f80 | 22 | #define AS289_QR_ERR_LVL_H 0x48 |
MACRUM | 0:c407b8539f80 | 23 | |
MACRUM | 0:c407b8539f80 | 24 | #define AS289_BCODE_UPC_A 0x30 |
MACRUM | 0:c407b8539f80 | 25 | #define AS289_BCODE_JAN13 0x32 |
MACRUM | 0:c407b8539f80 | 26 | #define AS289_BCODE_JAN8 0x33 |
MACRUM | 0:c407b8539f80 | 27 | #define AS289_BCODE_CODE39 0x34 |
MACRUM | 0:c407b8539f80 | 28 | #define AS289_BCODE_ITF 0x35 |
MACRUM | 0:c407b8539f80 | 29 | #define AS289_BCODE_CODABAR 0x36 |
MACRUM | 0:c407b8539f80 | 30 | |
MACRUM | 0:c407b8539f80 | 31 | void LineFeed(uint32_t lines); |
MACRUM | 0:c407b8539f80 | 32 | void Init(void); |
MACRUM | 0:c407b8539f80 | 33 | void ClearBuffer(void); |
MACRUM | 0:c407b8539f80 | 34 | void DoubleSizeHeight(uint32_t param); |
MACRUM | 0:c407b8539f80 | 35 | void DoubleSizeWidth(uint32_t param); |
MACRUM | 0:c407b8539f80 | 36 | void SetLargeFont(uint32_t param); |
MACRUM | 0:c407b8539f80 | 37 | void PrintQRCode(uint32_t err, char* buf); |
MACRUM | 0:c407b8539f80 | 38 | void PrintBarCode(uint32_t code, char* param); |
MACRUM | 0:c407b8539f80 | 39 | void SetANKFont(uint32_t font); |
MACRUM | 0:c407b8539f80 | 40 | void PrintBitmapImage(uint32_t cmd, uint32_t lines, char * image); |
MACRUM | 0:c407b8539f80 | 41 | |
MACRUM | 0:c407b8539f80 | 42 | |
MACRUM | 0:c407b8539f80 | 43 | void LineFeed(uint32_t lines) |
MACRUM | 0:c407b8539f80 | 44 | { |
MACRUM | 0:c407b8539f80 | 45 | for(uint32_t i = 0; i < lines; i++) { |
MACRUM | 0:c407b8539f80 | 46 | _serial.putc('\r'); |
MACRUM | 0:c407b8539f80 | 47 | } |
MACRUM | 0:c407b8539f80 | 48 | } |
MACRUM | 0:c407b8539f80 | 49 | |
MACRUM | 0:c407b8539f80 | 50 | void Init(void) |
MACRUM | 0:c407b8539f80 | 51 | { |
MACRUM | 0:c407b8539f80 | 52 | _serial.printf("\x1B\x40"); |
MACRUM | 0:c407b8539f80 | 53 | } |
MACRUM | 0:c407b8539f80 | 54 | |
MACRUM | 0:c407b8539f80 | 55 | void ClearBuffer(void) |
MACRUM | 0:c407b8539f80 | 56 | { |
MACRUM | 0:c407b8539f80 | 57 | _serial.putc(0x18); |
MACRUM | 0:c407b8539f80 | 58 | } |
MACRUM | 0:c407b8539f80 | 59 | |
MACRUM | 0:c407b8539f80 | 60 | void DoubleSizeHeight(uint32_t param) |
MACRUM | 0:c407b8539f80 | 61 | { |
MACRUM | 0:c407b8539f80 | 62 | char buf[3] = {0x1B, 0x4E}; |
MACRUM | 0:c407b8539f80 | 63 | buf[2] = param; |
MACRUM | 0:c407b8539f80 | 64 | for (uint32_t i = 0; i < sizeof(buf); i++) { |
MACRUM | 0:c407b8539f80 | 65 | _serial.putc(buf[i]); |
MACRUM | 0:c407b8539f80 | 66 | } |
MACRUM | 0:c407b8539f80 | 67 | } |
MACRUM | 0:c407b8539f80 | 68 | |
MACRUM | 0:c407b8539f80 | 69 | void DoubleSizeWidth(uint32_t param) |
MACRUM | 0:c407b8539f80 | 70 | { |
MACRUM | 0:c407b8539f80 | 71 | char buf[3] = {0x1B, 0x57}; |
MACRUM | 0:c407b8539f80 | 72 | buf[2] = param; |
MACRUM | 0:c407b8539f80 | 73 | for (uint32_t i = 0; i < sizeof(buf); i++) { |
MACRUM | 0:c407b8539f80 | 74 | _serial.putc(buf[i]); |
MACRUM | 0:c407b8539f80 | 75 | } |
MACRUM | 0:c407b8539f80 | 76 | } |
MACRUM | 0:c407b8539f80 | 77 | |
MACRUM | 0:c407b8539f80 | 78 | void SetLargeFont(uint32_t param) |
MACRUM | 0:c407b8539f80 | 79 | { |
MACRUM | 0:c407b8539f80 | 80 | char buf[3] = {0x1B, 0x4C}; |
MACRUM | 0:c407b8539f80 | 81 | buf[2] = param; |
MACRUM | 0:c407b8539f80 | 82 | for (uint32_t i = 0; i < sizeof(buf); i++) { |
MACRUM | 0:c407b8539f80 | 83 | _serial.putc(buf[i]); |
MACRUM | 0:c407b8539f80 | 84 | } |
MACRUM | 0:c407b8539f80 | 85 | } |
MACRUM | 0:c407b8539f80 | 86 | |
MACRUM | 0:c407b8539f80 | 87 | void SetANKFont(uint32_t font) |
MACRUM | 0:c407b8539f80 | 88 | { |
MACRUM | 0:c407b8539f80 | 89 | char buf[3] = {0x1B, 0x68}; |
MACRUM | 0:c407b8539f80 | 90 | buf[2] = font; |
MACRUM | 0:c407b8539f80 | 91 | for (uint32_t i = 0; i < sizeof(buf); i++) { |
MACRUM | 0:c407b8539f80 | 92 | _serial.putc(buf[i]); |
MACRUM | 0:c407b8539f80 | 93 | } |
MACRUM | 0:c407b8539f80 | 94 | } |
MACRUM | 0:c407b8539f80 | 95 | |
MACRUM | 0:c407b8539f80 | 96 | void PrintQRCode(uint32_t err, char* param) |
MACRUM | 0:c407b8539f80 | 97 | { |
MACRUM | 0:c407b8539f80 | 98 | uint32_t len = strlen(param); |
MACRUM | 0:c407b8539f80 | 99 | char buf[4] = {0x1D, 0x78}; |
MACRUM | 0:c407b8539f80 | 100 | buf[2] = err; |
MACRUM | 0:c407b8539f80 | 101 | buf[3] = len; |
MACRUM | 0:c407b8539f80 | 102 | for (uint32_t i = 0; i < sizeof(buf); i++) { |
MACRUM | 0:c407b8539f80 | 103 | _serial.putc(buf[i]); |
MACRUM | 0:c407b8539f80 | 104 | } |
MACRUM | 0:c407b8539f80 | 105 | for (uint32_t i = 0; i < len; i++) { |
MACRUM | 0:c407b8539f80 | 106 | _serial.putc(param[i]); |
MACRUM | 0:c407b8539f80 | 107 | } |
MACRUM | 0:c407b8539f80 | 108 | } |
MACRUM | 0:c407b8539f80 | 109 | |
MACRUM | 0:c407b8539f80 | 110 | void PrintBarCode(uint32_t code, char* param) |
MACRUM | 0:c407b8539f80 | 111 | { |
MACRUM | 0:c407b8539f80 | 112 | char buf[3] = {0x1D, 0x6B}; |
MACRUM | 0:c407b8539f80 | 113 | buf[2] = code; |
MACRUM | 0:c407b8539f80 | 114 | for (uint32_t i = 0; i < sizeof(buf); i++) { |
MACRUM | 0:c407b8539f80 | 115 | _serial.putc(buf[i]); |
MACRUM | 0:c407b8539f80 | 116 | } |
MACRUM | 0:c407b8539f80 | 117 | for (uint32_t i = 0; i < strlen(param); i++) { |
MACRUM | 0:c407b8539f80 | 118 | _serial.putc(param[i]); |
MACRUM | 0:c407b8539f80 | 119 | } |
MACRUM | 0:c407b8539f80 | 120 | _serial.putc('\0'); |
MACRUM | 0:c407b8539f80 | 121 | } |
MACRUM | 0:c407b8539f80 | 122 | |
MACRUM | 0:c407b8539f80 | 123 | void PrintBitmapImage(uint32_t mode, uint16_t lines, char * image) |
MACRUM | 0:c407b8539f80 | 124 | { |
MACRUM | 0:c407b8539f80 | 125 | char buf[3] = {0x1C, 0x2A}; |
MACRUM | 0:c407b8539f80 | 126 | buf[2] = mode; |
MACRUM | 0:c407b8539f80 | 127 | for (uint32_t i = 0; i < sizeof(buf); i++) { |
MACRUM | 0:c407b8539f80 | 128 | _serial.putc(buf[i]); |
MACRUM | 0:c407b8539f80 | 129 | } |
MACRUM | 0:c407b8539f80 | 130 | _serial.putc((lines >> 8) & 0xFF); // n1 |
MACRUM | 0:c407b8539f80 | 131 | _serial.putc((lines >> 0) & 0xFF); // n2 |
MACRUM | 0:c407b8539f80 | 132 | |
MACRUM | 0:c407b8539f80 | 133 | if (mode == 0x61) { |
MACRUM | 0:c407b8539f80 | 134 | return; |
MACRUM | 0:c407b8539f80 | 135 | } |
MACRUM | 0:c407b8539f80 | 136 | |
MACRUM | 0:c407b8539f80 | 137 | for (uint32_t i = 0; i < (48 * lines); i++) { |
MACRUM | 0:c407b8539f80 | 138 | _serial.putc(image[i]); |
MACRUM | 0:c407b8539f80 | 139 | } |
MACRUM | 0:c407b8539f80 | 140 | } |
MACRUM | 0:c407b8539f80 | 141 | |
MACRUM | 0:c407b8539f80 | 142 | int main() |
MACRUM | 0:c407b8539f80 | 143 | { |
MACRUM | 0:c407b8539f80 | 144 | Init(); |
MACRUM | 0:c407b8539f80 | 145 | LineFeed(2); |
MACRUM | 0:c407b8539f80 | 146 | |
MACRUM | 0:c407b8539f80 | 147 | // Text print |
MACRUM | 0:c407b8539f80 | 148 | _serial.printf("** Thermal Printer Shield **\r"); |
MACRUM | 0:c407b8539f80 | 149 | DoubleSizeWidth(AS289_FNC_ENABLE); |
MACRUM | 0:c407b8539f80 | 150 | _serial.printf("AS-289R2\r\r"); |
MACRUM | 0:c407b8539f80 | 151 | DoubleSizeWidth(AS289_FNC_DISABLE); |
MACRUM | 0:c407b8539f80 | 152 | _serial.printf("日本語文字列の印字テストです。\r\r"); |
MACRUM | 0:c407b8539f80 | 153 | DoubleSizeHeight(AS289_FNC_DISABLE); |
MACRUM | 0:c407b8539f80 | 154 | _serial.printf("0123456789\r"); |
MACRUM | 0:c407b8539f80 | 155 | LineFeed(2); |
MACRUM | 0:c407b8539f80 | 156 | |
MACRUM | 0:c407b8539f80 | 157 | // Barcode print |
MACRUM | 0:c407b8539f80 | 158 | SetANKFont(AS289_ANK_8x16); |
MACRUM | 0:c407b8539f80 | 159 | _serial.printf("QR\r"); |
MACRUM | 0:c407b8539f80 | 160 | PrintQRCode(AS289_QR_ERR_LVL_M, "https://developer.mbed.org"); |
MACRUM | 0:c407b8539f80 | 161 | LineFeed(2); |
MACRUM | 0:c407b8539f80 | 162 | |
MACRUM | 0:c407b8539f80 | 163 | _serial.printf("UPC-A\r"); |
MACRUM | 0:c407b8539f80 | 164 | PrintBarCode(AS289_BCODE_UPC_A, "01234567890"); |
MACRUM | 0:c407b8539f80 | 165 | LineFeed(6); |
MACRUM | 0:c407b8539f80 | 166 | |
MACRUM | 0:c407b8539f80 | 167 | while(1); |
MACRUM | 0:c407b8539f80 | 168 | } |