aboutsummaryrefslogtreecommitdiff
path: root/ql/status.go
blob: 8be89a31633b9df5463d3fc482efa470d6a8ade5 (plain)
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
package ql

import (
	"fmt"
	"io"
	"strings"
)

// Status is a decoder for the status packed returned by the printer.
type Status [32]byte

func (s *Status) MediaWidthMM() int  { return int(s[10]) }
func (s *Status) MediaLengthMM() int { return int(s[17]) }

type StatusType byte

const (
	StatusTypeReplyToRequest    StatusType = 0x00
	StatusTypePrintingCompleted            = 0x01
	StatusTypeErrorOccurred                = 0x02
	StatusTypeTurnedOff                    = 0x04
	StatusTypeNotification                 = 0x05
	StatusTypePhaseChange                  = 0x06
)

func (s *Status) Type() StatusType { return StatusType(s[18]) }

type StatusPhase byte

const (
	StatusPhaseReceiving StatusPhase = 0x00
	StatusPhasePrinting              = 0x01
)

func (s *Status) Phase() StatusPhase { return StatusPhase(s[19]) }

func decodeBitfieldErrors(b byte, errors [8]string) []string {
	var result []string
	for i := uint(0); i < 8; i++ {
		if b&(1<<i) != 0 {
			result = append(result, errors[i])
		}
	}
	return result
}

func (s *Status) Errors() (errors []string) {
	errors = append(errors, decodeBitfieldErrors(s[8], [8]string{
		"no media", "end of media", "cutter jam", "?", "printer in use",
		"printer turned off", "high-voltage adapter", "fan motor error"})...)
	errors = append(errors, decodeBitfieldErrors(s[9], [8]string{
		"replace media", "expansion buffer full", "communication error",
		"communication buffer full", "cover open", "cancel key",
		"media cannot be fed", "system error"})...)
	return
}

// -----------------------------------------------------------------------------

// String implements the Stringer interface.
func (s *Status) String() string {
	var b strings.Builder
	s.Dump(&b)
	return b.String()
}

// Dump writes the status data to an io.Writer in a human-readable format.
func (s *Status) Dump(f io.Writer) {
	/*
		if s[0] != 0x80 || s[1] != 0x20 || s[2] != 0x42 || s[3] != 0x34 {
			fmt.Fprintln(f, "unexpected status fixed bytes")
		}
	*/

	// Model code.
	switch m := s[4]; m {
	case 0x38:
		fmt.Fprintln(f, "model: QL-800")
	case 0x39:
		fmt.Fprintln(f, "model: QL-810W")
	case 0x41:
		fmt.Fprintln(f, "model: QL-820NWB")
	case 0x43:
		fmt.Fprintln(f, "model: QL-1100")
	case 0x44:
		fmt.Fprintln(f, "model: QL-1110NWB")
	case 0x45:
		fmt.Fprintln(f, "model: QL-1115NWB")
	default:
		fmt.Fprintln(f, "model:", m)
	}

	/*
		// s[6] seems to be 0x00 in a real-world QL-800, as in QL-1100 docs.
		if s[5] != 0x30 || s[6] != 0x30 || s[7] != 0x00 {
			fmt.Fprintln(f, "unexpected status fixed bytes")
		}
	*/

	// Error information 1.
	for _, e := range decodeBitfieldErrors(s[8], [8]string{
		"no media", "end of media", "cutter jam", "?", "printer in use",
		"printer turned off", "high-voltage adapter", "fan motor error"}) {
		fmt.Fprintln(f, "error 1:", e)
	}

	// Error information 2.
	for _, e := range decodeBitfieldErrors(s[9], [8]string{
		"replace media", "expansion buffer full", "communication error",
		"communication buffer full", "cover open", "cancel key",
		"media cannot be fed", "system error"}) {
		fmt.Fprintln(f, "error 2:", e)
	}

	// Media width.
	fmt.Fprintln(f, "media width:", s[10], "mm")

	// Media type.
	switch t := s[11]; t {
	case 0x00:
		fmt.Fprintln(f, "media: no media")
	case 0x4a, 0x0a: // 0x4a = J, in reality we get 0x0a, as in QL-1100 docs.
		fmt.Fprintln(f, "media: continuous length tape")
	case 0x4b, 0x0b: // 0x4b = K, in reality we get 0x0b, as in QL-1100 docs.
		fmt.Fprintln(f, "media: die-cut labels")
	default:
		fmt.Fprintln(f, "media:", t)
	}

	/*
		// In a real-world QL-800, s[14] seems to be:
		//  0x01 with die-cut 29mm long labels,
		//  0x14 with 29mm tape,
		//  0x23 with red-black 62mm tape,
		// and directly corresponds to physical pins on the tape.
		if s[12] != 0x00 || s[13] != 0x00 || s[14] != 0x3f {
			fmt.Fprintln(f, "unexpected status fixed bytes")
		}
	*/

	// Mode.
	fmt.Fprintln(f, "mode:", s[15])

	/*
		if s[16] != 0x00 {
			fmt.Fprintln(f, "unexpected status fixed bytes")
		}
	*/

	// Media length.
	fmt.Fprintln(f, "media length:", s[17], "mm")

	// Status type.
	switch t := s[18]; StatusType(t) {
	case StatusTypeReplyToRequest:
		fmt.Fprintln(f, "status type: reply to status request")
	case StatusTypePrintingCompleted:
		fmt.Fprintln(f, "status type: printing completed")
	case StatusTypeErrorOccurred:
		fmt.Fprintln(f, "status type: error occurred")
	case StatusTypeTurnedOff:
		fmt.Fprintln(f, "status type: turned off")
	case StatusTypeNotification:
		fmt.Fprintln(f, "status type: notification")
	case StatusTypePhaseChange:
		fmt.Fprintln(f, "status type: phase change")
	default:
		fmt.Fprintln(f, "status type:", t)
	}

	// Phase type.
	switch t := s[19]; StatusPhase(t) {
	case StatusPhaseReceiving:
		fmt.Fprintln(f, "phase state: receiving state")
	case StatusPhasePrinting:
		fmt.Fprintln(f, "phase state: printing state")
	default:
		fmt.Fprintln(f, "phase state:", t)
	}

	// Phase number.
	fmt.Fprintln(f, "phase number:", int(s[20])*256+int(s[21]))

	// Notification number.
	switch n := s[22]; n {
	case 0x00:
		fmt.Fprintln(f, "notification number: not available")
	case 0x03:
		fmt.Fprintln(f, "notification number: cooling (started)")
	case 0x04:
		fmt.Fprintln(f, "notification number: cooling (finished)")
	default:
		fmt.Fprintln(f, "notification number:", n)
	}

	/*
		// In a real-world QL-800, s[25] seems to be:
		//  0x01 with 29mm tape or die-cut 29mm long labels,
		//  0x81 with red-black 62mm tape.
		if s[23] != 0x00 || s[24] != 0x00 || s[25] != 0x00 || s[26] != 0x00 ||
			s[27] != 0x00 || s[28] != 0x00 || s[29] != 0x00 || s[30] != 0x00 ||
			s[31] != 0x00 {
			fmt.Fprintln(f, "unexpected status fixed bytes")
		}
	*/
}