mirror of
https://github.com/Anshorei/epd-waveshare.git
synced 2026-10-03 22:06:43 +00:00
Run cargo fmt (reverted for constants and some graphic calculations)
This commit is contained in:
+7
-10
@@ -1,9 +1,9 @@
|
||||
use epd2in9::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT};
|
||||
use epd2in9::{DEFAULT_BACKGROUND_COLOR, HEIGHT, WIDTH};
|
||||
|
||||
/// Full size buffer for use with the 2in9 EPD
|
||||
///
|
||||
///
|
||||
/// Can also be manuall constructed:
|
||||
/// `buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value(); WIDTH / 8 * HEIGHT]`
|
||||
/// `buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value(); WIDTH / 8 * HEIGHT]`
|
||||
pub struct Buffer2in9 {
|
||||
pub buffer: [u8; WIDTH as usize * HEIGHT as usize / 8],
|
||||
}
|
||||
@@ -11,15 +11,12 @@ pub struct Buffer2in9 {
|
||||
impl Default for Buffer2in9 {
|
||||
fn default() -> Self {
|
||||
Buffer2in9 {
|
||||
buffer: [
|
||||
DEFAULT_BACKGROUND_COLOR.get_byte_value();
|
||||
WIDTH as usize * HEIGHT as usize / 8
|
||||
]
|
||||
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value();
|
||||
WIDTH as usize * HEIGHT as usize / 8],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -32,7 +29,7 @@ mod tests {
|
||||
let display = Display::new(WIDTH, HEIGHT, &mut buffer.buffer);
|
||||
assert_eq!(display.buffer().len(), 4736);
|
||||
}
|
||||
|
||||
|
||||
// test default background color on all bytes
|
||||
#[test]
|
||||
fn graphics_default() {
|
||||
@@ -42,4 +39,4 @@ mod tests {
|
||||
assert_eq!(byte, DEFAULT_BACKGROUND_COLOR.get_byte_value());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+77
-42
@@ -3,21 +3,21 @@
|
||||
//! Untested!
|
||||
//!
|
||||
//! # Example for the 2.9 in E-Ink Display
|
||||
//!
|
||||
//!
|
||||
//! ```ignore
|
||||
//! use eink_waveshare_rs::{
|
||||
//! epd2in9::{EPD2in9, Buffer2in9},
|
||||
//! graphics::{Display, DisplayRotation},
|
||||
//! prelude::*,
|
||||
//! };
|
||||
//!
|
||||
//! };
|
||||
//!
|
||||
//! // Setup EPD
|
||||
//! let mut epd = EPD2in9::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay)?;
|
||||
//!
|
||||
//! // Use display graphics
|
||||
//! let mut buffer = Buffer2in9::default();
|
||||
//! let mut display = Display::new(epd.width(), epd.height(), &mut buffer.buffer);
|
||||
//!
|
||||
//!
|
||||
//! // Write some hello world in the screenbuffer
|
||||
//! display.draw(
|
||||
//! Font6x8::render_str("Hello World!")
|
||||
@@ -30,7 +30,7 @@
|
||||
//! // Display updated frame
|
||||
//! epd.update_frame(&mut spi, &display.buffer()).unwrap();
|
||||
//! epd.display_frame(&mut spi).expect("display frame new graphics");
|
||||
//!
|
||||
//!
|
||||
//! // Set the EPD to sleep
|
||||
//! epd.sleep(&mut spi).expect("sleep");
|
||||
//! ```
|
||||
@@ -45,8 +45,8 @@ use hal::{
|
||||
};
|
||||
|
||||
use type_a::{
|
||||
command::Command,
|
||||
constants::{LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE}
|
||||
command::Command,
|
||||
constants::{LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE},
|
||||
};
|
||||
|
||||
use color::Color;
|
||||
@@ -77,7 +77,11 @@ where
|
||||
DC: OutputPin,
|
||||
RST: OutputPin,
|
||||
{
|
||||
fn init<DELAY: DelayMs<u8>>(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
|
||||
fn init<DELAY: DelayMs<u8>>(
|
||||
&mut self,
|
||||
spi: &mut SPI,
|
||||
delay: &mut DELAY,
|
||||
) -> Result<(), SPI::Error> {
|
||||
self.interface.reset(delay);
|
||||
|
||||
// 3 Databytes:
|
||||
@@ -85,34 +89,42 @@ where
|
||||
// 0.. A[8]
|
||||
// 0.. B[2:0]
|
||||
// Default Values: A = Height of Screen (0x127), B = 0x00 (GD, SM and TB=0?)
|
||||
self.interface.cmd_with_data(spi, Command::DRIVER_OUTPUT_CONTROL, &[0x27, 0x01, 0x00])?;
|
||||
self.interface
|
||||
.cmd_with_data(spi, Command::DRIVER_OUTPUT_CONTROL, &[0x27, 0x01, 0x00])?;
|
||||
|
||||
// 3 Databytes: (and default values from datasheet and arduino)
|
||||
// 1 .. A[6:0] = 0xCF | 0xD7
|
||||
// 1 .. B[6:0] = 0xCE | 0xD6
|
||||
// 1 .. C[6:0] = 0x8D | 0x9D
|
||||
//TODO: test
|
||||
self.interface.cmd_with_data(spi, Command::BOOSTER_SOFT_START_CONTROL, &[0xD7, 0xD6, 0x9D])?;
|
||||
self.interface.cmd_with_data(
|
||||
spi,
|
||||
Command::BOOSTER_SOFT_START_CONTROL,
|
||||
&[0xD7, 0xD6, 0x9D],
|
||||
)?;
|
||||
|
||||
// One Databyte with value 0xA8 for 7V VCOM
|
||||
self.interface.cmd_with_data(spi, Command::WRITE_VCOM_REGISTER, &[0xA8])?;
|
||||
self.interface
|
||||
.cmd_with_data(spi, Command::WRITE_VCOM_REGISTER, &[0xA8])?;
|
||||
|
||||
// One Databyte with default value 0x1A for 4 dummy lines per gate
|
||||
self.interface.cmd_with_data(spi, Command::SET_DUMMY_LINE_PERIOD, &[0x1A])?;
|
||||
self.interface
|
||||
.cmd_with_data(spi, Command::SET_DUMMY_LINE_PERIOD, &[0x1A])?;
|
||||
|
||||
// One Databyte with default value 0x08 for 2us per line
|
||||
self.interface.cmd_with_data(spi, Command::SET_GATE_LINE_WIDTH, &[0x08])?;
|
||||
self.interface
|
||||
.cmd_with_data(spi, Command::SET_GATE_LINE_WIDTH, &[0x08])?;
|
||||
|
||||
// One Databyte with default value 0x03
|
||||
// -> address: x increment, y increment, address counter is updated in x direction
|
||||
self.interface.cmd_with_data(spi, Command::DATA_ENTRY_MODE_SETTING, &[0x03])?;
|
||||
self.interface
|
||||
.cmd_with_data(spi, Command::DATA_ENTRY_MODE_SETTING, &[0x03])?;
|
||||
|
||||
self.set_lut(spi, None)
|
||||
}
|
||||
}
|
||||
|
||||
impl<SPI, CS, BUSY, DC, RST>
|
||||
WaveshareDisplay<SPI, CS, BUSY, DC, RST>
|
||||
impl<SPI, CS, BUSY, DC, RST> WaveshareDisplay<SPI, CS, BUSY, DC, RST>
|
||||
for EPD2in9<SPI, CS, BUSY, DC, RST>
|
||||
where
|
||||
SPI: Write<u8>,
|
||||
@@ -130,7 +142,12 @@ where
|
||||
}
|
||||
|
||||
fn new<DELAY: DelayMs<u8>>(
|
||||
spi: &mut SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY,
|
||||
spi: &mut SPI,
|
||||
cs: CS,
|
||||
busy: BUSY,
|
||||
dc: DC,
|
||||
rst: RST,
|
||||
delay: &mut DELAY,
|
||||
) -> Result<Self, SPI::Error> {
|
||||
let interface = DisplayInterface::new(cs, busy, dc, rst);
|
||||
|
||||
@@ -145,30 +162,34 @@ where
|
||||
Ok(epd)
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn sleep(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
|
||||
// 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode
|
||||
//TODO: is 0x00 needed here? (see also epd1in54)
|
||||
self.interface.cmd_with_data(spi, Command::DEEP_SLEEP_MODE, &[0x00])?;
|
||||
self.interface
|
||||
.cmd_with_data(spi, Command::DEEP_SLEEP_MODE, &[0x00])?;
|
||||
|
||||
self.wait_until_idle();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn wake_up<DELAY: DelayMs<u8>>(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
|
||||
fn wake_up<DELAY: DelayMs<u8>>(
|
||||
&mut self,
|
||||
spi: &mut SPI,
|
||||
delay: &mut DELAY,
|
||||
) -> Result<(), SPI::Error> {
|
||||
self.init(spi, delay)
|
||||
}
|
||||
|
||||
fn update_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
|
||||
self.use_full_frame(spi)?;
|
||||
|
||||
self.interface.cmd_with_data(spi, Command::WRITE_RAM, buffer)
|
||||
self.interface
|
||||
.cmd_with_data(spi, Command::WRITE_RAM, buffer)
|
||||
}
|
||||
|
||||
//TODO: update description: last 3 bits will be ignored for width and x_pos
|
||||
fn update_partial_frame(
|
||||
&mut self,
|
||||
&mut self,
|
||||
spi: &mut SPI,
|
||||
buffer: &[u8],
|
||||
x: u32,
|
||||
@@ -179,13 +200,15 @@ where
|
||||
self.set_ram_area(spi, x, y, x + width, y + height)?;
|
||||
self.set_ram_counter(spi, x, y)?;
|
||||
|
||||
self.interface.cmd_with_data(spi, Command::WRITE_RAM, buffer)
|
||||
self.interface
|
||||
.cmd_with_data(spi, Command::WRITE_RAM, buffer)
|
||||
}
|
||||
|
||||
fn display_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
|
||||
// enable clock signal, enable cp, display pattern -> 0xC4 (tested with the arduino version)
|
||||
//TODO: test control_1 or control_2 with default value 0xFF (from the datasheet)
|
||||
self.interface.cmd_with_data(spi, Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?;
|
||||
self.interface
|
||||
.cmd_with_data(spi, Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?;
|
||||
|
||||
self.interface.cmd(spi, Command::MASTER_ACTIVATION)?;
|
||||
// MASTER Activation should not be interupted to avoid currption of panel images
|
||||
@@ -212,17 +235,17 @@ where
|
||||
&self.background_color
|
||||
}
|
||||
|
||||
fn set_lut(&mut self, spi: &mut SPI, refresh_rate: Option<RefreshLUT>) -> Result<(), SPI::Error> {
|
||||
fn set_lut(
|
||||
&mut self,
|
||||
spi: &mut SPI,
|
||||
refresh_rate: Option<RefreshLUT>,
|
||||
) -> Result<(), SPI::Error> {
|
||||
if let Some(refresh_lut) = refresh_rate {
|
||||
self.refresh = refresh_lut;
|
||||
}
|
||||
match self.refresh {
|
||||
RefreshLUT::FULL => {
|
||||
self.set_lut_helper(spi, &LUT_FULL_UPDATE)
|
||||
},
|
||||
RefreshLUT::QUICK => {
|
||||
self.set_lut_helper(spi, &LUT_PARTIAL_UPDATE)
|
||||
}
|
||||
RefreshLUT::FULL => self.set_lut_helper(spi, &LUT_FULL_UPDATE),
|
||||
RefreshLUT::QUICK => self.set_lut_helper(spi, &LUT_PARTIAL_UPDATE),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -248,7 +271,7 @@ where
|
||||
}
|
||||
|
||||
fn set_ram_area(
|
||||
&mut self,
|
||||
&mut self,
|
||||
spi: &mut SPI,
|
||||
start_x: u32,
|
||||
start_y: u32,
|
||||
@@ -262,23 +285,35 @@ where
|
||||
// aren't relevant
|
||||
self.interface.cmd_with_data(
|
||||
spi,
|
||||
Command::SET_RAM_X_ADDRESS_START_END_POSITION,
|
||||
&[(start_x >> 3) as u8, (end_x >> 3) as u8]
|
||||
Command::SET_RAM_X_ADDRESS_START_END_POSITION,
|
||||
&[(start_x >> 3) as u8, (end_x >> 3) as u8],
|
||||
)?;
|
||||
|
||||
// 2 Databytes: A[7:0] & 0..A[8] for each - start and end
|
||||
self.interface.cmd_with_data(spi, Command::SET_RAM_Y_ADDRESS_START_END_POSITION,
|
||||
&[start_y as u8, (start_y >> 8) as u8, end_y as u8, (end_y >> 8) as u8]
|
||||
self.interface.cmd_with_data(
|
||||
spi,
|
||||
Command::SET_RAM_Y_ADDRESS_START_END_POSITION,
|
||||
&[
|
||||
start_y as u8,
|
||||
(start_y >> 8) as u8,
|
||||
end_y as u8,
|
||||
(end_y >> 8) as u8,
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
fn set_ram_counter(&mut self, spi: &mut SPI, x: u32, y: u32) -> Result<(), SPI::Error> {
|
||||
// x is positioned in bytes, so the last 3 bits which show the position inside a byte in the ram
|
||||
// aren't relevant
|
||||
self.interface.cmd_with_data(spi, Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?;
|
||||
self.interface
|
||||
.cmd_with_data(spi, Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?;
|
||||
|
||||
// 2 Databytes: A[7:0] & 0..A[8]
|
||||
self.interface.cmd_with_data(spi, Command::SET_RAM_Y_ADDRESS_COUNTER, &[y as u8, (y >> 8) as u8])?;
|
||||
self.interface.cmd_with_data(
|
||||
spi,
|
||||
Command::SET_RAM_Y_ADDRESS_COUNTER,
|
||||
&[y as u8, (y >> 8) as u8],
|
||||
)?;
|
||||
|
||||
self.wait_until_idle();
|
||||
Ok(())
|
||||
@@ -286,11 +321,11 @@ where
|
||||
|
||||
fn set_lut_helper(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
|
||||
assert!(buffer.len() == 30);
|
||||
self.interface.cmd_with_data(spi, Command::WRITE_LUT_REGISTER, buffer)
|
||||
self.interface
|
||||
.cmd_with_data(spi, Command::WRITE_LUT_REGISTER, buffer)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -301,4 +336,4 @@ mod tests {
|
||||
assert_eq!(HEIGHT, 296);
|
||||
assert_eq!(DEFAULT_BACKGROUND_COLOR, Color::White);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user