From d6c4bab52e5d70bd6f0a2db58c96a6a905142c3a Mon Sep 17 00:00:00 2001 From: TT-392 Date: Thu, 12 Sep 2024 22:58:05 +0200 Subject: [PATCH] Dead end, my brain can't handle writing this bridge rn --- library_bridge.go | 56 +++++++++++++++++++++++++++++++++++++++++++---- library_bridge.h | 3 ++- test.py | 6 ++++- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/library_bridge.go b/library_bridge.go index a258879..ebed2e9 100644 --- a/library_bridge.go +++ b/library_bridge.go @@ -21,11 +21,59 @@ import ( import "C" -//export printimg -func printimg(path *C.char) { - fmt.Print(C.GoString(path)) +var err error - p, err := escpos.StartUSBPrinter(C.GoString(path), protocol.TMT88IV, escpos.FlagNone) +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 +} + +//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) if err != nil { log.Fatalf("failed to start printer: %v\n", err) } diff --git a/library_bridge.h b/library_bridge.h index 597c8b7..2a5e8ca 100644 --- a/library_bridge.h +++ b/library_bridge.h @@ -74,7 +74,8 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; extern "C" { #endif -extern void printimg(char* path); +extern void cut(char* path); +extern void print_text(char* path, char* str); #ifdef __cplusplus } diff --git a/test.py b/test.py index 0aaea4b..6dd39d7 100644 --- a/test.py +++ b/test.py @@ -2,4 +2,8 @@ import ctypes mylib = ctypes.CDLL('./library_bridge.so') -mylib.printimg("/dev/usb/lp0".encode('utf-8')) +mylib.print_text("/dev/usb/lp0".encode('utf-8'), "test\n".encode('utf-8')) +mylib.print_text("/dev/usb/lp0".encode('utf-8'), "test\n".encode('utf-8')) +mylib.print_text("/dev/usb/lp0".encode('utf-8'), "test\n".encode('utf-8')) + +mylib.cut("/dev/usb/lp0".encode('utf-8'))