generated from luke-else/esp32-std-template
Compare commits
No commits in common. "a2a9abc4b3948284b79f82f0c7ac0126f93db57e" and "278444b620de53134e99305db6869be8562ac425" have entirely different histories.
a2a9abc4b3
...
278444b620
@ -1,21 +1,25 @@
|
|||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
|
|
||||||
use display_interface_i2c::I2CInterface;
|
|
||||||
|
|
||||||
use embedded_graphics::{
|
use embedded_graphics::{
|
||||||
mono_font::{ascii::FONT_10X20, MonoTextStyle},
|
|
||||||
pixelcolor::BinaryColor,
|
|
||||||
prelude::*,
|
prelude::*,
|
||||||
text::{Alignment, Text},
|
mono_font::{ascii::FONT_7X13, ascii::FONT_10X20, MonoTextStyle},
|
||||||
|
pixelcolor::BinaryColor,
|
||||||
|
text::{Text, Alignment},
|
||||||
};
|
};
|
||||||
|
|
||||||
use esp_idf_hal::i2c::I2cDriver;
|
use esp_idf_hal::{
|
||||||
|
self,
|
||||||
use ssd1306::{
|
prelude::Peripherals,
|
||||||
prelude::*, rotation::DisplayRotation, size::DisplaySize, I2CDisplayInterface, Ssd1306,
|
units::Hertz,
|
||||||
|
i2c::{I2cConfig, I2cDriver, I2c}
|
||||||
};
|
};
|
||||||
|
|
||||||
// clear the stage of failure of the display
|
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
|
||||||
|
|
||||||
|
use display_interface_i2c;
|
||||||
|
|
||||||
|
|
||||||
|
/// Enum to make clear the stage of failure of the display
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum DisplayError {
|
pub enum DisplayError {
|
||||||
SetupError(display_interface::DisplayError),
|
SetupError(display_interface::DisplayError),
|
||||||
@ -24,24 +28,20 @@ pub enum DisplayError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Display<'a, SIZE: DisplaySize> {
|
pub struct Display<'a, SIZE: DisplaySize> {
|
||||||
display: Ssd1306<I2CInterface<I2cDriver<'a>>, SIZE, ssd1306::mode::BufferedGraphicsMode<SIZE>>,
|
display: Ssd1306<I2CInterface<I2cDriver<'a>>, SIZE, ssd1306::mode::BufferedGraphicsMode<SIZE>>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, SIZE: DisplaySize> Display<'a, SIZE> {
|
impl<'a, SIZE: DisplaySize> Display<'a, SIZE>
|
||||||
|
{
|
||||||
/// Function to create a new display interface abstraction
|
/// Function to create a new display interface abstraction
|
||||||
pub fn new(
|
pub fn new(i2c: I2cDriver<'a>, size: SIZE, rotation: DisplayRotation) -> Result<Display<'a, SIZE>, Error> {
|
||||||
i2c: I2cDriver<'a>,
|
|
||||||
size: SIZE,
|
|
||||||
rotation: DisplayRotation,
|
|
||||||
) -> Result<Display<'a, SIZE>, Error> {
|
|
||||||
// Construct the I2C driver into a display interface
|
// Construct the I2C driver into a display interface
|
||||||
let interface = I2CDisplayInterface::new(i2c);
|
let interface = I2CDisplayInterface::new(i2c);
|
||||||
|
|
||||||
// Construct display with new anew driver
|
// Construct display with new anew driver
|
||||||
let mut display = Ssd1306::new(interface, size, rotation).into_buffered_graphics_mode();
|
let mut display = Ssd1306::new(interface, size, rotation)
|
||||||
display
|
.into_buffered_graphics_mode();
|
||||||
.init()
|
display.init().map_err(|err| Error::DisplayError(DisplayError::SetupError(err)))?;
|
||||||
.map_err(|err| Error::DisplayError(DisplayError::SetupError(err)))?;
|
|
||||||
|
|
||||||
Ok(Display{display})
|
Ok(Display{display})
|
||||||
}
|
}
|
||||||
@ -50,18 +50,21 @@ impl<'a, SIZE: DisplaySize> Display<'a, SIZE> {
|
|||||||
pub fn draw(&mut self, text: &str) -> Result<(), Error> {
|
pub fn draw(&mut self, text: &str) -> Result<(), Error> {
|
||||||
// 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);
|
||||||
self.display
|
self.display.clear(BinaryColor::Off)
|
||||||
.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, Point::new(64, 20), style, Alignment::Center)
|
Text::with_alignment(
|
||||||
|
text,
|
||||||
|
Point::new(64, 20),
|
||||||
|
style,
|
||||||
|
Alignment::Center,
|
||||||
|
)
|
||||||
.draw(&mut self.display)
|
.draw(&mut self.display)
|
||||||
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
|
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
|
||||||
|
|
||||||
//Flush data to the display
|
//Flush data to the display
|
||||||
self.display
|
self.display.flush()
|
||||||
.flush()
|
|
||||||
.map_err(|_| Error::DisplayError(DisplayError::FlushError))?;
|
.map_err(|_| Error::DisplayError(DisplayError::FlushError))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -16,13 +16,12 @@ pub struct GpsData {
|
|||||||
/// Altitude reported from GPS location
|
/// Altitude reported from GPS location
|
||||||
altitude: Option<f64>,
|
altitude: Option<f64>,
|
||||||
/// Timestamp of the last report
|
/// Timestamp of the last report
|
||||||
timestamp: Option<DateTime<Utc>>,
|
timestamp: Option<DateTime<Utc>>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for GpsData {
|
impl ToString for GpsData {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
format!(
|
format!("Latitude: {:.4?} \nLongitude: {:.4?} \nSpeed: {:.1?}mph \nAltitude: {:.1?}m",
|
||||||
"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(),
|
||||||
self.speed.unwrap_or_default(),
|
self.speed.unwrap_or_default(),
|
||||||
@ -41,8 +40,7 @@ impl GpsData {
|
|||||||
|
|
||||||
// Format the nmea buffer into a usable string
|
// Format the nmea buffer into a usable string
|
||||||
let nmea_raw = String::from_utf8(buf.to_owned())
|
let nmea_raw = String::from_utf8(buf.to_owned())
|
||||||
.map_err(|_| GpsError::ReadError())
|
.map_err(|_| GpsError::ReadError()).unwrap_or_default();
|
||||||
.unwrap_or_default();
|
|
||||||
let nmea_vec: Vec<&str> = nmea_raw.split('$').collect();
|
let nmea_vec: Vec<&str> = nmea_raw.split('$').collect();
|
||||||
|
|
||||||
// Loop through each sentence and use the information to update GPS data
|
// Loop through each sentence and use the information to update GPS data
|
||||||
@ -59,9 +57,7 @@ impl GpsData {
|
|||||||
let nmea = match nmea_parser.parse_sentence(sentence.as_str()) {
|
let nmea = match nmea_parser.parse_sentence(sentence.as_str()) {
|
||||||
Ok(nmea) => nmea,
|
Ok(nmea) => nmea,
|
||||||
// Don't continue processing a sentence if we know that it isn't supported
|
// Don't continue processing a sentence if we know that it isn't supported
|
||||||
Err(_) => {
|
Err(_) => { continue; }
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// print decoded gps data to serial
|
// print decoded gps data to serial
|
||||||
@ -71,7 +67,7 @@ impl GpsData {
|
|||||||
// Update Altitude above ground level
|
// Update Altitude above ground level
|
||||||
self.altitude = match gga.altitude {
|
self.altitude = match gga.altitude {
|
||||||
Some(_) => gga.altitude,
|
Some(_) => gga.altitude,
|
||||||
None => self.altitude,
|
None => self.altitude
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
nmea_parser::ParsedMessage::Gll(gll) => {
|
nmea_parser::ParsedMessage::Gll(gll) => {
|
||||||
@ -92,16 +88,14 @@ impl GpsData {
|
|||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
nmea_parser::ParsedMessage::Rmc(rms) => {
|
nmea_parser::ParsedMessage::Rmc(rms) => {
|
||||||
// Update Ground Speed
|
// Update Ground Speed
|
||||||
match rms.sog_knots {
|
match rms.sog_knots {
|
||||||
Some(_) => {
|
Some(_) => self.speed = Some(knots_to_mph(rms.sog_knots.unwrap_or_default())),
|
||||||
self.speed = Some(knots_to_mph(rms.sog_knots.unwrap_or_default()))
|
|
||||||
}
|
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -110,8 +104,11 @@ impl GpsData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Function to get a &str of the current speed reported by the GPS module
|
/// Function to get a &str of the current speed reported by the GPS module
|
||||||
pub fn get_speed(&self) -> Option<f64> {
|
pub fn get_speed(&self) -> String {
|
||||||
self.speed
|
match self.speed {
|
||||||
|
Some(speed) => format!("{:.2?}mph\n", speed),
|
||||||
|
None => format!("AWAITING\nGPS\nDATA")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
26
src/main.rs
26
src/main.rs
@ -14,10 +14,10 @@ use error::Error;
|
|||||||
mod appstate;
|
mod appstate;
|
||||||
|
|
||||||
mod gps;
|
mod gps;
|
||||||
use gps::{gpsdata::GpsData, GPS};
|
use gps::{gpsdata, GPS};
|
||||||
|
|
||||||
mod display;
|
mod display;
|
||||||
use display::Display;
|
use display::{Display, DisplayError};
|
||||||
use ssd1306::prelude::*;
|
use ssd1306::prelude::*;
|
||||||
|
|
||||||
fn main() -> Result<(), Error> {
|
fn main() -> Result<(), Error> {
|
||||||
@ -42,26 +42,20 @@ fn main() -> Result<(), Error> {
|
|||||||
let i2c =
|
let i2c =
|
||||||
I2cDriver::new(peripherals.i2c0, sda, scl, &config).map_err(|err| Error::EspError(err))?;
|
I2cDriver::new(peripherals.i2c0, sda, scl, &config).map_err(|err| Error::EspError(err))?;
|
||||||
|
|
||||||
// Prepare components for processing
|
|
||||||
let mut gps: GPS = GPS::new(peripherals.uart0, gps_rx)?;
|
let mut gps: GPS = GPS::new(peripherals.uart0, gps_rx)?;
|
||||||
let mut display: Display<DisplaySize128x64> =
|
let mut display: Display<DisplaySize128x64> =
|
||||||
Display::new(i2c, DisplaySize128x64, DisplayRotation::Rotate0)?;
|
Display::new(i2c, DisplaySize128x64, DisplayRotation::Rotate0)?;
|
||||||
let mut _app_state = appstate::AppState::default();
|
let mut app_state = appstate::AppState::default();
|
||||||
const NO_DATA: &str = "NO\nGPS\nDATA";
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let text: String;
|
let text: &str;
|
||||||
|
|
||||||
// Get the latest data from GPS module and flush to Display
|
// Get the latest data from GPS module and flush to Display
|
||||||
text = match gps.poll() {
|
match gps.poll() {
|
||||||
Ok(res) => match res.get_speed() {
|
Ok(res) => display.draw(&res.get_speed().as_str())?,
|
||||||
Some(s) => format!("{:.2} MPH", s),
|
|
||||||
None => NO_DATA.to_string(),
|
|
||||||
},
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("{:?}", e);
|
display.draw("NO\nGPS\nDATA");
|
||||||
NO_DATA.to_string()
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
display.draw(text.as_str())?
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user