Removed some unecessary code, cleaned up the main UI to show only speed, and finally made a start on implementing some sort of app state for different screens / menus

This commit is contained in:
Luke Else 2023-07-25 21:15:59 +01:00
parent 5828d5ead6
commit 8c7181add2
3 changed files with 85 additions and 24 deletions

25
src/appstate.rs Normal file
View File

@ -0,0 +1,25 @@
/// Enum to store the state e.g. what should currently being displayed on the screen
pub enum AppState {
All,
Position,
Speed,
Altitude,
}
impl Default for AppState {
/// Function to get the default state of the application
fn default() -> Self {
AppState::All
}
}
impl AppState {
pub fn next(&mut self) {
match self {
AppState::All => *self = AppState::Position,
AppState::Position => *self = AppState::Speed,
AppState::Speed => *self = AppState::Altitude,
AppState::Altitude => *self = AppState::All,
}
}
}

View File

@ -1,6 +1,7 @@
use super::GpsError; use super::GpsError;
use nmea_parser; use nmea_parser;
use nmea_parser::chrono::{DateTime, Utc};
use nmea_parser::gnss::FaaMode; use nmea_parser::gnss::FaaMode;
/// Data structure to store all relevant data collected from the gps /// Data structure to store all relevant data collected from the gps
@ -14,6 +15,8 @@ pub struct GpsData {
speed: Option<f64>, speed: Option<f64>,
/// Altitude reported from GPS location /// Altitude reported from GPS location
altitude: Option<f64>, altitude: Option<f64>,
/// Timestamp of the last report
timestamp: Option<DateTime<Utc>>
} }
impl ToString for GpsData { impl ToString for GpsData {
@ -22,13 +25,16 @@ impl ToString for GpsData {
self.latitude.unwrap_or_default(), self.latitude.unwrap_or_default(),
self.longitude.unwrap_or_default(), self.longitude.unwrap_or_default(),
self.speed.unwrap_or_default(), self.speed.unwrap_or_default(),
self.altitude.unwrap_or_default() self.altitude.unwrap_or_default(),
) )
} }
} }
impl GpsData { impl GpsData {
/// Function to go through and update GPS data from a nmea stream /// Function to go through and update GPS data from a nmea stream
///
/// Takes in a reference to a buffer of raw serial data and then
/// tries to read it in as NMEA Data
pub fn update(&mut self, buf: &Vec<u8>) -> Result<(), GpsError> { pub fn update(&mut self, buf: &Vec<u8>) -> Result<(), GpsError> {
let mut nmea_parser = nmea_parser::NmeaParser::new(); let mut nmea_parser = nmea_parser::NmeaParser::new();
@ -56,19 +62,39 @@ impl GpsData {
// print decoded gps data to serial // print decoded gps data to serial
match nmea { match nmea {
// TODO: Investigate why the GGA Packets seem to come in mangled
nmea_parser::ParsedMessage::Gga(gga) => { nmea_parser::ParsedMessage::Gga(gga) => {
self.altitude = gga.altitude; // Update Altitude above ground level
self.altitude = match gga.altitude {
Some(_) => gga.altitude,
None => self.altitude
};
} }
nmea_parser::ParsedMessage::Gll(gll) => { nmea_parser::ParsedMessage::Gll(gll) => {
// FAA mode is used to determine the validity of the data in this case
if gll.faa_mode.unwrap_or(FaaMode::NotValid) == FaaMode::Autonomous { if gll.faa_mode.unwrap_or(FaaMode::NotValid) == FaaMode::Autonomous {
self.latitude = gll.latitude; // Only make updates to position if we have new data, do not overwrite
self.longitude = gll.longitude; // stale data
match gll.latitude {
Some(_) => self.latitude = gll.latitude,
None => {}
}
match gll.longitude {
Some(_) => self.longitude = gll.longitude,
None => {}
}
match gll.timestamp {
Some(_) => self.timestamp = gll.timestamp,
None => {}
}
} }
}, },
nmea_parser::ParsedMessage::Rmc(rms) => { nmea_parser::ParsedMessage::Rmc(rms) => {
self.latitude = rms.latitude; // Update Ground Speed
self.longitude = rms.longitude; match rms.sog_knots {
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 => {}
}
}, },
_ => {} _ => {}
} }
@ -76,9 +102,17 @@ impl GpsData {
Ok(()) Ok(())
} }
/// 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")
}
}
} }
/// Function to simply convert knots to mph /// Function to simply convert knots to mph
fn knots_to_mph(knots: f64) -> f64{ fn knots_to_mph(knots: f64) -> f64{
knots * 1.150779 knots * 1.150779
} }

