-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.go
147 lines (126 loc) · 3.36 KB
/
print.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
package main
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"log"
"net"
"time"
)
var (
tcpDialTimeout = 1 * time.Second
tcpWriteTimeout = 5 * time.Second
printTimeout = 30 * time.Second
)
func (t Tape) writeImage(w io.Writer) {
bytes := (t.Width() + 7) / 8
for x := t.Length() - 1; x >= 0; x-- {
w.Write([]byte{0x1b, 0x2e, 0x00, 0x00, 0x00, 0x01})
binary.Write(w, binary.LittleEndian, t.Width())
for i := uint16(0); i < bytes; i++ {
var b byte
for j := uint16(0); j < 8; j++ {
y := i*8 + j
if y < t.Width() {
b |= t.getPixel(x, y)
}
if j < 7 {
b <<= 1
}
}
w.Write([]byte{b})
}
if x == 0 {
break
}
}
w.Write([]byte{0x0c})
}
func createSetTapeWidthPacket(width uint32) (b [10]byte) {
b[0], b[1] = 0x1b, 0x7b
b[2] = 0x07
b[3] = 0x4c
binary.LittleEndian.PutUint32(b[4:8], width)
// Checksum
for _, v := range b[3:8] {
b[8] += v
}
b[9] = 0x7d
return b
}
func (t Tape) writePrintRequest(w io.Writer) {
// https://github.com/hikalium/sr5900p/blob/aa1f3d42a5fbcf0a1ad2917e3bbb005196bf1bb9/src/print.rs#L82
w.Write([]byte{0x1b, 0x7b, 0x03, 0x40, 0x40, 0x7d})
w.Write([]byte{0x1b, 0x7b, 0x07, 0x7b, 0x00, 0x00, 0x53, 0x54, 0x22, 0x7d})
w.Write([]byte{0x1b, 0x7b, 0x07, 0x43, 0x02, 0x02, 0x01, 0x01, 0x49, 0x7d})
w.Write([]byte{0x1b, 0x7b, 0x04, 0x44, 0x05, 0x49, 0x7d})
w.Write([]byte{0x1b, 0x7b, 0x03, 0x47, 0x47, 0x7d})
b := createSetTapeWidthPacket(t.Length() + 4)
w.Write(b[:])
w.Write([]byte{0x1b, 0x7b, 0x05, 0x54, 0x2a, 0x00, 0x7e, 0x7d})
w.Write([]byte{0x1b, 0x7b, 0x04, 0x48, 0x05, 0x4d, 0x7d})
w.Write([]byte{0x1b, 0x7b, 0x04, 0x73, 0x00, 0x73, 0x7d})
t.writeImage(w)
w.Write([]byte{0x1b, 0x7b, 0x03, 0x40, 0x40, 0x7d})
}
func (t Tape) buildPrintRequest() *bytes.Buffer {
b := new(bytes.Buffer)
t.writePrintRequest(b)
return b
}
func (t Tape) Print(addr string) (err error) {
s, err := getStatus(addr)
if err != nil {
return fmt.Errorf("checking status: %w", err)
}
if !s.IsIdle() {
return fmt.Errorf("not idle: %x", s)
}
if s.TapeType().Width() < t.Width() {
return fmt.Errorf("print width is longer than %d (got %d)", s.TapeType().Width(), t.Width())
}
if err := lockPrinter(addr); err != nil {
return fmt.Errorf("requesting lock: %w", err)
}
log.Printf("connecting tcp")
c, err := net.DialTimeout("tcp4", addr, tcpDialTimeout)
if err != nil {
return fmt.Errorf("connecting tcp: %w", err)
}
log.Printf("connected")
if err := startPrint(addr); err != nil {
return fmt.Errorf("requesting start: %w", err)
}
log.Printf("sending print: %x", t.buildPrintRequest().Bytes())
c.SetWriteDeadline(time.Now().Add(tcpWriteTimeout))
if _, err := t.buildPrintRequest().WriteTo(c); err != nil {
c.Close()
return fmt.Errorf("writing print data: %w", err)
}
log.Printf("sended print data")
log.Printf("closing tcp")
if err := c.Close(); err != nil {
return fmt.Errorf("closing tcp: %w", err)
}
log.Printf("closed tcp")
time.Sleep(500 * time.Millisecond)
printDeadline := time.Now().Add(printTimeout)
for {
if time.Now().After(printDeadline) {
return fmt.Errorf("print take too long time")
}
time.Sleep(100 * time.Millisecond)
s, err := getStatus(addr)
if err != nil {
return fmt.Errorf("waiting print finish: %w", err)
}
if !s.IsPrinting() {
break
}
}
if err := unlockPrinter(addr); err != nil {
return fmt.Errorf("requesting unlock: %w", err)
}
return nil
}