-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
124 additions
and
63 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,70 @@ | ||
use crate::screen::{self, Screen}; | ||
use embedded_graphics::pixelcolor::BinaryColor; | ||
use embedded_graphics::prelude::*; | ||
use embedded_hal::i2c::I2c; | ||
use embedded_hal_async::i2c::I2c; | ||
use log::{debug, info}; | ||
use ssd1306::mode::{BufferedGraphicsMode, DisplayConfig}; | ||
use ssd1306::mode::{BufferedGraphicsModeAsync, DisplayConfigAsync}; | ||
use ssd1306::prelude::I2CInterface; | ||
use ssd1306::rotation::DisplayRotation; | ||
use ssd1306::size::DisplaySize128x64; | ||
use ssd1306::Ssd1306; | ||
|
||
// The `ssd1306` crate unfortunately doesn't support async yet (though `display-interface`, | ||
// `display-interface-i2c` and `embedded-hal-bus` do), so we can't use async here yet. | ||
// See also https://github.com/rust-embedded-community/ssd1306/pull/189 | ||
use ssd1306::Ssd1306Async; | ||
|
||
/// Display error | ||
pub type Error = screen::Error<display_interface::DisplayError>; | ||
|
||
/// Convenient hardware-agnostic display driver | ||
pub struct Display<I2C> { | ||
driver: Ssd1306<I2CInterface<I2C>, DisplaySize128x64, BufferedGraphicsMode<DisplaySize128x64>>, | ||
driver: Ssd1306Async< | ||
I2CInterface<I2C>, | ||
DisplaySize128x64, | ||
BufferedGraphicsModeAsync<DisplaySize128x64>, | ||
>, | ||
} | ||
|
||
impl<I2C: I2c> Display<I2C> { | ||
/// Create display driver and initialize display hardware | ||
pub fn new(i2c: I2C, addr: u8) -> Result<Self, Error> { | ||
pub async fn new(i2c: I2C) -> Result<Self, Error> { | ||
debug!("Display: Initializing SSD1306..."); | ||
|
||
// Build SSD1306 driver and switch to buffered graphics mode | ||
let mut driver = Ssd1306::new( | ||
I2CInterface::new(i2c, addr, 0x40), | ||
let mut driver = Ssd1306Async::new( | ||
I2CInterface::new(i2c, 0x3c, 0x40), | ||
DisplaySize128x64, | ||
DisplayRotation::Rotate0, | ||
) | ||
.into_buffered_graphics_mode(); | ||
|
||
// Initialize and clear display | ||
driver.init()?; | ||
driver.init().await?; | ||
driver.clear(BinaryColor::Off)?; | ||
driver.flush()?; | ||
driver.flush().await?; | ||
|
||
info!("Display: SSD1306 initialized"); | ||
Ok(Self { driver }) | ||
} | ||
|
||
/// Turn display off | ||
pub fn turn_off(&mut self) -> Result<(), Error> { | ||
pub async fn turn_off(&mut self) -> Result<(), Error> { | ||
debug!("Display: Power off"); | ||
self.driver.set_display_on(false)?; | ||
self.driver.set_display_on(false).await?; | ||
Ok(()) | ||
} | ||
|
||
/// Clear display | ||
#[allow(dead_code)] | ||
pub fn clear(&mut self) -> Result<(), Error> { | ||
pub async fn clear(&mut self) -> Result<(), Error> { | ||
self.driver.clear(BinaryColor::Off)?; | ||
self.driver.flush()?; | ||
self.driver.set_display_on(true)?; | ||
self.driver.flush().await?; | ||
self.driver.set_display_on(true).await?; | ||
Ok(()) | ||
} | ||
|
||
/// Show screen | ||
pub fn screen<S: Screen>(&mut self, screen: &S) -> Result<(), Error> { | ||
pub async fn screen<S: Screen>(&mut self, screen: &S) -> Result<(), Error> { | ||
self.driver.clear(BinaryColor::Off)?; | ||
screen.draw(&mut self.driver)?; | ||
self.driver.flush()?; | ||
self.driver.set_display_on(true)?; | ||
self.driver.flush().await?; | ||
self.driver.set_display_on(true).await?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.