generated from luke-else/esp32-std-template
Moved everything out into threads
This commit is contained in:
parent
33396317b2
commit
56e7021395
@ -9,7 +9,7 @@ use esp_idf_hal::i2c::I2cDriver;
|
|||||||
use ssd1306::{prelude::*, Ssd1306};
|
use ssd1306::{prelude::*, Ssd1306};
|
||||||
use crate::error::Error;
|
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
|
where T: ToString
|
||||||
{
|
{
|
||||||
let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
|
let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
|
||||||
|
@ -17,8 +17,8 @@ pub struct GpsData {
|
|||||||
altitude: Option<f64>,
|
altitude: Option<f64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GpsData {
|
impl ToString for GpsData {
|
||||||
pub fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
format!("Latitude: {:.4?} \nLongitude: {:.4?} \nSpeed: {:.1?}mph \nAltitude: {:.1?}m",
|
format!("Latitude: {:.4?} \nLongitude: {:.4?} \nSpeed: {:.1?}mph \nAltitude: {:.1?}m",
|
||||||
self.latitude.unwrap_or_default(),
|
self.latitude.unwrap_or_default(),
|
||||||
self.longitude.unwrap_or_default(),
|
self.longitude.unwrap_or_default(),
|
||||||
@ -26,7 +26,9 @@ impl GpsData {
|
|||||||
self.altitude.unwrap_or_default()
|
self.altitude.unwrap_or_default()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GpsData {
|
||||||
/// Function to asynchronously go through and update GPS data from a nmea stream
|
/// Function to asynchronously go through and update GPS data from a nmea stream
|
||||||
pub async fn update(&mut self, buf: &Vec<u8>) -> Result<(), GpsError> {
|
pub async 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();
|
||||||
|
36
src/main.rs
36
src/main.rs
@ -10,7 +10,10 @@ use esp_idf_hal::{
|
|||||||
use esp_idf_hal;
|
use esp_idf_hal;
|
||||||
use ssd1306::I2CDisplayInterface;
|
use ssd1306::I2CDisplayInterface;
|
||||||
|
|
||||||
|
use std::thread;
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
|
use error::Error;
|
||||||
mod gps;
|
mod gps;
|
||||||
use gps::gpsdata::GpsData;
|
use gps::gpsdata::GpsData;
|
||||||
|
|
||||||
@ -57,16 +60,33 @@ fn main() -> Result<(), EspError> {
|
|||||||
)?;
|
)?;
|
||||||
let interface = I2CDisplayInterface::new(i2c);
|
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 {
|
loop {
|
||||||
// Read buffer from UART
|
read_from_buf(&uart, &mut buf);
|
||||||
let mut buf: Vec<u8> = (0..uart.count()? as u8).collect();
|
latest_data.lock().unwrap().update(&buf);
|
||||||
uart.read(&mut buf[..], BLOCK)?;
|
|
||||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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(())
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user