@cutos/device-tspl-printer
Version:
@cutos/device-tspl-printer is a JavaScript library that provides a unified interface for accessing and controlling printers that support the TSPL command set.
74 lines (59 loc) • 2.15 kB
JavaScript
import { CoreClass } from '@cutos/core';
const TYPE = 'device-tspl-printer';
const CMD = {
CONNECT: 'connect',
PRINT: 'print',
};
class DeviceTSPLPrinter extends CoreClass.Device {
constructor(name, callback) {
super(name, TYPE, callback);
this.commands = [];
}
connect(path, callback) {
this.sendCommand({cmd: CMD.CONNECT, args: {path: path}}, callback);
}
setLabelSize(width, height) {
const cmd = `SIZE ${width} mm, ${height} mm`;
this.commands.push(cmd);
}
setLabelGap(gap) {
const cmd = `GAP ${gap} mm, 0 mm`;
this.commands.push(cmd);
}
setDirection(direction = 0, mirror = 0) {
const cmd = `DIRECTION ${direction},${mirror}}`;
this.commands.push(cmd);
}
setClearBuff() {
const cmd = "CLS";
this.commands.push(cmd);
}
addText(text, x, y, opts = {}) {
const default_opts = {font: '24', rotation: 0, align: 0, mx: 1, my: 1};
const merged_opts = {...default_opts, ...opts};
const {font, rotation, align, mx, my} = merged_opts;
const cmd = `TEXT ${x},${y},"${font}",${rotation},${mx},${my},${align},"${text}"`;
this.commands.push(cmd);
}
addQRCode(content, x, y, opts = {}) {
const default_opts = {level: 'H', cw: 4, mode: 0, rotation: 0};
const merged_opts = {...default_opts, ...opts};
const {level, cw, mode, rotation} = merged_opts;
const cmd = `QRCODE ${x},${y},${level},${cw},${mode},${rotation},"${content}"`;
this.commands.push(cmd);
}
addBox(x, y, width, height, thick, radius = 0) {
const cmd = `BOX ${x},${y},${width},${height},${thick},${radius}`;
this.commands.push(cmd);
}
print(callback) {
let data = '';
this.commands.push('PRINT 1,1');
data = this.commands.join('\r\n');
data += '\r\n';
this.commands.length = 0;
this.sendCommand({cmd: CMD.PRINT, args: {data: data}}, callback);
console.log(data);
}
}
export { DeviceTSPLPrinter };