Compare commits

..

7 Commits

9 changed files with 150 additions and 181 deletions

View File

@ -5,12 +5,16 @@ target = "xtensa-esp32-espidf"
linker = "ldproxy" linker = "ldproxy"
# runner = "espflash --monitor" # Select this runner for espflash v1.x.x # runner = "espflash --monitor" # Select this runner for espflash v1.x.x
runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x
rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110
[unstable] [unstable]
build-std = ["std", "panic_abort"] build-std = ["std", "panic_abort"]
[env] [env]
# Note: these variables are not used when using pio builder (`cargo build --features pio`) # Note: these variables are not used when using pio builder (`cargo build --features pio`)
ESP_IDF_VERSION = "release/v4.4" # ESP_IDF_VERSION = "v5.1.1"
ESP_IDF_VERSION = "v5.2.2"
ESP_IDF_PATH_ISSUES = "warn" # or "ignore"
# Workaround for https://github.com/esp-rs/esp-idf-template/issues/174
CRATE_CC_NO_DEFAULTS = "1"

View File

@ -4,7 +4,6 @@ version = "0.1.0"
authors = ["Luke Else <mail@luke-else.co.uk>"] authors = ["Luke Else <mail@luke-else.co.uk>"]
edition = "2021" edition = "2021"
resolver = "2" resolver = "2"
rust-version = "1.66"
[profile.release] [profile.release]
opt-level = "s" opt-level = "s"
@ -15,29 +14,24 @@ opt-level = "z"
[features] [features]
default = ["std", "hal", "esp-idf-sys/native"] default = ["std", "embassy", "esp-idf-svc/native"]
pio = ["esp-idf-svc/pio"]
std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"]
alloc = ["esp-idf-svc/alloc"]
nightly = ["esp-idf-svc/nightly"]
experimental = ["esp-idf-svc/experimental"]
embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"]
pio = ["esp-idf-sys/pio"]
all = ["std", "nightly", "experimental", "embassy"]
hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"]
std = ["alloc", "esp-idf-sys/std", "esp-idf-sys/binstart", "embedded-svc?/std", "esp-idf-hal?/std", "esp-idf-svc?/std"]
alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"]
nightly = ["embedded-svc?/nightly", "esp-idf-svc?/nightly"] # Future: "esp-idf-hal?/nightly"
experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"]
embassy = ["esp-idf-hal?/embassy-sync", "esp-idf-hal?/critical-section", "esp-idf-hal?/edge-executor", "esp-idf-svc?/embassy-time-driver", "esp-idf-svc?/embassy-time-isr-queue"]
[dependencies] [dependencies]
log = { version = "0.4.17", default-features = false } log = { version = "0.4.22", default-features = false }
esp-idf-sys = { version = "0.33", default-features = false } esp-idf-svc = { version = "0.49.1", default-features = false }
esp-idf-hal = { version = "0.41", optional = true, default-features = false } embedded-svc = { version = "0.28", optional = true, default-features = false }
esp-idf-svc = { version = "0.46", optional = true, default-features = false }
embedded-svc = { version = "0.25", optional = true, default-features = false }
nmea-parser = "0.10.0"
embedded-graphics = "0.8.0" embedded-graphics = "0.8.0"
ssd1306 = "0.8.0" ssd1306 = "0.9.0"
display-interface = "0.4.1" display-interface = "0.5.0"
display-interface-i2c = "0.4.0" display-interface-i2c = "0.5.0"
nmea = { version = "0.7.0", default-features = false }
[build-dependencies] [build-dependencies]
embuild = "0.31.2" embuild = "0.32.0"

View File

@ -1,6 +1,4 @@
// Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 // Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() {
embuild::build::CfgArgs::output_propagated("ESP_IDF")?; embuild::espidf::sysenv::output();
embuild::build::LinkArgs::output_propagated("ESP_IDF")?;
Ok(())
} }

View File

@ -1,5 +1,5 @@
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K)
CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000 CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000
# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default).
# This allows to use 1 ms granuality for thread sleeps (10 ms by default). # This allows to use 1 ms granuality for thread sleeps (10 ms by default).

View File

