1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use crate::stack_str::PackArrayString;

/// Description of an angle in Degrees + Minutes
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
pub struct DegreeMinutes {
    pub degrees: i64,
    pub minutes: f32,
}

impl From<f32> for DegreeMinutes {
    // Create DegreeMinutes from a number of degrees
    fn from(dd: f32) -> Self {
        let degrees = (dd
            - match dd % 1.0 >= 0.0 {
                true => dd % 1.0,
                false => 1.0 + dd % 1.0,
            }) as i64;
        return DegreeMinutes {
            degrees,
            minutes: (dd - degrees as f32) * 60.0,
        };
    }
}

impl PackArrayString for DegreeMinutes {
    fn pack_into<const SIZE: usize>(
        &self,
        s: &mut arrayvec::ArrayString<SIZE>,
    ) -> Result<(), crate::errors::PackError> {
        // Handle Degrees
        s.try_push(('0' as u8 + ((self.degrees / 10) % 10) as u8) as char)?;
        s.try_push(('0' as u8 + (self.degrees % 10) as u8) as char)?;

        // Handle Minutes
        s.try_push(('0' as u8 + ((self.minutes / 10.0) % 10.0) as u8) as char)?;
        s.try_push(('0' as u8 + (self.minutes % 10.0) as u8) as char)?;

        // Push decimal
        s.try_push('.')?;

        // Handle Minute decimals
        s.try_push(('0' as u8 + ((self.minutes * 10.0) % 10.0) as u8) as char)?;
        s.try_push(('0' as u8 + ((self.minutes * 100.0) % 10.0) as u8) as char)?;

        Ok(())
    }
}

/// Enum representation of the 4 cardinal Directions
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
pub enum CardinalDirection {
    North,
    East,
    South,
    West,
}

impl PackArrayString for CardinalDirection {
    fn pack_into<const SIZE: usize>(
        &self,
        s: &mut arrayvec::ArrayString<SIZE>,
    ) -> Result<(), crate::errors::PackError> {
        s.try_push(match self {
            Self::North => 'N',
            Self::East => 'E',
            Self::South => 'S',
            Self::West => 'W',
        })?;
        Ok(())
    }
}

/// Representation of an angle in Degree/Decimal/Minutes
pub trait DdmAngle {
    fn get_direction(&self) -> CardinalDirection;
    fn get_degree_minutes(&self) -> DegreeMinutes;
}

/// Latitude in DDM
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
pub struct DdmLatitude {
    ddm: DegreeMinutes,
    direction: CardinalDirection,
}

impl From<f32> for DdmLatitude {
    fn from(latitude: f32) -> Self {
        Self {
            ddm: match latitude < 0.0 {
                true => latitude * -1.0,
                false => latitude,
            }
            .into(),
            direction: match latitude < 0.0 {
                true => CardinalDirection::South,
                false => CardinalDirection::North,
            },
        }
    }
}

impl DdmAngle for DdmLatitude {
    fn get_direction(&self) -> CardinalDirection {
        self.direction
    }

    fn get_degree_minutes(&self) -> DegreeMinutes {
        self.ddm
    }
}

impl PackArrayString for DdmLatitude {
    fn pack_into<const SIZE: usize>(
        &self,
        s: &mut arrayvec::ArrayString<SIZE>,
    ) -> Result<(), crate::errors::PackError> {
        // Push DDM
        self.ddm.pack_into(s)?;

        // Push Direction
        self.direction.pack_into(s)?;

        Ok(())
    }
}

/// Longitude in DDM
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
pub struct DdmLongitude {
    ddm: DegreeMinutes,
    direction: CardinalDirection,
}

impl From<f32> for DdmLongitude {
    fn from(longitude: f32) -> Self {
        Self {
            ddm: match longitude < 0.0 {
                true => longitude * -1.0,
                false => longitude,
            }
            .into(),
            direction: match longitude < 0.0 {
                true => CardinalDirection::West,
                false => CardinalDirection::East,
            },
        }
    }
}

impl DdmAngle for DdmLongitude {
    fn get_direction(&self) -> CardinalDirection {
        self.direction
    }

    fn get_degree_minutes(&self) -> DegreeMinutes {
        self.ddm
    }
}

impl PackArrayString for DdmLongitude {
    fn pack_into<const SIZE: usize>(
        &self,
        s: &mut arrayvec::ArrayString<SIZE>,
    ) -> Result<(), crate::errors::PackError> {
        // Push DDM
        s.try_push(('0' as u8 + (self.ddm.degrees / 100) as u8) as char)?;
        self.ddm.pack_into(s)?;

        // Push Direction
        self.direction.pack_into(s)?;

        Ok(())
    }
}