From 78456105ac61e1b4afea1df40572b1c5209e2324 Mon Sep 17 00:00:00 2001 From: TT-392 Date: Sun, 15 Sep 2024 17:46:04 +0200 Subject: [PATCH] Working cut, text, and image through bridge --- .gitignore | 1 + library_bridge.go | 78 ++++++++++++++++++++------------------------ library_bridge.h | 82 ----------------------------------------------- test.py | 14 +++++--- 4 files changed, 45 insertions(+), 130 deletions(-) delete mode 100644 library_bridge.h diff --git a/.gitignore b/.gitignore index 552cd9d..1930c0b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.so +*.h *.swp */__pycache__ flask/static diff --git a/library_bridge.go b/library_bridge.go index ebed2e9..aa76f0b 100644 --- a/library_bridge.go +++ b/library_bridge.go @@ -2,21 +2,22 @@ package main import ( "fmt" - _ "image" + _ "time" + "image" _ "image/gif" _ "image/jpeg" _ "image/png" "log" - _ "os" + "os" + "unsafe" + "bytes" _ "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" @@ -47,17 +48,27 @@ func printer_init(path string, speed int) (*escpos.Printer) { 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) } - if err = p.Wait(); err != nil { - log.Fatalf("failed to print: %v\n", err) - } + printer_close(p) + //time.Sleep(1000 * time.Millisecond) } //export print_text @@ -67,57 +78,38 @@ func print_text(path *C.char, str *C.char) { fmt.Fprint(p, C.GoString(str)) fmt.Fprint(p, "\n") - if err = p.Wait(); err != nil { - log.Fatalf("failed to print: %v\n", err) - } + printer_close(p) } -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) - } - defer p.Close() +//export print_image +func print_image(path *C.char, data *C.uchar, size C.int) { + p := printer_init(C.GoString(path), 1) - 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) + 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) } - //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 len(os.Args) > 2 && os.Args[2] != "" { + fmt.Fprintf(p, "\n%s\n", os.Args[2]) } 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 deleted file mode 100644 index 2a5e8ca..0000000 --- a/library_bridge.h +++ /dev/null @@ -1,82 +0,0 @@ -/* Code generated by cmd/cgo; DO NOT EDIT. */ - -/* package command-line-arguments */ - - -#line 1 "cgo-builtin-export-prolog" - -#include - -#ifndef GO_CGO_EXPORT_PROLOGUE_H -#define GO_CGO_EXPORT_PROLOGUE_H - -#ifndef GO_CGO_GOSTRING_TYPEDEF -typedef struct { const char *p; ptrdiff_t n; } _GoString_; -#endif - -#endif - -/* Start of preamble from import "C" comments. */ - - - - -/* End of preamble from import "C" comments. */ - - -/* Start of boilerplate cgo prologue. */ -#line 1 "cgo-gcc-export-header-prolog" - -#ifndef GO_CGO_PROLOGUE_H -#define GO_CGO_PROLOGUE_H - -typedef signed char GoInt8; -typedef unsigned char GoUint8; -typedef short GoInt16; -typedef unsigned short GoUint16; -typedef int GoInt32; -typedef unsigned int GoUint32; -typedef long long GoInt64; -typedef unsigned long long GoUint64; -typedef GoInt64 GoInt; -typedef GoUint64 GoUint; -typedef size_t GoUintptr; -typedef float GoFloat32; -typedef double GoFloat64; -#ifdef _MSC_VER -#include -typedef _Fcomplex GoComplex64; -typedef _Dcomplex GoComplex128; -#else -typedef float _Complex GoComplex64; -typedef double _Complex GoComplex128; -#endif - -/* - static assertion to make sure the file is being used on architecture - at least with matching size of GoInt. -*/ -typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; - -#ifndef GO_CGO_GOSTRING_TYPEDEF -typedef _GoString_ GoString; -#endif -typedef void *GoMap; -typedef void *GoChan; -typedef struct { void *t; void *v; } GoInterface; -typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; - -#endif - -/* End of boilerplate cgo prologue. */ - -#ifdef __cplusplus -extern "C" { -#endif - -extern void cut(char* path); -extern void print_text(char* path, char* str); - -#ifdef __cplusplus -} -#endif diff --git a/test.py b/test.py index 6dd39d7..c51655f 100644 --- a/test.py +++ b/test.py @@ -1,9 +1,13 @@ import ctypes +import time -mylib = ctypes.CDLL('./library_bridge.so') +epson = ctypes.CDLL('./library_bridge.so') -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')) +#epson.cut(b"/dev/usb/lp0") +#epson.cut(b"/dev/usb/lp0") +#epson.print_text(b"/dev/usb/lp0", b"test\n") +data = open("/tmp/image.png", "rb").read() +#Bytes = (ctypes.c_ubyte * len(data))(*data) -mylib.cut("/dev/usb/lp0".encode('utf-8')) +epson.print_image(b"/dev/usb/lp0", data, len(data)) +#epson.cut(b"/dev/usb/lp0")