@ -1,25 +1,23 @@
use crate::error::Error; use crate::error::Error;
use display_interface_i2c::I2CInterface;
use embedded_graphics::{ use embedded_graphics::{
prelude::*, mono_font::{ascii::FONT_10X20, MonoTextStyle},
mono_font::{ascii::FONT_7X13, ascii::FONT_10X20, MonoTextStyle},
pixelcolor::BinaryColor, pixelcolor::BinaryColor,
text::{Text, Alignment}, prelude::*,
text::{Alignment, Text},
}; };
use esp_idf_hal::{ use esp_idf_svc::hal::i2c::I2cDriver;
self,
prelude::Peripherals,
units::Hertz,
i2c::{I2cConfig, I2cDriver, I2c} use ssd1306::{
prelude::*, rotation::DisplayRotation, size::DisplaySize, I2CDisplayInterface, Ssd1306,
}; };
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306}; // clear the stage of failure of the display
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),
@ -28,44 +26,45 @@ 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(i2c: I2cDriver<'a>, size: SIZE, rotation: DisplayRotation) -> Result<Display<'a, SIZE>, Error> { pub fn new(
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) let mut display = Ssd1306::new(interface, size, rotation).into_buffered_graphics_mode();
.into_buffered_graphics_mode(); display
display.init().map_err(|err| Error::DisplayError(DisplayError::SetupError(err)))?; .init()
.map_err(|err| Error::DisplayError(DisplayError::SetupError(err)))?;
Ok(Display{display}) Ok(Display { display })
} }
/// Function to draw a given set of text to a display /// Function to draw a given set of text to a display
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.clear(BinaryColor::Off) self.display
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?; .clear(BinaryColor::Off)
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
// Draw text to Display // Draw text to Display
Text::with_alignment( Text::with_alignment(text, Point::new(64, 20), style, Alignment::Center)
text, .draw(&mut self.display)
Point::new(64, 20), .map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
style,
Alignment::Center,
)
.draw(&mut self.display)
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
//Flush data to the display //Flush data to the display
self.display.flush() self.display
.map_err(|_| Error::DisplayError(DisplayError::FlushError))?; .flush()
.map_err(|_| Error::DisplayError(DisplayError::FlushError))?;
Ok(()) Ok(())
} }

View File

@ -1,4 +1,4 @@
use esp_idf_sys::EspError; use esp_idf_svc::sys::EspError;
use crate::display::DisplayError; use crate::display::DisplayError;
use crate::gps::GpsError; use crate::gps::GpsError;

View File

@ -1,31 +1,21 @@
use super::GpsError; use super::GpsError;
use nmea_parser; use nmea::Nmea;
use nmea_parser::chrono::{DateTime, Utc};
use nmea_parser::gnss::FaaMode;
/// Data structure to store all relevant data collected from the gps /// Data structure to store all relevant data collected from the gps
#[derive(Default)] #[derive(Default)]
pub struct GpsData { pub struct GpsData {
/// Latitude of reported GPS location pub nmea: Nmea,
latitude: Option<f64>,
/// Longitude of reported GPS location
longitude: Option<f64>,
/// Calculated speed from GPS reciever
speed: Option<f64>,
/// Altitude reported from GPS location
altitude: Option<f64>,
/// Timestamp of the last report
timestamp: Option<DateTime<Utc>>
} }
impl ToString for GpsData { impl ToString for GpsData {
fn to_string(&self) -> String { fn to_string(&self) -> String {
format!("Latitude: {:.4?} \nLongitude: {:.4?} \nSpeed: {:.1?}mph \nAltitude: {:.1?}m", format!(
self.latitude.unwrap_or_default(), "Latitude: {:.4?} \nLongitude: {:.4?} \nSpeed: {:.1?}mph \nAltitude: {:.1?}m",
self.longitude.unwrap_or_default(), self.nmea.latitude.unwrap_or_default(),
self.speed.unwrap_or_default(), self.nmea.longitude.unwrap_or_default(),
self.altitude.unwrap_or_default(), knots_to_mph(self.nmea.speed_over_ground.unwrap_or_default()),
self.nmea.altitude.unwrap_or_default(),
) )
} }
} }
@ -36,83 +26,34 @@ impl GpsData {
/// Takes in a reference to a buffer of raw serial data and then /// Takes in a reference to a buffer of raw serial data and then
/// tries to read it in as NMEA Data /// tries to read it in as NMEA Data
pub fn update(&mut self, buf: &Vec<u8>) -> Result<(), GpsError> { pub fn update(&mut self, buf: &Vec<u8>) -> Result<(), GpsError> {
let mut nmea_parser = nmea_parser::NmeaParser::new();
// 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()).unwrap_or_default(); .map_err(|_| GpsError::ReadError())
let nmea_vec: Vec<&str> = nmea_raw.split('$').collect(); .unwrap_or_default();
// Loop through each sentence and use the information to update GPS data // Split at $ and then prepend the '$' back to each string
for nmea_line in nmea_vec { let nmea_vec: Vec<String> = nmea_raw.split('$').map(|str| format!("${}", str)).collect();
// Don't try and process / parse if the string is empty
if nmea_line.is_empty() { for nmea_string in nmea_vec {
if nmea_string.is_empty() {
continue; continue;
} }
// Construct string that is in the correct format for parsing match self.nmea.parse(&nmea_string) {
let mut sentence = "$".to_string(); Ok(sentence) => {
sentence.push_str(nmea_line); println!("Recieved GPS Packet: {}", sentence)
}
let nmea = match nmea_parser.parse_sentence(sentence.as_str()) {
Ok(nmea) => nmea, Err(e) => {
// Don't continue processing a sentence if we know that it isn't supported eprintln!("Couldn't parse Packet: {}", e)
Err(_) => { continue; }
};
// print decoded gps data to serial
match nmea {
// TODO: Investigate why the GGA Packets seem to come in mangled
nmea_parser::ParsedMessage::Gga(gga) => {
// Update Altitude above ground level
self.altitude = match gga.altitude {
Some(_) => gga.altitude,
None => self.altitude
};
} }
nmea_parser::ParsedMessage::Gll(gll) => {
// FAA mode is used to determine the validity of the data in this case
if gll.faa_mode.unwrap_or(FaaMode::NotValid) == FaaMode::Autonomous {
// Only make updates to position if we have new data, do not overwrite
// stale data
match gll.latitude {
Some(_) => self.latitude = gll.latitude,
None => {}
}
match gll.longitude {
Some(_) => self.longitude = gll.longitude,
None => {}
}
match gll.timestamp {
Some(_) => self.timestamp = gll.timestamp,
None => {}
}
}
},
nmea_parser::ParsedMessage::Rmc(rms) => {
// Update Ground Speed
match rms.sog_knots {
Some(_) => self.speed = Some(knots_to_mph(rms.sog_knots.unwrap_or_default())),
None => {}
}
},
_ => {}
} }
} }
Ok(()) Ok(())
} }
/// Function to get a &str of the current speed reported by the GPS module
pub fn get_speed(&self) -> String {
match self.speed {
Some(speed) => format!("{:.2?}mph\n", speed),
None => format!("AWAITING\nGPS\nDATA")
}
}
} }
/// Function to simply convert knots to mph /// Function to simply convert knots to mph
fn knots_to_mph(knots: f64) -> f64{ pub fn knots_to_mph(knots: f32) -> f32 {
knots * 1.150779 knots * 1.150779
} }

