generated from luke-else/esp32-std-template
Added draw function to Display
This commit is contained in:
@ -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(())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user