Skip to content

Commit 0779bba

Browse files
authored
Merge pull request #77 from JasminFragnaud/mtw-sentence-parsing
Add MTW sentence parser
2 parents 7af7db4 + e74a9b5 commit 0779bba

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ NMEA Standard Sentences
2222
- GSA
2323
- GSV
2424
- MDA
25+
- MTW
2526
- MWV
2627
- RMC
2728
- VTG

src/parse.rs

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub enum ParseResult {
108108
GSA(GsaData),
109109
GSV(GsvData),
110110
MDA(MdaData),
111+
MTW(MtwData),
111112
MWV(MwvData),
112113
RMC(RmcData),
113114
TXT(TxtData),

src/parser.rs

+1
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ impl<'a> Nmea {
359359
| ParseResult::AAM(_)
360360
| ParseResult::ALM(_)
361361
| ParseResult::PGRMZ(_)
362+
| ParseResult::MTW(_)
362363
| ParseResult::MWV(_)
363364
| ParseResult::MDA(_) => return Ok(FixType::Invalid),
364365

src/sentences/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod gns;
1111
mod gsa;
1212
mod gsv;
1313
mod mda;
14+
mod mtw;
1415
mod mwv;
1516
mod rmc;
1617
mod rmz;
@@ -37,6 +38,7 @@ pub use {
3738
gsa::{parse_gsa, GsaData},
3839
gsv::{parse_gsv, GsvData},
3940
mda::{parse_mda, MdaData},
41+
mtw::{parse_mtw, MtwData, MtwUnit},
4042
mwv::{parse_mwv, MwvData, MwvReference, MwvWindSpeedUnits},
4143
rmc::{parse_rmc, RmcData, RmcStatusOfFix},
4244
rmz::{parse_pgrmz, PgrmzData},

src/sentences/mtw.rs

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use nom::{
2+
character::complete::{char, one_of},
3+
combinator::opt,
4+
number::complete::double,
5+
sequence::preceded,
6+
IResult,
7+
};
8+
9+
use crate::{parse::NmeaSentence, Error, SentenceType};
10+
11+
/// MTW - Mean Temperature of Water
12+
///
13+
/// <https://gpsd.gitlab.io/gpsd/NMEA.html#_mtw_mean_temperature_of_water>
14+
///
15+
/// ```text
16+
/// 1 2 3
17+
/// | | |
18+
/// $--MTW,x.x,C*hh<CR><LF>
19+
/// ```
20+
/// 1: Temperature, degrees
21+
/// 2: Unit of Measurement, (only) Celsius
22+
/// 3: Mandatory NMEA checksum
23+
#[derive(Debug, PartialEq)]
24+
pub struct MtwData {
25+
pub temperature: Option<f64>,
26+
}
27+
28+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29+
pub enum MtwUnit {
30+
Celsius,
31+
}
32+
33+
/// # Parse MTW message
34+
///
35+
/// Information from mtw:
36+
///
37+
/// NMEA 0183 standard Mean Temperature of Water.
38+
/// <https://gpsd.gitlab.io/gpsd/NMEA.html#_mtw_mean_temperature_of_water>
39+
///
40+
/// ## Example (Ignore the line break):
41+
/// ```text
42+
/// $INMTW,17.9,C*1B
43+
///```
44+
///
45+
/// 1: 17.9 Temperature, degrees
46+
/// 2: C Unit of Measurement, (only) Celsius
47+
/// 3: *16 Mandatory NMEA checksum
48+
pub fn parse_mtw(sentence: NmeaSentence) -> Result<MtwData, Error> {
49+
if sentence.message_id != SentenceType::MTW {
50+
Err(Error::WrongSentenceHeader {
51+
expected: SentenceType::MTW,
52+
found: sentence.message_id,
53+
})
54+
} else {
55+
Ok(do_parse_mtw(sentence.data)?.1)
56+
}
57+
}
58+
59+
fn do_parse_mtw(i: &str) -> IResult<&str, MtwData> {
60+
let (i, temperature_value) = opt(double)(i)?;
61+
preceded(char(','), one_of("C"))(i)?;
62+
Ok((
63+
i,
64+
MtwData {
65+
temperature: temperature_value,
66+
},
67+
))
68+
}
69+
70+
#[cfg(test)]
71+
mod tests {
72+
use super::*;
73+
use crate::parse::parse_nmea_sentence;
74+
75+
#[test]
76+
fn test_parse_mtw() {
77+
let s = parse_nmea_sentence("$INMTW,17.9,C*1B").unwrap();
78+
assert_eq!(s.checksum, s.calc_checksum());
79+
assert_eq!(s.checksum, 0x1B);
80+
let mtw_data = parse_mtw(s).unwrap();
81+
assert_eq!(Some(17.9), mtw_data.temperature);
82+
}
83+
84+
#[test]
85+
fn test_parse_mtw_invalid_unit() {
86+
let s = parse_nmea_sentence("$INMTW,17.9,x*20").unwrap();
87+
assert_eq!(s.checksum, s.calc_checksum());
88+
assert_eq!(s.checksum, 0x20);
89+
assert_eq!(true, parse_mtw(s).is_err());
90+
}
91+
92+
#[test]
93+
fn test_parse_mtw_invalid_temp() {
94+
let s = parse_nmea_sentence("$INMTW,x.9,C*65").unwrap();
95+
assert_eq!(s.checksum, s.calc_checksum());
96+
assert_eq!(s.checksum, 0x65);
97+
assert_eq!(true, parse_mtw(s).is_err());
98+
}
99+
}

0 commit comments

Comments
 (0)