diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a3e8756 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +build: + cd faxmachine &&\ + go build -o ../library_bridge.so -buildmode=c-shared ../library_bridge.go + diff --git a/library_bridge.go b/library_bridge.go index 933c3e9..a258879 100644 --- a/library_bridge.go +++ b/library_bridge.go @@ -1,11 +1,75 @@ package main +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" +) + import "C" -//export Add -func Add(a, b int) int { - return a + b +//export printimg +func printimg(path *C.char) { + fmt.Print(C.GoString(path)) + + p, err := escpos.StartUSBPrinter(C.GoString(path), protocol.TMT88IV, escpos.FlagNone) + 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) + } } func main() {} - diff --git a/library_bridge.h b/library_bridge.h index d48349d..597c8b7 100644 --- a/library_bridge.h +++ b/library_bridge.h @@ -74,7 +74,7 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; extern "C" { #endif -extern GoInt Add(GoInt a, GoInt b); +extern void printimg(char* path); #ifdef __cplusplus } diff --git a/test.py b/test.py index db0e280..0aaea4b 100644 --- a/test.py +++ b/test.py @@ -1,9 +1,5 @@ import ctypes -# Load the Go shared library mylib = ctypes.CDLL('./library_bridge.so') -# Call the Add function from Go -result = mylib.Add(3, 5) -print(result) - +mylib.printimg("/dev/usb/lp0".encode('utf-8'))