Fully integrate the go library and reorganize files

This commit is contained in:
TT-392 2024-09-15 20:53:03 +02:00
parent 78456105ac
commit 1d0f5cf199
40 changed files with 86 additions and 74 deletions

6
.gitignore vendored
View file

@ -1,6 +1,6 @@
*.so
*.h
*.swp
*/__pycache__
flask/static
*/output.png
__pycache__
static
*output.png

3
.gitmodules vendored
View file

@ -1,3 +0,0 @@
[submodule "flask/epson"]
path = flask/epson
url = https://mid-kid.root.sx/git/mid-kid/epson.git

View file

@ -1,4 +1,4 @@
build:
cd faxmachine &&\
cd epson/faxmachine &&\
go build -o ../library_bridge.so -buildmode=c-shared ../library_bridge.go

View file

@ -8,7 +8,6 @@ import (
_ "image/jpeg"
_ "image/png"
"log"
"os"
"unsafe"
"bytes"
@ -22,17 +21,22 @@ import (
import "C"
const red = "\033[31m"
const reset = "\033[0m"
var err error
func printer_init(path string, speed int) (*escpos.Printer) {
func printer_init(path string, speed int) (*escpos.Printer, error) {
p, err := escpos.StartUSBPrinter(path, protocol.TMT88IV, escpos.FlagNone)
if err != nil {
log.Fatalf("failed to start printer: %v", err)
fmt.Printf("%sError: failed to start printer: %v%s\n", red, err, reset)
return p, err
}
if err := p.EnableASB(protocol.ASBReportAll); err != nil {
p.Close()
log.Fatalf("failed to enable ASB: %v", err)
fmt.Printf("%sError: failed enable ASB: %v%s\n", red, err, reset)
return p, err
}
go func() {
@ -42,38 +46,49 @@ func printer_init(path string, speed int) (*escpos.Printer) {
}()
if err := p.SetPrintSpeed(speed); err != nil {
log.Fatalf("failed to set print speed: %v\n", err)
fmt.Printf("%sError: Failed to set print speed: %v%s\n", red, err, reset)
return p, err
}
return p
return p, nil
}
func printer_close(p *escpos.Printer) {
func printer_close(p *escpos.Printer) (error) {
if err = p.Wait(); err != nil {
log.Fatalf("failed to print: %v\n", err)
fmt.Printf("%sError: Failed to print: %v%s\n", red, err, reset)
return err
}
if err = p.Close(); err != nil {
log.Fatalf("failed to close printer: %v\n", err)
fmt.Printf("%sError: Failed close printer: %v%s\n", red, err, reset)
return err
}
fmt.Print("printer closed\n");
return nil
}
//export cut
func cut(path *C.char) {
fmt.Print("Starting cut\n");
p := printer_init(C.GoString(path), 1)
p, err := printer_init(C.GoString(path), 1)
if err != nil {
return
}
if err = p.CutPaper(); err != nil {
log.Fatalf("failed to cut paper: %v", err)
fmt.Printf("%sError: Failed to cut paper: %v%s\n", red, err, reset)
return
}
printer_close(p)
//time.Sleep(1000 * time.Millisecond)
}
//export print_text
func print_text(path *C.char, str *C.char) {
p := printer_init(C.GoString(path), 1)
p, err := printer_init(C.GoString(path), 1)
if err != nil {
return
}
fmt.Fprint(p, C.GoString(str))
fmt.Fprint(p, "\n")
@ -83,7 +98,10 @@ func print_text(path *C.char, str *C.char) {
//export print_image
func print_image(path *C.char, data *C.uchar, size C.int) {
p := printer_init(C.GoString(path), 1)
p, err := printer_init(C.GoString(path), 1)
if err != nil {
return
}
go func() {
for status := range p.ASBStatus() {
@ -95,20 +113,15 @@ func print_image(path *C.char, data *C.uchar, size C.int) {
imgReader := bytes.NewReader(byteData)
img, _, err := image.Decode(imgReader)
if err != nil {
log.Fatalf("failed to get decode image: %v\n", err)
fmt.Printf("%sError: Failed get decode image: %v%s\n", red, err, reset)
return
}
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])
}
if err := p.Wait(); err != nil {
log.Fatalf("failed to print: %v\n", err)
fmt.Printf("%sError: Failed print image: %v%s\n", red, err, reset)
return
}
printer_close(p)
}

@ -1 +0,0 @@
Subproject commit 37ab1729e6cdb212619bd4c12097ef0d926c0c2c

View file

@ -1,27 +0,0 @@
dev_path="/dev/usb/lp0"
from format_image import *
from PIL import Image
import os
def print_text(text):
stream = open('/dev/usb/lp0', 'wb')
stream.write(text.encode('utf-8'))
stream.close()
def cut_paper():
stream = open('/dev/usb/lp0', 'wb')
stream.write(b'\x1DV\x41\0')
stream.close()
def print_image(image):
image.save("/tmp/image.png")
# I'm sorry
os.system("cd epson/; ./print_image.sh /tmp/image.png; cd ..")
#image = format_image("static/text/text.png")
#image.save("/tmp/image.png")
#print_image(image)
#cut_paper()

View file

@ -2,9 +2,11 @@ from PIL import Image, ImageDraw, ImageFont
from colorama import Fore, Style
from printer_info import *
font = "resources/ComicMono.ttf"
def gen_image(height, text1, text2):
font1 = ImageFont.truetype("ComicMono.ttf", size=text1["size"])
font2 = ImageFont.truetype("ComicMono.ttf", size=text2["size"])
font1 = ImageFont.truetype(font, size=text1["size"])
font2 = ImageFont.truetype(font, size=text2["size"])
text1["string"] = text1["string"].replace("\r\n", "\n")
text2["string"] = text2["string"].replace("\r\n", "\n")

41
print.py Normal file
View file

@ -0,0 +1,41 @@
dev_path="/dev/usb/lp0"
from format_image import *
from PIL import Image
import ctypes
import io
printer = ctypes.CDLL('./epson/library_bridge.so')
def print_text(text):
try:
printer.print_text(b"/dev/usb/lp0", text.encode('utf-8'))
except Exception as e:
print(f"An error occurred: {e}")
def cut_paper():
try:
printer.cut(b"/dev/usb/lp0")
except Exception as e:
print(f"An error occurred: {e}")
def print_image(image):
Bytes = io.BytesIO()
image.save(Bytes, format='PNG')
Bytes = Bytes.getvalue()
try:
printer.print_image(b"/dev/usb/lp0", Bytes, len(Bytes))
except Exception as e:
print(f"An error occurred: {e}")
# I'm sorry
#os.system("cd epson/; ./print_image.sh /tmp/image.png; cd ..")
#image = format_image("static/text/text.png")
#image.save("/tmp/image.png")
#print_image(image)
#cut_paper()

13
test.py
View file

@ -1,13 +0,0 @@
import ctypes
import time
epson = ctypes.CDLL('./library_bridge.so')
#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)
epson.print_image(b"/dev/usb/lp0", data, len(data))
#epson.cut(b"/dev/usb/lp0")