From d64dd4b0fa1fe53c64d5e2064cd59ef064cd2d07 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Mon, 13 Nov 2023 14:19:50 +0000 Subject: [PATCH] Added draw function to Display --- src/display/mod.rs | 29 +++++++++++++++++++++++++++-- src/main.rs | 37 +++++++------------------------------ 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/display/mod.rs b/src/display/mod.rs index 6593ad2..aac968e 100644 --- a/src/display/mod.rs +++ b/src/display/mod.rs @@ -33,15 +33,40 @@ pub struct Display<'a, SIZE: DisplaySize> { impl<'a, SIZE: DisplaySize> Display<'a, SIZE> { - pub fn new(i2c: I2cDriver<'a>, size: SIZE) -> Result, Error> { + /// Function to create a new display interface abstraction + pub fn new(i2c: I2cDriver<'a>, size: SIZE, rotation: DisplayRotation) -> Result, 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) + 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}) } + + /// Function to draw a given set of text to a display + 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) + .map_err(|_| Error::DisplayError(DisplayError::DrawingError))?; + + // Draw text to Display + 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() + .map_err(|_| Error::DisplayError(DisplayError::FlushError))?; + + Ok(()) + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1d6cb6e..3005948 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,20 +8,17 @@ use esp_idf_hal::{ i2c::{I2cConfig, I2cDriver} }; -use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306}; - -mod appstate; - mod error; use error::Error; +mod appstate; + mod gps; -use gps::gpsdata; +use gps::{gpsdata, GPS}; mod display; -use display::DisplayError; - - +use display::{DisplayError, Display}; +use ssd1306::prelude::*; fn main() -> Result<(), Error> { // It is necessary to call this function once. Otherwise some patches to the runtime @@ -49,30 +46,10 @@ fn main() -> Result<(), Error> { &config ).map_err(|err| Error::EspError(err))?; + let gps: GPS = GPS::new(peripherals.uart0, gps_rx)?; + let display: Display = Display::new(i2c, DisplaySize128x64, DisplayRotation::Rotate0)?; let mut app_state = appstate::AppState::default(); let mut latest_data = gpsdata::GpsData::default(); Err(error::Error::DisplayError(DisplayError::DrawingError)) - - // loop { - - // // Clear display ready for next write - // let style = MonoTextStyle::new(&FONT_10X20, BinaryColor::On); - // display.clear(BinaryColor::Off) - // .map_err(|_| Error::DisplayError(DisplayError::DrawingError))?; - - // // Draw text to Display - // Text::with_alignment( - // &latest_data.get_speed().as_str(), - // Point::new(64, 20), - // style, - // Alignment::Center, - // ) - // .draw(&mut display) - // .map_err(|_| Error::DisplayError(DisplayError::DrawingError))?; - - // //Flush data to the display - // display.flush() - // .map_err(|_| Error::DisplayError(DisplayError::FlushError))?; - // } } \ No newline at end of file