-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtdata2.go
75 lines (63 loc) · 2.12 KB
/
mtdata2.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
package xsens
import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
)
const (
packetDataIdentifierLength = 2
packetSizeLength = 1
packetDataIdentifierStart = 0
packetDataLengthStart = packetDataIdentifierStart + packetDataIdentifierLength
packetDataStart = packetDataLengthStart + packetSizeLength
)
// MTData2 represents an Xsens MTData2 payload.
type MTData2 []byte
func (m MTData2) String() string {
return fmt.Sprintf("MTData2(%s)", hex.EncodeToString(m))
}
// PacketAt returns the MTData2 packet starting at the provided index.
func (m MTData2) PacketAt(i int) (MTData2Packet, error) {
if len(m) < i+packetDataIdentifierLength+packetSizeLength {
return nil, errors.New("insufficient data")
}
packetDataLength := int(m[i+packetDataLengthStart])
if len(m) < i+packetDataIdentifierLength+packetSizeLength+packetDataLength {
return nil, errors.New("insufficient data")
}
return MTData2Packet(m[i : i+packetDataIdentifierLength+packetSizeLength+packetDataLength]), nil
}
// MTData2Packet represents an individual packet of an XSens MTData2 message.
type MTData2Packet []byte
func NewMTData2Package(length uint8, identifier DataIdentifier) MTData2Packet {
d := make(MTData2Packet, packetDataStart+length)
d.SetLength(length)
d.SetIdentifier(identifier)
return d
}
// String returns a string representation of the packet.
func (m MTData2Packet) String() string {
return fmt.Sprintf("MTData2Packet(%s)", hex.EncodeToString(m))
}
// Identifier returns the packet's data identifier.
func (m MTData2Packet) Identifier() DataIdentifier {
var identifier DataIdentifier
identifier.SetUint16(
binary.BigEndian.Uint16(m[packetDataIdentifierStart : packetDataIdentifierStart+packetDataIdentifierLength]),
)
return identifier
}
func (m MTData2Packet) SetIdentifier(id DataIdentifier) {
binary.BigEndian.PutUint16(
m[packetDataIdentifierStart:packetDataIdentifierStart+packetDataIdentifierLength],
id.Uint16(),
)
}
func (m MTData2Packet) SetLength(length uint8) {
m[packetDataLengthStart] = length
}
// Data returns the packet data.
func (m MTData2Packet) Data() []byte {
return m[packetDataStart:]
}