Reverted back to pre async attempt

This commit is contained in:
Luke Else 2023-07-21 10:23:23 +01:00
parent 46ca82f379
commit 620ff4b207
6 changed files with 147 additions and 83 deletions

View File

@ -36,6 +36,7 @@ embedded-svc = { version = "0.25", optional = true, default-features = false }
nmea-parser = "0.10.0"
embedded-graphics = "0.8.0"
ssd1306 = "0.8.0"
display-interface = "0.4.1"
[build-dependencies]
embuild = "0.31.2"

7
src/display/mod.rs Normal file
View File

@ -0,0 +1,7 @@
/// Enum to make clear the stage of failure of the display
#[derive(Debug)]
pub enum DisplayError {
SetupError(display_interface::DisplayError),
DrawingError,
FlushError,
}

10
src/error.rs Normal file
View File

@ -0,0 +1,10 @@
use esp_idf_sys::EspError;
use crate::display::DisplayError;
use crate::gps::GpsError;
#[derive(Debug)]
pub enum Error {
EspError(EspError),
DisplayError(DisplayError),
GpsError(GpsError),
}

86
src/gps/gpsdata.rs Normal file
View File

@ -0,0 +1,86 @@
use super::GpsError;
use nmea_parser;
use nmea_parser::gnss::FaaMode;
/// Data structure to store all relevant data collected from the gps
#[derive(Default)]
pub struct GpsData {
/// Latitude of reported GPS location
latitude: Option<f64>,
/// Longitude of reported GPS location
longitude: Option<f64>,
/// Calculated speed from GPS reciever
speed: Option<f64>,
/// Altitude reported from GPS location
altitude: Option<f64>,
}
impl ToString for GpsData {
fn to_string(&self) -> String {
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(),
self.altitude.unwrap_or_default()
)
}
}
impl GpsData {
/// Function to go through and update GPS data from a nmea stream
pub fn update(&mut self, buf: &Vec<u8>) -> Result<(), GpsError> {
let mut nmea_parser = nmea_parser::NmeaParser::new();
// Format the nmea buffer into a usable string
let nmea_raw = String::from_utf8(buf.to_owned())
.map_err(|_| GpsError::ReadError())?;
let nmea_vec: Vec<&str> = nmea_raw.split('$').collect();
// Loop through each sentence and use the information to update GPS data
for nmea_line in nmea_vec {
// Don't try and process / parse if the string is empty
if nmea_line.is_empty() {
continue;
}
// Construct string that is in the correct format for parsing
let mut sentence = "$".to_string();
sentence.push_str(nmea_line);
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; }
};
// print decoded gps data to serial
match nmea {
nmea_parser::ParsedMessage::Gga(gga) => {
self.latitude = gga.latitude;
self.longitude = gga.longitude;
self.altitude = gga.altitude;
}
nmea_parser::ParsedMessage::Gll(gll) => {
if gll.faa_mode.unwrap_or(FaaMode::NotValid) == FaaMode::Autonomous {
self.latitude = gll.latitude;
self.longitude = gll.longitude;
}
},
nmea_parser::ParsedMessage::Rmc(rms) => {
self.latitude = rms.latitude;
self.longitude = rms.longitude;
self.speed = Some(knots_to_mph(rms.sog_knots.unwrap_or_default()));
},
_ => {}
}
}
Ok(())
}
}
/// Function to simply convert knots to mph
fn knots_to_mph(knots: f64) -> f64{
knots * 1.150779
}

10
src/gps/mod.rs Normal file
View File

@ -0,0 +1,10 @@
pub mod gpsdata;
#[derive(Debug)]
#[allow(unused)]
pub enum GpsError {
ParseError(nmea_parser::ParseError),
UpdateError(),
ReadError(),
NoData(),
}

View File

@ -1,4 +1,4 @@
use esp_idf_sys::{self as _, EspError}; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use esp_idf_sys::{self as _}; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use esp_idf_hal::{
prelude::Peripherals,
gpio::{AnyInputPin, AnyOutputPin},
@ -8,8 +8,6 @@ use esp_idf_hal::{
i2c::{I2cConfig, I2cDriver}
};
use esp_idf_hal;
use nmea_parser;
use nmea_parser::gnss::FaaMode;
use embedded_graphics::{
prelude::*,
@ -21,7 +19,16 @@ use embedded_graphics::{
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
fn main() -> Result<(), EspError> {
mod error;
use error::Error;
mod gps;
use gps::gpsdata;
mod display;
use display::DisplayError;
fn main() -> Result<(), Error> {
// It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_sys::link_patches();
@ -47,7 +54,7 @@ fn main() -> Result<(), EspError> {
None::<AnyInputPin>,
None::<AnyOutputPin>,
&config
)?;
).map_err(|err| Error::EspError(err))?;
// Setup I2C driver
let config = I2cConfig::new().baudrate(Hertz(921600));
@ -56,7 +63,7 @@ fn main() -> Result<(), EspError> {
sda,
scl,
&config
)?;
).map_err(|err| Error::EspError(err))?;
let interface = I2CDisplayInterface::new(i2c);
let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
@ -66,95 +73,38 @@ fn main() -> Result<(), EspError> {
let raw: ImageRaw<BinaryColor> = ImageRaw::new(include_bytes!("./rust.raw"), 64);
let im: Image<'_, _> = Image::new(&raw, Point::new(32, 0));
im.draw(&mut display).unwrap();
im.draw(&mut display).map_err(|err| Error::DisplayError(DisplayError::SetupError(err)))?;
let mut nmea_parser = nmea_parser::NmeaParser::new();
let mut latest_data = GpsData{latitude: None, longitude: None, speed: None, direction: None};
let mut latest_data = gpsdata::GpsData::default();
loop {
// Read buffer from UART
let mut buf: Vec<u8> = (0..uart.count()? as u8).collect();
uart.read(&mut buf[..], BLOCK)?;
std::thread::sleep(std::time::Duration::from_millis(10));
let mut buf: Vec<u8> = (
0..uart.count().map_err(|err| Error::EspError(err))? as u8
).collect();
uart.read(&mut buf[..], BLOCK).map_err(|err| Error::EspError(err))?;
let nmea_raw = String::from_utf8(buf).unwrap_or_default();
let nmea_vec: Vec<&str> = nmea_raw.split('$').collect();
for nmea_line in nmea_vec {
// Don't try and process / parse if the string is empty
if nmea_line.is_empty() {
continue;
}
// Construct string that is in the correct format for parsing
let mut sentence = "$".to_string();
sentence.push_str(nmea_line);
let nmea = match nmea_parser.parse_sentence(sentence.as_str()) {
Ok(nmea) => nmea,
// Don't continue processin sentence if we know that it isn't supported
Err(_) => { continue; }
};
// print decoded gps data to serial
match nmea {
nmea_parser::ParsedMessage::Gll(gll) => {
println!("Latitude: {:.6?}", gll.latitude.unwrap_or_default());
println!("Longitude: {:.6?}", gll.longitude.unwrap_or_default());
println!("FAA Mode: {:?}m", gll.faa_mode.unwrap_or(nmea_parser::gnss::FaaMode::NotValid));
println!("Data Valid: {:?}", gll.data_valid.unwrap_or(false));
if gll.faa_mode.unwrap_or(FaaMode::NotValid) == FaaMode::Autonomous {
latest_data.latitude = gll.latitude;
latest_data.longitude = gll.longitude;
}
println!("\n\n");
},
nmea_parser::ParsedMessage::Gns(gns) => {
println!("Altitude: {:.2?}", gns.altitude.unwrap_or_default());
println!("Num Sat: {:?}", gns.satellite_count.unwrap_or_default());
println!("Time: {:?}", gns.timestamp.unwrap_or_default());
},
nmea_parser::ParsedMessage::Rmc(rms) => {
println!("Speed: {:.2?}mph", rms.sog_knots.unwrap_or_default() * 1.150779);
println!("Current Track: {:.2?}deg", rms.bearing.unwrap_or_default());
latest_data.speed = Some(rms.sog_knots.unwrap_or_default() * 1.150779);
latest_data.direction = rms.bearing;
},
_ => {}
}
}
//Update GPS Data Struct with the latest data fetched from UART buffer
latest_data.update(&buf)
.map_err(|err| Error::GpsError(err))?;
// Clear display ready for next write
let style = MonoTextStyle::new(&FONT_7X13, BinaryColor::On);
display.clear(BinaryColor::Off)
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
display.clear(BinaryColor::Off).unwrap();
// Draw text to Display
Text::with_alignment(
&latest_data.to_string().as_str(),
Point::new(64, 10),
style,
Alignment::Center,
)
.draw(&mut display).unwrap();
.draw(&mut display)
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
display.flush().unwrap();
}
}
/// Data structure to store all relevant data collected from the gps
struct GpsData {
latitude: Option<f64>,
longitude: Option<f64>,
speed: Option<f64>,
direction: Option<f64>
}
impl GpsData {
pub fn to_string(&self) -> String {
format!("Latitude: {:.4?} \nLongitude: {:.4?} \nSpeed: {:.1?}mph \nDirection: {:.4?}deg",
self.latitude.unwrap_or_default(),
self.longitude.unwrap_or_default(),
self.speed.unwrap_or_default(),
self.direction.unwrap_or_default()
)
//Flush data to the display
display.flush()
.map_err(|_| Error::DisplayError(DisplayError::FlushError))?;
}
}