From c9e5b4d1e9affe3309fc2eae2aed73bc554117b9 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch
Date: Fri, 12 Apr 2019 02:15:28 +0200
Subject: Create a package for the printer
---
brother-info/main.go | 66 +++-------------
label-exp/main.go | 46 +++--------
ql/ql.go | 218 +++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 237 insertions(+), 93 deletions(-)
create mode 100644 ql/ql.go
diff --git a/brother-info/main.go b/brother-info/main.go
index b65b1dc..a1584e1 100644
--- a/brother-info/main.go
+++ b/brother-info/main.go
@@ -1,10 +1,9 @@
package main
import (
- "io"
"log"
- "os"
- "time"
+
+ "janouch.name/sklad/ql"
)
func decodeBitfieldErrors(b byte, errors [8]string) []string {
@@ -19,25 +18,6 @@ func decodeBitfieldErrors(b byte, errors [8]string) []string {
// -----------------------------------------------------------------------------
-type brotherStatus struct {
- errors []string
-}
-
-// TODO: What exactly do we need? Probably extend as needed.
-func decodeStatusInformation(d []byte) brotherStatus {
- var status brotherStatus
- status.errors = append(status.errors, decodeBitfieldErrors(d[8], [8]string{
- "no media", "end of media", "cutter jam", "?", "printer in use",
- "printer turned off", "high-voltage adapter", "fan motor error"})...)
- status.errors = append(status.errors, decodeBitfieldErrors(d[9], [8]string{
- "replace media", "expansion buffer full", "communication error",
- "communication buffer full", "cover open", "cancel key",
- "media cannot be fed", "system error"})...)
- return status
-}
-
-// -----------------------------------------------------------------------------
-
func printStatusInformation(d []byte) {
if d[0] != 0x80 || d[1] != 0x20 || d[2] != 0x42 || d[3] != 0x34 {
log.Println("unexpected status fixed bytes")
@@ -157,51 +137,23 @@ func printStatusInformation(d []byte) {
}
func main() {
- // Linux usblp module, located in /drivers/usb/class/usblp.c
- // (at least that's where the trails go, I don't understand the code)
- f, err := os.OpenFile("/dev/usb/lp0", os.O_RDWR, 0)
+ printer, err := ql.Open()
if err != nil {
log.Fatalln(err)
}
- defer f.Close()
-
- // Flush any former responses in the printer's queue.
- for {
- dummy := make([]byte, 32)
- if _, err := f.Read(dummy); err == io.EOF {
- break
- }
- }
-
- // Clear the print buffer.
- invalidate := make([]byte, 400)
- if _, err := f.Write(invalidate); err != nil {
- log.Fatalln(err)
+ if printer == nil {
+ log.Fatalln("no suitable printer found")
}
+ defer printer.Close()
- // Initialize.
- if _, err := f.WriteString("\x1b\x40"); err != nil {
+ if err := printer.Initialize(); err != nil {
log.Fatalln(err)
}
- // Request status information.
- if _, err := f.WriteString("\x1b\x69\x53"); err != nil {
+ status, err := printer.GetStatus()
+ if err != nil {
log.Fatalln(err)
}
- // We need to poll the device.
- status := make([]byte, 32)
- for {
- if n, err := f.Read(status); err == io.EOF {
- time.Sleep(10 * time.Millisecond)
- } else if err != nil {
- log.Fatalln(err)
- } else if n < 32 {
- log.Fatalln("invalid read")
- } else {
- break
- }
- }
-
printStatusInformation(status)
}
diff --git a/label-exp/main.go b/label-exp/main.go
index b530f2f..d1c203f 100644
--- a/label-exp/main.go
+++ b/label-exp/main.go
@@ -18,6 +18,7 @@ import (
"github.com/boombuler/barcode/qr"
"janouch.name/sklad/bdf"
+ "janouch.name/sklad/ql"
)
// scaler is a scaling image.Image wrapper.
@@ -233,55 +234,28 @@ func printLabel(src image.Image) error {
// ---
- // Linux usblp module, located in /drivers/usb/class/usblp.c
- // (at least that's where the trails go, I don't understand the code)
- f, err := os.OpenFile("/dev/usb/lp0", os.O_RDWR, 0)
+ printer, err := ql.Open()
if err != nil {
return err
}
- defer f.Close()
-
- // Flush any former responses in the printer's queue.
- for {
- dummy := make([]byte, 32)
- if _, err := f.Read(dummy); err == io.EOF {
- break
- }
+ if printer == nil {
+ return errors.New("no suitable printer found")
}
+ defer printer.Close()
- // Clear the print buffer.
- invalidate := make([]byte, 400)
- if _, err := f.Write(invalidate); err != nil {
+ if err := printer.Initialize(); err != nil {
return err
}
- // Initialize.
- if _, err := f.WriteString("\x1b\x40"); err != nil {
- return err
- }
-
- // Request status information.
- if _, err := f.WriteString("\x1b\x69\x53"); err != nil {
+ status, err := printer.GetStatus()
+ if err != nil {
return err
}
- // We need to poll the device.
- status := make([]byte, 32)
- for {
- if n, err := f.Read(status); err == io.EOF {
- time.Sleep(10 * time.Millisecond)
- } else if err != nil {
- return err
- } else if n < 32 {
- return errors.New("invalid read")
- } else {
- break
- }
- }
printStatusInformation(status)
// Print the prepared data.
- if _, err := f.Write(data); err != nil {
+ if _, err := printer.File.Write(data); err != nil {
return err
}
@@ -294,7 +268,7 @@ func printLabel(src image.Image) error {
if time.Now().Sub(start) > 3*time.Second {
break
}
- if n, err := f.Read(status); err == io.EOF {
+ if n, err := printer.File.Read(status); err == io.EOF {
time.Sleep(100 * time.Millisecond)
} else if err != nil {
return err
diff --git a/ql/ql.go b/ql/ql.go
new file mode 100644
index 0000000..9f507da
--- /dev/null
+++ b/ql/ql.go
@@ -0,0 +1,218 @@
+// Package ql is a Linux driver for Brother QL-series printers.
+package ql
+
+// Resources:
+// http://www.undocprint.org/formats/page_description_languages/brother_p-touch
+
+import (
+ "errors"
+ "io"
+ "os"
+ "path/filepath"
+ "regexp"
+ "strings"
+ "syscall"
+ "time"
+ "unsafe"
+)
+
+// #include