This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmit.go
91 lines (73 loc) · 2.15 KB
/
transmit.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
package hoin
import "fmt"
func (p Printer) realTimeStatusTransmission(n int) (byte, error) {
errMsg := "could not transmit real-time status: %w"
err := checkRange(n, 1, 4, "n status type")
if err != nil {
return 0, fmt.Errorf(errMsg, err)
}
_, err = p.Write([]byte{DLE, 0x04, byte(n)})
if err != nil {
return 0, fmt.Errorf(errMsg, err)
}
b := make([]byte, 1)
_, err = p.Read(b)
if err != nil {
return 0, fmt.Errorf(errMsg, err)
}
return b[0], nil
}
type PrinterStatus struct {
DrawerOpen bool
}
func (p Printer) TransmitPrinterStatus() (PrinterStatus, error) {
b, err := p.realTimeStatusTransmission(1)
if err != nil {
return PrinterStatus{}, fmt.Errorf("could not transmit printer status: %w", err)
}
return PrinterStatus{
DrawerOpen: b&0b0100 == 0b0100,
}, nil
}
type OfflineStatus struct {
CoverOpen, FeedButton, PrintingStopped, ErrorOccured bool
}
func (p Printer) TransmitOfflineStatus() (OfflineStatus, error) {
b, err := p.realTimeStatusTransmission(2)
if err != nil {
return OfflineStatus{}, fmt.Errorf("could not transmit offline status: %w", err)
}
return OfflineStatus{
CoverOpen: b&0b0000_0100 == 0b0000_0100,
FeedButton: b&0b0000_1000 == 0b0000_1000,
PrintingStopped: b&0b0010_0000 == 0b0010_0000,
ErrorOccured: b&0b0100_0000 == 0b0100_0000,
}, nil
}
type ErrorStatus struct {
AutoCutter, UnRecoverable, AutoRecoverable bool
}
func (p Printer) TransmitErrorStatus() (ErrorStatus, error) {
b, err := p.realTimeStatusTransmission(3)
if err != nil {
return ErrorStatus{}, fmt.Errorf("could not transmit error status: %w", err)
}
return ErrorStatus{
AutoCutter: b&0b0000_1000 == 0b0000_1000,
UnRecoverable: b&0b0010_0000 == 0b0010_0000,
AutoRecoverable: b&0b0100_0000 == 0b0100_0000,
}, nil
}
type PaperSensorStatus struct {
NearEnd, RollEnd bool
}
func (p Printer) TransmitPaperSensorStatus() (PaperSensorStatus, error) {
b, err := p.realTimeStatusTransmission(4)
if err != nil {
return PaperSensorStatus{}, fmt.Errorf("could not transmit error status: %w", err)
}
return PaperSensorStatus{
NearEnd: b&0b0000_1100 == 0b0000_1100,
RollEnd: b&0b0110_0000 == 0b0110_0000,
}, nil
}