From 56e70213950eccd27c3fdb7e404c869d70918473 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Wed, 19 Jul 2023 21:45:26 +0100 Subject: [PATCH] Moved everything out into threads --- src/display/mod.rs | 2 +- src/gps/gpsdata.rs | 6 ++++-- src/main.rs | 38 +++++++++++++++++++++++++++++--------- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/display/mod.rs b/src/display/mod.rs index 22004c1..03412ae 100644 --- a/src/display/mod.rs +++ b/src/display/mod.rs @@ -9,7 +9,7 @@ use esp_idf_hal::i2c::I2cDriver; use ssd1306::{prelude::*, Ssd1306}; use crate::error::Error; -pub fn update_display(interface: I2CInterface>, data: Arc>) -> Result<(), Error> +pub async fn update_display(interface: I2CInterface>, data: Arc>) -> Result<(), Error> where T: ToString { let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0) diff --git a/src/gps/gpsdata.rs b/src/gps/gpsdata.rs index a1370a4..ba9a550 100644 --- a/src/gps/gpsdata.rs +++ b/src/gps/gpsdata.rs @@ -17,8 +17,8 @@ pub struct GpsData { altitude: Option, } -impl GpsData { - pub fn to_string(&self) -> String { +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(), @@ -26,7 +26,9 @@ impl GpsData { self.altitude.unwrap_or_default() ) } +} +impl GpsData { /// Function to asynchronously go through and update GPS data from a nmea stream pub async fn update(&mut self, buf: &Vec) -> Result<(), GpsError> { let mut nmea_parser = nmea_parser::NmeaParser::new(); diff --git a/src/main.rs b/src/main.rs index 751027b..86f51ab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,10 @@ use esp_idf_hal::{ use esp_idf_hal; use ssd1306::I2CDisplayInterface; +use std::thread; + mod error; +use error::Error; mod gps; use gps::gpsdata::GpsData; @@ -57,16 +60,33 @@ fn main() -> Result<(), EspError> { )?; let interface = I2CDisplayInterface::new(i2c); - let mut latest_data = GpsData::new(); + let latest_data = GpsData::new(); - loop { - // Read buffer from UART - let mut buf: Vec = (0..uart.count()? as u8).collect(); - uart.read(&mut buf[..], BLOCK)?; - std::thread::sleep(std::time::Duration::from_millis(10)); + + // Spawn a thread to read data from the start + thread::spawn(|| { + let mut buf: Vec = Vec::new(); + loop { + read_from_buf(&uart, &mut buf); + latest_data.lock().unwrap().update(&buf); + } + }); - - } + // Spawn a thread to write the data to the screen + thread::spawn(|| { + let mut buf: Vec = Vec::new(); + loop { + display::update_display(interface, latest_data); + } + }); + + Ok(()) + } - +/// Reads all of the data in a buffer given a UART driver and a vector to write buffer into +pub async fn read_from_buf<'a>(uart_drv: &uart::UartRxDriver<'a>, buf: &mut Vec) -> Result<(), Error> { + *buf = (0..uart_drv.count().map_err(|err|Error::EspError(err))? as u8).collect(); + uart_drv.read(&mut buf[..], BLOCK).map_err(|err| Error::EspError(err))?; + Ok(()) +}