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_Gunnlaug_18
//!
//! This program reads GPGGA and print data from GPS

//! # Example Setting UART
//! ``` 
//!GPS-Gunnlaug_18::GPS;
//! use rpi_embedded::uart::{Uart,Parity};
//!
//! pub fn settings_uart(&mut self){
//!    let mut uart = Uart::new(Settings::Baudgps.get_baud(), Parity::None,Settings::Data.get_baud() as u8,Settings::Stopbit.get_baud() as u8).expect("Initializing failed!"); //115_200
//!   loop{
//!        let string_read = uart.read_line().expect("GPS reading failed");
//!``` 



use rpi_embedded::uart::{Uart,Parity};
use std::string::String;

///Enum for settings
pub enum Settings { 
    Baudgps,
    Baudarduino,
    Data,
    Stopbit,
    
}
//implementing enum settings
impl Settings{
    pub fn get_baud(&self) -> u32{
        let value : u32;
        match self{
            Settings::Baudgps => {value = 9600}
            Settings::Baudarduino => {value = 115_200}
            Settings::Data =>{value = 8}
            Settings::Stopbit =>{value = 1} 

        }
        value
        }

}
///GPS struct
struct GPS{
    pub longitude: String, 
    pub latitude: String, 
    pub altitude: String,
    pub time: String,
    pub satellites: String,
    pub indicator: String
}


//implementing 
impl GPS{
    //new struct
    pub fn new() -> Self{
        let mut _out = Self{
            longitude: "".to_string(), //string
            latitude: "".to_string(),
            altitude: "".to_string(),
            time:"".to_string(),
            satellites: "".to_string(),
            indicator:"".to_string(),
        };
        _out
    
    }
/// splits string,  and collects items to vector, goes through all vector and if read_vec[0] =$GPGGA then calls gps_system and gps_print 
    pub fn spliting(&mut self,string_read:String){
        let reading = string_read.split(',');
        let read_vec: Vec<&str> = reading.collect();
        //println!("{}",read_vec[0]);
        self.indicator = read_vec[0].to_string();
        if read_vec[0] == "$GPGGA"{
            self.gps_system(read_vec);
            self.gps_print();
            }

        }
    pub fn  gps_system(&mut self,read_vec: Vec<&str>){ //takes a value from the list and converts it to string for latitude ,longitude ,altidute, satellite and time 
        self.latitude = read_vec[2].to_string() + "" + &read_vec[3].to_string();
        self.longitude = read_vec[4].to_string()+ "" + &read_vec[5].to_string();
        self.altitude = read_vec[9].to_string() + "" + &read_vec[10].to_string();
        self.time = read_vec[1].to_string();
        self.satellites = read_vec[7].to_string();

    }

//
    pub fn gps_print(&mut self){//function to print
        let print_str=format! (
        "\n
        Time: {}h{}m{}s \n
        Latitude: {} \n
        Longitude: {} \n
        Altitude: {} \n
        Number of Satellites: {} \n",&self.time[0..2],&self.time[2..4],&self.time[4..6],self.latitude,self.longitude,self.altitude,self.satellites);
        println!("Useful data: {}",print_str);
    }
    ///setting uart
    pub fn settings_uart(&mut self){
        let mut uart = Uart::new(Settings::Baudgps.get_baud(), Parity::None,Settings::Data.get_baud() as u8,Settings::Stopbit.get_baud() as u8).expect("Initializing failed!"); //115_200
        loop{
        //uart.set_read_mode(1, Duration::default()).expect("read mode fail");
            let string_read = uart.read_line().expect("GPS reading failed");
            self.spliting(string_read);
        }
    }
}




fn main(){
    let mut tracking = GPS::new(); //makes init struct
    tracking.settings_uart(); //calls all other functions of the struct
    }



//test cases :)     
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_string_indication(){
        let mut track = GPS::new();
        track.spliting("$GPGGA,112503.101,6507.4485,N,02255.5355,W,1,04,2.26,17.7,M,60.6,M,,*44".to_string());

        assert_eq!(track.indicator,"$GPGGA");
    }


    #[test]
    fn test_lattitude_direction(){
        let mut track = GPS::new();

        track.spliting("$GPGGA,112503.101,6507.4485,N,02255.5355,W,1,04,2.26,17.7,M,60.6,M,,*44".to_string());
        
        assert_eq!(track.latitude[9..10],"N".to_string());
    }

    #[test]
    fn test_longitude_direction(){
        let mut track = GPS::new();
        track.spliting("$GPGGA,112503.101,6507.4485,N,02255.5355,W,1,04,2.26,17.7,M,60.6,M,,*44".to_string());
        assert_eq!(track.longitude[10..11],"W".to_string());
    }

    #[test]
    fn test_longitude_length(){
        let mut track = GPS::new();
        track.spliting("$GPGGA,112503.101,6507.4485,N,02255.5355,W,1,04,2.26,17.7,M,60.6,M,,*44".to_string());
        assert_eq!(track.longitude.len(),11 as usize); //11 because 10numbers + direction
    }

    #[test]
    fn test_latitude_length(){
        let mut track = GPS::new();
        track.spliting("$GPGGA,112503.101,6507.4485,N,02255.5355,W,1,04,2.26,17.7,M,60.6,M,,*44".to_string());
        
        assert_eq!(track.latitude.len(),10 as usize);
    }
    #[test]
    fn test_altitude_length(){
        let mut track = GPS::new();
        track.spliting("$GPGGA,112503.101,6507.4485,N,02255.5355,W,1,04,2.26,17.7,M,60.6,M,,*44".to_string());
        assert_eq!(track.altitude.len(),5 as usize);
    }
    #[test]
    #[should_panic]
    fn test_if_panic(){
        let mut track = GPS::new();
        track.spliting("$GPGGA,112503.101,6507.4485,N,02255.5355,W,1,04,2.26,17.7,M,60.6,M,,*44".to_string());
        assert_eq!(track.latitude.len(),15 as usize);
    }
}