Added draw function to Display

This commit is contained in:
Luke Else 2023-11-13 14:19:50 +00:00
parent 6c157f65f6
commit d64dd4b0fa
2 changed files with 34 additions and 32 deletions

View File

@ -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<Display<'a, SIZE>, Error> {
/// Function to create a new display interface abstraction
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, 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(())
}
}

View File

@ -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<DisplaySize128x64> = 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))?;
// }
}