From 721ac37c5662aa6d371a2f5d05120659e9c26460 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 5 Dec 2016 12:29:23 +0100 Subject: Add set methods --- protocol.go | 212 +++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 140 insertions(+), 72 deletions(-) diff --git a/protocol.go b/protocol.go index d1792b9..4e488b2 100644 --- a/protocol.go +++ b/protocol.go @@ -1,5 +1,7 @@ package anki +import "encoding/binary" + const ( VehicleMsgMaxSize = 20 VehicleMsgPayloadMaxSize = 18 @@ -50,75 +52,6 @@ const ( VehicleMsgC2VSDKMode = 0x90 ) -const ( - VehicleTurnNone = iota - VehicleTurnLeft - VehicleTurnRight - VehicleTurnUTurn - VehicleTurnUTurnJump -) - -const ( - VehicleTurnTriggerImmediate = iota // Run immediately - VehicleTurnTriggerIntersection // Run at the next intersection -) - -const ( - Forward = iota - Reverse -) - -const ( - IntersectionCodeEntryFirst = iota - IntersectionCodeExitFirst - IntersectionCodeEntrySecond - IntersectionCodeExitSecond -) - -// Lights -// The bits in the simple light message corresponding to each type of light -const ( - LightHeadlights = 1 << iota - LightBrakelights - LightFrontlights - LightEngine -) - -const ( - VehicleMaxLightIntensity = 14 - VehicleMaxLightTime = 11 -) - -// LED channel definitions - for RGB engine, front, and tail lights -const ( - LightRed = iota - LightTail - LightBlue - LightGreen - LightFrontL - LightFrontR -) - -const ( - EffectSteady = iota // Simply set the light intensity to 'start' value - EffectFade // Fade intensity from 'start' to 'end' - EffectThrob // Fade intensity from 'start' to 'end' and back to 'start' - EffectFlash // Tun on LED between time 'start' and time 'end' inclusive - EffectRandom // Flash the LED erratically - ignoring start/end -) - -const ( - TrackMaterialPlastic = iota - TrackMaterialVinyl -) - -const ( - SupercodeNone = iota - SupercodeBoostJump -) - -//////////////////////////////////////////////////////////////////////// - type VehicleMsg struct { Size uint8 MsgID uint8 @@ -144,6 +77,13 @@ type VehicleMsgSDKMode struct { Flags uint8 } +func (v *VehicleMsgSDKMode) Set(on, flags uint8) { + v.Size = uint8(binary.Size(v) - 1) + v.MsgID = VehicleMsgC2VSDKMode + v.On = on + v.Flags = flags +} + type VehicleMsgSetSpeed struct { Size uint8 MsgID uint8 @@ -152,6 +92,26 @@ type VehicleMsgSetSpeed struct { SpeedLimit uint8 // respect road piece speed limit } +func (v *VehicleMsgSetSpeed) Set(speed, accel uint16) { + v.Size = uint8(binary.Size(v) - 1) + v.MsgID = VehicleMsgC2VSetSpeed + v.Speed = speed + v.Accel = accel +} + +const ( + VehicleTurnNone = iota + VehicleTurnLeft + VehicleTurnRight + VehicleTurnUTurn + VehicleTurnUTurnJump +) + +const ( + VehicleTurnTriggerImmediate = iota // Run immediately + VehicleTurnTriggerIntersection // Run at the next intersection +) + type VehicleMsgTurn struct { Size uint8 MsgID uint8 @@ -165,6 +125,12 @@ type VehicleMsgSetOffsetFromRoadCenter struct { Offset float32 // mm } +func (v *VehicleMsgSetOffsetFromRoadCenter) Set(offset float32) { + v.Size = uint8(binary.Size(v) - 1) + v.MsgID = VehicleMsgC2VSetOffsetFromRoadCenter + v.Offset = offset +} + type VehicleMsgChangeLane struct { Size uint8 MsgID uint8 @@ -175,6 +141,21 @@ type VehicleMsgChangeLane struct { Tag uint8 } +func (v *VehicleMsgChangeLane) Set(hspeed, haccel uint16, offset float32) { + v.Size = uint8(binary.Size(v) - 1) + v.MsgID = VehicleMsgC2VChangeLane + v.HorizontalSpeed = hspeed + v.HorizontalAccel = haccel + v.Offset = offset +} + +const ( + ParseflagsMaskNumBits = 0x0f + ParseflagsMaskInvertedColor = 0x80 + ParseflagsMaskReverseParsing = 0x40 + ParseflagsMaskReverseDriving = 0x20 +) + type VehicleLocalizationPositionUpdate struct { Size uint8 MsgID uint8 @@ -191,13 +172,20 @@ type VehicleLocalizationPositionUpdate struct { LastDesiredSpeed uint16 // mm/sec } +// VehicleDrivingDirection +const ( + Forward = iota + Reverse +) + type VehicleMsgLocalizationTransitionUpdate struct { Size uint8 MsgID uint8 RoadPieceIDX uint8 RoadPieceIDXPrev uint8 Offset float32 // from road center mm - Direction uint8 // driving direction + + Direction uint8 // driving direction // ACK commands received LastRecvLaneChangeCmdID uint8 @@ -214,6 +202,13 @@ type VehicleMsgLocalizationTransitionUpdate struct { RightWheelDist uint8 // cm } +const ( + IntersectionCodeEntryFirst = iota + IntersectionCodeExitFirst + IntersectionCodeEntrySecond + IntersectionCodeExitSecond +) + type VehicleMsgLocalizationIntersectionUpdate struct { Size uint8 MsgID uint8 @@ -233,12 +228,50 @@ type VehicleMsgOffsetFromRoadCenterUpdate struct { LaneChangeID uint8 } +// Lights +// The bits in the simple light message corresponding to each type of light +const ( + LightHeadlights = 1 << iota + LightBrakelights + LightFrontlights + LightEngine +) + type VehicleMsgSetLights struct { Size uint8 MsgID uint8 LightMask uint8 // valid and value bits for lights } +func (v *VehicleMsgSetLights) Set(mask uint8) { + v.Size = uint8(binary.Size(v) - 1) + v.MsgID = VehicleMsgC2VSetLights + v.LightMask = mask +} + +const ( + VehicleMaxLightIntensity = 14 + VehicleMaxLightTime = 11 +) + +// LED channel definitions - for RGB engine, front, and tail lights +const ( + LightRed = iota + LightTail + LightBlue + LightGreen + LightFrontL + LightFrontR +) + +const ( + EffectSteady = iota // Simply set the light intensity to 'start' value + EffectFade // Fade intensity from 'start' to 'end' + EffectThrob // Fade intensity from 'start' to 'end' and back to 'start' + EffectFlash // Tun on LED between time 'start' and time 'end' inclusive + EffectRandom // Flash the LED erratically - ignoring start/end +) + type VehicleLightConfig struct { Channel uint8 Effect uint8 @@ -247,6 +280,24 @@ type VehicleLightConfig struct { Cycles uint8 // per 10 sec } +func (v *VehicleLightConfig) Set(channel, effect uint8, start, end uint8, cycles uint16) { + v.Channel = channel + v.Effect = effect + v.Start = start + if v.Start > VehicleMaxLightIntensity { + v.Start = VehicleMaxLightIntensity + } + v.End = end + if v.End > VehicleMaxLightIntensity { + v.End = VehicleMaxLightIntensity + } + cpm := cycles + if cpm > VehicleMaxLightTime { + cpm = VehicleMaxLightTime + } + v.Cycles = uint8(cpm / 6) +} + type VehicleMsgLightsPattern struct { Size uint8 MsgID uint8 @@ -254,11 +305,28 @@ type VehicleMsgLightsPattern struct { ChannelConfig [3]VehicleLightConfig } +func (v *VehicleMsgLightsPattern) Set(config VehicleLightConfig) { + v.Size = uint8(binary.Size(v) - 1) + v.MsgID = VehicleMsgC2VLightsPattern + if int(v.ChannelCount) < len(v.ChannelConfig) { + v.ChannelConfig[v.ChannelCount] = config + v.ChannelCount++ + } +} + +const ( + TrackMaterialPlastic = iota + TrackMaterialVinyl +) + +const ( + SupercodeNone = iota + SupercodeBoostJump +) + type VehicleMsgSetConfigParams struct { Size uint8 MsgID uint8 SuperCodeParseMask uint8 TrackMaterial uint8 } - -//////////////////////////////////////////////////////////////////////// -- cgit v1.2.3