diff options
author | Přemysl Janouch <p@janouch.name> | 2019-04-12 02:15:28 +0200 |
---|---|---|
committer | Přemysl Janouch <p@janouch.name> | 2019-04-12 02:31:22 +0200 |
commit | c9e5b4d1e9affe3309fc2eae2aed73bc554117b9 (patch) | |
tree | 0ce08688143cef43809fde4831639e486b8a5b43 /label-exp/main.go | |
parent | e605078d8932fd76979cad6e923257e4f190a89b (diff) | |
download | sklad-c9e5b4d1e9affe3309fc2eae2aed73bc554117b9.tar.gz sklad-c9e5b4d1e9affe3309fc2eae2aed73bc554117b9.tar.xz sklad-c9e5b4d1e9affe3309fc2eae2aed73bc554117b9.zip |
Create a package for the printer
Diffstat (limited to 'label-exp/main.go')
-rw-r--r-- | label-exp/main.go | 46 |
1 files changed, 10 insertions, 36 deletions
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 |