import { Buffer } from 'buffer'; import { Align, Style, Cut, Drawer } from '../actions'; import { Profile } from '.'; export default class RPPrinter extends Profile { async feed(lines: number): Promise { if (lines > 1) { const count = Math.trunc(lines / 255); let cmd = ('\x1Bd' + String.fromCharCode(Math.min(lines, 255))).repeat( count, ); const remaining = lines - count * 255; if (remaining > 0) { cmd += '\x1Bd' + String.fromCharCode(remaining); } return this.connection.write(Buffer.from(cmd)); } else { return this.connection.write(Buffer.from('\r\n', 'ascii')); } } async cutter(_: Cut): Promise { return this.connection.write(Buffer.from('\x1Bm', 'ascii')); } async buzzer(): Promise { return this.connection.write(Buffer.from('\x07', 'ascii')); } async drawer(_: Drawer, __: number, ___: number): Promise { return this.connection.write(Buffer.from('\x10\x14\x00\x00\x00', 'ascii')); } async setAlignment(align: Align) { const cmd = { [Align.Left]: '\x1B\x61\x00', [Align.Center]: '\x1B\x61\x01', [Align.Right]: '\x1B\x61\x02', }; return this.connection.write(Buffer.from(cmd[align], 'ascii')); } protected async setMode(mode: number, enable: boolean): Promise { let byte = 0b00000000; // keep Font A selected if (this.font.name == 'Font B') { byte |= 0b00000001; // keep Font B selected } const before = byte; if (Style.DoubleHeight & mode) { byte |= 0b00010000; } if (Style.DoubleWidth & mode) { byte |= 0b00100000; } let mask = 0b00000001; if (enable) { mask = 0b00110001; } if (before != byte) { return this.connection.write( Buffer.from('\x1B!' + String.fromCharCode(byte & mask), 'ascii'), ); } } protected async setStyle(style: Style, enable: boolean): Promise { if (enable) { // enable styles if (Style.Underline == style) { return this.connection.write(Buffer.from('\x1B-1', 'ascii')); } if (Style.Bold == style) { return this.connection.write(Buffer.from('\x1BE1', 'ascii')); } if (Style.Italic == style) { return this.connection.write(Buffer.from('\x1B4', 'ascii')); } } else { // disable styles if (Style.Underline == style) { return this.connection.write(Buffer.from('\x1B-0', 'ascii')); } if (Style.Bold == style) { return this.connection.write(Buffer.from('\x1BE0', 'ascii')); } if (Style.Italic == style) { return this.connection.write(Buffer.from('\x1B5', 'ascii')); } } } protected async setCharSize({ width = 1, height = 1, }: { width: number; height: number; }) { width = Math.max(1, Math.min(width, 8)); height = Math.max(1, Math.min(height, 8)); const n = (height - 1) | ((width - 1) << 4); return this.connection.write( Buffer.from(`\x1D!${String.fromCharCode(n)}`, 'ascii'), ); } async qrcode(data: string, size: number) { const tipo = '2'; const _size = String.fromCharCode(size || 4); const error = '0'; const len = data.length + 3; const pL = String.fromCharCode(len & 0xff); const pH = String.fromCharCode((len >> 8) & 0xff); await this.connection.write( Buffer.from('\x1D(k\x04\x001A' + tipo + '\x00', 'ascii'), ); await this.connection.write( Buffer.from('\x1D(k\x03\x001C' + _size, 'ascii'), ); await this.connection.write( Buffer.from('\x1D(k\x03\x001E' + error, 'ascii'), ); await this.connection.write( Buffer.from('\x1D(k' + pL + pH + '1P0', 'ascii'), ); await this.connection.write(Buffer.from(data, 'ascii')); await this.connection.write(Buffer.from('\x1D(k\x03\x001Q0', 'ascii')); // line feed return this.connection.write(Buffer.from('\x0A')); } }