use core::time::Duration;
use embedded_graphics::{
pixelcolor::BinaryColor,
prelude::{Dimensions, Point, Size},
primitives::Rectangle,
};
use embedded_hal::digital::{InputPin, OutputPin};
use embedded_hal_async::{delay::DelayNs, digital::Wait, spi::SpiBus};
use crate::{
buffer::{binary_buffer_length, BinaryBuffer},
log::{debug, trace},
Epd, EpdHw, Error,
};
const LUT_FULL_UPDATE: [u8; 30] = [
0x50, 0xAA, 0x55, 0xAA, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
const LUT_PARTIAL_UPDATE: [u8; 30] = [
0x10, 0x18, 0x18, 0x08, 0x18, 0x18, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x13, 0x14, 0x44, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RefreshMode {
Full,
Partial,
}
impl RefreshMode {
pub fn lut(&self) -> &[u8; 30] {
match self {
RefreshMode::Full => &LUT_FULL_UPDATE,
RefreshMode::Partial => &LUT_PARTIAL_UPDATE,
}
}
}
pub const DISPLAY_HEIGHT: u16 = 296;
pub const DISPLAY_WIDTH: u16 = 128;
pub const RECOMMENDED_MIN_FULL_REFRESH_INTERVAL: Duration = Duration::from_secs(180);
pub const RECOMMENDED_MAX_FULL_REFRESH_INTERVAL: Duration = Duration::from_secs(24 * 60 * 60);
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Command {
DriverOutputControl = 0x01,
BoosterSoftStartControl = 0x0C,
DeepSleepMode = 0x10,
DataEntryModeSetting = 0x11,
SwReset = 0x12,
TemperatureSensorControl = 0x1A,
MasterActivation = 0x20,
DisplayUpdateControl1 = 0x21,
DisplayUpdateControl2 = 0x22,
WriteRam = 0x24,
WriteVcom = 0x2C,
WriteLut = 0x32,
SetDummyLinePeriod = 0x3A,
SetGateLineWidth = 0x3B,
BorderWaveformControl = 0x3C,
SetRamXStartEnd = 0x44,
SetRamYStartEnd = 0x45,
SetRamX = 0x4E,
SetRamY = 0x4F,
Noop = 0xFF,
}
impl Command {
fn register(&self) -> u8 {
*self as u8
}
}
const DRIVER_OUTPUT_INIT_DATA: [u8; 3] = [0x27, 0x01, 0x00];
const BOOSTER_SOFT_START_INIT_DATA: [u8; 3] = [0xD7, 0xD6, 0x9D];
pub struct Epd2in9<HW>
where
HW: EpdHw,
{
hw: HW,
}
impl<HW> Epd2in9<HW>
where
HW: EpdHw,
{
pub fn new(hw: HW) -> Self {
Epd2in9 { hw }
}
pub async fn set_border(
&mut self,
spi: &mut HW::Spi,
color: BinaryColor,
) -> Result<(), HW::Error> {
let border_setting: u8 = match color {
BinaryColor::Off => 0x00,
BinaryColor::On => 0x01,
};
self.send(spi, Command::BorderWaveformControl, &[border_setting])
.await
}
}
impl<HW> Epd<HW> for Epd2in9<HW>
where
HW: EpdHw,
{
type Command = Command;
type RefreshMode = RefreshMode;
type Buffer = BinaryBuffer<
{ binary_buffer_length(Size::new(DISPLAY_WIDTH as u32, DISPLAY_HEIGHT as u32)) },
>;
fn new_buffer(&self) -> Self::Buffer {
BinaryBuffer::new(Size::new(DISPLAY_WIDTH as u32, DISPLAY_HEIGHT as u32))
}
fn width(&self) -> u32 {
DISPLAY_WIDTH as u32
}
fn height(&self) -> u32 {
DISPLAY_HEIGHT as u32
}
async fn init(&mut self, spi: &mut HW::Spi, mode: RefreshMode) -> Result<(), HW::Error> {
self.reset().await?;
self.send(spi, Command::SwReset, &[]).await?;
self.send(spi, Command::DriverOutputControl, &DRIVER_OUTPUT_INIT_DATA)
.await?;
self.send(
spi,
Command::BoosterSoftStartControl,
&BOOSTER_SOFT_START_INIT_DATA,
)
.await?;
self.send(spi, Command::DataEntryModeSetting, &[0b11])
.await?;
self.send(spi, Command::WriteVcom, &[0xA8]).await?;
self.send(spi, Command::SetDummyLinePeriod, &[0x1A]).await?;
self.send(spi, Command::SetGateLineWidth, &[0x08]).await?;
self.send(spi, Command::WriteLut, mode.lut()).await?;
Ok(())
}
async fn set_refresh_mode(
&mut self,
spi: &mut <HW as EpdHw>::Spi,
mode: Self::RefreshMode,
) -> Result<(), <HW as EpdHw>::Error> {
debug!("Changing refresh mode to {:?}", mode);
self.send(spi, Command::WriteLut, mode.lut()).await
}
async fn reset(&mut self) -> Result<(), HW::Error> {
debug!("Resetting EPD");
self.hw.reset().set_low()?;
self.hw.delay().delay_ms(10).await;
self.hw.reset().set_high()?;
self.hw.delay().delay_ms(10).await;
Ok(())
}
async fn sleep(&mut self, spi: &mut HW::Spi) -> Result<(), <HW as EpdHw>::Error> {
debug!("Sleeping EPD");
self.send(spi, Command::DeepSleepMode, &[0x01]).await
}
async fn wake(&mut self, _spi: &mut HW::Spi) -> Result<(), <HW as EpdHw>::Error> {
debug!("Waking EPD");
self.reset().await
}
async fn display_buffer(
&mut self,
spi: &mut HW::Spi,
buffer: &Self::Buffer,
) -> Result<(), <HW as EpdHw>::Error> {
debug!("Displaying buffer");
let buffer_bounds = buffer.bounding_box();
self.set_window(spi, buffer_bounds).await?;
self.set_cursor(spi, buffer_bounds.top_left).await?;
self.write_image(spi, buffer.data()).await?;
self.update_display(spi).await?;
Ok(())
}
async fn set_window(
&mut self,
spi: &mut HW::Spi,
shape: Rectangle,
) -> Result<(), <HW as EpdHw>::Error> {
let x_start = shape.top_left.x;
let x_end = x_start + shape.size.width as i32 - 1;
if x_start % 8 != 0 || x_end % 8 != 7 {
Err(Error::InvalidArgument)?
}
let x_start_byte = ((x_start >> 3) & 0xFF) as u8;
let x_end_byte = ((x_end >> 3) & 0xFF) as u8;
self.send(spi, Command::SetRamXStartEnd, &[x_start_byte, x_end_byte])
.await?;
let (y_start_low, y_start_high) = split_low_and_high(shape.top_left.y as u16);
let (y_end_low, y_end_high) =
split_low_and_high((shape.top_left.y + shape.size.height as i32 - 1) as u16);
self.send(
spi,
Command::SetRamYStartEnd,
&[y_start_low, y_start_high, y_end_low, y_end_high],
)
.await?;
Ok(())
}
async fn set_cursor(
&mut self,
spi: &mut HW::Spi,
position: Point,
) -> Result<(), <HW as EpdHw>::Error> {
if position.x % 8 != 0 {
Err(Error::InvalidArgument)?
}
self.send(spi, Command::SetRamX, &[(position.x >> 3) as u8])
.await?;
let (y_low, y_high) = split_low_and_high(position.y as u16);
self.send(spi, Command::SetRamY, &[y_low, y_high]).await?;
Ok(())
}
async fn update_display(&mut self, spi: &mut HW::Spi) -> Result<(), <HW as EpdHw>::Error> {
self.send(spi, Command::DisplayUpdateControl2, &[0xC4])
.await?;
self.send(spi, Command::MasterActivation, &[]).await?;
self.send(spi, Command::Noop, &[]).await?;
Ok(())
}
async fn write_image(
&mut self,
spi: &mut HW::Spi,
image: &[u8],
) -> Result<(), <HW as EpdHw>::Error> {
self.send(spi, Command::WriteRam, image).await
}
async fn send(
&mut self,
spi: &mut HW::Spi,
command: Command,
data: &[u8],
) -> Result<(), HW::Error> {
trace!("Sending EPD command: {:?}", command);
self.wait_if_busy().await?;
self.hw.cs().set_low()?;
self.hw.dc().set_low()?;
spi.write(&[command.register()]).await?;
if !data.is_empty() {
self.hw.dc().set_high()?;
spi.write(data).await?;
}
spi.flush().await?;
self.hw.cs().set_high()?;
Ok(())
}
async fn wait_if_busy(&mut self) -> Result<(), HW::Error> {
let busy = self.hw.busy();
if busy.is_high().unwrap() {
trace!("Waiting for busy EPD");
busy.wait_for_low().await?;
}
Ok(())
}
}
#[inline(always)]
fn split_low_and_high(value: u16) -> (u8, u8) {
let low = (value & 0xFF) as u8;
let high = ((value >> 8) & 0xFF) as u8;
(low, high)
}