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
//! Interface to the data read from a gps module.
//!
//! NOTE, the only data accepted in this crate is from the "$GPGGA" data stream.
//! BE SURE the data you want is included in that data stream.
//! 
//! ## Output
//!
//! The data is output and printed in the terminal screen by using a vector to split the
//! data stream at every `,` instance. Every gps data is stored in a String format so it can 
//! be easily accessed if you so choose.
//!
//! ## Examples
//!
//! Basic example:
//!
//! ```
//! use gps_data_reader_bjarki18::GPS;
//! use rpi_embedded::uart::{Uart, Parity};
//!
//! fn main(){
//! 	let mut gps = GPS::default();
//! 	gps.set_baud(9600);
//!
//! 	let mut uart = Uart::new(gps.baud, Parity::None, 8, 1).unwrap();
//! 
//! 	let data_stream = uart.read_line().unwrap();
//! 	gps.read_data(data_stream);
//! }
//! ```
//!
//!
//!




/// Provides access to the data read from the gps module.
pub struct GPS {
	pub baud: u32,
	pub latitude: String, //latitude
	pub longitude: String, //longitude
	pub north_south: String, //direction, either north or south
	pub east_west: String, //direction, either east or west
	pub satellites: String, //num of satellites in use
	pub altitude: String //altitude in meters
}

impl GPS {
	/// Constructs a default empty GPS struct.
	pub fn default() -> Self{
		let mut _out = Self{
			baud: 0,
			latitude: "".to_string(),
			longitude: "".to_string(),
			north_south: "".to_string(),
			east_west: "".to_string(),
			satellites: "".to_string(),
			altitude: "".to_string()
		};
		_out
	}
	
	/// Formats data from the struct and prints it.
	pub fn print_data(&mut self) {
		let print_str = format!(
			"\n
			Latitude: {}\n
			Longitude: {}\n
			Altitude: {}\n
			Orientation: {}{}\n
			Num of active satellites: {}\n
			",self.latitude,self.longitude,self.altitude,self.north_south,self.east_west,self.satellites);
			println!("{}",print_str);
	}
	
	/// Sets the struct data from Vec<&str> format
	pub fn set_data(&mut self, vektor: Vec<&str>){
		self.latitude = vektor[2].to_string();
		self.longitude = vektor[4].to_string();
		self.north_south = vektor[3].to_string();
		self.east_west = vektor[5].to_string();
		self.satellites = vektor[7].to_string();
		self.altitude = vektor[9].to_string() + &vektor[10].to_string();
	} 
	
	/// Reads in data from an input String type - only reads gps data with the identifier "$GPGGA".
	pub fn read_data(&mut self, res: String){		
		let my_vec = res.split(',');
		let vec: Vec<&str> = my_vec.collect();
		if vec[0] == "$GPGGA" {
			self.set_data(vec);
			self.print_data();
		}
	}
	
	/// checks the baud rate with an Option<u32> enum.
	pub fn check_baud(&mut self, value: u32) -> Option<u32> {
		if value == 9600{
			Some(value)
		}else{
			None
		}
	}	
	
	/// sets the baud rate of the system, only accepts the default 9600 of the gps module.
	pub fn set_baud(&mut self, value: u32){
		match self.check_baud(value){
			None => {panic!("Incorrect baudrate input");}
			Some(val) => {self.baud = val;}
		}
	}
}