CatPrint

1.0.3

A library for printing to cheap cat thermoprinters written in Swift
Benzoate/CatPrint.swift

What's New

1.0.3

2023-12-30T11:49:14Z

Add optional config for run length encoding

Full Changelog: 1.0.2...1.0.3

CatPrint.swift

A swift library for printing to the cheap β€œCat” thermoprinters that can be found on sites such as Aliexpress. I have tested this with the MX10, but I believe that these models will work with this library.

  • CYLO BT PRINTER
  • MXTP-100
  • AZ-P2108X
  • MX10 βœ… [confirmed]
  • MX11
  • BQ02
  • EWTTO ET-Z049

Installation

Add the repo to your project using SwiftPackage Manager

Usage

You need to create an instance of CatPrinter and retain it for the duration of your session.

e.g.

import CatPrint

@MainActor
final class MyViewModel {
    let printer = CatPrinter(settings: .default)
}

You can then search for a printer with try await printer.startScan(). A bluetoothNotPoweredOn error will be raised if CoreBluetooth is not ready to scan, if you handle this error then you can try again after some time.

func searchForPrinters() async {
    do {
        try await printer.startScan()
    } catch CatPrinterError.bluetoothNotPoweredOn {
        try? await Task.sleep(nanoseconds: 1_000_000_000)
        await searchForPrinters()
    } catch { }
}

You can then listen for available cat printers by subscribing to availablePrinters and handle that in a method of your choosing. This property will automatically update as printers connect and disconnect.

await printer.$availablePrinters
    .dropFirst()
    .filter { $0.isEmpty == false }
    .map { State.foundPrinters(Array($0)) }
    .receive(on: RunLoop.main)
    .assign(to: &$state)

Finally, once you have a printer you want to print to β€” you can print by providing a CGImage. The CGImage will automatically be re-sampled to the pixel width of the printer and converted to grayscale. There are some image processing options such as dithering available via the imageProcessing property.

do {
    try await printer.printImage(
                image, // CGImage
                printer: printerInfo
              )
} catch CatPrinterError.noSuchPrinterConnected {
} catch { }

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Thu Mar 28 2024 07:40:26 GMT-0900 (Hawaii-Aleutian Daylight Time)