label_printer/library_bridge.go

116 lines
2.3 KiB
Go
Raw Normal View History

2024-09-12 18:44:00 +00:00
package main
2024-09-12 19:46:29 +00:00
import (
"fmt"
_ "time"
"image"
2024-09-12 19:46:29 +00:00
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"os"
"unsafe"
"bytes"
2024-09-12 19:46:29 +00:00
_ "github.com/jbuchbinder/gopnm"
_ "golang.org/x/image/webp"
"git.sr.ht/~guacamolie/faxmachine/escpos"
"git.sr.ht/~guacamolie/faxmachine/escpos/protocol"
2024-09-12 19:46:29 +00:00
)
2024-09-12 18:44:00 +00:00
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)
}
2024-09-12 19:46:29 +00:00
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)
2024-09-12 19:46:29 +00:00
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)
2024-09-12 19:46:29 +00:00
}
if len(os.Args) > 2 && os.Args[2] != "" {
fmt.Fprintf(p, "\n%s\n", os.Args[2])
2024-09-12 19:46:29 +00:00
}
if err := p.Wait(); err != nil {
log.Fatalf("failed to print: %v\n", err)
}
2024-09-12 18:44:00 +00:00
}
2024-09-12 18:44:00 +00:00
func main() {}