diff --git a/README.md b/README.md index 9d32ae6..e60d144 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ At this moment, this library supports the following sentence types: - [GPVTG](http://aprs.gids.nl/nmea/#vtg) - Track Made Good and Ground Speed - [GPZDA](http://aprs.gids.nl/nmea/#zda) - Date & time data - [PGRME](http://aprs.gids.nl/nmea/#rme) - Estimated Position Error (Garmin proprietary sentence) +- [GPHDT](http://aprs.gids.nl/nmea/#hdt) - Actual vessel heading in degrees True ## Example diff --git a/gphdt.go b/gphdt.go new file mode 100644 index 0000000..522882f --- /dev/null +++ b/gphdt.go @@ -0,0 +1,25 @@ +package nmea + +const ( + // PrefixGPHDT prefix of GPHDT sentence type + PrefixGPHDT = "GPHDT" +) + +// GPHDT is the Actual vessel heading in degrees True. +// http://aprs.gids.nl/nmea/#hdt +type GPHDT struct { + BaseSentence + Heading float64 // Heading in degrees + True bool // heading relative to true north if T +} + +// newGPHDT constructor +func newGPHDT(s BaseSentence) (GPHDT, error) { + p := newParser(s, PrefixGPHDT) + m := GPHDT{ + BaseSentence: s, + Heading: p.Float64(0, "heading"), + True: p.EnumString(1, "true", "T") == "T", + } + return m, p.Err() +} diff --git a/gphdt_test.go b/gphdt_test.go new file mode 100644 index 0000000..0e5296b --- /dev/null +++ b/gphdt_test.go @@ -0,0 +1,50 @@ +package nmea + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +var gphdttests = []struct { + name string + raw string + err string + msg GPHDT +}{ + { + name: "good sentence", + raw: "$GPHDT,123.456,T*32", + msg: GPHDT{ + Heading: 123.456, + True: true, + }, + }, + { + name: "invalid True", + raw: "$GPHDT,123.456,X*3E", + err: "nmea: GPHDT invalid true: X", + }, + { + name: "invalid Heading", + raw: "$GPHDT,XXX,T*43", + err: "nmea: GPHDT invalid heading: XXX", + }, +} + +func TestGPHDT(t *testing.T) { + for _, tt := range gphdttests { + t.Run(tt.name, func(t *testing.T) { + m, err := Parse(tt.raw) + if tt.err != "" { + assert.Error(t, err) + assert.EqualError(t, err, tt.err) + } else { + assert.NoError(t, err) + gphdt := m.(GPHDT) + gphdt.BaseSentence = BaseSentence{} + assert.Equal(t, tt.msg, gphdt) + } + }) + } +} diff --git a/sentence.go b/sentence.go index 00ca926..bed8a18 100644 --- a/sentence.go +++ b/sentence.go @@ -104,6 +104,8 @@ func Parse(raw string) (Sentence, error) { return newGPGSV(s) case PrefixGLGSV: return newGLGSV(s) + case PrefixGPHDT: + return newGPHDT(s) default: return nil, fmt.Errorf("nmea: sentence type '%s' not implemented", s.Type) }