GPS_project_aisha 0.1.1

A basic program that reads and print data from from a GPS Antenna
//! # GPS_project_aisha
//! 
//! A program to read GPGGA and print data from a GPS antenna. 
use rpi_embedded::gpio::{Gpio, OutputPin, InputPin};
use std::error::Error;

use std::time::{Duration,Instant};
use rpi_embedded::uart::{Uart, Parity};



/// The UART setting types for the GPS antenna
pub enum Settings {
    
    Baud,       //9_600
    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
    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();

}


#[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(),7);
    }

    #[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);
    }


}