RKTK API Docs RKTK Home Repo

rktk_drivers_common/display/
ssd1306.rs

1//! SSD1306 OLED display driver
2
3use display_interface::DisplayError;
4use embedded_hal::i2c::I2c as I2cSync;
5use embedded_hal_async::i2c::I2c as I2cAsync;
6use rktk::drivers::interface::display::DisplayDriver;
7pub use ssd1306::prelude;
8use ssd1306::{
9    I2CDisplayInterface, Ssd1306Async, mode::BufferedGraphicsModeAsync, prelude::*,
10    size::DisplaySizeAsync,
11};
12
13type Ssd1306<I2C, SIZE> = Ssd1306Async<I2CInterface<I2C>, SIZE, BufferedGraphicsModeAsync<SIZE>>;
14
15pub struct Ssd1306Driver<I2C: I2cAsync + I2cSync, SIZE: DisplaySizeAsync>(Ssd1306<I2C, SIZE>);
16
17impl<I2C, SIZE> Ssd1306Driver<I2C, SIZE>
18where
19    I2C: I2cAsync + I2cSync + 'static,
20    SIZE: DisplaySizeAsync + DisplaySize + 'static,
21{
22    pub fn new(i2c: I2C, size: SIZE, rotation: DisplayRotation) -> Self {
23        let interface = I2CDisplayInterface::new(i2c);
24        Self(Ssd1306Async::new(interface, size, rotation).into_buffered_graphics_mode())
25    }
26}
27
28impl<I2C, SIZE> DisplayDriver for Ssd1306Driver<I2C, SIZE>
29where
30    I2C: I2cAsync + I2cSync + 'static,
31    SIZE: DisplaySizeAsync + DisplaySize + 'static,
32{
33    async fn init(&mut self) -> Result<(), DisplayError> {
34        self.0.init().await
35    }
36
37    async fn flush(&mut self) -> Result<(), DisplayError> {
38        self.0.flush().await
39    }
40
41    async fn set_brightness(&mut self, brightness: u8) -> Result<(), DisplayError> {
42        self.0
43            .set_brightness(Brightness::custom(1, brightness))
44            .await
45    }
46
47    async fn set_display_on(&mut self, on: bool) -> Result<(), DisplayError> {
48        self.0.set_display_on(on).await
49    }
50
51    type Display = Ssd1306Async<I2CInterface<I2C>, SIZE, BufferedGraphicsModeAsync<SIZE>>;
52}
53
54impl<I2C, SIZE> AsRef<Ssd1306<I2C, SIZE>> for Ssd1306Driver<I2C, SIZE>
55where
56    I2C: I2cAsync + I2cSync + 'static,
57    SIZE: DisplaySizeAsync + DisplaySize + 'static,
58{
59    fn as_ref(&self) -> &Ssd1306<I2C, SIZE> {
60        &self.0
61    }
62}
63
64impl<I2C, SIZE> AsMut<Ssd1306<I2C, SIZE>> for Ssd1306Driver<I2C, SIZE>
65where
66    I2C: I2cAsync + I2cSync + 'static,
67    SIZE: DisplaySizeAsync + DisplaySize + 'static,
68{
69    fn as_mut(&mut self) -> &mut Ssd1306<I2C, SIZE> {
70        &mut self.0
71    }
72}