Created generic display module

This commit is contained in:
Luke Else 2023-11-13 13:59:22 +00:00
parent 6625351e38
commit 6c157f65f6
3 changed files with 33 additions and 5 deletions

View File

@ -37,6 +37,7 @@ nmea-parser = "0.10.0"
embedded-graphics = "0.8.0" embedded-graphics = "0.8.0"
ssd1306 = "0.8.0" ssd1306 = "0.8.0"
display-interface = "0.4.1" display-interface = "0.4.1"
display-interface-i2c = "0.4.0"
[build-dependencies] [build-dependencies]
embuild = "0.31.2" embuild = "0.31.2"

View File

@ -1,3 +1,5 @@
use crate::error::Error;
use embedded_graphics::{ use embedded_graphics::{
prelude::*, prelude::*,
mono_font::{ascii::FONT_7X13, ascii::FONT_10X20, MonoTextStyle}, mono_font::{ascii::FONT_7X13, ascii::FONT_10X20, MonoTextStyle},
@ -5,6 +7,17 @@ use embedded_graphics::{
text::{Text, Alignment}, text::{Text, Alignment},
}; };
use esp_idf_hal::{
self,
prelude::Peripherals,
units::Hertz,
i2c::{I2cConfig, I2cDriver, I2c}
};
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
use display_interface_i2c;
/// 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)]
@ -13,3 +26,22 @@ pub enum DisplayError {
DrawingError, DrawingError,
FlushError, FlushError,
} }
pub struct Display<'a, SIZE: DisplaySize> {
display: Ssd1306<I2CInterface<I2cDriver<'a>>, SIZE, ssd1306::mode::BufferedGraphicsMode<SIZE>>
}
impl<'a, SIZE: DisplaySize> Display<'a, SIZE>
{
pub fn new(i2c: I2cDriver<'a>, size: SIZE) -> 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, DisplayRotation::Rotate0)
.into_buffered_graphics_mode();
display.init().map_err(|err| Error::DisplayError(DisplayError::SetupError(err)))?;
Ok(Display{display})
}
}

View File

@ -49,11 +49,6 @@ fn main() -> Result<(), Error> {
&config &config
).map_err(|err| Error::EspError(err))?; ).map_err(|err| Error::EspError(err))?;
let interface = I2CDisplayInterface::new(i2c);
let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
.into_buffered_graphics_mode();
display.init().map_err(|err| Error::DisplayError(DisplayError::SetupError(err)))?;
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();