Working cut, text, and image through bridge
This commit is contained in:
parent
10d218bfba
commit
78456105ac
4 changed files with 45 additions and 130 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
||||||
*.so
|
*.so
|
||||||
|
*.h
|
||||||
*.swp
|
*.swp
|
||||||
*/__pycache__
|
*/__pycache__
|
||||||
flask/static
|
flask/static
|
||||||
|
|
|
@ -2,21 +2,22 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "image"
|
_ "time"
|
||||||
|
"image"
|
||||||
_ "image/gif"
|
_ "image/gif"
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
"log"
|
"log"
|
||||||
_ "os"
|
"os"
|
||||||
|
"unsafe"
|
||||||
|
"bytes"
|
||||||
|
|
||||||
_ "github.com/jbuchbinder/gopnm"
|
_ "github.com/jbuchbinder/gopnm"
|
||||||
_ "golang.org/x/image/webp"
|
_ "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"
|
||||||
"git.sr.ht/~guacamolie/faxmachine/escpos/protocol"
|
"git.sr.ht/~guacamolie/faxmachine/escpos/protocol"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
import "C"
|
import "C"
|
||||||
|
@ -47,17 +48,27 @@ func printer_init(path string, speed int) (*escpos.Printer) {
|
||||||
return p
|
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
|
//export cut
|
||||||
func cut(path *C.char) {
|
func cut(path *C.char) {
|
||||||
|
fmt.Print("Starting cut\n");
|
||||||
p := printer_init(C.GoString(path), 1)
|
p := printer_init(C.GoString(path), 1)
|
||||||
|
|
||||||
if err = p.CutPaper(); err != nil {
|
if err = p.CutPaper(); err != nil {
|
||||||
log.Fatalf("failed to cut paper: %v", err)
|
log.Fatalf("failed to cut paper: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = p.Wait(); err != nil {
|
printer_close(p)
|
||||||
log.Fatalf("failed to print: %v\n", err)
|
//time.Sleep(1000 * time.Millisecond)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//export print_text
|
//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, C.GoString(str))
|
||||||
fmt.Fprint(p, "\n")
|
fmt.Fprint(p, "\n")
|
||||||
|
|
||||||
if err = p.Wait(); err != nil {
|
printer_close(p)
|
||||||
log.Fatalf("failed to print: %v\n", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printimg(path *C.char) {
|
//export print_image
|
||||||
p, err := escpos.StartUSBPrinter("/dev/usb/lp0", protocol.TMT88IV, escpos.FlagNone)
|
func print_image(path *C.char, data *C.uchar, size C.int) {
|
||||||
if err != nil {
|
p := printer_init(C.GoString(path), 1)
|
||||||
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() {
|
go func() {
|
||||||
for status := range p.ASBStatus() {
|
for status := range p.ASBStatus() {
|
||||||
log.Printf("received ASB status: %#v\n", status)
|
log.Printf("received ASB status: %#v\n", status)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
//img, _, err := image.Decode(os.Stdin)
|
byteData := C.GoBytes(unsafe.Pointer(data), C.int(size))
|
||||||
//if err != nil {
|
imgReader := bytes.NewReader(byteData)
|
||||||
// log.Fatalf("failed to get decode image: %v\n", err)
|
img, _, err := image.Decode(imgReader)
|
||||||
//}
|
if err != nil {
|
||||||
//img = resize.Resize(512, 0, img, resize.Lanczos3)
|
log.Fatalf("failed to get decode image: %v\n", err)
|
||||||
//img = dither.Monochrome(dither.FloydSteinberg, img, 1.18)
|
}
|
||||||
|
if err := p.PrintImage(img); err != nil {
|
||||||
// if err := p.SetEncoding(escpos.CharPagePC427); err != nil {
|
log.Fatalf("failed to print image: %v\n", err)
|
||||||
// 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] != "" {
|
if len(os.Args) > 2 && os.Args[2] != "" {
|
||||||
// fmt.Fprintf(p, "\n%s\n", 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 {
|
if err := p.Wait(); err != nil {
|
||||||
log.Fatalf("failed to print: %v\n", err)
|
log.Fatalf("failed to print: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {}
|
func main() {}
|
||||||
|
|
|
@ -1,82 +0,0 @@
|
||||||
/* Code generated by cmd/cgo; DO NOT EDIT. */
|
|
||||||
|
|
||||||
/* package command-line-arguments */
|
|
||||||
|
|
||||||
|
|
||||||
#line 1 "cgo-builtin-export-prolog"
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#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 <complex.h>
|
|
||||||
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
|
|
14
test.py
14
test.py
|
@ -1,9 +1,13 @@
|
||||||
import ctypes
|
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'))
|
#epson.cut(b"/dev/usb/lp0")
|
||||||
mylib.print_text("/dev/usb/lp0".encode('utf-8'), "test\n".encode('utf-8'))
|
#epson.cut(b"/dev/usb/lp0")
|
||||||
mylib.print_text("/dev/usb/lp0".encode('utf-8'), "test\n".encode('utf-8'))
|
#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")
|
||||||
|
|
Loading…
Reference in a new issue