Moved everything out into threads

This commit is contained in:
Luke Else 2023-07-19 21:45:26 +01:00
parent 33396317b2
commit 56e7021395
3 changed files with 34 additions and 12 deletions

View File

@ -9,7 +9,7 @@ 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>
pub async 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)

View File

@ -17,8 +17,8 @@ pub struct GpsData {
altitude: Option<f64>,
}
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<u8>) -> Result<(), GpsError> {
let mut nmea_parser = nmea_parser::NmeaParser::new();

View File

@ -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();
// Spawn a thread to read data from the start
thread::spawn(|| {
let mut buf: Vec<u8> = Vec::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));
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<u8> = 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<u8>) -> 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(())
}