use embedded_graphics_core::pixelcolor::Rgb565;
use embedded_hal_async::delay::DelayNs;
use crate::{
dcs::{
BitsPerPixel, ExitSleepMode, InterfaceExt, PixelFormat, SetAddressMode, SetDisplayOn,
SetInvertMode, SetPixelFormat,
},
interface::{Interface, InterfaceKind},
models::{Model, ModelInitError},
options::ModelOptions,
ConfigurationError,
};
pub struct ST7735s;
impl Model for ST7735s {
type ColorFormat = Rgb565;
const FRAMEBUFFER_SIZE: (u16, u16) = (132, 162);
async fn init<DELAY, DI>(
&mut self,
di: &mut DI,
delay: &mut DELAY,
options: &ModelOptions,
) -> Result<SetAddressMode, ModelInitError<DI::Error>>
where
DELAY: DelayNs,
DI: Interface,
{
if !matches!(
DI::KIND,
InterfaceKind::Serial4Line | InterfaceKind::Parallel8Bit | InterfaceKind::Parallel16Bit
) {
return Err(ModelInitError::InvalidConfiguration(
ConfigurationError::UnsupportedInterface,
));
}
let madctl = SetAddressMode::from(options);
delay.delay_us(200_000).await;
di.write_command(ExitSleepMode).await?; delay.delay_us(120_000).await;
di.write_command(SetInvertMode::new(options.invert_colors))
.await?; di.write_raw(0xB1, &[0x05, 0x3A, 0x3A]).await?; di.write_raw(0xB2, &[0x05, 0x3A, 0x3A]).await?; di.write_raw(0xB3, &[0x05, 0x3A, 0x3A, 0x05, 0x3A, 0x3A])
.await?; di.write_raw(0xB4, &[0b0000_0011]).await?; di.write_raw(0xC0, &[0x62, 0x02, 0x04]).await?; di.write_raw(0xC1, &[0xC0]).await?; di.write_raw(0xC2, &[0x0D, 0x00]).await?; di.write_raw(0xC3, &[0x8D, 0x6A]).await?; di.write_raw(0xC4, &[0x8D, 0xEE]).await?; di.write_raw(0xC5, &[0x0E]).await?; di.write_raw(
0xE0,
&[
0x10, 0x0E, 0x02, 0x03, 0x0E, 0x07, 0x02, 0x07, 0x0A, 0x12, 0x27, 0x37, 0x00, 0x0D,
0x0E, 0x10,
],
)
.await?; di.write_raw(
0xE1,
&[
0x10, 0x0E, 0x03, 0x03, 0x0F, 0x06, 0x02, 0x08, 0x0A, 0x13, 0x26, 0x36, 0x00, 0x0D,
0x0E, 0x10,
],
)
.await?;
let pf = PixelFormat::with_all(BitsPerPixel::from_rgb_color::<Self::ColorFormat>());
di.write_command(SetPixelFormat::new(pf)).await?;
di.write_command(madctl).await?; di.write_command(SetDisplayOn).await?;
Ok(madctl)
}
}