aprs_encode/aprs/
wx.rs

1use crate::{errors::PackError, stack_str::PackArrayString};
2
3/// Represents the weather component of an APRS packet
4#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
5pub struct AprsWx {
6    pub wind_direction_degrees: Option<u8>,
7    pub wind_speed_mph: Option<u8>,
8    pub temperature_f: Option<i8>,
9}
10
11impl PackArrayString for AprsWx {
12    fn pack_into<const SIZE: usize>(
13        &self,
14        s: &mut arrayvec::ArrayString<SIZE>,
15    ) -> Result<(), PackError> {
16        // Push wind direction
17        if self.wind_direction_degrees.is_some() {
18            s.try_push('_')?;
19            let direction = self.wind_direction_degrees.as_ref().unwrap();
20            s.try_push(('0' as u8 + ((direction / 100) % 10) as u8) as char)?;
21            s.try_push(('0' as u8 + ((direction / 10) % 10) as u8) as char)?;
22            s.try_push(('0' as u8 + (direction % 10) as u8) as char)?;
23        }
24
25        // Push wind speed
26        if self.wind_speed_mph.is_some() {
27            s.try_push('/')?;
28            let speed = self.wind_speed_mph.as_ref().unwrap();
29            s.try_push(('0' as u8 + ((speed / 100) % 10) as u8) as char)?;
30            s.try_push(('0' as u8 + ((speed / 10) % 10) as u8) as char)?;
31            s.try_push(('0' as u8 + (speed % 10) as u8) as char)?;
32        }
33
34        // Push temperature
35        if self.temperature_f.is_some() {
36            s.try_push('t')?;
37            let temp = self.temperature_f.as_ref().unwrap();
38            s.try_push(('0' as u8 + ((temp / 100) % 10) as u8) as char)?;
39            s.try_push(('0' as u8 + ((temp / 10) % 10) as u8) as char)?;
40            s.try_push(('0' as u8 + (temp % 10) as u8) as char)?;
41        }
42
43        Ok(())
44    }
45}
46
47#[cfg(test)]
48mod tests {
49
50    use arrayvec::ArrayString;
51
52    use super::*;
53
54    #[test]
55    fn test_wx_packing() {
56        // Build a buffer
57        let mut buffer = ArrayString::<128>::new();
58
59        // Add the packet
60        let data = AprsWx {
61            wind_speed_mph: Some(10),
62            wind_direction_degrees: Some(35),
63            temperature_f: Some(65),
64        };
65
66        // Pack
67        data.pack_into(&mut buffer).unwrap();
68
69        assert_eq!(*buffer, *"_035/010t065");
70    }
71}