/* * m_epson_pk2.c * * Created on: 2016年7月6日 * Author: jhting */ #include //#include "uart_comm.h" #include "m_printer.h" #include "config.h" #include #include #include #include #include "m_printCmd.h" #include "m_epson_pk2.h" #include #include "productInit.h" #define round(x) floor((x) < 0 ? (x) - 0.5 : (x) + 0.5) void escpk2_rest() { char* cmd="\x1B\x40"; KM_Printer_Print(cmd, 2); KM_Printer_Print("\x1B\x28\x55\x1\x0\10", 6); /* 10/3600 最小单位 */ /*列位置*/ KM_Printer_Print("\x1B\x6c\x0", 3); /*绝对水平位置*/ KM_Printer_Print("\x1B\x24\x0\x0", 4); } void escpk2_pos(float x, float y) { /*10/3600最小单位 * n1:15 约 0.1mm * n2:256约 18mm */ int n1 = 0, n2 = 0; int mod; /*设置绝对垂直打印位置*/ char cmd[7] = "\x1B\x28\x56\x2\x0\x0\x0"; mod = 256 / 360.00 * 25.4; n2 = y / 18; n1 = round((y - n2 * 18) / 25.40 * 360.00); cmd[5] = n1; cmd[6] = n2; //printf("y pos n1:%d, n2:%d, %f\n", n1, n2, (n1+n2*256)/360.00*25.40); KM_Printer_Print(cmd, 7); //KM_Printer_Print("\x1B\x28\x55\x1\x0\10", 6); /* 10/3600 最小单位 */ char cmd1[4] = "\x1B\x24\x0\x0"; mod = 256 / 60.00 * 25.4; n2 = x / mod; n1 = round((x - n2 * mod) / 25.40 * 60.00); //水平移位的最小单位是1/60,用指令改变不了 cmd1[2] = n1; cmd1[3] = n2; //printf("pos n1:%d, n2:%d, x:%f, y:%f\n",n1, n2, x, y); KM_Printer_Print(cmd1, 4); } void escpk2_end(float high) { int n1 = 0, n2 = 0; /*设置绝对垂直打印位置*/ char cmd[7] = "\x1B\x28\x56\x2\x0\x0\x0"; n2 = high / 18; n1 = round((high - n2 * 18) / 25.40 * 360.00); cmd[5] = n1; cmd[6] = n2; KM_Printer_Print(cmd, 7); KM_Printer_Print("\x1b\x40", 2); //KM_Printer_Print("\x1B\x28\x55\x1\x0\10", 6); /* 10/3600 最小单位 */ } void escpk2_print(char* buff, int len) { KM_Printer_Print(buff, len); } int escpk2_prepare_print_page(int checklevel) { char cmd[16] = {0}; if(KM_GetMPrinterPaperType() == BLACK_PAPER) memcpy(cmd, "\x1B\x40\x1B\x1D\x1E\x04\x31\x04\xB0\x20\x1B\x1D\x1F", 13); else if(KM_GetMPrinterPaperType() == LABLE_PAPER) memcpy(cmd, "\x1B\x40\x1B\x1D\x1E\x04\x31\x02\xB0\x10\x1B\x1D\x1F", 13); else memcpy(cmd, "\x1B\x40\x1B\x1D\x1E\x04\x31\x01\xB0\x10\x1B\x1D\x1F", 13); if (checklevel != -1) { cmd[9] = checklevel < 0x8 ? 0x10 : checklevel; } return KM_Printer_Print(cmd, 13); } int escpk2_set_paper_check(int pasend) { char cmd[] = "\x1B\x40\x1B\x1D\x1E\x04\x33\x80\x1B\x1D\x1F"; cmd[7] = cmd[7] + pasend; return KM_Printer_Print(cmd, 11); } int escpk2_end_print_page(int checklevel) { char cmd[64] = "\x0C\x1B\x1D\x1E\x04\x32\x00\xC8\x1B\x1D\x1F"; if (checklevel == 1) cmd[7] = 0xC4; else if (checklevel == 3) cmd[7] = 0xCC; else if (checklevel == 4) cmd[7] = 0xD0; return KM_Printer_Print(cmd, 11); } /* ** set print mode: 1: page 2: other ** just a temporary way. ** escpk2_set_print_mode(enum page_mode) */ int escpk2_set_print_mode(void) { char cmd[] = "\x1B\x4C"; return KM_Printer_Print(cmd, 2); } /* ** 打印方向: 00: 不翻转 01: 翻转90 ** 02: 翻转180 03: 翻转270 ** ** escpk2_set_print_direction(REVERSEMODE reversemode) */ int escpk2_set_print_direction(REVERSEMODE reversemode) { char cmd[] = "\x1B\x54\x00"; switch(reversemode) { case REVERSE_270: cmd[2] = 0x03; break; case REVERSE_180: cmd[2] = 0x02; break; case REVERSE_90: cmd[2] = 0x01; break; default: cmd[2] = 0x00; break; } return KM_Printer_Print(cmd, 3); } int escpk2_set_print_area(int high, int width, int left, int top, int reverse) { int m1 = 0, m2 = 0, temp = 0; int tmphigh = 0; unsigned char cmd[] = "\x1B\x57\x00\x00\x00\x00\x00\x00\x00\x00"; /* calculate width for print area ** (nl + nh * 256)/8 = width */ temp = width > 852? 852 : width; m2 = (temp >> 8) & 0xff; m1 = temp & 0xff; cmd[6] = m1; cmd[7] = m2; printf("calculate width m1 = %d, m2 =%d\n", m1, m2); /* calculate high for print area ** (nl + nh * 256)/8 = high */ temp = high; if (temp < 20) temp = 20; if (temp > 2400) temp = 2400; tmphigh = temp; m2 = (temp >> 8) & 0xff; m1 = temp & 0xff; cmd[8] = m1; cmd[9] = m2; /* calculate margin for print area ** 108-WIDTH = 2x/8 */ if(reverse == 2) { printf("2222reverse = %d\n", reverse); temp = (int)round(852.0 - width - left - width/20) / 2; } else { printf("11reverse = %d\n", reverse); temp = (int)round(852.0 - width - left + width/40) / 2; } if(temp < 0) temp = 0; m2 = (temp >> 8) & 0xff; m1 = temp & 0xff; cmd[2] = m1; cmd[3] = m2; printf("m1 = %d, m2 =%d\n", m1, m2); /* set top margin for print area ** default value: (nl+nh*256)/8(mm) ** 默认距离顶部1mm */ temp = (int)round(top); temp += 0; temp = temp > tmphigh ? tmphigh:temp; m2 = (temp >> 8) & 0xff; m1 = temp & 0xff; cmd[4] = m1; cmd[5] = m2; return KM_Printer_Print(cmd, 10); } int escpk2_set_pos(float x, float y) { char cmd[] = "\x1B\x24\x00\x00\x1D\x24\x00\x00"; int m1 = 0, m2 = 0, temp = 0; /* set pos x */ temp = (int)round(x); m2 = (temp >> 8) & 0xff; m1 = temp & 0xff; cmd[2] = m1; cmd[3] = m2; /* set pos y */ temp = (int)round(y); m2 = (temp >> 8) & 0xff; m1 = temp & 0xff; cmd[6] = m1; cmd[7] = m2; return KM_Printer_Print(cmd, 8); } int escpk2_set_content(char *buf, int len) { return KM_Printer_Print(buf, len); } int escpk2_set_font_size(FONTSIZE fontsize) { switch(fontsize) { case FONTSIZE_24PX: { return KM_Printer_Print(MP_FONT_SIZE24); } case FONTSIZE_48PX: { return KM_Printer_Print(MP_FONT_SIZE48); } default: { return KM_Printer_Print(MP_FONT_SIZE12); } } } int escpk2_set_emphasized_mode(FONTWEIGHT fontweight) { switch(fontweight) { case FONTWEIGHT_BOLD: { return KM_Printer_Print("\x1B\x45\x01", 3); } default: { return KM_Printer_Print("\x1B\x45\x00", 3); } } } int escpk2_set_font_underline(UNDERLINEMODE fontunderline) { switch(fontunderline) { case UNDERLINEMODE_ON: { return KM_Printer_Print("\x1B\x2D\x02\x1C\x2D\x02", 6); } default: { return KM_Printer_Print("\x1B\x2D\x00\x1C\x2D\x00", 6); } } } int escpk2_set_barcode_width(unsigned char width) { unsigned char cmd[] = "\x1D\x77\x00"; cmd[2] = (width > 6 || width < 1) ? 3 : width; KM_DEBUG("escpk2_set_barcode_width %02x\n", cmd[2]); return KM_Printer_Print(cmd, 3); } int escpk2_set_barcode_heigth(float heigth) { unsigned char cmd[] = "\x1D\x68\x00"; cmd[2] = (int)heigth; return KM_Printer_Print(cmd, 3); } void get_code128_codetype(char *buf, int length, char *cmd) { int i; for (i = 0; i < length; i++) if ((unsigned char)buf[i] <= 0x1f) { (*cmd) = 0x41; return; } (*cmd) = 0x42; } int escpk2_set_barcode(char *buf, int length, char *type) { unsigned char cmd[512] = "\x1D\x6B\x00\x00\x00\x00"; int offset = 0; offset = 2; if (strcmp(type, "CODE128") == 0) { length = length > 253 ? 253 : length; cmd[offset++] = 0x49; cmd[offset++] = length + 2; cmd[offset++] = 0x7B; get_code128_codetype(buf, length, cmd+offset); } else { length = length > 466 ? 466 : length; //临时,后续需对每种code做详细的编码检查 if (strcmp(type, "UPC-A") == 0) cmd[offset] = 0x00; else if (strcmp(type, "UPC-E") == 0) cmd[offset] = 0x01; else if (strcmp(type, "EAN13") == 0) cmd[offset] = 0x02; else if (strcmp(type, "EAN8") == 0) cmd[offset] = 0x03; else if (strcmp(type, "CODE39") == 0) cmd[offset] = 0x04; else if (strcmp(type, "ITF") == 0) cmd[offset] = 0x05; else if (strcmp(type, "CODABAR") == 0) cmd[offset] = 0x06; } offset++; memcpy(cmd + offset, buf, length * sizeof(char)); return KM_Printer_Print(cmd, length + offset + 1); } int escpk2_set_HRI_mode(int mode) { char cmd[] = "\x1D\x48\x00"; cmd[2] = mode; return KM_Printer_Print(cmd, 3); } int escpk2_set_qrcode_content(char *buf, int len) { char cmd[128] = "\x1D\x6B\x0B"; memcpy(cmd + 3, buf, len); return KM_Printer_Print(cmd, len + 4); } void escpk2_setpage(float high) { int m1 = 0, m2 = 0; int temp = 0; char page_high[7] = "\x1B\x28\x43\x1\x0\x0\x0"; KM_Printer_Print("\x1b\x40\x1B\x28\x55\x1\x0\xa", 8); /* 10/3600 最小单位 */ if(high == 280) { high = 279.4; } else if(high == 140) { #ifdef SPECIAL_535_HEIGHT high = 140; #else high = 139.7; #endif } else if(high == 69) { high = 69.85; } else if(high == 101) { high = 101.6; } //temp = high*36/2.54 if(high == 93) { temp = 1320; } else { temp = high*1800; temp = temp / 127; } m2 = (temp >> 8) & 0xff; m1 = temp & 0xff; page_high[5] = m1; page_high[6] = m2; KM_Printer_Print(page_high, 7); } void escpk2_pageend(void) { KM_Printer_Print("\x0c", 1); }