-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptprint.go
506 lines (458 loc) · 11.9 KB
/
ptprint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package main
// Much of this was based on ptprint.rb, linked from
// http://www.undocprint.org/formats/page_description_languages/brother_p-touch
// as well as the other docs linked from that page
import (
"bytes"
"encoding/base64"
"encoding/binary"
"errors"
"flag"
"fmt"
"html/template"
"image"
_ "image/png"
"io"
"log"
"net/http"
"os"
"os/exec"
"time"
)
var dev = flag.String("dev", "/dev/usb/lp1", "The USB device of the label printer")
var port = flag.Int("port", 40404, "The port the server should listen on")
var convert = flag.String("convert", "/usr/bin/convert", "Path to ImageMagick's convert utility")
type TransientError int
const (
AllIsWell TransientError = iota
NoTapeCartridge
TapeRanOut
TapeJammed
CoverOpen
)
type HWVersion byte
const (
PT2430PC HWVersion = 0x5a
PTP700 HWVersion = 0x67
)
type Printer struct {
f *os.File
wc chan []byte
ec chan error
mediaWidth int
te TransientError
hwVer HWVersion
}
// MediaWidth returns the width of the currently-inserted media in mm
func (p *Printer) MediaWidth() int {
return p.mediaWidth
}
func (p *Printer) TransientError() TransientError {
return p.te
}
func (p *Printer) Status() string {
switch p.te {
case AllIsWell:
return fmt.Sprintf("Printer OK<br />%dmm tape inserted", p.mediaWidth)
case NoTapeCartridge:
return "No tape inserted!"
case TapeRanOut:
return "The tape has run out!"
case TapeJammed:
return "The tape is jammed!"
case CoverOpen:
return "The printer's cover is open!"
}
return "Unknown transient error, this should never happen"
}
func (p *Printer) rawWrite(b []byte) error {
n, err := p.f.Write(b)
if n != len(b) || err != nil {
return fmt.Errorf("failed writing, wrote %d bytes, err %v", n, err)
}
return nil
}
func (p *Printer) Write(b []byte) error {
p.wc <- b
return <-p.ec
}
// Status is the somewhat-silly mostly-zeroes status reply from the printer.
// All of the fields prefixed "res" are marked "reserved" in the documentation,
// although some of them have actual meanings.
type Status struct {
PrintHeadMark byte
Size byte
ResFixed1 byte
ResFixed2 byte
ResHWVersion byte
ResFixed3 byte
ResZero0 byte
ResZero1 byte
Error1 byte
Error2 byte
MediaWidth byte
Mediatype byte
ResZero2 byte
ResZero3 byte
ResZero4 byte
ResZero5 byte
ResZero6 byte
MediaLength byte
StatusType byte
PhaseType byte
PhaseHigh byte
PhaseLow byte
NotifNum byte
ResZero7 byte
ResZero8 byte
ResZero9 byte
ResZeroA byte
ResZeroB byte
ResZeroC byte
ResZeroD byte
ResZeroE byte
ResZeroF byte
}
func (p *Printer) readStatus() (*Status, error) {
s := Status{}
var err error
// The printer is slow and its USB interface is buggy. This leads to
// it wantonly returning EOF rather than blocking, even when data will
// still be delivered.
for i := 0; i < 10; i++ {
err = binary.Read(p.f, binary.LittleEndian, &s)
if err == nil || err != io.EOF {
break
}
log.Printf("EOF reading status, try %d", i)
time.Sleep(100 * time.Millisecond)
}
if err != nil {
return nil, fmt.Errorf("Could not read status: %v", err)
}
return &s, nil
}
func checkStatus(s *Status) (TransientError, error) {
if s.PrintHeadMark != 0x80 {
return 0, fmt.Errorf("wanted PrintHeadMark 0x80, got 0x%02X", s.PrintHeadMark)
}
if s.Size != 32 {
return 0, fmt.Errorf("wanted Size 32, got %d", s.Size)
}
if s.ResFixed1 != 0x42 {
return 0, fmt.Errorf("wanted Fixed1 0x42, got 0x%02X", s.ResFixed1)
}
if s.ResFixed2 != 0x30 {
return 0, fmt.Errorf("wanted Fixed2 0x30, got 0x%02X", s.ResFixed2)
}
switch HWVersion(s.ResHWVersion) {
case PT2430PC:
log.Println("Hardware is a PT-2430PC")
case PTP700:
log.Println("Hardware is a PT-P700")
default:
return 0, fmt.Errorf("unknown ResHWVersion, got 0x%02X", s.ResHWVersion)
}
if s.ResFixed3 != 0x30 {
return 0, fmt.Errorf("wanted Fixed3 0x30, got 0x%02X", s.ResFixed3)
}
var te TransientError
if (s.Error1 & 0x01) != 0x00 {
te = NoTapeCartridge
} else if (s.Error1 & 0x02) != 0x00 {
te = TapeRanOut
} else if (s.Error1 & 0x04) != 0x00 {
te = TapeJammed
} else if s.Error1 != 0x00 {
return 0, fmt.Errorf("unknown Error1 %02X", s.Error1)
}
if (s.Error2 & 0x04) != 0x00 {
return 0, errors.New("transmission error")
} else if (s.Error2 & 0x40) != 0x00 {
return 0, errors.New("cannot feed print media")
} else if (s.Error2 & 0x10) != 0x00 {
if te == AllIsWell {
te = CoverOpen
}
} else if s.Error2 != 0x00 {
return 0, fmt.Errorf("unknown ErrorInfo2 %02X", s.Error2)
}
return te, nil
}
func (p *Printer) run() {
for {
select {
case w := <-p.wc:
err := p.rawWrite(w)
p.ec <- err
case <-time.After(10 * time.Second):
var err error
p.te, err = p.checkStatus()
if err != nil {
log.Fatalf("Regular status inquiry failed: %v", err)
}
log.Printf("Status OK, TransientError %v, Media width %d", p.te, p.mediaWidth)
}
}
}
func (p *Printer) checkStatus() (TransientError, error) {
getStatus := []byte{0x1B, 'i', 'S'}
err := p.rawWrite(getStatus)
if err != nil {
return 0, fmt.Errorf("unable to ask printer for status: %v", err)
}
s, err := p.readStatus()
if err != nil {
return 0, fmt.Errorf("error reading printer status: %v", err)
}
te, err := checkStatus(s)
if err != nil {
return 0, fmt.Errorf("printer reports error: %v", err)
}
p.mediaWidth = int(s.MediaWidth)
p.hwVer = HWVersion(s.ResHWVersion)
return te, nil
}
func NewPrinter(devicePath string) (*Printer, error) {
f, err := os.OpenFile(devicePath, os.O_RDWR, 0)
if err != nil {
return nil, fmt.Errorf("unable to open printer %s: %v", devicePath, err)
}
p := Printer{}
p.f = f
p.wc = make(chan []byte, 1)
p.ec = make(chan error, 1)
start := make([]byte, 200)
reset := []byte{0x1B, '@'}
setAutoCut := []byte{0x1B, 'i', 'M', 0x48} // Auto cut, small feed amount
setFullCut := []byte{0x1B, 'i', 'K', 0x08} // Cut all the way through after every print
setCompression := []byte{'M', 0x02} // Use RLE compression (which we won't actually do, but whatever)
err = p.rawWrite(start)
if err != nil {
return nil, fmt.Errorf("unable to start communication: %v", err)
}
err = p.rawWrite(reset)
if err != nil {
return nil, fmt.Errorf("unable to reset printer: %v", err)
}
p.te, err = p.checkStatus()
if err != nil {
return nil, fmt.Errorf("status problem: %v", err)
}
err = p.rawWrite(setAutoCut)
if err != nil {
return nil, fmt.Errorf("unable to set auto-cut: %v", err)
}
err = p.rawWrite(setFullCut)
if err != nil {
return nil, fmt.Errorf("unable to set full cut: %v", err)
}
err = p.rawWrite(setCompression)
if err != nil {
return nil, fmt.Errorf("unable to set compression: %v", err)
}
return &p, nil
}
func mediaWidthToPixels(w int) int {
switch w {
case 9:
return 64
case 12:
return 96
default:
return 128
}
}
type rootHandler struct {
p *Printer
t *template.Template
}
func NewRootHandler(p *Printer) (*rootHandler, error) {
rh := rootHandler{}
rh.p = p
var err error
rh.t, err = template.ParseFiles("textform.html")
if err != nil {
return nil, fmt.Errorf("failed parsing root template: %v", err)
}
return &rh, nil
}
func (rh *rootHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
d := struct {
StatusOK bool
Status template.HTML
}{
rh.p.TransientError() == AllIsWell,
template.HTML(rh.p.Status()),
}
rh.t.Execute(w, d)
}
type previewHandler struct {
p *Printer
t *template.Template
}
func NewPreviewHandler(p *Printer) (*previewHandler, error) {
ph := previewHandler{}
ph.p = p
var err error
ph.t, err = template.ParseFiles("preview.html")
if err != nil {
return nil, fmt.Errorf("failed parsing preview template: %v", err)
}
return &ph, nil
}
func (h *previewHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
text := r.FormValue("text")
s := fmt.Sprintf("x%d", mediaWidthToPixels(h.p.MediaWidth()))
c := exec.Command(*convert, "+antialias", "-background", "white", "-fill", "black", "-size", s, "-gravity", "South", "label:"+text, "png:-")
log.Printf("Preview running command '%v'", c)
png, err := c.Output()
if err != nil {
w.WriteHeader(502)
fmt.Fprintf(w, "Error generating preview: %v", err)
return
}
d := struct {
StatusOK bool
Status template.HTML
Text string
Image string
}{
h.p.TransientError() == AllIsWell,
template.HTML(h.p.Status()),
text,
base64.StdEncoding.EncodeToString(png),
}
h.t.Execute(w, d)
}
type printHandler struct {
p *Printer
t *template.Template
}
func NewPrintHandler(p *Printer) (*printHandler, error) {
ph := printHandler{}
ph.p = p
var err error
ph.t, err = template.ParseFiles("print.html")
if err != nil {
return nil, fmt.Errorf("failed parsing print template: %v", err)
}
return &ph, nil
}
func (h *printHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
text := r.FormValue("text")
s := fmt.Sprintf("x%d", mediaWidthToPixels(h.p.MediaWidth()))
c := exec.Command(*convert, "+antialias", "-background", "white", "-fill", "black", "-size", s, "-gravity", "South", "-rotate", "-90", "label:"+text, "png:-")
log.Printf("Print running command '%v'", c)
png, err := c.Output()
if err != nil {
w.WriteHeader(502)
fmt.Fprintf(w, "Error generating print data: %v", err)
return
}
pngr := bytes.NewReader(png)
pngc, ifmt, err := image.DecodeConfig(pngr)
if err != nil {
w.WriteHeader(502)
fmt.Fprintf(w, "Error decoding print PNG config: %v", err)
return
}
_, err = pngr.Seek(0, 0)
if err != nil {
w.WriteHeader(502)
fmt.Fprintf(w, "Error seeking print PNG: %v", err)
return
}
pngi, ifmt, err := image.Decode(pngr)
if err != nil {
w.WriteHeader(502)
fmt.Fprintf(w, "Error decoding print PNG: %v", err)
return
}
if ifmt != "png" {
w.WriteHeader(502)
fmt.Fprintf(w, "Error with print PNG, want format 'png', got '%s'", ifmt)
return
}
log.Printf("Read print data in format '%s', width %d, height %d", ifmt, pngc.Width, pngc.Height)
padLeft := (128 - pngc.Width) / 2
padRight := 128 - (pngc.Width + padLeft)
for y := pngc.Height - 1; y >= 0; y-- {
line := make([]byte, 128/8+4) // The printer always wants 128 pixels of data, but for narrow band, only prints the middle bit
line[0] = 'G'
line[1] = 0x11
line[2] = 0x00
line[3] = 0x0f
lc := 4
for x := 0; x < padLeft; x += 8 {
line[lc] = byte(0)
lc++
}
for x := 0; x < pngc.Width; x += 8 {
by := uint32(0)
for b := 0; b < 8; b++ {
pr, _, _, _ := pngi.At(x+b, y).RGBA()
if pr == 0 {
by = by | (128 >> uint(b))
}
}
line[lc] = byte(by)
lc++
}
for x := 0; x < padRight; x += 8 {
line[lc] = byte(0)
lc++
}
err = h.p.Write(line)
if err != nil {
w.WriteHeader(502)
fmt.Fprintf(w, "Error writing print data: %v", err)
return
}
}
endPrint := []byte{0x1a}
err = h.p.Write(endPrint)
if err != nil {
w.WriteHeader(502)
fmt.Fprintf(w, "Error writing end-of-print: %v", err)
return
}
d := struct {
StatusOK bool
Status template.HTML
Text string
Image string
}{
h.p.TransientError() == AllIsWell,
template.HTML(h.p.Status()),
text,
base64.StdEncoding.EncodeToString(png),
}
h.t.Execute(w, d)
}
func main() {
flag.Parse()
p, err := NewPrinter(*dev)
if err != nil {
log.Fatalf("Could not initialize printer: %v", err)
}
log.Printf("Printer initialized successfully. Media width is %dmm.\n", p.MediaWidth())
go p.run()
rh, err := NewRootHandler(p)
if err != nil {
log.Fatalf("Could not make root handler: %v", err)
}
http.Handle("/", rh)
preh, err := NewPreviewHandler(p)
if err != nil {
log.Fatalf("Could not make preview handler: %v", err)
}
http.Handle("/preview", preh)
prih, err := NewPrintHandler(p)
if err != nil {
log.Fatalf("Could not make print handler: %v", err)
}
http.Handle("/print", prih)
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
}