Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HDT sentence parser #82

Merged
merged 7 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ NMEA Standard Sentences
- GNS *
- GSA *
- GSV *
- HDT
- MDA
- MTW
- MWV
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//! - GNS *
//! - GSA *
//! - GSV *
//! - HDT
//! - MDA
//! - MWV
//! - RMC *
Expand Down
3 changes: 3 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub enum ParseResult {
GNS(GnsData),
GSA(GsaData),
GSV(GsvData),
HDT(HdtData),
MDA(MdaData),
MTW(MtwData),
MWV(MwvData),
Expand All @@ -131,6 +132,7 @@ impl From<&ParseResult> for SentenceType {
ParseResult::GNS(_) => SentenceType::GNS,
ParseResult::GSA(_) => SentenceType::GSA,
ParseResult::GSV(_) => SentenceType::GSV,
ParseResult::HDT(_) => SentenceType::HDT,
ParseResult::MDA(_) => SentenceType::MDA,
ParseResult::MTW(_) => SentenceType::MTW,
ParseResult::MWV(_) => SentenceType::MWV,
Expand Down Expand Up @@ -178,6 +180,7 @@ pub fn parse_str(sentence_input: &str) -> Result<ParseResult, Error> {
SentenceType::GBS => parse_gbs(nmea_sentence).map(ParseResult::GBS),
SentenceType::GGA => parse_gga(nmea_sentence).map(ParseResult::GGA),
SentenceType::GSV => parse_gsv(nmea_sentence).map(ParseResult::GSV),
SentenceType::HDT => parse_hdt(nmea_sentence).map(ParseResult::HDT),
SentenceType::RMC => parse_rmc(nmea_sentence).map(ParseResult::RMC),
SentenceType::GSA => parse_gsa(nmea_sentence).map(ParseResult::GSA),
SentenceType::VTG => parse_vtg(nmea_sentence).map(ParseResult::VTG),
Expand Down
1 change: 1 addition & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ impl<'a> Nmea {
| ParseResult::GBS(_)
| ParseResult::AAM(_)
| ParseResult::ALM(_)
| ParseResult::HDT(_)
| ParseResult::PGRMZ(_)
| ParseResult::MTW(_)
| ParseResult::MWV(_)
Expand Down
84 changes: 84 additions & 0 deletions src/sentences/hdt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use nom::{
bytes::complete::take_until,
character::complete::char,
combinator::{map_res, opt},
IResult,
};

use super::utils::parse_float_num;
use crate::{Error, NmeaSentence, SentenceType};

/// HDT - Heading - True
///
/// <https://gpsd.gitlab.io/gpsd/NMEA.html#_hdt_heading_true>
///
/// ```text
/// 1 2 3
/// | | |
/// $--HDT,x.x,T*hh<CR><LF>
/// ```
/// 1. Heading, degrees True
/// 2. T = True
/// 3. Checksum
#[derive(Debug, PartialEq)]
pub struct HdtData {
/// Heading, degrees True
pub heading: Option<f32>,
}

/// # Parse HDT message
///
/// From gpsd/driver_nmea0183.c
///
/// ```text
/// $HEHDT,341.8,T*21
///
/// HDT,x.x*hh<cr><lf>
/// ```
///
/// The only data field is true heading in degrees.
/// The following field is required to be 'T' indicating a true heading.
/// It is followed by a mandatory nmea_checksum.
pub fn parse_hdt(sentence: NmeaSentence) -> Result<HdtData, Error> {
if sentence.message_id != SentenceType::HDT {
Err(Error::WrongSentenceHeader {
expected: SentenceType::HDT,
found: sentence.message_id,
})
} else {
Ok(do_parse_hdt(sentence.data)?.1)
}
}

fn do_parse_hdt(i: &str) -> IResult<&str, HdtData> {
let (i, heading) = opt(map_res(take_until(","), parse_float_num::<f32>))(i)?;
let (i, _) = char(',')(i)?;
let (i, _) = char('T')(i)?;
Ok((i, HdtData { heading }))
}

#[cfg(test)]
mod tests {
use approx::assert_relative_eq;

use super::*;
use crate::parse::parse_nmea_sentence;

#[test]
fn test_parse_hdt_full() {
let data = parse_hdt(NmeaSentence {
talker_id: "GP",
message_id: SentenceType::HDT,
data: "274.07,T",
checksum: 0x03,
})
.unwrap();
assert_relative_eq!(data.heading.unwrap(), 274.07);

let s = parse_nmea_sentence("$GPHDT,,T*1B").unwrap();
assert_eq!(s.checksum, s.calc_checksum());

let data = parse_hdt(s);
assert_eq!(data, Ok(HdtData { heading: None }));
}
}
2 changes: 2 additions & 0 deletions src/sentences/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod gll;
mod gns;
mod gsa;
mod gsv;
mod hdt;
mod mda;
mod mtw;
mod mwv;
Expand Down Expand Up @@ -37,6 +38,7 @@ pub use {
gnss_type::GnssType,
gsa::{parse_gsa, GsaData},
gsv::{parse_gsv, GsvData},
hdt::{parse_hdt, HdtData},
mda::{parse_mda, MdaData},
mtw::{parse_mtw, MtwData, MtwUnit},
mwv::{parse_mwv, MwvData, MwvReference, MwvWindSpeedUnits},
Expand Down
2 changes: 2 additions & 0 deletions tests/all_supported_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ fn test_all_supported_messages() {
(SentenceType::GSA, "$GPGSA,A,3,23,31,22,16,03,07,,,,,,,1.8,1.1,1.4*3E"),
// GSV
(SentenceType::GSV, "$GPGSV,3,1,12,01,49,196,41,03,71,278,32,06,02,323,27,11,21,196,39*72"),
// HDT
(SentenceType::HDT, "$GPHDT,274.07,T*03"),
// MDA
(SentenceType::MDA, "$WIMWV,041.1,R,01.0,N,A*16"),
// MWV
Expand Down