Skip to content

Commit

Permalink
Merge 0aaf168 into 7572fbe
Browse files Browse the repository at this point in the history
  • Loading branch information
freman committed Apr 12, 2019
2 parents 7572fbe + 0aaf168 commit 25443da
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 16 deletions.
28 changes: 28 additions & 0 deletions gsensord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package nmea

const (
// TypeGSensord type for GSensord sentences
TypeGSensord = "GSENSORD"
)

// GSensord represents measured g-loadings in the x, y and z axis.
// http://aprs.gids.nl/nmea/#gsa
type GSensord struct {
BaseSentence
X float64 // X-axis G value
Y float64 // Y-axis G value
Z float64 // Z-axis G valye
}

// newGSensord parses the GSensord sentence into this struct.
func newGSensord(s BaseSentence) (GSensord, error) {
p := newParser(s)
p.AssertType(TypeGSensord)
m := GSensord{
BaseSentence: s,
X: p.Float64(0, "x-axis g value"),
Y: p.Float64(1, "y-axis g value"),
Z: p.Float64(2, "z-axis g value"),
}
return m, p.Err()
}
41 changes: 41 additions & 0 deletions gsensord_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package nmea

import (
"testing"

"github.com/stretchr/testify/assert"
)

var gsensoredtests = []struct {
name string
raw string
err string
msg GSensord
}{
{
name: "good sentence",
raw: "$GSENSORD,0.060,0.060,-0.180",
msg: GSensord{
X: 0.06,
Y: 0.06,
Z: -0.180,
},
},
}

func TestGSensord(t *testing.T) {
for _, tt := range gsensoredtests {
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)
gsensored := m.(GSensord)
gsensored.BaseSentence = BaseSentence{}
assert.Equal(t, tt.msg, gsensored)
}
})
}
}
40 changes: 26 additions & 14 deletions sentence.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nmea

import (
"errors"
"fmt"
"strings"
)
Expand Down Expand Up @@ -60,21 +61,26 @@ func parseSentence(raw string) (BaseSentence, error) {
if startIndex != 0 {
return BaseSentence{}, fmt.Errorf("nmea: sentence does not start with a '$' or '!'")
}
sumSepIndex := strings.Index(raw, ChecksumSep)
if sumSepIndex == -1 {
return BaseSentence{}, fmt.Errorf("nmea: sentence does not contain checksum separator")
}
var (
fieldsRaw = raw[startIndex+1 : sumSepIndex]
fields = strings.Split(fieldsRaw, FieldSep)
checksumRaw = strings.ToUpper(raw[sumSepIndex+1:])
checksum = xorChecksum(fieldsRaw)
)
// Validate the checksum
if checksum != checksumRaw {
return BaseSentence{}, fmt.Errorf(
"nmea: sentence checksum mismatch [%s != %s]", checksum, checksumRaw)

var checksumRaw string
splice := strings.Split(raw[startIndex+1:], ChecksumSep)
fieldsRaw := splice[0]
fields := strings.Split(fieldsRaw, FieldSep)

if len(splice) == 2 {
checksumRaw = strings.ToUpper(splice[1])
if checksumRaw == "" {
return BaseSentence{}, errors.New("nmea: sentence does not contain checksum")
}
checksum := xorChecksum(fieldsRaw)

// Validate the checksum
if checksum != checksumRaw {
return BaseSentence{}, fmt.Errorf(
"nmea: sentence checksum mismatch [%s != %s]", checksum, checksumRaw)
}
}

talker, typ := parsePrefix(fields[0])
return BaseSentence{
Talker: talker,
Expand All @@ -87,6 +93,9 @@ func parseSentence(raw string) (BaseSentence, error) {

// parsePrefix takes the first field and splits it into a talker id and data type.
func parsePrefix(s string) (string, string) {
if s == TypeGSensord {
return "", s
}
if strings.HasPrefix(s, "P") {
return "P", s[1:]
}
Expand All @@ -112,6 +121,7 @@ func Parse(raw string) (Sentence, error) {
if err != nil {
return nil, err
}

if strings.HasPrefix(s.Raw, SentenceStart) {
switch s.Type {
case TypeRMC:
Expand All @@ -136,6 +146,8 @@ func Parse(raw string) (Sentence, error) {
return newGNS(s)
case TypeTHS:
return newTHS(s)
case TypeGSensord:
return newGSensord(s)
}
}
if strings.HasPrefix(s.Raw, SentenceStartEncapsulated) {
Expand Down
4 changes: 2 additions & 2 deletions sentence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ var sentencetests = []struct {
},
{
name: "bad checksum delimiter",
raw: "$GPFOO,1,2,3,x,y,z",
err: "nmea: sentence does not contain checksum separator",
raw: "$GPFOO,1,2,3,x,y,z*",
err: "nmea: sentence does not contain checksum",
},
{
name: "no start delimiter",
Expand Down

0 comments on commit 25443da

Please sign in to comment.