Chore: Started moving individual components out into modules. Started with GPS. Display and i2c driver to follow.

This commit is contained in:
Luke Else 2023-11-06 23:20:12 +00:00
parent a880586321
commit 6625351e38
3 changed files with 82 additions and 51 deletions

View File

@ -1,3 +1,11 @@
use embedded_graphics::{
prelude::*,
mono_font::{ascii::FONT_7X13, ascii::FONT_10X20, MonoTextStyle},
pixelcolor::BinaryColor,
text::{Text, Alignment},
};
/// Enum to make clear the stage of failure of the display
#[derive(Debug)]
pub enum DisplayError {

View File

@ -1,3 +1,14 @@
use esp_idf_hal::{
self,
peripheral::Peripheral,
gpio::{AnyInputPin, AnyOutputPin, InputPin},
units::Hertz,
delay::BLOCK,
uart::{self, Uart},
};
use crate::error::Error;
pub mod gpsdata;
#[derive(Debug)]
@ -8,3 +19,44 @@ pub enum GpsError {
ReadError(),
NoData(),
}
pub struct GPS<'a> {
uart: uart::UartRxDriver<'a>,
latest: gpsdata::GpsData
}
impl<'a> GPS<'a> {
/// Returns a new GPS struct with interfaces to allow for fetching
/// uart: Uart connection to GPS module.
pub fn new(
uart: impl Peripheral<P = impl Uart> + 'a,
gps_rx: impl Peripheral<P = impl InputPin> + 'a,
) -> Result<GPS<'a>, Error> {
// Setup UART to read serial data from GPS
let config = uart::config::Config::default().baudrate(Hertz(9_600));
let uart: uart::UartRxDriver = uart::UartRxDriver::new(
uart,
gps_rx,
None::<AnyInputPin>,
None::<AnyOutputPin>,
&config
).map_err(|err| Error::EspError(err))?;
Ok(GPS { uart, latest: gpsdata::GpsData::default() })
}
pub fn poll(self: &mut GPS<'a>) -> Result<&gpsdata::GpsData, Error> {
//Read buffer from UART
let mut buf: Vec<u8> = (
0..self.uart.count().map_err(|err| Error::EspError(err))? as u8
).collect();
self.uart.read(&mut buf[..], BLOCK).map_err(|err| Error::EspError(err))?;
//Update GPS Data Struct with the latest data fetched from UART buffer
self.latest.update(&buf)
.map_err(|err| Error::GpsError(err))?;
// Return the latest stored data
Ok(&self.latest)
}
}

View File

@ -1,22 +1,13 @@
#![allow(unused)]
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use esp_idf_hal::{
self,
interrupt,
prelude::Peripherals,
gpio::{AnyInputPin, AnyOutputPin},
delay::BLOCK,
units::Hertz,
uart,
i2c::{I2cConfig, I2cDriver}
};
use embedded_graphics::{
prelude::*,
mono_font::{ascii::FONT_7X13, ascii::FONT_10X20, MonoTextStyle},
pixelcolor::BinaryColor,
text::{Text, Alignment},
};
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
mod appstate;
@ -49,17 +40,6 @@ fn main() -> Result<(), Error> {
let gps_rx = pins.gpio16;
let _lcd_address: u8 = 0x3C;
// Setup UART to read serial data from GPS
let config = uart::config::Config::default().baudrate(Hertz(9_600));
let uart: uart::UartRxDriver = uart::UartRxDriver::new(
peripherals.uart1,
gps_rx,
None::<AnyInputPin>,
None::<AnyOutputPin>,
&config
).map_err(|err| Error::EspError(err))?;
// Setup I2C driver
let config = I2cConfig::new().baudrate(Hertz(921600));
let i2c = I2cDriver::new(
@ -77,36 +57,27 @@ fn main() -> Result<(), Error> {
let mut app_state = appstate::AppState::default();
let mut latest_data = gpsdata::GpsData::default();
loop {
// Read buffer from UART
let mut buf: Vec<u8> = (
0..uart.count().map_err(|err| Error::EspError(err))? as u8
).collect();
uart.read(&mut buf[..], BLOCK).map_err(|err| Error::EspError(err))?;
Err(error::Error::DisplayError(DisplayError::DrawingError))
//Update GPS Data Struct with the latest data fetched from UART buffer
latest_data.update(&buf)
.map_err(|err| Error::GpsError(err))?;
// loop {
// Clear display ready for next write
let style = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
display.clear(BinaryColor::Off)
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
// // Clear display ready for next write
// let style = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
// display.clear(BinaryColor::Off)
// .map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
// Draw text to Display
Text::with_alignment(
&latest_data.get_speed().as_str(),
//&latest_data.to_string().as_str(),
Point::new(64, 20),
style,
Alignment::Center,
)
.draw(&mut display)
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
// // Draw text to Display
// Text::with_alignment(
// &latest_data.get_speed().as_str(),
// Point::new(64, 20),
// style,
// Alignment::Center,
// )
// .draw(&mut display)
// .map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
//Flush data to the display
display.flush()
.map_err(|_| Error::DisplayError(DisplayError::FlushError))?;
// //Flush data to the display
// display.flush()
// .map_err(|_| Error::DisplayError(DisplayError::FlushError))?;
// }
}
}