label_printer/library_bridge.go

124 lines
2.7 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"
_ "image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
_ "os"
_ "github.com/jbuchbinder/gopnm"
_ "golang.org/x/image/webp"
_ "github.com/lestrrat-go/dither"
_ "github.com/nfnt/resize"
"git.sr.ht/~guacamolie/faxmachine/escpos"
"git.sr.ht/~guacamolie/faxmachine/escpos/protocol"
)
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
}
//export cut
func cut(path *C.char) {
p := printer_init(C.GoString(path), 1)
if err = p.CutPaper(); err != nil {
log.Fatalf("failed to cut paper: %v", err)
}
if err = p.Wait(); err != nil {
log.Fatalf("failed to print: %v\n", err)
}
}
//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")
if err = p.Wait(); err != nil {
log.Fatalf("failed to print: %v\n", err)
}
}
func printimg(path *C.char) {
p, err := escpos.StartUSBPrinter("/dev/usb/lp0", protocol.TMT88IV, escpos.FlagNone)
2024-09-12 19:46:29 +00:00
if err != nil {
log.Fatalf("failed to start printer: %v\n", err)
}
defer p.Close()
if err := p.EnableASB(protocol.ASBReportAll); err != nil {
log.Fatalf("failed to enable ASB: %v\n", err)
}
go func() {
for status := range p.ASBStatus() {
log.Printf("received ASB status: %#v\n", status)
}
}()
//img, _, err := image.Decode(os.Stdin)
//if err != nil {
// log.Fatalf("failed to get decode image: %v\n", err)
//}
//img = resize.Resize(512, 0, img, resize.Lanczos3)
//img = dither.Monochrome(dither.FloydSteinberg, img, 1.18)
// if err := p.SetEncoding(escpos.CharPagePC427); err != nil {
// log.Fatalf("failed to set encoding: %v\n", err)
// }
if err := p.SetPrintSpeed(1); err != nil {
log.Fatalf("failed to set print speed: %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])
//}
fmt.Fprint(p, "\n\n\n")
if err := p.CutPaper(); err != nil {
log.Fatalf("failed to cut paper: %v", err)
}
if err := p.Wait(); err != nil {
log.Fatalf("failed to print: %v\n", err)
}
2024-09-12 18:44:00 +00:00
}
func main() {}