thermoprint

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2025 License: MIT Imports: 19 Imported by: 0

README

Thermal Bluetooth Printer LX-D02

Allows to print on a Bluetooth thermal printer LX-D02 from your computer.

Supports printing images and (somewhat) text and test patterns.

Images

Resize, dither and print image:

thermoprint -i image.png

Selecting different dither method:

thermoprint -i image.png -dither stucki

The following dithering algorithms are supported:

  • atkinson
  • bayer
  • floyd-steinberg
  • stucki

Default is "atkinson".

Text

Printing text:

ls -l | fold -w 49 | thermoprint -t -

If you don't want text to be resized, you can use -crop option, it will chop off the image at the 384 pixel boundary.

thermoprint -crop -t "very long text that doesn't fit 58mm roll" 

Test patterns

You can print test patterns to check printer quality:

thermoprint -pattern MillimeterLines

Using as a library

See pkg.go.dev for library functions.

Credits

This is based on the work in this repository https://github.com/big-vl/catcombo, which was used to understand the protocol, and used as a reference for document images detection.

Reason I didn't use it directly as my printer works slightly differently - it sends, what I called "hold"/"restart from" packages which are not handled in the original library.

References

Documentation

Overview

Package thermoprint implements printing on a LX-D02 (Dolebo) bluetooth thermal printer.

Index

Constants

View Source
const (
	// DefaultPrintDelay is the default interval between sending packets to the printer
	DefaultPrintDelay = 7 * time.Millisecond
)

Variables

View Source
var LXD02Rasteriser = &GenericRasteriser{
	Width:          384,
	Dpi:            203,
	LinesPerPacket: 2,
	PrefixFunc: func(packetIndex int) []byte {
		m := byte((packetIndex >> 8) & 0xFF)
		n := byte(packetIndex & 0xFF)
		return []byte{0x55, m, n}
	},
	Terminator: 0x00,
	Threshold:  bitmap.DefaultThreshold,
	DitherFunc: bitmap.DitherDefault,
}
View Source
var TestBufferPatterns = map[string]func(int) [][]byte{
	"BinaryPattern": BufferBinaryPattern,
}
View Source
var TestImagePatterns = map[string]func(int) image.Image{
	"RunningLinesImage": TestImgRunningLines,
	"Millimetres":       TestImgMillimetres,
	"Sine":              TestImgSine,
}

Functions

func BufferBinaryPattern

func BufferBinaryPattern(width int) [][]byte

func TestImgMillimetres

func TestImgMillimetres(maxX int) image.Image

TestImgMillimetres draws a running pattern of millimeter lines. Each horizontal line is 8 dots wide, and 1 dot high. Each horizontal line is repeated every 40 dots, so that the first line is at 0, the second at 40, the third at 80, and so on, until the maximum X coordinate is reached. THe output looks like this:

 0 1 ..  40 ..  80 .. 120 .. 160 .. 200 .. 240 .. 280 .. 320 .. 360 .. 400
	--      -- ...
	  --      -- ...
	    --      -- ...
	      --      --
	        --      --
	          --      --

etc.

func TestImgRunningLines

func TestImgRunningLines(maxX int) image.Image

TestImgRunningLines generates 8 lines each of which is 2 pixels high shifted by one pixel to the right, so that thermal unit is expected to print 4 times.

The output looks like this:

| | | |
 | | | |
  | | | |
   | | | |
    | | | |
     | | | |
      | | | |
       | | | |

func TestImgSine

func TestImgSine(maxX int) image.Image

Types

type GenericRasteriser

type GenericRasteriser struct {
	Width          int
	Dpi            int
	LinesPerPacket int
	PrefixFunc     func(packetIndex int) []byte // returns 55 m n
	Terminator     byte                         // 00
	DitherFunc     bitmap.DitherFunc            // optional dither function
	Threshold      uint8                        // threshold for dark pixels, default is 128
}

func (*GenericRasteriser) DPI

func (r *GenericRasteriser) DPI() int

func (*GenericRasteriser) Enumerate

func (r *GenericRasteriser) Enumerate(data [][]byte) ([][]byte, error)

Enumerate converts the raw data to printer specific packets ready to be sent to printer.

func (*GenericRasteriser) LineWidth

func (r *GenericRasteriser) LineWidth() int

func (*GenericRasteriser) ResizeAndDither

