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
//! # test of DataGPS
//!
//! `DataGPS` is a collection of utilities to read GPS adafruit data.
//! Print of all important data of GPS adafruit like Altitude, Latitude, Longitude
//!
//! # Examples of getting data
//!
//! ```
//!
//! let x = DataGPS::new(Bauds::Baud9600);
//! x.read_all_data();
//!
//!
//! ```

use std::{time::Duration};
use rpi_embedded::uart::{Parity, Uart};
    /// Enum to be able to set the settings
pub enum Bauds{
    Baud9600,
    Baud57600,
    Baud115200
}

impl Bauds{
    pub fn get_value(&self) -> u32{
        let v: u32;
        match self{
            Bauds::Baud9600 => { v = 9600 }
            Bauds::Baud57600 => { v = 57600 }
            Bauds::Baud115200 => { v = 115200 }
        }
        v
    }
}
/// Struct of DataGPS for latitude, longitude, altitude, time and uart
pub struct DataGPS{ //$GPGGA
    uart: Uart,
    time:String,
    latitude:String,
    longitude:String,
    altitude:String,
}
/// Implementation of functions for DataGPS
impl DataGPS{
    ///Create a new Uart connections and the object DataGPS
    pub fn new(baud: Bauds) -> Self{
        let mut _uart = Uart::new(baud.get_value(), Parity::None, 8, 1).expect("Error UART");
        _uart.set_read_mode(1, Duration::default()).expect("Error setting read mode");

        Self{
            uart: _uart,
            time: "".to_string(),
            latitude: "".to_string(),
            longitude: "".to_string(),
            altitude: "".to_string()
        }
    }
    ///Read the data from the GPS module and put in the values of the struct with a println
    pub fn read_all_data(&mut self) {
        let input = self.uart.read_line().expect("Error reading from Arduino");
        if input.contains("$GPGGA"){
            let x : Vec<&str> = input.split(",").collect();
            self.set_data(x[1].to_string(), x[2].to_string()+x[3], x[4].to_string()+x[5], x[9].to_string()+x[10]);
            println!("Latitude: {:?}, Longitude: {:?},Time: {:?},Altitude: {:?}", self.latitude, self.longitude, self.time, self.altitude);
        }
    }
    ///Set the important data
    fn set_data(&mut self, time:String, latitude: String, longitude: String, altitude: String){
        self.time = time;
        self.latitude = latitude;
        self.longitude = longitude;
        self.altitude = altitude;

    }
    ///Return altitude if it is present
    pub fn get_altitude(&mut self) -> String{
        if self.altitude.is_empty(){
           "No altitude".to_string()
        }else{
           self.altitude.to_string()
        }
    }

/// # Examples of get_altitude
///
/// ```
///
/// let x = DataGPS::new(Bauds::Baud9600);
/// x.read_all_data();
/// println!("Altitude: {:?}",x.get_altitude());
///
///
/// ```

    ///Return latitude if it is present
    pub fn get_latitude(&mut self) -> String{
        if self.latitude.is_empty(){
            "No latitude".to_string()
        }else{
            self.latitude.to_string()
        }
    }
    ///Return time if it is present
    pub fn get_time(&mut self) -> String{
        if self.time.is_empty(){
            "No value".to_string()
        }else{
            self.time.to_string()
        }
    }
    ///Return longitude if it is present
    pub fn get_longitude(&mut self) -> String{
        if self.longitude.is_empty(){
            "No value".to_string()
        }else{
            self.longitude.to_string()
        }
    }
    ///Return baud rate
    pub fn get_baud_rate(&self) -> u32{
        self.uart.baud_rate()
    }
    ///Set baud rate of UART connection
    pub fn set_baud_rate(&mut self,baud_rate:&Bauds) {
        self.uart.set_baud_rate(baud_rate.get_value()).expect("Error setting baud rate");
    }
}