-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsg.go
More file actions
50 lines (42 loc) · 1.08 KB
/
msg.go
File metadata and controls
50 lines (42 loc) · 1.08 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
// Copyright 2022 TrueLevel SA
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0
package nd300
const (
MsgLen = 6 // Length of a serial message (command or status).
idxStx = 0
idxMS = 1 // Indicate if master (command) or slave (status).
idxMachine = 2
idxCmd = 3
idxStatus = 3 // Same as idxCmd, helps make the code clearer.
idxData = 4
idxCksum = 5
STX byte = 0x01
CmdFlag byte = 0x10
StatusFlag byte = 0x01
)
type Msg []byte
func (m Msg) SetData(data byte) {
m[idxData] = data
m[idxCksum] = computeChecksum(m)
}
func (m Msg) Bytes() []byte {
return m
}
func (m Msg) CmdOrStatus() string {
switch m[idxMS] {
case CmdFlag:
return m.CmdString()
case StatusFlag:
return m.Status().String()
default:
return "invalid msg"
}
}
func computeChecksum(msg []byte) byte {
return msg[idxStx] + msg[idxMS] + msg[idxMachine] + msg[idxCmd] + msg[idxData]
}