generated from luke-else/esp32-std-template
Chore: Cleaned up code
This commit is contained in:
parent
278444b620
commit
6b1a461e11
@ -1,25 +1,21 @@
|
||||
use crate::error::Error;
|
||||
|
||||
use display_interface_i2c::I2CInterface;
|
||||
|
||||
use embedded_graphics::{
|
||||
prelude::*,
|
||||
mono_font::{ascii::FONT_7X13, ascii::FONT_10X20, MonoTextStyle},
|
||||
mono_font::{ascii::FONT_10X20, MonoTextStyle},
|
||||
pixelcolor::BinaryColor,
|
||||
text::{Text, Alignment},
|
||||
prelude::*,
|
||||
text::{Alignment, Text},
|
||||
};
|
||||
|
||||
use esp_idf_hal::{
|
||||
self,
|
||||
prelude::Peripherals,
|
||||
units::Hertz,
|
||||
i2c::{I2cConfig, I2cDriver, I2c}
|
||||
use esp_idf_hal::i2c::I2cDriver;
|
||||
|
||||
use ssd1306::{
|
||||
prelude::*, rotation::DisplayRotation, size::DisplaySize, I2CDisplayInterface, Ssd1306,
|
||||
};
|
||||
|
||||
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
|
||||
|
||||
use display_interface_i2c;
|
||||
|
||||
|
||||
/// Enum to make clear the stage of failure of the display
|
||||
// clear the stage of failure of the display
|
||||
#[derive(Debug)]
|
||||
pub enum DisplayError {
|
||||
SetupError(display_interface::DisplayError),
|
||||
@ -28,20 +24,24 @@ pub enum DisplayError {
|
||||
}
|
||||
|
||||
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
|
||||
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
|
||||
let interface = I2CDisplayInterface::new(i2c);
|
||||
|
||||
// Construct display with new anew driver
|
||||
let mut display = Ssd1306::new(interface, size, rotation)
|
||||
.into_buffered_graphics_mode();
|
||||
display.init().map_err(|err| Error::DisplayError(DisplayError::SetupError(err)))?;
|
||||
let mut display = Ssd1306::new(interface, size, rotation).into_buffered_graphics_mode();
|
||||
display
|
||||
.init()
|
||||
.map_err(|err| Error::DisplayError(DisplayError::SetupError(err)))?;
|
||||
|
||||
Ok(Display { display })
|
||||
}
|
||||
@ -50,21 +50,18 @@ impl<'a, SIZE: DisplaySize> Display<'a, SIZE>
|
||||
pub fn draw(&mut self, text: &str) -> Result<(), Error> {
|
||||
// Clear display ready for next write
|
||||
let style = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
|
||||
self.display.clear(BinaryColor::Off)
|
||||
self.display
|
||||
.clear(BinaryColor::Off)
|
||||
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
|
||||
|
||||
// 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)
|
||||
.map_err(|_| Error::DisplayError(DisplayError::DrawingError))?;
|
||||
|
||||
//Flush data to the display
|
||||
self.display.flush()
|
||||
self.display
|
||||
.flush()
|
||||
.map_err(|_| Error::DisplayError(DisplayError::FlushError))?;
|
||||
|
||||
Ok(())
|
||||
|
12
src/main.rs
12
src/main.rs
@ -14,10 +14,10 @@ use error::Error;
|
||||
mod appstate;
|
||||
|
||||
mod gps;
|
||||
use gps::{gpsdata, GPS};
|
||||
use gps::GPS;
|
||||
|
||||
mod display;
|
||||
use display::{Display, DisplayError};
|
||||
use display::Display;
|
||||
use ssd1306::prelude::*;
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
@ -42,19 +42,19 @@ fn main() -> Result<(), Error> {
|
||||
let i2c =
|
||||
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 display: Display<DisplaySize128x64> =
|
||||
Display::new(i2c, DisplaySize128x64, DisplayRotation::Rotate0)?;
|
||||
let mut app_state = appstate::AppState::default();
|
||||
let mut _app_state = appstate::AppState::default();
|
||||
|
||||
loop {
|
||||
let text: &str;
|
||||
|
||||
// Get the latest data from GPS module and flush to Display
|
||||
match gps.poll() {
|
||||
Ok(res) => display.draw(&res.get_speed().as_str())?,
|
||||
Err(e) => {
|
||||
display.draw("NO\nGPS\nDATA");
|
||||
display.draw("NO\nGPS\nDATA")?;
|
||||
eprintln!("{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user