mech1_gpsreader/
datagps.rs

1use std::{time::Duration};
2use rpi_embedded::uart::{Parity, Uart};
3
4///Defines and implements some Baud rates, which are the most common while communicating using uart.
5pub enum Bauds {
6    Baud9600,
7    Baud57600,
8    Baud115200
9}
10
11impl Bauds{
12    pub fn get_value(&self) -> u32 {
13        let v: u32;
14
15        match self {
16            Bauds::Baud9600 => { v = 9600 }
17            Bauds::Baud57600 => { v = 57600 }
18            Bauds::Baud115200 => { v = 115200 }
19        }
20
21        v
22    }
23}
24
25pub struct DataGPS { //$GPGGA
26    uart: Uart,
27    time:String,
28    latitude:String,
29    longitude:String,
30    altitude:String,
31}
32
33impl DataGPS {
34    /// Initializes a new uart communication using the specified settings (baud rate) and the default ones (Parity, data_bits, stop_bits).
35    /// After opening the connection, the Struct holding the receivable values is also initialized.
36    ///
37    /// # Examples
38    ///
39    /// ```
40    /// let mut connection = DataGPS::new(Bauds::Baud9600);
41    /// let reading = connection.read_all_data();
42    ///
43    /// assert_ne!("", reading);
44    /// ```
45    pub fn new(baud: Bauds) -> Self {
46        let mut _uart = Uart::new(baud.get_value(), Parity::None, 8, 1).expect("Error UART");
47        _uart.set_read_mode(1, Duration::default()).expect("Error setting read mode");
48        Self {
49            uart: _uart,
50            time: "".to_string(),
51            latitude: "".to_string(),
52            longitude: "".to_string(),
53            altitude: "".to_string()
54        }
55    }
56
57    /// Reads all the data at once from the uart, splits it in single information and puts it in the corresponding Struct fields.
58    ///
59    /// # Examples
60    ///
61    /// ```
62    /// let mut connection = DataGPS::new(Bauds::Baud9600);
63    /// let reading = connection.read_all_data();
64    ///
65    /// assert_ne!("", reading);
66    /// ```
67    pub fn read_all_data(&mut self) {
68        let input = self.uart.read_line().expect("Error reading from Arduino");
69        if input.contains("$GPGGA") {
70            let x : Vec<&str> = input.split(",").collect();
71            self.set_data(x[0].to_string(), x[2].to_string()+x[3], x[4].to_string()+x[5], x[9].to_string()+x[10])
72        }
73
74    }
75
76    /// Puts received data in the Struct
77    fn set_data(&mut self, time:String, latitude: String, longitude: String, altitude: String) {
78        self.time = time;
79        self.latitude = latitude;
80        self.longitude = longitude;
81        self.altitude = altitude;
82
83    }
84
85    ///Returns altitude
86    pub fn get_altitude(&mut self) -> String {
87        if self.altitude.is_empty() {
88           "No value".to_string()
89        }
90        else {
91           self.altitude.to_string()
92        }
93    }
94
95    ///Returns latitude
96    pub fn get_latitude(&mut self) -> String {
97            if self.latitude.is_empty() {
98                "No value".to_string()
99            }
100            else {
101                self.latitude.to_string()
102            }
103    }
104    
105    ///Returns time
106    pub fn get_time(&mut self) -> String {
107            if self.time.is_empty() {
108                "No value".to_string()
109            }
110            else {
111                self.time.to_string()
112            }
113    }
114
115    ///Returns longitude
116    pub fn get_longitude(&mut self) -> String {
117            if self.longitude.is_empty() {
118                "No value".to_string()
119            }
120            else {
121                self.longitude.to_string()
122            }
123    }
124
125    ///Returns baud rate
126    pub fn get_baud_rate(&self) -> u32 {
127        self.uart.baud_rate()
128    }
129
130    ///Returns uart status
131    pub fn get_uart_status(&self) -> rpi_embedded::uart::Status {
132        self.uart.status().expect("Error getting status")
133    }
134
135    ///Sets the new baud rate if modified during execution
136    pub fn set_baud_rate(&mut self,baud_rate:&Bauds) {
137        self.uart.set_baud_rate(baud_rate.get_value()).expect("Error setting baud rate");
138    }
139}