Control a Adafruit thermal printer over different adapters. This library is very WIP.
·
Report Bug
·
Request Feature
·
I wanted to talk to a thermal printer over an api. I experimented with a esp32 and quickly came to its limits.
After investigating the Adafruit library and many many failed other attempts, I concluded that I can extract the heavy lifting to TypeScript and only run a light mqtt to serial implementation on the esp32.
To allow different 'streams' like mqtt I came up with the adapter concept.
So now you can utilize the versatile package landscape of NPM to generate bitmaps, wrap it in REST APIs and and and.
npm i @beuluis/thermaltastic
The next
dist-tag is kept in sync with the latest commit on main. So this contains always the latest changes but is highly unstable.
npm i @beuluis/thermaltastic@next
const printer = new Thermaltastic(adapter);
await printer.begin();
await printer.println('Hello World!');
The original library used a serial stream to send the bytes to the printer. In this implementation we use adapters to achieve this.
A adapter defines how the printer receives the bytes.
Send the to print bytes over mqtt.
⚠️ You need the corresponding arduino MQTT client also listening: See ThermalMqttasticPrinter for more details.
You also need a MQTT broker. An example would be eclipse-mosquitto.
const adapter = new MqttasticAdapter({
mqttUrl: 'mqtt://localhost:1883',
mqttOptions: {
password: '12345678',
},
});
new Thermaltastic(adapter);
mqttOptions
is the option interface of the MQTT package. Please refer to this documentation on how to establish the connection.
For your own adapter you just need to implement the Adapter
interface.
export class MyAdapter implements Adapter {
public async begin() {}
public async write(...bytes: [number, number?, number?, number?]) {}
public async writeBytes(...bytes: [number, number?, number?, number?]) {}
}
Parameters get validated using zod. Please refer to this documentation on how the parameters get validated.
This method sets the times (in microseconds) for the paper to advance one vertical 'dot' when printing and when feeding.
z.number().int().nonnegative().parse(dotPrintTime);
z.number().int().nonnegative().parse(dotFeedTime);
printer.setTimes(10, 15);
Prints the message.
await printer.print('Hello World!');
Prints the message with a line break at the end.
await printer.println('Hello World!');
Initializes the printer and set default values. Needs to be called before performing any operations!
z.number().int().nonnegative().parse(firmware);
await printer.begin();
Resets the printer!
await printer.begin();
Resets all text formatting back to the defaults.
await printer.setDefaults();
Prints a test.
await printer.test();
Prints a test page.
await printer.testPage();
Sets the printing height of the barcode.
z.number().int().nonnegative().parse(barcodeHeight);
await printer.setBarcodeHeight(60);
Prints a barcode.
z.string().max(255).parse(text);
await printer.printBarcode('ADAFRUT', Barcode.CODE39);
Sets print mode to normal.
await printer.normal();
Turn on inverse print mode.
await printer.inverseOn();
Turn off inverse print mode.
await printer.inverseOff();
Turn on upside down print mode.
await printer.upsideDownOn();
Turn off upside down print mode.
await printer.upsideDownOff();
Turn on double height print mode.
await printer.doubleHeightOn();
Turn off double height print mode.
await printer.doubleHeightOff();
Turn on double width print mode.
await printer.doubleWidthOn();
Turn off double width print mode.
await printer.doubleWidthOff();
Turn on strike print mode.
await printer.strikeOn();
Turn off strike print mode.
await printer.strikeOff();
Turn on bold print mode.
await printer.boldOn();
Turn off bold print mode.
await printer.boldOff();
Justifies the content.
await printer.justify('C');
Feeds lines of paper.
z.number().int().min(1).parse(lines);
await printer.feed(2);
Feeds rows of paper.
z.number().int().min(1).parse(rows);
await printer.feedRows(2);
Flush the printer.
await printer.flush(2);
Set the text size.
await printer.setSize('L');
Sets the printer density.
z.number().int().nonnegative().max(31).parse(density);
z.number().int().nonnegative().max(7).parse(breakTime);
await printer.setPrintDensity(11, 3);
Turn on underline.
await printer.underlineOn();
Turn off underline.
await printer.underlineOff();
⚠️ WIP
Prints a bitmap.
z.number().int().nonnegative().max(384).parse(width);
z.number().int().min(1).parse(height);
await printer.printBitmap(2, 2, new Uint8Array([0, 255, 255, 0]));
Take the printer offline. Print commands sent after this will be ignored until online
is called.
await printer.offline();
Take the printer online.
await printer.online();
Put the printer into a low-energy state immediately.
await printer.sleep();
Put the printer into a low-energy state after the given number of seconds.
z.number().int().min(1).parse(seconds);
await printer.sleepAfter(1);
Wake the printer from a low-energy state.
await printer.wake();
Set maximum chunk height for bitmap printing.
z.number().int().min(1).parse(value);
printer.setMaxChunkHeight(200);
Set maximum chunk height for bitmap printing. May only work in recent firmware.
z.number().int().nonnegative().max(15).parse(value);
printer.setCharset(10);
Select alternate characters for upper ASCII. May only work in recent firmware.
z.number().int().nonnegative().max(47).parse(value);
await printer.setCodePage(12);
Print a tab. May only work in recent firmware.
await printer.tab();
Sets font type. May only work in recent firmware.
await printer.setFont('B');
Set character spacing. May only work in recent firmware.
z.number().int().nonnegative().parse(spacing);
await printer.setCharSpacing(10);
You can enable the debugging logger when you provide a logger to the constructor.
const printer = new Thermaltastic(adapter, {
logger: console,
});