label_printer/escpos/protocol/build.go
TT-392 4a0a055234 Squashed 'faxmachine/' content from commit d23200b
git-subtree-dir: faxmachine
git-subtree-split: d23200bcfdedb9f8cc57e6a3c65b5ef93fcbfd19
2024-09-12 20:45:00 +02:00

166 lines
4.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package protocol
import (
"fmt"
)
const (
esc = '\x1b'
gs = '\x1d'
)
// InitializePrinter executes ESC @ ("Initialize printer")
func (p Protocol) InitializePrinter() ([]byte, error) {
return []byte{esc, '@'}, nil
}
// SelectCharacterCodeTable executes ESC t n ("Select character code table")
func (p Protocol) SelectCharacterCodeTable(n uint8) ([]byte, error) {
if (n > 5 && n < 16) || (n > 19 && n < 255) {
return nil, fmt.Errorf("range error: 0 ≤ n ≤ 5, 16 ≤ n ≤ 19, n = 255")
}
return []byte{esc, 't', n}, nil
}
const (
PrintModeDefault = 0
PrintModeFont2 = 1 << 0
_ = 1 << 1
_ = 1 << 2
PrintModeEmphasized = 1 << 3
PrintModeDoubleHeight = 1 << 4
PrintModeDoubleWidth = 1 << 5
_ = 1 << 6
PrintModeUnderline = 1 << 7
)
// SelectPrintModes executes ESC ! n ("Select print mode(s)")
func (p Protocol) SelectPrintModes(n uint8) ([]byte, error) {
return []byte{esc, '!', n}, nil
}
// SetPrintSpeed executes GS ( K <Function 50> ("Select the print speed")
func (p Protocol) SelectPrintSpeed(m uint8) ([]byte, error) {
if (m > 9 && m < 48) || m > 57 {
return nil, fmt.Errorf("range error: m = 0 ≤ m ≤ 9, 48 ≤ m ≤ 57")
}
return []byte{gs, '(', 'K', 2, 0, 50, m}, nil
}
const (
ASBReportNothing = 0
ASBReportKickoutConnector = 1 << 0
ASBReportOnlineStatus = 1 << 1
ASBReportErrors = 1 << 2
ASBReportRollPaperSensorStatus = 1 << 3
_ = 1 << 4
_ = 1 << 5
ASBReportPanelSwitchStatus = 1 << 6
_ = 1 << 7
ASBReportAll = ASBReportKickoutConnector |
ASBReportOnlineStatus |
ASBReportErrors |
ASBReportRollPaperSensorStatus |
ASBReportPanelSwitchStatus
)
func (p Protocol) SetAutomaticStatusBack(n uint8) ([]byte, error) {
return []byte{gs, 'a', n}, nil
}
const (
TransmitPaperSensorStatus = 1
TransmitKickoutConnectorStatus = 2
)
func (p Protocol) TransmitStatus(n uint8) ([]byte, error) {
if n != 1 && n != 2 && n != 49 && n != 50 {
return nil, fmt.Errorf("range error: n = 1, 2, 49, 50")
}
return []byte{gs, 'r', n}, nil
}
func (p Protocol) gs8l(m uint8, fn uint8, parameters ...uint8) ([]byte, error) {
p_ := 1 /* m */ + 1 /* fn */ + len(parameters)
p1 := uint8(p_ >> 0)
p2 := uint8(p_ >> 8)
p3 := uint8(p_ >> 16)
p4 := uint8(p_ >> 24)
command := []uint8{gs, '8', 'L', p1, p2, p3, p4, m, fn}
command = append(command, parameters...)
return command, nil
}
const (
Color1 = 49
Color2 = 50
)
// StoreGraphicsData executes GS 8 L p1 p2 p3 p4 m fn a bx by c xL xH yL yH
// d1...dk <Function 112> ("Store the graphics data in the print buffer (raster
// format).")
func (p Protocol) StoreGraphicsData(bx uint8, by uint8, c uint8, x uint16, y uint16, d ...uint8) ([]byte, error) {
var m uint8 = 0x30
var fn uint8 = 0x70
var a uint8 = 0x30
if bx != 1 && bx != 2 {
return nil, fmt.Errorf("range error: bx = 1, 2")
}
if by != 1 && by != 2 {
return nil, fmt.Errorf("range error: by = 1, 2")
}
if c != Color1 && c != Color2 {
return nil, fmt.Errorf("range error: c = 49, 50")
}
if x > 2047 {
return nil, fmt.Errorf("range error: 1 ≤ x ≤ 2047 (x = %d)", x)
}
if by == 1 && y > 1662 {
return nil, fmt.Errorf("range error: (by = 1): 1 ≤ y ≤ 1662 (y = %d)", y)
}
if by == 2 && y > 831 {
return nil, fmt.Errorf("range error: (by = 2): 1 ≤ y ≤ 831 (y = %d)", y)
}
if (int(x)+7)/8*int(y) != len(d) {
return nil, fmt.Errorf("range error: k = (int(x + 7)/8) × y (k = %d, x = %d, y = %d)", len(d), x, y)
}
xl := uint8(x >> 0)
xh := uint8(x >> 8)
yl := uint8(y >> 0)
yh := uint8(y >> 8)
paramters := []byte{a, bx, by, c, xl, xh, yl, yh}
paramters = append(paramters, d...)
return p.gs8l(m, fn, paramters...)
}
// PrintGraphicsData executes GS 8 L p1 p2 p3 p4 m fn <Function 50> ("Print the
// graphics data in the print buffer.")
func (p Protocol) PrintGraphicsData() ([]byte, error) {
var m uint8 = 48
var fn uint8 = 50
return p.gs8l(m, fn)
}
const (
FullCut = 0
PartialCut = 1
FeedAndFullCut = 65
FeedAndPartialCut = 66
)
// CutPaper executes GS V ("Select cut mode and cut paper")
func (p Protocol) CutPaper(m uint8, n uint8) ([]byte, error) {
if m == FullCut || m == PartialCut {
return []byte{gs, 'V', m}, nil
} else {
return []byte{gs, 'V', m, n}, nil
}
}