gps_data_reader_bjarki18 0.1.0

A crate which uses rpi_embedded to read data from a gps module.
Documentation
//! 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(gps.baud, Parity::None, 8, 1).unwrap();
//! 
//! 	let data_stream = uart.read_line().unwrap();
//! 	gps.read_data();
//! }
//! ```
//!
//!
//!




/// 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;}
		}
	}
}