|
| 1 | +import { zlibSync } from "fflate"; |
| 2 | +import type { ImageBitmap1bpp, PrintCommand } from "../types.js"; |
| 3 | + |
| 4 | +/** 6 zero bytes to wake the printer */ |
| 5 | +export function wakeup(): PrintCommand { |
| 6 | + return { label: "wakeup", data: new Uint8Array(6) }; |
| 7 | +} |
| 8 | + |
| 9 | +/** Start print job: 1F C0 01 00 */ |
| 10 | +export function enable(): PrintCommand { |
| 11 | + return { label: "enable", data: Uint8Array.from([0x1f, 0xc0, 0x01, 0x00]) }; |
| 12 | +} |
| 13 | + |
| 14 | +/** End print job: 1F C0 01 01 */ |
| 15 | +export function stop(): PrintCommand { |
| 16 | + return { label: "stop", data: Uint8Array.from([0x1f, 0xc0, 0x01, 0x01]) }; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Set density: 1F 70 02 <value> |
| 21 | + * Density mapping: 1→3, 2→8, 3→14 |
| 22 | + */ |
| 23 | +export function setDensity(density: number): PrintCommand { |
| 24 | + const DENSITY_MAP: Record<number, number> = { 1: 3, 2: 8, 3: 14 }; |
| 25 | + const value = DENSITY_MAP[density] ?? 8; |
| 26 | + return { |
| 27 | + label: "set-density", |
| 28 | + data: Uint8Array.from([0x1f, 0x70, 0x02, value & 0xff]), |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +/** Set paper type to gap: 1F 80 02 20 */ |
| 33 | +export function setPaperTypeGap(): PrintCommand { |
| 34 | + return { |
| 35 | + label: "set-paper-type", |
| 36 | + data: Uint8Array.from([0x1f, 0x80, 0x02, 0x20]), |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +/** Feed n dots: 1B 4A <lo> <hi> 00 */ |
| 41 | +export function feedDots(dots: number): PrintCommand { |
| 42 | + return { |
| 43 | + label: "feed-dots", |
| 44 | + data: Uint8Array.from([0x1b, 0x4a, dots & 0xff, (dots >> 8) & 0xff, 0x00]), |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +/** Adjust position auto: 1F 11 <param> */ |
| 49 | +export function adjustPositionAuto(param: number): PrintCommand { |
| 50 | + return { |
| 51 | + label: "adjust-position", |
| 52 | + data: Uint8Array.from([0x1f, 0x11, param & 0xff]), |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +/** Printer location: 1F 12 <x> <y> */ |
| 57 | +export function printerLocation(x: number, y: number): PrintCommand { |
| 58 | + return { |
| 59 | + label: "printer-location", |
| 60 | + data: Uint8Array.from([0x1f, 0x12, x & 0xff, y & 0xff]), |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Build a compressed bitmap command: 1F 10 <wh> <wl> <hh> <hl> <len4> + zlib data |
| 66 | + * Compression: standard zlib compress() with default parameters (level 6). |
| 67 | + */ |
| 68 | +export function printBitmap(image: ImageBitmap1bpp): PrintCommand { |
| 69 | + const { data: pixels, bytesPerRow, height } = image; |
| 70 | + const compressed = zlibSync(pixels, { level: 6 }); |
| 71 | + |
| 72 | + const header = Uint8Array.from([ |
| 73 | + 0x1f, |
| 74 | + 0x10, |
| 75 | + (bytesPerRow >> 8) & 0xff, |
| 76 | + bytesPerRow & 0xff, |
| 77 | + (height >> 8) & 0xff, |
| 78 | + height & 0xff, |
| 79 | + (compressed.length >> 24) & 0xff, |
| 80 | + (compressed.length >> 16) & 0xff, |
| 81 | + (compressed.length >> 8) & 0xff, |
| 82 | + compressed.length & 0xff, |
| 83 | + ]); |
| 84 | + |
| 85 | + const command = new Uint8Array(header.length + compressed.length); |
| 86 | + command.set(header, 0); |
| 87 | + command.set(compressed, header.length); |
| 88 | + |
| 89 | + return { label: "print-bitmap", data: command, bulk: true }; |
| 90 | +} |
| 91 | + |
| 92 | +/** Query battery level: 10 FF 50 F1 */ |
| 93 | +export function getBattery(): PrintCommand { |
| 94 | + return { |
| 95 | + label: "get-battery", |
| 96 | + data: Uint8Array.from([0x10, 0xff, 0x50, 0xf1]), |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +/** Query printer status: 10 FF 40 */ |
| 101 | +export function getStatus(): PrintCommand { |
| 102 | + return { label: "get-status", data: Uint8Array.from([0x10, 0xff, 0x40]) }; |
| 103 | +} |
| 104 | + |
| 105 | +/** Query model string: 10 FF 20 F0 */ |
| 106 | +export function getModel(): PrintCommand { |
| 107 | + return { |
| 108 | + label: "get-model", |
| 109 | + data: Uint8Array.from([0x10, 0xff, 0x20, 0xf0]), |
| 110 | + }; |
| 111 | +} |
| 112 | + |
| 113 | +/** Query firmware version: 10 FF 20 F1 */ |
| 114 | +export function getFirmware(): PrintCommand { |
| 115 | + return { |
| 116 | + label: "get-firmware", |
| 117 | + data: Uint8Array.from([0x10, 0xff, 0x20, 0xf1]), |
| 118 | + }; |
| 119 | +} |
| 120 | + |
| 121 | +/** Query serial number: 10 FF 20 F2 */ |
| 122 | +export function getSerial(): PrintCommand { |
| 123 | + return { |
| 124 | + label: "get-serial", |
| 125 | + data: Uint8Array.from([0x10, 0xff, 0x20, 0xf2]), |
| 126 | + }; |
| 127 | +} |
| 128 | + |
| 129 | +/** Query Bluetooth MAC address: 10 FF 20 F3 */ |
| 130 | +export function getMac(): PrintCommand { |
| 131 | + return { |
| 132 | + label: "get-mac", |
| 133 | + data: Uint8Array.from([0x10, 0xff, 0x20, 0xf3]), |
| 134 | + }; |
| 135 | +} |
| 136 | + |
| 137 | +/** Query BT module version: 10 FF 30 10 */ |
| 138 | +export function getBtVersion(): PrintCommand { |
| 139 | + return { |
| 140 | + label: "get-bt-version", |
| 141 | + data: Uint8Array.from([0x10, 0xff, 0x30, 0x10]), |
| 142 | + }; |
| 143 | +} |
| 144 | + |
| 145 | +/** Query BT device name: 10 FF 30 11 */ |
| 146 | +export function getBtName(): PrintCommand { |
| 147 | + return { |
| 148 | + label: "get-bt-name", |
| 149 | + data: Uint8Array.from([0x10, 0xff, 0x30, 0x11]), |
| 150 | + }; |
| 151 | +} |
| 152 | + |
| 153 | +/** Query print speed: 1F 60 00 */ |
| 154 | +export function getSpeed(): PrintCommand { |
| 155 | + return { |
| 156 | + label: "get-speed", |
| 157 | + data: Uint8Array.from([0x1f, 0x60, 0x00]), |
| 158 | + }; |
| 159 | +} |
0 commit comments