From 4c2f29384dc13f0e63393ac21ee33ee0881b750b Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Fri, 12 Apr 2019 21:50:36 +0200 Subject: Move printer stuff into the ql package --- label-exp/main.go | 254 ++++-------------------------------------------------- 1 file changed, 17 insertions(+), 237 deletions(-) (limited to 'label-exp') diff --git a/label-exp/main.go b/label-exp/main.go index 673ee65..e15f71c 100644 --- a/label-exp/main.go +++ b/label-exp/main.go @@ -7,12 +7,10 @@ import ( "image/color" "image/draw" "image/png" - "io" "log" "net/http" "os" "strconv" - "time" "github.com/boombuler/barcode" "github.com/boombuler/barcode/qr" @@ -74,218 +72,6 @@ func (lr *leftRotate) At(x, y int) color.Color { // ----------------------------------------------------------------------------- -func decodeBitfieldErrors(b byte, errors [8]string) []string { - var result []string - for i := uint(0); i < 8; i++ { - if b&(1<= bounds.Min.X; x-- { - // TODO: Anything to do with the ColorModel? - r, g, b, a := src.At(x, y).RGBA() - pixels[off] = r == 0 && g == 0 && b == 0 && a != 0 - off++ - } - - data = append(data, 'g', 0x00, 90) - for i := 0; i < 90; i++ { - var b byte - for j := 0; j < 8; j++ { - b <<= 1 - if pixels[i*8+j] { - b |= 1 - } - } - data = append(data, b) - } - } - for ; length > 0; length-- { - data = append(data, 'g', 0x00, 90) - data = append(data, make([]byte, 90)...) - } - return -} - -func printLabel(printer *ql.Printer, src image.Image, - status *ql.Status, mediaInfo *ql.MediaInfo) error { - data := []byte(nil) - - // Raster mode. - // Should be the only supported mode for QL-800. - data = append(data, 0x1b, 0x69, 0x61, 0x01) - - // Automatic status mode (though it's the default). - data = append(data, 0x1b, 0x69, 0x21, 0x00) - - // Print information command. - dy := src.Bounds().Dy() - if mediaInfo.PrintAreaLength != 0 { - dy = mediaInfo.PrintAreaLength - } - - mediaType := byte(0x0a) - if status.MediaLengthMM != 0 { - mediaType = byte(0x0b) - } - - data = append(data, 0x1b, 0x69, 0x7a, 0x02|0x04|0x40|0x80, mediaType, - byte(status.MediaWidthMM), byte(status.MediaLengthMM), - byte(dy), byte(dy>>8), byte(dy>>16), byte(dy>>24), 0, 0x00) - - // Auto cut, each 1 label. - data = append(data, 0x1b, 0x69, 0x4d, 0x40) - data = append(data, 0x1b, 0x69, 0x41, 0x01) - - // Cut at end (though it's the default). - // Not sure what it means, doesn't seem to have any effect to turn it off. - data = append(data, 0x1b, 0x69, 0x4b, 0x08) - - if status.MediaLengthMM != 0 { - // 3mm margins along the direction of feed. 0x23 = 35 dots, the minimum. - data = append(data, 0x1b, 0x69, 0x64, 0x23, 0x00) - } else { - // May not set anything other than zero. - data = append(data, 0x1b, 0x69, 0x64, 0x00, 0x00) - } - - // Compression mode: no compression. - // Should be the only supported mode for QL-800. - data = append(data, 0x4d, 0x00) - - // The graphics data itself. - data = append(data, genLabelData(src, mediaInfo.SideMarginPins, dy)...) - - // Print command with feeding. - data = append(data, 0x1a) - - // --- - - // Print the prepared data. - if _, err := printer.File.Write(data); err != nil { - return err - } - - // TODO: We specifically need to wait for a transition to the receiving - // state, and try to figure out something from the statuses. - // We may also receive an error status instead of the transition to - // the printing state. Or even after it. - start, b := time.Now(), make([]byte, 32) - for { - if time.Now().Sub(start) > 3*time.Second { - break - } - if n, err := printer.File.Read(b); err == io.EOF { - time.Sleep(100 * time.Millisecond) - } else if err != nil { - return err - } else if n < 32 { - return errors.New("invalid read") - } else { - printStatusInformation(b) - } - } - return nil -} - -// ----------------------------------------------------------------------------- - var font *bdf.Font func genLabelForHeight(text string, height, scale int) image.Image { @@ -339,9 +125,9 @@ var tmpl = template.Must(template.New("form").Parse(`

Printer: {{ .Printer.Manufacturer }} {{ .Printer.Model }}

Tape: - {{ if .Status }} - {{ .Status.MediaWidthMM }} mm × - {{ .Status.MediaLengthMM }} mm + {{ if .Printer.LastStatus }} + {{ .Printer.LastStatus.MediaWidthMM }} mm × + {{ .Printer.LastStatus.MediaLengthMM }} mm {{ if .MediaInfo }} (offset: {{ .MediaInfo.SideMarginPins }} pt, @@ -350,8 +136,8 @@ var tmpl = template.Must(template.New("form").Parse(` (unknown media) {{ end }} - {{ if .Status.Errors }} - {{ range .Status.Errors }} + {{ if .Printer.LastStatus.Errors }} + {{ range .Printer.LastStatus.Errors }}

Error: {{ . }} {{ end }} {{ end }} @@ -389,16 +175,15 @@ func getPrinter() (*ql.Printer, error) { return printer, nil } -func getStatus(printer *ql.Printer) (*ql.Status, error) { +func getStatus(printer *ql.Printer) error { if err := printer.Initialize(); err != nil { - return nil, err + return err } - if data, err := printer.GetStatus(); err != nil { - return nil, err - } else { - printStatusInformation(data) - return ql.DecodeStatus(data), nil + if err := printer.UpdateStatus(); err != nil { + return err } + log.Printf("status\n%s", printer.LastStatus) + return nil } func handle(w http.ResponseWriter, r *http.Request) { @@ -407,25 +192,22 @@ func handle(w http.ResponseWriter, r *http.Request) { return } - var ( - status *ql.Status - initErr error - ) + var initErr error printer, printerErr := getPrinter() if printerErr == nil { defer printer.Close() - status, initErr = getStatus(printer) + initErr = getStatus(printer) } var mediaInfo *ql.MediaInfo - if status != nil { - mediaInfo = ql.GetMediaInfo(status.MediaWidthMM, status.MediaLengthMM) + if printer.LastStatus != nil { + mediaInfo = ql.GetMediaInfo(printer.LastStatus.MediaWidthMM(), + printer.LastStatus.MediaLengthMM()) } var params = struct { Printer *ql.Printer PrinterErr error - Status *ql.Status InitErr error MediaInfo *ql.MediaInfo Text string @@ -433,7 +215,6 @@ func handle(w http.ResponseWriter, r *http.Request) { }{ Printer: printer, PrinterErr: printerErr, - Status: status, InitErr: initErr, MediaInfo: mediaInfo, Text: r.FormValue("text"), @@ -450,8 +231,7 @@ func handle(w http.ResponseWriter, r *http.Request) { label = &leftRotate{image: genLabelForHeight( params.Text, mediaInfo.PrintAreaPins, params.Scale)} if r.FormValue("print") != "" { - if err := printLabel( - printer, label, status, mediaInfo); err != nil { + if err := printer.Print(label); err != nil { log.Println("print error:", err) } } -- cgit v1.2.3-70-g09d2