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
175
176
177
178
179
180
181
//! # GPS_project_aisha
//! 
//! A program to read GPGGA and print data from a GPS antenna. 
//! 
//!  # Examples
//! 
//! ```
//! use GPS_project_aisha::GPS;
//! use rpi_embedded::uart::{Uart, Parity};
//! 
//!fn main() {
//!    let mut gps =  GPS::init();
//!    gps.set_uart();
//!}
//! ```

use rpi_embedded::uart::{Uart, Parity};



/// The UART setting types for the GPS antenna
pub enum Settings {

    Baud,       
    ParityBit,
    StopBit,

}



impl Settings {
    pub fn get_data (&self) -> u32{
        let data: u32;
        match self {
            Settings::Baud => {data = 9600}
            Settings::ParityBit => {data = 8}
            Settings::StopBit => {data = 1}
        }
        data
    }
}

#[derive(Debug)]

/// The field of the data received from the GPS antenna
pub struct GPS {
    longitude: String, //insert enum type
    longitude_dir: String,
    latitude: String, //insert enum type
    latitude_dir: String,
    indicator: String,
    altitude: String,
    altitude_dir:String,
    time: String,

}

impl GPS {
    /// The initialization of the struct values
    pub fn init() -> Self{
        Self {
            longitude:String::from(""),
            longitude_dir:String::from(""),
            latitude: String::from(""),
            latitude_dir: String::from(""),
            indicator: String::from(""),
            altitude: String::from(""),
            altitude_dir: String::from(""),
            time: String::from(""),

            
        }
    }

    /// Reads values from a vector to assign to the fields in the struct
    pub fn convert(&mut self, gps_string:Vec<&str> ){
        self.indicator = gps_string[0].to_string();
        self.latitude = gps_string[2].to_string();
        self.latitude_dir = gps_string[3].to_string();
        self.longitude = gps_string[4].to_string();
        self.longitude_dir = gps_string[5].to_string();
        self.altitude = gps_string[9].to_string();
        self.altitude_dir = gps_string[10].to_string();
        self.time = gps_string[1].to_string();
        }

    /// Reads raw data from the GPS antenna and converts to a vector
    pub fn collect_data(&mut self, data:String){

            let to_collect = data.split(',');
            let vec: Vec<&str> = to_collect.collect();
            
            if vec[0].to_string() ==  String::from("$GPGGA"){

                self.convert(vec);
                println!("Time: {}:{}:{},  Longitude: {} {},  Latitude: {} {}, Altitude: {} {}", &self.time[0..2],&self.time[2..4],&self.time[4..6],self.longitude,self.longitude_dir,self.latitude,self.latitude_dir,self.altitude,self.altitude_dir);

        
    }
    }

    /// Sets the UART settings to enable the raspberry pi to read 
    pub fn set_uart(&mut self){
        let mut sensor = Uart::new(Settings::Baud.get_data(), Parity::None, Settings::ParityBit.get_data() as u8, Settings::StopBit.get_data() as u8).unwrap();

        loop{
            
            let data = sensor.read_line().expect("fail");
            self.collect_data(data);
            
        }
        
    }

}




fn main() {
    let mut gps =  GPS::init();
    gps.set_uart();

}


// Test cases 

#[cfg(test)]
mod tests {
    use super::*;

    #[test]

    fn test_indicator() {
        let mut gps_test1 = GPS::init();

        gps_test1.collect_data("$GPGGA,112503.101,6507.4485,N,02255.5355,W,1,04,2.26,17.7,M,60.6,M,,*44".to_string());
        assert_eq!(gps_test1.indicator,"$GPGGA");
    }


    #[test]
    fn test_baud_rate() {
        let gps_test2 = Settings::Baud.get_data();
        assert_eq!(gps_test2,9600);
    }


    #[test]
    fn test_parity_bit() {
        let gps_test3 = Settings::ParityBit.get_data();
        assert_eq!(gps_test3,8);
    }

    #[test]

    fn test_stop_bit() {
        let gps_test4 = Settings::StopBit.get_data();
        assert_eq!(gps_test4,1);
    }

    #[test]

    fn test_latitude() {
        let mut gps_test5 = GPS::init();
        gps_test5.collect_data("$GPGGA,112503.101,6507.4485,N,02255.5355,W,1,04,2.26,17.7,M,60.6,M,,*44".to_string());
        assert_eq!(gps_test5.latitude.len(),9);
    }

    #[test]

    fn test_longitude() {
        let mut gps_test6 = GPS::init();
        gps_test6.collect_data("$GPGGA,112503.101,6507.4485,N,02255.5355,W,1,04,2.26,17.7,M,60.6,M,,*44".to_string());
        assert_eq!(gps_test6.longitude.len(),10);
    }


}