func (r *GenericRasteriser) ResizeAndDither(src image.Image, gamma float64, autoDither bool) image.Image

func (*GenericRasteriser) Serialise

func (r *GenericRasteriser) Serialise(img image.Image) ([][]byte, error)

func (*GenericRasteriser) SetDitherFunc

func (r *GenericRasteriser) SetDitherFunc(fn bitmap.DitherFunc)

type LXD02

type LXD02 struct {
	// contains filtered or unexported fields
}

LXD02 represents a LX-D02 printer. Instance is not safe for concurrent use. Zero value is unusable, initialise with NewLXD02

func NewLXD02

func NewLXD02(ctx context.Context, adapter *bluetooth.Adapter, sp SearchParameters, opt ...Option) (*LXD02, error)

func (*LXD02) Connect added in v0.0.4

func (p *LXD02) Connect(ctx context.Context, adapter *bluetooth.Adapter, sp SearchParameters) error

Connect connects to the LX-D02 printer using the provided adapter and search parameters.

func (*LXD02) DPI

func (p *LXD02) DPI() float64

func (*LXD02) Disconnect

func (p *LXD02) Disconnect() error

func (*LXD02) PrintImage

func (p *LXD02) PrintImage(ctx context.Context, img image.Image) error

PrintImage prints an image on the printer. If dry run is enabled, it saves the preview file to disk and exits.

func (*LXD02) PrintPattern

func (p *LXD02) PrintPattern(ctx context.Context, pattern string) error

func (*LXD02) PrintRAW

func (p *LXD02) PrintRAW(ctx context.Context, data [][]byte) error

func (*LXD02) PrintTextTTF

func (p *LXD02) PrintTextTTF(ctx context.Context, text string, face font.Face) error

func (*LXD02) SetOptions added in v0.0.4

func (p *LXD02) SetOptions(opts ...Option) error

SetOptions

func (*LXD02) Width

func (p *LXD02) Width() int

Width returns the maximum width of the print output in pixels.

type Option

type Option func(*printOptions)

func WithAutoDither

func WithAutoDither(isEnabled bool) Option

func WithCrop

func WithCrop(crop bool) Option

func WithDither

func WithDither(name string) Option

func WithDryRun

func WithDryRun(dryrun bool) Option

func WithEnergy

func WithEnergy(v uint8) Option

func WithGamma

func WithGamma(gamma float64) Option

WithGamma allows to specify rasteriser gamma correction value. [DefaultGamma] value or 0.0 means use default for the selected dither function.

func WithPrintInterval

func WithPrintInterval(d time.Duration) Option

type Rasteriser

type Rasteriser interface {
	ResizeAndDither(img image.Image, gamma float64, autoDither bool) image.Image
	// Serialise should return a slice of byte slices that are sent to printer.
	Serialise(src image.Image) ([][]byte, error)
	// Enumerate prepares the raw data for printing running the packet func
	// for each byte slice and returning the data ready to be sent to printer.
	Enumerate(data [][]byte) ([][]byte, error)
	// DPI should return the DPI of the rasteriser.
	DPI() int
	// LineWidth should return the line width in pixels, i.e. for 203 dpi
	// thermal printer that uses 58mm paper, it is 384.
	LineWidth() int
	// SetDitherFunc should set the dither function.
	SetDitherFunc(fn bitmap.DitherFunc)
}

type SearchParameters

type SearchParameters struct {
	Name       string
	MACAddress string
}

Directories

Path Synopsis
Package bitmap provides bitmap image manipulation funcitons.
Package bitmap provides bitmap image manipulation funcitons.
cmd
tp command
tp/internal/cfg
Package cfg contains common configuration variables.
Package cfg contains common configuration variables.
tp/internal/cmdimage
Package cmdimage provides image printing subcommand.
Package cmdimage provides image printing subcommand.
tp/internal/cmdpattern
Package cmdpattern provides pattern printing subcommand.
Package cmdpattern provides pattern printing subcommand.
tp/internal/cmdtext
Package cmdtext provides a text printing subcommand.
Package cmdtext provides a text printing subcommand.
tp/internal/golang/base
Package base defines shared basic pieces of the tp command.
Package base defines shared basic pieces of the tp command.
tp/internal/golang/help
Package help implements "slackdump help" command.
Package help implements "slackdump help" command.
Package ippsrv implements an IPP server.
Package ippsrv implements an IPP server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL