Started moving some of the code out to separate files. From here I can then make everything asynchronous and more performant

This commit is contained in:
Luke Else 2023-07-18 21:25:34 +01:00
parent 46ca82f379
commit 33396317b2
5 changed files with 164 additions and 98 deletions

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

@ -0,0 +1,48 @@
use std::sync::{Arc, Mutex};
use embedded_graphics::{
prelude::*,
mono_font::{ascii::FONT_7X13, MonoTextStyle},
pixelcolor::BinaryColor,
text::{Text, Alignment},
};
use esp_idf_hal::i2c::I2cDriver;
use ssd1306::{prelude::*, Ssd1306};
use crate::error::Error;
pub fn update_display<T>(interface: I2CInterface<I2cDriver<'_>>, data: Arc<Mutex<T>>) -> Result<(), Error>
where T: ToString
{
let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
.into_buffered_graphics_mode();
display.init()
.map_err(|_| Error::DisplayError(DisplayError::SetupError))?;
// 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();
let style = MonoTextStyle::new(&FONT_7X13, BinaryColor::On);
display.clear(BinaryColor::Off)
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
Text::with_alignment(
&data.lock().unwrap().to_string().as_str(),
Point::new(64, 10),
style,
Alignment::Center,
)
.draw(&mut display)
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
display.flush().map_err(|_| Error::DisplayError(DisplayError::FlushError))?;
Ok(())
}
/// Enum to make clear the stage of failure of the display
pub enum DisplayError {
SetupError,
DrawingError,
FlushError,
}

9
src/error.rs Normal file
View File

@ -0,0 +1,9 @@
use esp_idf_sys::EspError;
use crate::display::DisplayError;
use crate::gps::GpsError;
pub enum Error {
EspError(EspError),
DisplayError(DisplayError),
GpsError(GpsError),
}

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

@ -0,0 +1,89 @@
use std::sync::{Mutex, Arc};
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 GpsData {
pub 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()
)
}
/// Function to asynchronously go through and update GPS data from a nmea stream
pub async 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(())
}
pub fn new() -> Arc<Mutex<GpsData>> {
return Arc::new(Mutex::new(GpsData::default()))
}
}
/// Function to simply convert knots to mph
fn knots_to_mph(knots: f64) -> f64{
knots * 1.150779
}

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

@ -0,0 +1,8 @@
pub mod gpsdata;
pub enum GpsError {
ParseError(nmea_parser::ParseError),
UpdateError(),
ReadError(),
NoData(),
}

View File

@ -8,18 +8,16 @@ use esp_idf_hal::{
i2c::{I2cConfig, I2cDriver}
};
use esp_idf_hal;
use nmea_parser;
use nmea_parser::gnss::FaaMode;
use ssd1306::I2CDisplayInterface;
mod error;
mod gps;
use gps::gpsdata::GpsData;
mod display;
use embedded_graphics::{
prelude::*,
image::{Image, ImageRaw},
mono_font::{ascii::FONT_7X13, MonoTextStyle},
pixelcolor::BinaryColor,
text::{Text, Alignment},
};
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
fn main() -> Result<(), EspError> {
// It is necessary to call this function once. Otherwise some patches to the runtime
@ -57,104 +55,18 @@ fn main() -> Result<(), EspError> {
scl,
&config
)?;
let interface = I2CDisplayInterface::new(i2c);
let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
.into_buffered_graphics_mode();
display.init().unwrap();
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();
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::new();
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 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;
},
_ => {}
}
}
let style = MonoTextStyle::new(&FONT_7X13, BinaryColor::On);
display.clear(BinaryColor::Off).unwrap();
Text::with_alignment(
&latest_data.to_string().as_str(),
Point::new(64, 10),
style,
Alignment::Center,
)
.draw(&mut display).unwrap();
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()
)
}
}