generated from luke-else/esp32-std-template
Chore: code cleaning
This commit is contained in:
parent
6b1a461e11
commit
a2a9abc4b3
@ -16,12 +16,13 @@ pub struct GpsData {
|
||||
/// Altitude reported from GPS location
|
||||
altitude: Option<f64>,
|
||||
/// Timestamp of the last report
|
||||
timestamp: Option<DateTime<Utc>>
|
||||
timestamp: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
impl ToString for GpsData {
|
||||
fn to_string(&self) -> String {
|
||||
format!("Latitude: {:.4?} \nLongitude: {:.4?} \nSpeed: {:.1?}mph \nAltitude: {:.1?}m",
|
||||
format!(
|
||||
"Latitude: {:.4?} \nLongitude: {:.4?} \nSpeed: {:.1?}mph \nAltitude: {:.1?}m",
|
||||
self.latitude.unwrap_or_default(),
|
||||
self.longitude.unwrap_or_default(),
|
||||
self.speed.unwrap_or_default(),
|
||||
@ -40,7 +41,8 @@ impl GpsData {
|
||||
|
||||
// Format the nmea buffer into a usable string
|
||||
let nmea_raw = String::from_utf8(buf.to_owned())
|
||||
.map_err(|_| GpsError::ReadError()).unwrap_or_default();
|
||||
.map_err(|_| GpsError::ReadError())
|
||||
.unwrap_or_default();
|
||||
let nmea_vec: Vec<&str> = nmea_raw.split('$').collect();
|
||||
|
||||
// Loop through each sentence and use the information to update GPS data
|
||||
@ -57,7 +59,9 @@ impl GpsData {
|
||||
let nmea = match nmea_parser.parse_sentence(sentence.as_str()) {
|
||||
Ok(nmea) => nmea,
|
||||
// Don't continue processing a sentence if we know that it isn't supported
|
||||
Err(_) => { continue; }
|
||||
Err(_) => {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// print decoded gps data to serial
|
||||
@ -67,7 +71,7 @@ impl GpsData {
|
||||
// Update Altitude above ground level
|
||||
self.altitude = match gga.altitude {
|
||||
Some(_) => gga.altitude,
|
||||
None => self.altitude
|
||||
None => self.altitude,
|
||||
};
|
||||
}
|
||||
nmea_parser::ParsedMessage::Gll(gll) => {
|
||||
@ -88,14 +92,16 @@ impl GpsData {
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
nmea_parser::ParsedMessage::Rmc(rms) => {
|
||||
// Update Ground Speed
|
||||
match rms.sog_knots {
|
||||
Some(_) => self.speed = Some(knots_to_mph(rms.sog_knots.unwrap_or_default())),
|
||||
Some(_) => {
|
||||
self.speed = Some(knots_to_mph(rms.sog_knots.unwrap_or_default()))
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@ -104,11 +110,8 @@ impl GpsData {
|
||||
}
|
||||
|
||||
/// Function to get a &str of the current speed reported by the GPS module
|
||||
pub fn get_speed(&self) -> String {
|
||||
match self.speed {
|
||||
Some(speed) => format!("{:.2?}mph\n", speed),
|
||||
None => format!("AWAITING\nGPS\nDATA")
|
||||
}
|
||||
pub fn get_speed(&self) -> Option<f64> {
|
||||
self.speed
|
||||
}
|
||||
}
|
||||
|
||||
|
18
src/main.rs
18
src/main.rs
@ -14,7 +14,7 @@ use error::Error;
|
||||
mod appstate;
|
||||
|
||||
mod gps;
|
||||
use gps::GPS;
|
||||
use gps::{gpsdata::GpsData, GPS};
|
||||
|
||||
mod display;
|
||||
use display::Display;
|
||||
@ -47,15 +47,21 @@ fn main() -> Result<(), Error> {
|
||||
let mut display: Display<DisplaySize128x64> =
|
||||
Display::new(i2c, DisplaySize128x64, DisplayRotation::Rotate0)?;
|
||||
let mut _app_state = appstate::AppState::default();
|
||||
const NO_DATA: &str = "NO\nGPS\nDATA";
|
||||
|
||||
loop {
|
||||
let text: String;
|
||||
// Get the latest data from GPS module and flush to Display
|
||||
match gps.poll() {
|
||||
Ok(res) => display.draw(&res.get_speed().as_str())?,
|
||||
text = match gps.poll() {
|
||||
Ok(res) => match res.get_speed() {
|
||||
Some(s) => format!("{:.2} MPH", s),
|
||||
None => NO_DATA.to_string(),
|
||||
},
|
||||
Err(e) => {
|
||||
display.draw("NO\nGPS\nDATA")?;
|
||||
eprintln!("{:?}", e);
|
||||
}
|
||||
}
|
||||
NO_DATA.to_string()
|
||||
}
|
||||
};
|
||||
display.draw(text.as_str())?
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user