diff --git a/.gitignore b/.gitignore index 1930c0b..333e5fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ *.so *.h *.swp -*/__pycache__ -flask/static -*/output.png +__pycache__ +static +*output.png diff --git a/.gitmodules b/.gitmodules index b45dcdf..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "flask/epson"] - path = flask/epson - url = https://mid-kid.root.sx/git/mid-kid/epson.git diff --git a/Makefile b/Makefile index a3e8756..c510656 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ build: - cd faxmachine &&\ + cd epson/faxmachine &&\ go build -o ../library_bridge.so -buildmode=c-shared ../library_bridge.go diff --git a/flask/app.py b/app.py similarity index 100% rename from flask/app.py rename to app.py diff --git a/faxmachine/LICENSE b/epson/faxmachine/LICENSE similarity index 100% rename from faxmachine/LICENSE rename to epson/faxmachine/LICENSE diff --git a/faxmachine/cmd/printimg/main.go b/epson/faxmachine/cmd/printimg/main.go similarity index 100% rename from faxmachine/cmd/printimg/main.go rename to epson/faxmachine/cmd/printimg/main.go diff --git a/faxmachine/cmd/printtext/main.go b/epson/faxmachine/cmd/printtext/main.go similarity index 100% rename from faxmachine/cmd/printtext/main.go rename to epson/faxmachine/cmd/printtext/main.go diff --git a/faxmachine/config/config.go b/epson/faxmachine/config/config.go similarity index 100% rename from faxmachine/config/config.go rename to epson/faxmachine/config/config.go diff --git a/faxmachine/escpos/component.go b/epson/faxmachine/escpos/component.go similarity index 100% rename from faxmachine/escpos/component.go rename to epson/faxmachine/escpos/component.go diff --git a/faxmachine/escpos/encoding.go b/epson/faxmachine/escpos/encoding.go similarity index 100% rename from faxmachine/escpos/encoding.go rename to epson/faxmachine/escpos/encoding.go diff --git a/faxmachine/escpos/escpos.go b/epson/faxmachine/escpos/escpos.go similarity index 100% rename from faxmachine/escpos/escpos.go rename to epson/faxmachine/escpos/escpos.go diff --git a/faxmachine/escpos/printer/printer.go b/epson/faxmachine/escpos/printer/printer.go similarity index 100% rename from faxmachine/escpos/printer/printer.go rename to epson/faxmachine/escpos/printer/printer.go diff --git a/faxmachine/escpos/protocol/build.go b/epson/faxmachine/escpos/protocol/build.go similarity index 100% rename from faxmachine/escpos/protocol/build.go rename to epson/faxmachine/escpos/protocol/build.go diff --git a/faxmachine/escpos/protocol/parse.go b/epson/faxmachine/escpos/protocol/parse.go similarity index 100% rename from faxmachine/escpos/protocol/parse.go rename to epson/faxmachine/escpos/protocol/parse.go diff --git a/faxmachine/escpos/protocol/protocol.go b/epson/faxmachine/escpos/protocol/protocol.go similarity index 100% rename from faxmachine/escpos/protocol/protocol.go rename to epson/faxmachine/escpos/protocol/protocol.go diff --git a/faxmachine/escpos/rasterize.go b/epson/faxmachine/escpos/rasterize.go similarity index 100% rename from faxmachine/escpos/rasterize.go rename to epson/faxmachine/escpos/rasterize.go diff --git a/faxmachine/go.mod b/epson/faxmachine/go.mod similarity index 100% rename from faxmachine/go.mod rename to epson/faxmachine/go.mod diff --git a/faxmachine/go.sum b/epson/faxmachine/go.sum similarity index 100% rename from faxmachine/go.sum rename to epson/faxmachine/go.sum diff --git a/faxmachine/keyboard/keyboard.go b/epson/faxmachine/keyboard/keyboard.go similarity index 100% rename from faxmachine/keyboard/keyboard.go rename to epson/faxmachine/keyboard/keyboard.go diff --git a/faxmachine/main.go b/epson/faxmachine/main.go similarity index 100% rename from faxmachine/main.go rename to epson/faxmachine/main.go diff --git a/faxmachine/matrix/bot.go b/epson/faxmachine/matrix/bot.go similarity index 100% rename from faxmachine/matrix/bot.go rename to epson/faxmachine/matrix/bot.go diff --git a/faxmachine/matrix/matrix.go b/epson/faxmachine/matrix/matrix.go similarity index 100% rename from faxmachine/matrix/matrix.go rename to epson/faxmachine/matrix/matrix.go diff --git a/faxmachine/matrix/slog.go b/epson/faxmachine/matrix/slog.go similarity index 100% rename from faxmachine/matrix/slog.go rename to epson/faxmachine/matrix/slog.go diff --git a/faxmachine/ntfy/ntfy.go b/epson/faxmachine/ntfy/ntfy.go similarity index 100% rename from faxmachine/ntfy/ntfy.go rename to epson/faxmachine/ntfy/ntfy.go diff --git a/faxmachine/printer/printer.go b/epson/faxmachine/printer/printer.go similarity index 100% rename from faxmachine/printer/printer.go rename to epson/faxmachine/printer/printer.go diff --git a/faxmachine/printjob/job.go b/epson/faxmachine/printjob/job.go similarity index 100% rename from faxmachine/printjob/job.go rename to epson/faxmachine/printjob/job.go diff --git a/faxmachine/slog_multi.go b/epson/faxmachine/slog_multi.go similarity index 100% rename from faxmachine/slog_multi.go rename to epson/faxmachine/slog_multi.go diff --git a/faxmachine/xkbcommon/xkbcommon.go b/epson/faxmachine/xkbcommon/xkbcommon.go similarity index 100% rename from faxmachine/xkbcommon/xkbcommon.go rename to epson/faxmachine/xkbcommon/xkbcommon.go diff --git a/library_bridge.go b/epson/library_bridge.go similarity index 55% rename from library_bridge.go rename to epson/library_bridge.go index aa76f0b..ab649e6 100644 --- a/library_bridge.go +++ b/epson/library_bridge.go @@ -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) } diff --git a/flask/epson b/flask/epson deleted file mode 160000 index 37ab172..0000000 --- a/flask/epson +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 37ab1729e6cdb212619bd4c12097ef0d926c0c2c diff --git a/flask/print.py b/flask/print.py deleted file mode 100644 index 795f682..0000000 --- a/flask/print.py +++ /dev/null @@ -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() - - diff --git a/flask/format_image.py b/format_image.py similarity index 100% rename from flask/format_image.py rename to format_image.py diff --git a/flask/gen_image.py b/gen_image.py similarity index 90% rename from flask/gen_image.py rename to gen_image.py index 1e9ea0f..5fa5f3f 100644 --- a/flask/gen_image.py +++ b/gen_image.py @@ -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") diff --git a/print.py b/print.py new file mode 100644 index 0000000..f6706bf --- /dev/null +++ b/print.py @@ -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() + + diff --git a/flask/printer_info.py b/printer_info.py similarity index 100% rename from flask/printer_info.py rename to printer_info.py diff --git a/flask/ComicMono.ttf b/resources/ComicMono.ttf similarity index 100% rename from flask/ComicMono.ttf rename to resources/ComicMono.ttf diff --git a/flask/templates/base.html b/templates/base.html similarity index 100% rename from flask/templates/base.html rename to templates/base.html diff --git a/flask/templates/image.html b/templates/image.html similarity index 100% rename from flask/templates/image.html rename to templates/image.html diff --git a/flask/templates/text.html b/templates/text.html similarity index 100% rename from flask/templates/text.html rename to templates/text.html diff --git a/test.py b/test.py deleted file mode 100644 index c51655f..0000000 --- a/test.py +++ /dev/null @@ -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")