From 2fbd8cb666de25da34b0ffb1cdeb3b9ceacd7876 Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Fri, 21 Oct 2022 22:47:05 +0100 Subject: [PATCH 1/9] Added navigation characteristics --- infinitime.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/infinitime.go b/infinitime.go index d48e902..964a93d 100644 --- a/infinitime.go +++ b/infinitime.go @@ -38,6 +38,10 @@ const ( FSTransferChar = "adaf0200-4669-6c65-5472-616e73666572" FSVersionChar = "adaf0100-4669-6c65-5472-616e73666572" WeatherDataChar = "00040001-78fc-48fe-8e23-433b3a1942d0" + NavFlagsChar = "00010001-78fc-48fe-8e23-433b3a1942d0" + NavNarrativeChar= "00010002-78fc-48fe-8e23-433b3a1942d0" + NavManDistChar = "00010003-78fc-48fe-8e23-433b3a1942d0" + NavProgressChar = "00010004-78fc-48fe-8e23-433b3a1942d0" ) var charNames = map[string]string{ @@ -52,10 +56,18 @@ var charNames = map[string]string{ FSTransferChar: "Filesystem Transfer", FSVersionChar: "Filesystem Version", WeatherDataChar: "Weather Data", + NavFlagsChar: "Navigation Icon", + NavNarrativeChar:"Navigation Instruction", + NavManDistChar: "Navigation Distance to next event", + NavProgressChar: "Navigation Progress", } type Device struct { device *device.Device1 + navflagsChar *gatt.GattCharacteristic1 + navnarrativeChar*gatt.GattCharacteristic1 + navmandistChar *gatt.GattCharacteristic1 + navprogressChar *gatt.GattCharacteristic1 newAlertChar *gatt.GattCharacteristic1 notifEventChar *gatt.GattCharacteristic1 stepCountChar *gatt.GattCharacteristic1 @@ -394,6 +406,14 @@ func (i *Device) resolveChars() error { charResolved := true // Set correct characteristics switch char.Properties.UUID { + case NavFlagsChar: + i.navflagsChar = char + case NavNarrativeChar: + i.navnarrativeChar = char + case NavManDistChar: + i.navmandistChar = char + case NavProgressChar: + i.navprogressChar = char case NewAlertChar: i.newAlertChar = char case NotifEventChar: -- 2.45.2 From 62987fa31bd20e17278c83e777047b185d117fef Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Fri, 21 Oct 2022 22:47:28 +0100 Subject: [PATCH 2/9] Added navigation event to device state --- infinitime.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/infinitime.go b/infinitime.go index 964a93d..247f93e 100644 --- a/infinitime.go +++ b/infinitime.go @@ -62,6 +62,13 @@ var charNames = map[string]string{ NavProgressChar: "Navigation Progress", } +type NavigationEvent struct { + flag string + narrative string + dist string + progress uint8 +} + type Device struct { device *device.Device1 navflagsChar *gatt.GattCharacteristic1 @@ -83,6 +90,7 @@ type Device struct { notifEventDone bool Music MusicCtrl DFU DFU + navigationEv NavigationEvent } var ( -- 2.45.2 From 912ccd14b392de2744889e5456d6f3269d0528e0 Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Fri, 21 Oct 2022 22:48:00 +0100 Subject: [PATCH 3/9] Pass navigation narrative --- infinitime.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/infinitime.go b/infinitime.go index 247f93e..0b3031c 100644 --- a/infinitime.go +++ b/infinitime.go @@ -751,6 +751,24 @@ func (i *Device) Notify(title, body string) error { ) } +// Navigation sends a NavigationEvent to the watch +func (i *Device) Navigation(flag string, narrative string, dist string, progress uint8) error { + + if narrative != i.navigationEv.narrative { + log.Debug().Str("func", "Navigation"). + Msg("Sending narrative") + if err := i.checkStatus(i.navnarrativeChar, NavNarrativeChar); err != nil { + return err + } + if err := i.navnarrativeChar.WriteValue([]byte(narrative), nil); err != nil { + return err + } + i.navigationEv.narrative = narrative + } + + return nil +} + // These constants represent the possible call statuses selected by the user const ( CallStatusDeclined uint8 = iota -- 2.45.2 From ba3169d1d0ad2c5d17d9003a0dc5fec5b00a4c46 Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Fri, 21 Oct 2022 23:08:15 +0100 Subject: [PATCH 4/9] Pass other navigation parameters --- infinitime.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/infinitime.go b/infinitime.go index 0b3031c..1089846 100644 --- a/infinitime.go +++ b/infinitime.go @@ -753,6 +753,17 @@ func (i *Device) Notify(title, body string) error { // Navigation sends a NavigationEvent to the watch func (i *Device) Navigation(flag string, narrative string, dist string, progress uint8) error { + if flag != i.navigationEv.flag { + log.Debug().Str("func", "Navigation"). + Msg("Sending flag") + if err := i.checkStatus(i.navflagsChar, NavFlagsChar); err != nil { + return err + } + if err := i.navflagsChar.WriteValue([]byte(flag), nil); err != nil { + return err + } + i.navigationEv.flag = flag + } if narrative != i.navigationEv.narrative { log.Debug().Str("func", "Navigation"). @@ -766,6 +777,32 @@ func (i *Device) Navigation(flag string, narrative string, dist string, progress i.navigationEv.narrative = narrative } + if dist != i.navigationEv.dist { + log.Debug().Str("func", "Navigation"). + Msg("Sending mandist") + if err := i.checkStatus(i.navmandistChar, NavManDistChar); err != nil { + return err + } + if err := i.navmandistChar.WriteValue([]byte(dist), nil); err != nil { + return err + } + i.navigationEv.dist = dist + } + + if progress != i.navigationEv.progress { + log.Debug().Str("func", "Navigation"). + Msg("Sending progress") + if err := i.checkStatus(i.navprogressChar, NavProgressChar); err != nil { + return err + } + buf := &bytes.Buffer{} + binary.Write(buf, binary.LittleEndian, progress) + if err := i.navprogressChar.WriteValue(buf.Bytes(), nil); err != nil { + return err + } + i.navigationEv.progress = progress + } + return nil } -- 2.45.2 From e6a1baa32b6fa2f3df9afcacbf9e126b7e15196a Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Fri, 21 Oct 2022 23:08:29 +0100 Subject: [PATCH 5/9] Validate navigation parameters before passing them on --- infinitime.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/infinitime.go b/infinitime.go index 1089846..23ceae7 100644 --- a/infinitime.go +++ b/infinitime.go @@ -62,6 +62,41 @@ var charNames = map[string]string{ NavProgressChar: "Navigation Progress", } +var NavFlagNames = []string{ + "arrive", "arrive-left", "arrive-right", "arrive-straight", + "close", + "continue", "continue-left", "continue-right", "continue-slight-left", + "continue-slight-right", "continue-straight", "continue-uturn", + "depart", "depart-left", "depart-right", "depart-straight", + "end-of-road-left", "end-of-road-right", + "ferry", "flag", + "fork", "fork-left", "fork-right", "fork-straight", + "fork-slight-left", "fork-slight-right", + "invalid", "invalid-left", "invalid-right", + "invalid-slight-left", "invalid-slight-right", + "invalid-straight", "invalid-uturn", + "merge-left", "merge-right", "merge-slight-left", + "merge-slight-right", "merge-straight", + "new-name-left", "new-name-right", "new-name-sharp-left", "new-name-sharp-right", + "new-name-slight-left", "new-name-slight-right", "new-name-straight", + "notification-left", "notification-right", + "notification-sharp-left", "notification-sharp-right", + "notification-slight-left", "notification-slight-right", + "notification-straight", + "off-ramp-left", "off-ramp-right", "off-ramp-sharp-left", "off-ramp-sharp-right", + "off-ramp-slight-left", "off-ramp-slight-right", "off-ramp-straight", + "on-ramp-left", "on-ramp-right", "on-ramp-sharp-left", "on-ramp-sharp-right", + "on-ramp-slight-left", "on-ramp-slight-right", "on-ramp-straight", + "rotary", "rotary-left", "rotary-right", "rotary-sharp-left", "rotary-sharp-right", + "rotary-slight-left", "rotary-slight-right", "rotary-straight", + "roundabout", "roundabout-left", "roundabout-right", + "roundabout-sharp-left", "roundabout-sharp-right", + "roundabout-slight-left", "roundabout-slight-right", "roundabout-straight", + "turn-left", "turn-right", "turn-sharp-left", "turn-sharp-right", + "turn-slight-left", "turn-slight-right", "turn-stright", + "updown", "uturn", +} + type NavigationEvent struct { flag string narrative string @@ -99,6 +134,8 @@ var ( ErrNotConnected = errors.New("not connected") ErrNoTimelineHeader = errors.New("events must contain the timeline header") ErrPairTimeout = errors.New("reached timeout while pairing") + ErrNavProgress = errors.New("progress needs to between 0 and 100") + ErrNavInvalidFlag = errors.New("this flag is invalid") ) type ErrCharNotAvail struct { @@ -751,8 +788,24 @@ func (i *Device) Notify(title, body string) error { ) } +func CheckValidNavFlag(flag string) error { + for _, v := range NavFlagNames { + if v == flag { + return nil + } + } + return ErrNavInvalidFlag +} + // Navigation sends a NavigationEvent to the watch func (i *Device) Navigation(flag string, narrative string, dist string, progress uint8) error { + if progress > 100 { + return ErrNavProgress + } + if err := CheckValidNavFlag(flag); err != nil { + return err + } + if flag != i.navigationEv.flag { log.Debug().Str("func", "Navigation"). Msg("Sending flag") -- 2.45.2 From 49544b9b47bbd6151f99cefb7c4770909ca5ad2d Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Fri, 21 Oct 2022 23:19:51 +0100 Subject: [PATCH 6/9] Upated readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9a7c7fa..37210a7 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ This library currently supports the following features: - Battery level - Music control - OTA firmware upgrades +- Navigation --- -- 2.45.2 From e00a4fce7084a2104ddae19b2eb2272b27e7ad8a Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Sat, 22 Oct 2022 13:36:59 +0100 Subject: [PATCH 7/9] 1. Moved navigation things into new files --- infinitime.go | 132 +++-------------------------------------------- navigation.go | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 126 deletions(-) create mode 100644 navigation.go diff --git a/infinitime.go b/infinitime.go index 23ceae7..e234949 100644 --- a/infinitime.go +++ b/infinitime.go @@ -38,10 +38,6 @@ const ( FSTransferChar = "adaf0200-4669-6c65-5472-616e73666572" FSVersionChar = "adaf0100-4669-6c65-5472-616e73666572" WeatherDataChar = "00040001-78fc-48fe-8e23-433b3a1942d0" - NavFlagsChar = "00010001-78fc-48fe-8e23-433b3a1942d0" - NavNarrativeChar= "00010002-78fc-48fe-8e23-433b3a1942d0" - NavManDistChar = "00010003-78fc-48fe-8e23-433b3a1942d0" - NavProgressChar = "00010004-78fc-48fe-8e23-433b3a1942d0" ) var charNames = map[string]string{ @@ -62,54 +58,8 @@ var charNames = map[string]string{ NavProgressChar: "Navigation Progress", } -var NavFlagNames = []string{ - "arrive", "arrive-left", "arrive-right", "arrive-straight", - "close", - "continue", "continue-left", "continue-right", "continue-slight-left", - "continue-slight-right", "continue-straight", "continue-uturn", - "depart", "depart-left", "depart-right", "depart-straight", - "end-of-road-left", "end-of-road-right", - "ferry", "flag", - "fork", "fork-left", "fork-right", "fork-straight", - "fork-slight-left", "fork-slight-right", - "invalid", "invalid-left", "invalid-right", - "invalid-slight-left", "invalid-slight-right", - "invalid-straight", "invalid-uturn", - "merge-left", "merge-right", "merge-slight-left", - "merge-slight-right", "merge-straight", - "new-name-left", "new-name-right", "new-name-sharp-left", "new-name-sharp-right", - "new-name-slight-left", "new-name-slight-right", "new-name-straight", - "notification-left", "notification-right", - "notification-sharp-left", "notification-sharp-right", - "notification-slight-left", "notification-slight-right", - "notification-straight", - "off-ramp-left", "off-ramp-right", "off-ramp-sharp-left", "off-ramp-sharp-right", - "off-ramp-slight-left", "off-ramp-slight-right", "off-ramp-straight", - "on-ramp-left", "on-ramp-right", "on-ramp-sharp-left", "on-ramp-sharp-right", - "on-ramp-slight-left", "on-ramp-slight-right", "on-ramp-straight", - "rotary", "rotary-left", "rotary-right", "rotary-sharp-left", "rotary-sharp-right", - "rotary-slight-left", "rotary-slight-right", "rotary-straight", - "roundabout", "roundabout-left", "roundabout-right", - "roundabout-sharp-left", "roundabout-sharp-right", - "roundabout-slight-left", "roundabout-slight-right", "roundabout-straight", - "turn-left", "turn-right", "turn-sharp-left", "turn-sharp-right", - "turn-slight-left", "turn-slight-right", "turn-stright", - "updown", "uturn", -} - -type NavigationEvent struct { - flag string - narrative string - dist string - progress uint8 -} - type Device struct { device *device.Device1 - navflagsChar *gatt.GattCharacteristic1 - navnarrativeChar*gatt.GattCharacteristic1 - navmandistChar *gatt.GattCharacteristic1 - navprogressChar *gatt.GattCharacteristic1 newAlertChar *gatt.GattCharacteristic1 notifEventChar *gatt.GattCharacteristic1 stepCountChar *gatt.GattCharacteristic1 @@ -121,9 +71,11 @@ type Device struct { fsVersionChar *gatt.GattCharacteristic1 fsTransferChar *gatt.GattCharacteristic1 weatherDataChar *gatt.GattCharacteristic1 + weatherdataChar *gatt.GattCharacteristic1 notifEventCh chan uint8 notifEventDone bool Music MusicCtrl + Navigation NavigationCtrl DFU DFU navigationEv NavigationEvent } @@ -134,8 +86,6 @@ var ( ErrNotConnected = errors.New("not connected") ErrNoTimelineHeader = errors.New("events must contain the timeline header") ErrPairTimeout = errors.New("reached timeout while pairing") - ErrNavProgress = errors.New("progress needs to between 0 and 100") - ErrNavInvalidFlag = errors.New("this flag is invalid") ) type ErrCharNotAvail struct { @@ -452,13 +402,13 @@ func (i *Device) resolveChars() error { // Set correct characteristics switch char.Properties.UUID { case NavFlagsChar: - i.navflagsChar = char + i.Navigation.flagsChar = char case NavNarrativeChar: - i.navnarrativeChar = char + i.Navigation.narrativeChar = char case NavManDistChar: - i.navmandistChar = char + i.Navigation.mandistChar = char case NavProgressChar: - i.navprogressChar = char + i.Navigation.progressChar = char case NewAlertChar: i.newAlertChar = char case NotifEventChar: @@ -788,76 +738,6 @@ func (i *Device) Notify(title, body string) error { ) } -func CheckValidNavFlag(flag string) error { - for _, v := range NavFlagNames { - if v == flag { - return nil - } - } - return ErrNavInvalidFlag -} - -// Navigation sends a NavigationEvent to the watch -func (i *Device) Navigation(flag string, narrative string, dist string, progress uint8) error { - if progress > 100 { - return ErrNavProgress - } - if err := CheckValidNavFlag(flag); err != nil { - return err - } - - if flag != i.navigationEv.flag { - log.Debug().Str("func", "Navigation"). - Msg("Sending flag") - if err := i.checkStatus(i.navflagsChar, NavFlagsChar); err != nil { - return err - } - if err := i.navflagsChar.WriteValue([]byte(flag), nil); err != nil { - return err - } - i.navigationEv.flag = flag - } - - if narrative != i.navigationEv.narrative { - log.Debug().Str("func", "Navigation"). - Msg("Sending narrative") - if err := i.checkStatus(i.navnarrativeChar, NavNarrativeChar); err != nil { - return err - } - if err := i.navnarrativeChar.WriteValue([]byte(narrative), nil); err != nil { - return err - } - i.navigationEv.narrative = narrative - } - - if dist != i.navigationEv.dist { - log.Debug().Str("func", "Navigation"). - Msg("Sending mandist") - if err := i.checkStatus(i.navmandistChar, NavManDistChar); err != nil { - return err - } - if err := i.navmandistChar.WriteValue([]byte(dist), nil); err != nil { - return err - } - i.navigationEv.dist = dist - } - - if progress != i.navigationEv.progress { - log.Debug().Str("func", "Navigation"). - Msg("Sending progress") - if err := i.checkStatus(i.navprogressChar, NavProgressChar); err != nil { - return err - } - buf := &bytes.Buffer{} - binary.Write(buf, binary.LittleEndian, progress) - if err := i.navprogressChar.WriteValue(buf.Bytes(), nil); err != nil { - return err - } - i.navigationEv.progress = progress - } - - return nil -} // These constants represent the possible call statuses selected by the user const ( diff --git a/navigation.go b/navigation.go new file mode 100644 index 0000000..3987649 --- /dev/null +++ b/navigation.go @@ -0,0 +1,140 @@ +package infinitime + +import ( + "bytes" + "encoding/binary" + "errors" + "github.com/muka/go-bluetooth/bluez/profile/gatt" +) + +const ( + NavFlagsChar = "00010001-78fc-48fe-8e23-433b3a1942d0" + NavNarrativeChar= "00010002-78fc-48fe-8e23-433b3a1942d0" + NavManDistChar = "00010003-78fc-48fe-8e23-433b3a1942d0" + NavProgressChar = "00010004-78fc-48fe-8e23-433b3a1942d0" +) + +type NavigationCtrl struct { + flagsChar *gatt.GattCharacteristic1 + narrativeChar*gatt.GattCharacteristic1 + mandistChar *gatt.GattCharacteristic1 + progressChar *gatt.GattCharacteristic1 +} + +type NavigationEvent struct { + flag string + narrative string + dist string + progress uint8 +} + +var ( + ErrNavProgress = errors.New("progress needs to between 0 and 100") + ErrNavInvalidFlag = errors.New("this flag is invalid") +) + +var NavFlagNames = []string{ + "arrive", "arrive-left", "arrive-right", "arrive-straight", + "close", + "continue", "continue-left", "continue-right", "continue-slight-left", + "continue-slight-right", "continue-straight", "continue-uturn", + "depart", "depart-left", "depart-right", "depart-straight", + "end-of-road-left", "end-of-road-right", + "ferry", "flag", + "fork", "fork-left", "fork-right", "fork-straight", + "fork-slight-left", "fork-slight-right", + "invalid", "invalid-left", "invalid-right", + "invalid-slight-left", "invalid-slight-right", + "invalid-straight", "invalid-uturn", + "merge-left", "merge-right", "merge-slight-left", + "merge-slight-right", "merge-straight", + "new-name-left", "new-name-right", "new-name-sharp-left", "new-name-sharp-right", + "new-name-slight-left", "new-name-slight-right", "new-name-straight", + "notification-left", "notification-right", + "notification-sharp-left", "notification-sharp-right", + "notification-slight-left", "notification-slight-right", + "notification-straight", + "off-ramp-left", "off-ramp-right", "off-ramp-sharp-left", "off-ramp-sharp-right", + "off-ramp-slight-left", "off-ramp-slight-right", "off-ramp-straight", + "on-ramp-left", "on-ramp-right", "on-ramp-sharp-left", "on-ramp-sharp-right", + "on-ramp-slight-left", "on-ramp-slight-right", "on-ramp-straight", + "rotary", "rotary-left", "rotary-right", "rotary-sharp-left", "rotary-sharp-right", + "rotary-slight-left", "rotary-slight-right", "rotary-straight", + "roundabout", "roundabout-left", "roundabout-right", + "roundabout-sharp-left", "roundabout-sharp-right", + "roundabout-slight-left", "roundabout-slight-right", "roundabout-straight", + "turn-left", "turn-right", "turn-sharp-left", "turn-sharp-right", + "turn-slight-left", "turn-slight-right", "turn-stright", + "updown", "uturn", +} + +func CheckValidNavFlag(flag string) error { + for _, v := range NavFlagNames { + if v == flag { + return nil + } + } + return ErrNavInvalidFlag +} + +// Navigation sends a NavigationEvent to the watch +func (i *Device) SetNavigation(flag string, narrative string, dist string, progress uint8) error { + if progress > 100 { + return ErrNavProgress + } + if err := CheckValidNavFlag(flag); err != nil { + return err + } + + if flag != i.navigationEv.flag { + log.Debug().Str("func", "Navigation"). + Msg("Sending flag") + if err := i.checkStatus(i.Navigation.flagsChar, NavFlagsChar); err != nil { + return err + } + if err := i.Navigation.flagsChar.WriteValue([]byte(flag), nil); err != nil { + return err + } + i.navigationEv.flag = flag + } + + if narrative != i.navigationEv.narrative { + log.Debug().Str("func", "Navigation"). + Msg("Sending narrative") + if err := i.checkStatus(i.Navigation.narrativeChar, NavNarrativeChar); err != nil { + return err + } + if err := i.Navigation.narrativeChar.WriteValue([]byte(narrative), nil); err != nil { + return err + } + i.navigationEv.narrative = narrative + } + + if dist != i.navigationEv.dist { + log.Debug().Str("func", "Navigation"). + Msg("Sending mandist") + if err := i.checkStatus(i.Navigation.mandistChar, NavManDistChar); err != nil { + return err + } + if err := i.Navigation.mandistChar.WriteValue([]byte(dist), nil); err != nil { + return err + } + i.navigationEv.dist = dist + } + + if progress != i.navigationEv.progress { + log.Debug().Str("func", "Navigation"). + Msg("Sending progress") + if err := i.checkStatus(i.Navigation.progressChar, NavProgressChar); err != nil { + return err + } + buf := &bytes.Buffer{} + binary.Write(buf, binary.LittleEndian, progress) + if err := i.Navigation.progressChar.WriteValue(buf.Bytes(), nil); err != nil { + return err + } + i.navigationEv.progress = progress + } + + return nil +} -- 2.45.2 From d46f5453f6c3828ef1fefbb3278bc94fc9374bf0 Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Sat, 22 Oct 2022 13:46:50 +0100 Subject: [PATCH 8/9] 2. Defined NavFlag type --- navigation.go | 146 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 97 insertions(+), 49 deletions(-) diff --git a/navigation.go b/navigation.go index 3987649..730ceac 100644 --- a/navigation.go +++ b/navigation.go @@ -21,8 +21,103 @@ type NavigationCtrl struct { progressChar *gatt.GattCharacteristic1 } +type NavFlag string +const ( + NavFlagArrive NavFlag = "arrive" + NavFlagArriveLeft NavFlag = "arrive-left" + NavFlagArriveRight NavFlag = "arrive-right" + NavFlagArriveStraight NavFlag = "arrive-straight" + NavFlagClose NavFlag = "close" + NavFlagContinue NavFlag = "continue" + NavFlagContinueLeft NavFlag = "continue-left" + NavFlagContinueRight NavFlag = "continue-right" + NavFlagContinueSlightLeft NavFlag = "continue-slight-left" + NavFlagContinueSlightRight NavFlag = "continue-slight-right" + NavFlagContinueStraight NavFlag = "continue-straight" + NavFlagContinueUturn NavFlag = "continue-uturn" + NavFlagDepart NavFlag = "depart" + NavFlagDepartLeft NavFlag = "depart-left" + NavFlagDepartRight NavFlag = "depart-right" + NavFlagDepartStraight NavFlag = "depart-straight" + NavFlagEndOfRoadLeft NavFlag = "end-of-road-left" + NavFlagEndOfRoadRight NavFlag = "end-of-road-right" + NavFlagFerry NavFlag = "ferry" + NavFlagFlag NavFlag = "flag" + NavFlagFork NavFlag = "fork" + NavFlagForkLeft NavFlag = "fork-left" + NavFlagForkRight NavFlag = "fork-right" + NavFlagForkSlightLeft NavFlag = "fork-slight-left" + NavFlagForkSlightRight NavFlag = "fork-slight-right" + NavFlagForkStraight NavFlag = "fork-straight" + NavFlagInvalid NavFlag = "invalid" + NavFlagInvalidLeft NavFlag = "invalid-left" + NavFlagInvalidRight NavFlag = "invalid-right" + NavFlagInvalidSlightLeft NavFlag = "invalid-slight-left" + NavFlagInvalidSlightRight NavFlag = "invalid-slight-right" + NavFlagInvalidStraight NavFlag = "invalid-straight" + NavFlagInvalidUturn NavFlag = "invalid-uturn" + NavFlagMergeLeft NavFlag = "merge-left" + NavFlagMergeRight NavFlag = "merge-right" + NavFlagMergeSlightLeft NavFlag = "merge-slight-left" + NavFlagMergeSlightRight NavFlag = "merge-slight-right" + NavFlagMergeStraight NavFlag = "merge-straight" + NavFlagNewNameLeft NavFlag = "new-name-left" + NavFlagNewNameRight NavFlag = "new-name-right" + NavFlagNewNameSharpLeft NavFlag = "new-name-sharp-left" + NavFlagNewNameSharpRight NavFlag = "new-name-sharp-right" + NavFlagNewNameSlightLeft NavFlag = "new-name-slight-left" + NavFlagNewNameSlightRight NavFlag = "new-name-slight-right" + NavFlagNewNameStraight NavFlag = "new-name-straight" + NavFlagNotificationLeft NavFlag = "notification-left" + NavFlagNotificationRight NavFlag = "notification-right" + NavFlagNotificationSharpLeft NavFlag = "notification-sharp-left" + NavFlagNotificationSharpRight NavFlag = "notification-sharp-right" + NavFlagNotificationSlightLeft NavFlag = "notification-slight-left" + NavFlagNotificationSlightRight NavFlag = "notification-slight-right" + NavFlagNotificationStraight NavFlag = "notification-straight" + NavFlagOffRampLeft NavFlag = "off-ramp-left" + NavFlagOffRampRight NavFlag = "off-ramp-right" + NavFlagOffRampSharpLeft NavFlag = "off-ramp-sharp-left" + NavFlagOffRampSharpRight NavFlag = "off-ramp-sharp-right" + NavFlagOffRampSlightLeft NavFlag = "off-ramp-slight-left" + NavFlagOffRampSlightRight NavFlag = "off-ramp-slight-right" + NavFlagOffRampStraight NavFlag = "off-ramp-straight" + NavFlagOnRampLeft NavFlag = "on-ramp-left" + NavFlagOnRampRight NavFlag = "on-ramp-right" + NavFlagOnRampSharpLeft NavFlag = "on-ramp-sharp-left" + NavFlagOnRampSharpRight NavFlag = "on-ramp-sharp-right" + NavFlagOnRampSlightLeft NavFlag = "on-ramp-slight-left" + NavFlagOnRampSlightRight NavFlag = "on-ramp-slight-right" + NavFlagOnRampStraight NavFlag = "on-ramp-straight" + NavFlagRotary NavFlag = "rotary" + NavFlagRotaryLeft NavFlag = "rotary-left" + NavFlagRotaryRight NavFlag = "rotary-right" + NavFlagRotarySharpLeft NavFlag = "rotary-sharp-left" + NavFlagRotarySharpRight NavFlag = "rotary-sharp-right" + NavFlagRotarySlightLeft NavFlag = "rotary-slight-left" + NavFlagRotarySlightRight NavFlag = "rotary-slight-right" + NavFlagRotaryStraight NavFlag = "rotary-straight" + NavFlagRoundabout NavFlag = "roundabout" + NavFlagRoundaboutLeft NavFlag = "roundabout-left" + NavFlagRoundaboutRight NavFlag = "roundabout-right" + NavFlagRoundaboutSharpLeft NavFlag = "roundabout-sharp-left" + NavFlagRoundaboutSharpRight NavFlag = "roundabout-sharp-right" + NavFlagRoundaboutSlightLeft NavFlag = "roundabout-slight-left" + NavFlagRoundaboutSlightRight NavFlag = "roundabout-slight-right" + NavFlagRoundaboutStraight NavFlag = "roundabout-straight" + NavFlagTurnLeft NavFlag = "turn-left" + NavFlagTurnRight NavFlag = "turn-right" + NavFlagTurnSharpLeft NavFlag = "turn-sharp-left" + NavFlagTurnSharpRight NavFlag = "turn-sharp-right" + NavFlagTurnSlightLeft NavFlag = "turn-slight-left" + NavFlagTurnSlightRight NavFlag = "turn-slight-right" + NavFlagTurnStright NavFlag = "turn-stright" + NavFlagUpdown NavFlag = "updown" + NavFlagUturn NavFlag = "uturn" +) + type NavigationEvent struct { - flag string + flag NavFlag narrative string dist string progress uint8 @@ -33,58 +128,11 @@ var ( ErrNavInvalidFlag = errors.New("this flag is invalid") ) -var NavFlagNames = []string{ - "arrive", "arrive-left", "arrive-right", "arrive-straight", - "close", - "continue", "continue-left", "continue-right", "continue-slight-left", - "continue-slight-right", "continue-straight", "continue-uturn", - "depart", "depart-left", "depart-right", "depart-straight", - "end-of-road-left", "end-of-road-right", - "ferry", "flag", - "fork", "fork-left", "fork-right", "fork-straight", - "fork-slight-left", "fork-slight-right", - "invalid", "invalid-left", "invalid-right", - "invalid-slight-left", "invalid-slight-right", - "invalid-straight", "invalid-uturn", - "merge-left", "merge-right", "merge-slight-left", - "merge-slight-right", "merge-straight", - "new-name-left", "new-name-right", "new-name-sharp-left", "new-name-sharp-right", - "new-name-slight-left", "new-name-slight-right", "new-name-straight", - "notification-left", "notification-right", - "notification-sharp-left", "notification-sharp-right", - "notification-slight-left", "notification-slight-right", - "notification-straight", - "off-ramp-left", "off-ramp-right", "off-ramp-sharp-left", "off-ramp-sharp-right", - "off-ramp-slight-left", "off-ramp-slight-right", "off-ramp-straight", - "on-ramp-left", "on-ramp-right", "on-ramp-sharp-left", "on-ramp-sharp-right", - "on-ramp-slight-left", "on-ramp-slight-right", "on-ramp-straight", - "rotary", "rotary-left", "rotary-right", "rotary-sharp-left", "rotary-sharp-right", - "rotary-slight-left", "rotary-slight-right", "rotary-straight", - "roundabout", "roundabout-left", "roundabout-right", - "roundabout-sharp-left", "roundabout-sharp-right", - "roundabout-slight-left", "roundabout-slight-right", "roundabout-straight", - "turn-left", "turn-right", "turn-sharp-left", "turn-sharp-right", - "turn-slight-left", "turn-slight-right", "turn-stright", - "updown", "uturn", -} - -func CheckValidNavFlag(flag string) error { - for _, v := range NavFlagNames { - if v == flag { - return nil - } - } - return ErrNavInvalidFlag -} - // Navigation sends a NavigationEvent to the watch -func (i *Device) SetNavigation(flag string, narrative string, dist string, progress uint8) error { +func (i *Device) SetNavigation(flag NavFlag, narrative string, dist string, progress uint8) error { if progress > 100 { return ErrNavProgress } - if err := CheckValidNavFlag(flag); err != nil { - return err - } if flag != i.navigationEv.flag { log.Debug().Str("func", "Navigation"). -- 2.45.2 From c3a87278a65707960f370a6e0c7fafa3f018ab04 Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Sat, 22 Oct 2022 13:49:52 +0100 Subject: [PATCH 9/9] 3. Passed NavigationEvent instead of pieces --- navigation.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/navigation.go b/navigation.go index 730ceac..16e7da4 100644 --- a/navigation.go +++ b/navigation.go @@ -117,10 +117,10 @@ const ( ) type NavigationEvent struct { - flag NavFlag - narrative string - dist string - progress uint8 + Flag NavFlag + Narrative string + Dist string + Progress uint8 } var ( @@ -129,59 +129,59 @@ var ( ) // Navigation sends a NavigationEvent to the watch -func (i *Device) SetNavigation(flag NavFlag, narrative string, dist string, progress uint8) error { - if progress > 100 { +func (i *Device) SetNavigation(ev NavigationEvent) error { + if ev.Progress > 100 { return ErrNavProgress } - if flag != i.navigationEv.flag { + if ev.Flag != i.navigationEv.Flag { log.Debug().Str("func", "Navigation"). Msg("Sending flag") if err := i.checkStatus(i.Navigation.flagsChar, NavFlagsChar); err != nil { return err } - if err := i.Navigation.flagsChar.WriteValue([]byte(flag), nil); err != nil { + if err := i.Navigation.flagsChar.WriteValue([]byte(ev.Flag), nil); err != nil { return err } - i.navigationEv.flag = flag + i.navigationEv.Flag = ev.Flag } - if narrative != i.navigationEv.narrative { + if ev.Narrative != i.navigationEv.Narrative { log.Debug().Str("func", "Navigation"). Msg("Sending narrative") if err := i.checkStatus(i.Navigation.narrativeChar, NavNarrativeChar); err != nil { return err } - if err := i.Navigation.narrativeChar.WriteValue([]byte(narrative), nil); err != nil { + if err := i.Navigation.narrativeChar.WriteValue([]byte(ev.Narrative), nil); err != nil { return err } - i.navigationEv.narrative = narrative + i.navigationEv.Narrative = ev.Narrative } - if dist != i.navigationEv.dist { + if ev.Dist != i.navigationEv.Dist { log.Debug().Str("func", "Navigation"). Msg("Sending mandist") if err := i.checkStatus(i.Navigation.mandistChar, NavManDistChar); err != nil { return err } - if err := i.Navigation.mandistChar.WriteValue([]byte(dist), nil); err != nil { + if err := i.Navigation.mandistChar.WriteValue([]byte(ev.Dist), nil); err != nil { return err } - i.navigationEv.dist = dist + i.navigationEv.Dist = ev.Dist } - if progress != i.navigationEv.progress { + if ev.Progress != i.navigationEv.Progress { log.Debug().Str("func", "Navigation"). Msg("Sending progress") if err := i.checkStatus(i.Navigation.progressChar, NavProgressChar); err != nil { return err } buf := &bytes.Buffer{} - binary.Write(buf, binary.LittleEndian, progress) + binary.Write(buf, binary.LittleEndian, ev.Progress) if err := i.Navigation.progressChar.WriteValue(buf.Bytes(), nil); err != nil { return err } - i.navigationEv.progress = progress + i.navigationEv.Progress = ev.Progress } return nil -- 2.45.2