aboutsummaryrefslogtreecommitdiff
path: root/protocol.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-12-29 22:41:38 +0100
committerDimitri Sokolyuk <demon@dim13.org>2016-12-29 22:41:38 +0100
commit55fe0c3c7bcd0243493976195a1e4d3283e2bfb7 (patch)
tree803b8800a896ccbccb86edf0b7dd0154ef639052 /protocol.go
parent8085261f5e086f1bbb091a4ca9ec4042e62c47a5 (diff)
Dig deeper
Diffstat (limited to 'protocol.go')
-rw-r--r--protocol.go42
1 files changed, 32 insertions, 10 deletions
diff --git a/protocol.go b/protocol.go
index e23143c..8662cf5 100644
--- a/protocol.go
+++ b/protocol.go
@@ -3,6 +3,8 @@ package anki
import (
"bytes"
"encoding/binary"
+ "fmt"
+ "log"
)
// VehicleMsgMaxSize = 20
@@ -72,7 +74,10 @@ func SplitMsg(b []byte) (id ID, payload []byte) {
func Encode(v interface{}) []byte {
buf := new(bytes.Buffer)
- binary.Write(buf, binary.LittleEndian, v)
+ err := binary.Write(buf, binary.LittleEndian, v)
+ if err != nil {
+ log.Println("Encode", err)
+ }
b := buf.Bytes()[:]
z := make([]byte, len(b))
copy(z, b) // FIXME workaround "cgo argument has Go pointer to Go pointer"
@@ -81,7 +86,10 @@ func Encode(v interface{}) []byte {
func Decode(b []byte, v interface{}) {
buf := bytes.NewBuffer(b)
- binary.Read(buf, binary.LittleEndian, v)
+ err := binary.Read(buf, binary.LittleEndian, v)
+ if err != nil {
+ log.Println("Decode", err)
+ }
}
type VehicleMsgVersionResponse struct {
@@ -103,7 +111,7 @@ type VehicleMsgSDKMode struct {
Flags uint8
}
-func (v *VehicleMsgSDKMode) Set(on, flags uint8) {
+func (v *VehicleMsgSDKMode) Set(on uint8, flags uint8) {
v.Size = uint8(binary.Size(v) - 1)
v.MsgID = VehicleMsgC2VSDKMode
v.On = on
@@ -175,13 +183,24 @@ func (v *VehicleMsgChangeLane) Set(hspeed, haccel uint16, offset float32) {
v.Offset = offset
}
+type Flags uint8
+
const (
- ParseflagsMaskNumBits = 0x0f
- ParseflagsMaskInvertedColor = 0x80
- ParseflagsMaskReverseParsing = 0x40
- ParseflagsMaskReverseDriving = 0x20
+ ParseflagsMaskNumBits Flags = 0x0f
+ ParseflagsMaskInvertedColor Flags = 0x80
+ ParseflagsMaskReverseParsing Flags = 0x40
+ ParseflagsMaskReverseDriving Flags = 0x20
)
+func (f Flags) String() string {
+ return fmt.Sprintf("{Bits: %d InvColor: %t RevParse: %t RevDrive: %t}",
+ f&ParseflagsMaskNumBits,
+ f&ParseflagsMaskInvertedColor != 0,
+ f&ParseflagsMaskReverseParsing != 0,
+ f&ParseflagsMaskReverseDriving != 0,
+ )
+}
+
type VehicleLocalizationPositionUpdate struct {
Size uint8
MsgID ID
@@ -189,7 +208,7 @@ type VehicleLocalizationPositionUpdate struct {
RoadPieceID uint8
Offset float32 // from road center mm
Speed uint16 // mm/sec
- ParsingFlags uint8
+ ParsingFlags Flags
// ACK commands received
LastRecvLaneChangeCmdID uint8
@@ -198,9 +217,12 @@ type VehicleLocalizationPositionUpdate struct {
LastDesiredSpeed uint16 // mm/sec
}
+//go:generate stringer -type=Direction
+type Direction uint8
+
// VehicleDrivingDirection
const (
- Forward uint8 = iota
+ Forward Direction = iota
Reverse
)
@@ -211,7 +233,7 @@ type VehicleMsgLocalizationTransitionUpdate struct {
RoadPieceIDXPrev uint8
Offset float32 // from road center mm
- Direction uint8 // driving direction
+ Direction Direction // driving direction
// ACK commands received
LastRecvLaneChangeCmdID uint8