View File

@ -1,5 +1,7 @@
use esp_idf_sys::{self as _}; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use esp_idf_hal::{ use esp_idf_hal::{
self,
interrupt,
prelude::Peripherals, prelude::Peripherals,
gpio::{AnyInputPin, AnyOutputPin}, gpio::{AnyInputPin, AnyOutputPin},
delay::BLOCK, delay::BLOCK,
@ -7,18 +9,18 @@ use esp_idf_hal::{
uart, uart,
i2c::{I2cConfig, I2cDriver} i2c::{I2cConfig, I2cDriver}
}; };
use esp_idf_hal;
use embedded_graphics::{ use embedded_graphics::{
prelude::*, prelude::*,
image::{Image, ImageRaw}, mono_font::{ascii::FONT_7X13, ascii::FONT_10X20, MonoTextStyle},
mono_font::{ascii::FONT_7X13, MonoTextStyle},
pixelcolor::BinaryColor, pixelcolor::BinaryColor,
text::{Text, Alignment}, text::{Text, Alignment},
}; };
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306}; use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
mod appstate;
mod error; mod error;
use error::Error; use error::Error;
@ -28,12 +30,14 @@ use gps::gpsdata;
mod display; mod display;
use display::DisplayError; use display::DisplayError;
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
// It is necessary to call this function once. Otherwise some patches to the runtime // 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 // 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(); esp_idf_sys::link_patches();
// Bind the log crate to the ESP Logging facilities // Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default(); //esp_idf_svc::log::EspLogger::initialize_default();
// Get Peripherals and sysloop ready // Get Peripherals and sysloop ready
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take().unwrap();
@ -55,7 +59,7 @@ fn main() -> Result<(), Error> {
None::<AnyOutputPin>, None::<AnyOutputPin>,
&config &config
).map_err(|err| Error::EspError(err))?; ).map_err(|err| Error::EspError(err))?;
// Setup I2C driver // Setup I2C driver
let config = I2cConfig::new().baudrate(Hertz(921600)); let config = I2cConfig::new().baudrate(Hertz(921600));
let i2c = I2cDriver::new( let i2c = I2cDriver::new(
@ -70,13 +74,9 @@ fn main() -> Result<(), Error> {
.into_buffered_graphics_mode(); .into_buffered_graphics_mode();
display.init().map_err(|err| Error::DisplayError(DisplayError::SetupError(err)))?; display.init().map_err(|err| Error::DisplayError(DisplayError::SetupError(err)))?;
let raw: ImageRaw<BinaryColor> = ImageRaw::new(include_bytes!("./rust.raw"), 64); let mut app_state = appstate::AppState::default();
let im: Image<'_, _> = Image::new(&raw, Point::new(32, 0));
im.draw(&mut display).map_err(|err| Error::DisplayError(DisplayError::SetupError(err)))?;
let mut latest_data = gpsdata::GpsData::default(); let mut latest_data = gpsdata::GpsData::default();
loop { loop {
// Read buffer from UART // Read buffer from UART
let mut buf: Vec<u8> = ( let mut buf: Vec<u8> = (
@ -89,14 +89,15 @@ fn main() -> Result<(), Error> {
.map_err(|err| Error::GpsError(err))?; .map_err(|err| Error::GpsError(err))?;
// Clear display ready for next write // Clear display ready for next write
let style = MonoTextStyle::new(&FONT_7X13, BinaryColor::On); let style = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
display.clear(BinaryColor::Off) display.clear(BinaryColor::Off)
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?; .map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
// Draw text to Display // Draw text to Display
Text::with_alignment( Text::with_alignment(
&latest_data.to_string().as_str(), &latest_data.get_speed().as_str(),
Point::new(64, 10), //&latest_data.to_string().as_str(),
Point::new(64, 20),
style, style,
Alignment::Center, Alignment::Center,
) )
@ -107,4 +108,5 @@ fn main() -> Result<(), Error> {
display.flush() display.flush()
.map_err(|_| Error::DisplayError(DisplayError::FlushError))?; .map_err(|_| Error::DisplayError(DisplayError::FlushError))?;
} }
} }