Skip to content

Commit

Permalink
add support for GPHDT message
Browse files Browse the repository at this point in the history
  • Loading branch information
icholy committed Jun 5, 2018
1 parent 7ad7ee7 commit 35ee665
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions gphdt.go
Original file line number Diff line number Diff line change
@@ -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()
}
50 changes: 50 additions & 0 deletions gphdt_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
2 changes: 2 additions & 0 deletions sentence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 35ee665

Please sign in to comment.