generated from luke-else/esp32-std-template
Chore: Started moving individual components out into modules. Started with GPS. Display and i2c driver to follow.
This commit is contained in:
parent
a880586321
commit
6625351e38
@ -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
|
/// Enum to make clear the stage of failure of the display
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum DisplayError {
|
pub enum DisplayError {
|
||||||
|
@ -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;
|
pub mod gpsdata;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -8,3 +19,44 @@ pub enum GpsError {
|
|||||||
ReadError(),
|
ReadError(),
|
||||||
NoData(),
|
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)
|
||||||
|
}
|
||||||
|
}
|
71
src/main.rs
71
src/main.rs
@ -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_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,
|
self,
|
||||||
interrupt,
|
|
||||||
prelude::Peripherals,
|
prelude::Peripherals,
|
||||||
gpio::{AnyInputPin, AnyOutputPin},
|
|
||||||
delay::BLOCK,
|
|
||||||
units::Hertz,
|
units::Hertz,
|
||||||
uart,
|
|
||||||
i2c::{I2cConfig, I2cDriver}
|
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};
|
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
|
||||||
|
|
||||||
mod appstate;
|
mod appstate;
|
||||||
@ -49,17 +40,6 @@ fn main() -> Result<(), Error> {
|
|||||||
let gps_rx = pins.gpio16;
|
let gps_rx = pins.gpio16;
|
||||||
let _lcd_address: u8 = 0x3C;
|
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
|
// 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(
|
||||||
@ -77,36 +57,27 @@ fn main() -> Result<(), Error> {
|
|||||||
let mut app_state = appstate::AppState::default();
|
let mut app_state = appstate::AppState::default();
|
||||||
let mut latest_data = gpsdata::GpsData::default();
|
let mut latest_data = gpsdata::GpsData::default();
|
||||||
|
|
||||||
loop {
|
Err(error::Error::DisplayError(DisplayError::DrawingError))
|
||||||
// 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))?;
|
|
||||||
|
|
||||||
//Update GPS Data Struct with the latest data fetched from UART buffer
|
// loop {
|
||||||
latest_data.update(&buf)
|
|
||||||
.map_err(|err| Error::GpsError(err))?;
|
|
||||||
|
|
||||||
// Clear display ready for next write
|
// // Clear display ready for next write
|
||||||
let style = MonoTextStyle::new(&FONT_10X20, 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.get_speed().as_str(),
|
// &latest_data.get_speed().as_str(),
|
||||||
//&latest_data.to_string().as_str(),
|
// Point::new(64, 20),
|
||||||
Point::new(64, 20),
|
// style,
|
||||||
style,
|
// Alignment::Center,
|
||||||
Alignment::Center,
|
// )
|
||||||
)
|
// .draw(&mut display)
|
||||||
.draw(&mut display)
|
// .map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
|
||||||
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
|
|
||||||
|
|
||||||
//Flush data to the display
|
// //Flush data to the display
|
||||||
display.flush()
|
// display.flush()
|
||||||
.map_err(|_| Error::DisplayError(DisplayError::FlushError))?;
|
// .map_err(|_| Error::DisplayError(DisplayError::FlushError))?;
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user