forked from JoshuaDoes/tensor-usbdl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
77 lines (63 loc) · 1.34 KB
/
command.go
File metadata and controls
77 lines (63 loc) · 1.34 KB
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
package tensorutils
import "github.com/JoshuaDoes/crunchio"
var (
OpDNW = []byte("\x1BDNW")
CmdDNW = NewCommand(OpDNW, nil, nil, nil)
CmdStop = NewCommand(OpDNW, make([]byte, 4), nil, []byte("\x01\x00"))
)
type Command struct {
cmd, arg, data, crc []byte
}
func NewCommand(cmd, arg, data, crc []byte) *Command {
return &Command{
cmd: cmd,
arg: arg,
data: data,
crc: crc,
}
}
func (c *Command) Bytes() []byte {
bytes := crunchio.NewBuffer(string(c.cmd))
if c.CmdLen() > 0 {
bytes.Write(c.Cmd()) //Usually 4 bytes, i.e. {ESC}DNW
if c.ArgLen() > 0 {
bytes.Write(c.Arg())
} else {
bytes.WriteAbstract(int32(4 + c.CmdLen() + c.CRCLen() + c.DataLen())) //Assume the argument is the command's byte length, including this
}
}
if c.DataLen() > 0 {
bytes.Write(c.Data())
}
if c.CRCLen() > 0 {
bytes.Write(c.CRC())
}
return bytes.Bytes()
}
func (c *Command) Cmd() []byte {
return []byte(c.cmd)
}
func (c *Command) Arg() []byte {
return c.arg
}
func (c *Command) Data() []byte {
return c.data
}
func (c *Command) CRC() []byte {
return c.crc
}
func (c *Command) CmdLen() int {
return len(c.Cmd())
}
func (c *Command) ArgLen() int {
return len(c.Arg())
}
func (c *Command) DataLen() int {
return len(c.Data())
}
func (c *Command) CRCLen() int {
return len(c.CRC())
}
func (c *Command) Len() int {
return len(c.Bytes())
}