View File

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

View File

@ -1,12 +1,14 @@
#![allow(unused)] #![allow(unused)]
use esp_idf_hal::{ use esp_idf_svc::hal::{
self, self,
gpio::*,
i2c::{I2cConfig, I2cDriver}, i2c::{I2cConfig, I2cDriver},
prelude::Peripherals, prelude::Peripherals,
units::Hertz, units::Hertz,
}; };
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use std::sync::atomic::{AtomicBool, Ordering};
mod error; mod error;
use error::Error; use error::Error;
@ -14,18 +16,23 @@ use error::Error;
mod appstate; mod appstate;
mod gps; mod gps;
use gps::{gpsdata, GPS}; use gps::{
gpsdata::{knots_to_mph, GpsData},
GPS,
};
mod display; mod display;
use display::{Display, DisplayError}; use display::Display;
use ssd1306::prelude::*; use ssd1306::prelude::*;
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
// It is necessary to call this function once. Otherwise some patches to the runtime // It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_sys::link_patches(); esp_idf_svc::sys::link_patches();
// Bind the log crate to the ESP Logging facilities // Bind the log crate to the ESP Logging facilities
//esp_idf_svc::log::EspLogger::initialize_default(); esp_idf_svc::log::EspLogger::initialize_default();
log::info!("hello");
// Get Peripherals and sysloop ready // Get Peripherals and sysloop ready
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take().unwrap();
@ -42,20 +49,43 @@ 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))?;
let mut gps: GPS = GPS::new(peripherals.uart0, gps_rx)?; // Setup GPIO interrupts
static FLAG: AtomicBool = AtomicBool::new(false);
let mut interrupt_button_handle = PinDriver::input(pins.gpio4).unwrap();
interrupt_button_handle.set_pull(Pull::Up);
interrupt_button_handle.set_interrupt_type(InterruptType::NegEdge);
unsafe {
interrupt_button_handle.subscribe(app_state_interrupt);
}
interrupt_button_handle.enable_interrupt();
// Prepare components for processing
let mut gps: GPS = GPS::new(peripherals.uart1, 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: &str; let text: String;
// Get the latest data from GPS module and flush to Display // Get the latest data from GPS module and flush to Display
match gps.poll() { text = match gps.poll() {
Ok(res) => display.draw(&res.get_speed().as_str())?, Ok(res) => match res.nmea.speed_over_ground {
Some(s) => format!("{:.2} MPH", knots_to_mph(s)),
None => NO_DATA.to_string(),
},
Err(e) => { Err(e) => {
display.draw("NO\nGPS\nDATA"); eprintln!("{:?}", e);
NO_DATA.to_string()
} }
} };
display.draw(text.as_str())?
} }
} }
fn app_state_interrupt() {
println!("inteerrupt");
}