// License-Identifier: GPL-3.0-or-later package main import ( "fmt" _ "time" "image" _ "image/gif" _ "image/jpeg" _ "image/png" "log" "unsafe" "bytes" _ "github.com/jbuchbinder/gopnm" _ "golang.org/x/image/webp" "git.sr.ht/~guacamolie/faxmachine/escpos" "git.sr.ht/~guacamolie/faxmachine/escpos/protocol" ) import "C" const red = "\033[31m" const reset = "\033[0m" var err error func printer_init(path string, speed int) (*escpos.Printer, error) { p, err := escpos.StartUSBPrinter(path, protocol.TMT88IV, escpos.FlagNone) if err != nil { fmt.Printf("%sError: failed to start printer: %v%s\n", red, err, reset) return p, err } if err := p.EnableASB(protocol.ASBReportAll); err != nil { p.Close() fmt.Printf("%sError: failed enable ASB: %v%s\n", red, err, reset) return p, err } go func() { for status := range p.ASBStatus() { log.Printf("received ASB status: %#v\n", status) } }() if err := p.SetPrintSpeed(speed); err != nil { fmt.Printf("%sError: Failed to set print speed: %v%s\n", red, err, reset) return p, err } return p, nil } func printer_close(p *escpos.Printer) (error) { if err = p.Wait(); err != nil { fmt.Printf("%sError: Failed to print: %v%s\n", red, err, reset) return err } if err = p.Close(); err != nil { fmt.Printf("%sError: Failed close printer: %v%s\n", red, err, reset) return err } fmt.Print("printer closed\n"); return nil } //export cut func cut(path *C.char) { fmt.Print("Starting cut\n"); p, err := printer_init(C.GoString(path), 1) if err != nil { return } if err = p.CutPaper(); err != nil { fmt.Printf("%sError: Failed to cut paper: %v%s\n", red, err, reset) return } printer_close(p) } //export print_text func print_text(path *C.char, str *C.char) { p, err := printer_init(C.GoString(path), 1) if err != nil { return } fmt.Fprint(p, C.GoString(str)) fmt.Fprint(p, "\n") printer_close(p) } //export print_image func print_image(path *C.char, data *C.uchar, size C.int) { p, err := printer_init(C.GoString(path), 1) if err != nil { return } byteData := C.GoBytes(unsafe.Pointer(data), C.int(size)) imgReader := bytes.NewReader(byteData) img, _, err := image.Decode(imgReader) if err != nil { fmt.Printf("%sError: Failed get decode image: %v%s\n", red, err, reset) return } if err := p.PrintImage(img); err != nil { fmt.Printf("%sError: Failed print image: %v%s\n", red, err, reset) return } printer_close(p) } func main() {}