2024-09-12 18:44:00 +00:00
|
|
|
package main
|
|
|
|
|
2024-09-12 19:46:29 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2024-09-15 15:46:04 +00:00
|
|
|
_ "time"
|
|
|
|
"image"
|
2024-09-12 19:46:29 +00:00
|
|
|
_ "image/gif"
|
|
|
|
_ "image/jpeg"
|
|
|
|
_ "image/png"
|
|
|
|
"log"
|
2024-09-15 15:46:04 +00:00
|
|
|
"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-15 15:46:04 +00:00
|
|
|
|
2024-09-12 19:46:29 +00:00
|
|
|
)
|
|
|
|
|
2024-09-12 18:44:00 +00:00
|
|
|
import "C"
|
|
|
|
|
2024-09-12 20:58:05 +00:00
|
|
|
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
|
|
|
|
2024-09-12 20:58:05 +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
|
|
|
|
}
|
|
|
|
|
2024-09-15 15:46:04 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2024-09-12 20:58:05 +00:00
|
|
|
//export cut
|
|
|
|
func cut(path *C.char) {
|
2024-09-15 15:46:04 +00:00
|
|
|
fmt.Print("Starting cut\n");
|
2024-09-12 20:58:05 +00:00
|
|
|
p := printer_init(C.GoString(path), 1)
|
|
|
|
|
|
|
|
if err = p.CutPaper(); err != nil {
|
|
|
|
log.Fatalf("failed to cut paper: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-09-15 15:46:04 +00:00
|
|
|
printer_close(p)
|
|
|
|
//time.Sleep(1000 * time.Millisecond)
|
2024-09-12 20:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//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")
|
|
|
|
|
2024-09-15 15:46:04 +00:00
|
|
|
printer_close(p)
|
2024-09-12 20:58:05 +00:00
|
|
|
}
|
|
|
|
|
2024-09-15 15:46:04 +00:00
|
|
|
//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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-09-15 15:46:04 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-09-15 15:46:04 +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-15 15:46:04 +00:00
|
|
|
|
2024-09-12 18:44:00 +00:00
|
|
|
}
|
|
|
|
|
2024-09-15 15:46:04 +00:00
|
|
|
|
2024-09-12 18:44:00 +00:00
|
|
|
func main() {}
|