package main import ( "fmt" _ "time" "image" _ "image/gif" _ "image/jpeg" _ "image/png" "log" "os" "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" var err error func printer_init(path string, speed int) (*escpos.Printer) { p, err := escpos.StartUSBPrinter(path, protocol.TMT88IV, escpos.FlagNone) if err != nil { log.Fatalf("failed to start printer: %v", err) } if err := p.EnableASB(protocol.ASBReportAll); err != nil { p.Close() log.Fatalf("failed to enable ASB: %v", err) } go func() { for status := range p.ASBStatus() { log.Printf("received ASB status: %#v\n", status) } }() if err := p.SetPrintSpeed(speed); err != nil { log.Fatalf("failed to set print speed: %v\n", err) } return p } func printer_close(p *escpos.Printer) { if err = p.Wait(); err != nil { log.Fatalf("failed to print: %v\n", err) } if err = p.Close(); err != nil { log.Fatalf("failed to close printer: %v\n", err) } fmt.Print("printer closed\n"); } //export cut func cut(path *C.char) { fmt.Print("Starting cut\n"); p := printer_init(C.GoString(path), 1) if err = p.CutPaper(); err != nil { log.Fatalf("failed to cut paper: %v", err) } printer_close(p) //time.Sleep(1000 * time.Millisecond) } //export print_text func print_text(path *C.char, str *C.char) { p := printer_init(C.GoString(path), 1) 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 := printer_init(C.GoString(path), 1) go func() { for status := range p.ASBStatus() { log.Printf("received ASB status: %#v\n", status) } }() byteData := C.GoBytes(unsafe.Pointer(data), C.int(size)) imgReader := bytes.NewReader(byteData) img, _, err := image.Decode(imgReader) if err != nil { log.Fatalf("failed to get decode image: %v\n", err) } if err := p.PrintImage(img); err != nil { log.Fatalf("failed to print image: %v\n", err) } if len(os.Args) > 2 && os.Args[2] != "" { fmt.Fprintf(p, "\n%s\n", os.Args[2]) } if err := p.Wait(); err != nil { log.Fatalf("failed to print: %v\n", err) } } func main() {}