Merge remote-tracking branch 'upstream/master' into epd2in9b

This commit is contained in:
Michael Beaumont
2020-03-27 18:47:13 +01:00
44 changed files with 790 additions and 1968 deletions
+8 -5
View File
@@ -1,5 +1,13 @@
//! B/W Color for EPDs
#[cfg(feature = "graphics")]
use embedded_graphics::pixelcolor::BinaryColor;
#[cfg(feature = "graphics")]
pub use BinaryColor::Off as White;
#[cfg(feature = "graphics")]
pub use BinaryColor::On as Black;
/// Only for the Black/White-Displays
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Color {
@@ -62,11 +70,6 @@ impl Color {
}
}
#[cfg(feature = "graphics")]
use embedded_graphics::prelude::*;
#[cfg(feature = "graphics")]
impl PixelColor for Color {}
impl From<u8> for Color {
fn from(value: u8) -> Self {
Color::from_u8(value)
+24 -30
View File
@@ -1,6 +1,6 @@
use crate::epd1in54::{DEFAULT_BACKGROUND_COLOR, HEIGHT, WIDTH};
use crate::graphics::{Display, DisplayRotation};
use crate::prelude::*;
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::*;
/// Full size buffer for use with the 1in54 EPD
@@ -22,12 +22,15 @@ impl Default for Display1in54 {
}
}
impl Drawing<Color> for Display1in54 {
fn draw<T>(&mut self, item_pixels: T)
where
T: IntoIterator<Item = Pixel<Color>>,
{
self.draw_helper(WIDTH, HEIGHT, item_pixels);
impl DrawTarget<BinaryColor> for Display1in54 {
type Error = core::convert::Infallible;
fn draw_pixel(&mut self, pixel: Pixel<BinaryColor>) -> Result<(), Self::Error> {
self.draw_helper(WIDTH, HEIGHT, pixel)
}
fn size(&self) -> Size {
Size::new(WIDTH, HEIGHT)
}
}
@@ -52,10 +55,9 @@ impl Display for Display1in54 {
#[cfg(test)]
mod tests {
use super::*;
use crate::color::Color;
use crate::color::{Black, Color};
use crate::graphics::{Display, DisplayRotation};
use embedded_graphics::coord::Coord;
use embedded_graphics::primitives::Line;
use embedded_graphics::{primitives::Line, style::PrimitiveStyle};
// test buffer length
#[test]
@@ -76,11 +78,9 @@ mod tests {
#[test]
fn graphics_rotation_0() {
let mut display = Display1in54::default();
display.draw(
Line::new(Coord::new(0, 0), Coord::new(7, 0))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(0, 0), Point::new(7, 0))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
@@ -95,11 +95,9 @@ mod tests {
fn graphics_rotation_90() {
let mut display = Display1in54::default();
display.set_rotation(DisplayRotation::Rotate90);
display.draw(
Line::new(Coord::new(0, 192), Coord::new(0, 199))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(0, 192), Point::new(0, 199))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
@@ -114,11 +112,9 @@ mod tests {
fn graphics_rotation_180() {
let mut display = Display1in54::default();
display.set_rotation(DisplayRotation::Rotate180);
display.draw(
Line::new(Coord::new(192, 199), Coord::new(199, 199))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(192, 199), Point::new(199, 199))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
@@ -136,11 +132,9 @@ mod tests {
fn graphics_rotation_270() {
let mut display = Display1in54::default();
display.set_rotation(DisplayRotation::Rotate270);
display.draw(
Line::new(Coord::new(199, 0), Coord::new(199, 7))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(199, 0), Point::new(199, 7))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
+49 -41
View File
@@ -2,41 +2,51 @@
//!
//! # Example for the 1.54 in E-Ink Display
//!
//! ```rust,ignore
//! use epd_waveshare::{
//! epd1in54::{EPD1in54, Display1in54},
//! graphics::{Display, DisplayRotation},
//! prelude::*,
//! };
//! use embedded_graphics::Drawing;
//!```rust, no_run
//!# use embedded_hal_mock::*;
//!# fn main() -> Result<(), MockError> {
//!use embedded_graphics::{
//! pixelcolor::BinaryColor::On as Black, prelude::*, primitives::Line, style::PrimitiveStyle,
//!};
//!use epd_waveshare::{epd1in54::*, prelude::*};
//!#
//!# let expectations = [];
//!# let mut spi = spi::Mock::new(&expectations);
//!# let expectations = [];
//!# let cs_pin = pin::Mock::new(&expectations);
//!# let busy_in = pin::Mock::new(&expectations);
//!# let dc = pin::Mock::new(&expectations);
//!# let rst = pin::Mock::new(&expectations);
//!# let mut delay = delay::MockNoop::new();
//!
//! // Setup EPD
//! let mut epd = EPD1in54::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay).unwrap();
//!// Setup EPD
//!let mut epd = EPD1in54::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay)?;
//!
//! // Use display graphics
//! let mut display = Display1in54::default();
//!// Use display graphics from embedded-graphics
//!let mut display = Display1in54::default();
//!
//! // Write some hello world in the screenbuffer
//! display.draw(
//! Font6x8::render_str("Hello World!")
//! .stroke(Some(Color::Black))
//! .fill(Some(Color::White))
//! .translate(Coord::new(5, 50))
//! .into_iter(),
//! );
//!// Use embedded graphics for drawing a line
//!let _ = Line::new(Point::new(0, 120), Point::new(0, 295))
//! .into_styled(PrimitiveStyle::with_stroke(Black, 1))
//! .draw(&mut display);
//!
//! // Display updated frame
//! epd.update_frame(&mut spi, &display.buffer()).unwrap();
//! epd.display_frame(&mut spi).expect("display frame new graphics");
//! // Display updated frame
//!epd.update_frame(&mut spi, &display.buffer())?;
//!epd.display_frame(&mut spi)?;
//!
//! // Set the EPD to sleep
//! epd.sleep(&mut spi).expect("sleep");
//! ```
//!// Set the EPD to sleep
//!epd.sleep(&mut spi)?;
//!# Ok(())
//!# }
//!```
/// Width of the display
pub const WIDTH: u32 = 200;
/// Height of the display
pub const HEIGHT: u32 = 200;
//const DPI: u16 = 184;
/// Default Background Color
pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White;
//const DPI: u16 = 184;
const IS_BUSY_LOW: bool = false;
use embedded_hal::{
@@ -179,21 +189,19 @@ where
}
fn sleep(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.wait_until_idle();
// 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode
//TODO: is 0x00 needed here or would 0x01 be even more efficient?
self.interface
.cmd_with_data(spi, Command::DEEP_SLEEP_MODE, &[0x00])?;
self.wait_until_idle();
Ok(())
}
fn update_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.use_full_frame(spi)?;
self.interface
.cmd_with_data(spi, Command::WRITE_RAM, buffer)?;
self.wait_until_idle();
Ok(())
}
@@ -207,17 +215,17 @@ where
width: u32,
height: u32,
) -> Result<(), SPI::Error> {
self.wait_until_idle();
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.wait_until_idle();
Ok(())
}
fn display_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.wait_until_idle();
// 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
@@ -227,12 +235,17 @@ where
// MASTER Activation should not be interupted to avoid currption of panel images
// therefore a terminate command is send
self.interface.cmd(spi, Command::NOP)?;
Ok(())
}
self.wait_until_idle();
fn update_and_display_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.update_frame(spi, buffer)?;
self.display_frame(spi)?;
Ok(())
}
fn clear_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.use_full_frame(spi)?;
// clear the ram with the background color
@@ -241,8 +254,6 @@ where
self.interface.cmd(spi, Command::WRITE_RAM)?;
self.interface
.data_x_times(spi, color, WIDTH / 8 * HEIGHT)?;
self.wait_until_idle();
Ok(())
}
@@ -301,6 +312,7 @@ where
end_x: u32,
end_y: u32,
) -> Result<(), SPI::Error> {
self.wait_until_idle();
assert!(start_x < end_x);
assert!(start_y < end_y);
@@ -323,8 +335,6 @@ where
(end_y >> 8) as u8,
],
)?;
self.wait_until_idle();
Ok(())
}
@@ -334,6 +344,7 @@ where
x: u32,
y: u32,
) -> Result<(), SPI::Error> {
self.wait_until_idle();
// 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
@@ -345,18 +356,15 @@ where
Command::SET_RAM_Y_ADDRESS_COUNTER,
&[y as u8, (y >> 8) as u8],
)?;
self.wait_until_idle();
Ok(())
}
fn set_lut_helper(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.wait_until_idle();
assert!(buffer.len() == 30);
self.interface
.cmd_with_data(spi, Command::WRITE_LUT_REGISTER, buffer)?;
self.wait_until_idle();
Ok(())
}
}
+13 -7
View File
@@ -1,8 +1,11 @@
use crate::epd1in54b::{DEFAULT_BACKGROUND_COLOR, HEIGHT, WIDTH};
use crate::graphics::{Display, DisplayRotation};
use crate::prelude::*;
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::*;
/// Full size buffer for use with the 1in54 EPD
///
/// Can also be manually constructed and be used together with VarDisplay
pub struct Display1in54b {
buffer: [u8; WIDTH as usize * HEIGHT as usize / 8],
rotation: DisplayRotation,
@@ -18,12 +21,15 @@ impl Default for Display1in54b {
}
}
impl Drawing<Color> for Display1in54b {
fn draw<T>(&mut self, item_pixels: T)
where
T: IntoIterator<Item = Pixel<Color>>,
{
self.draw_helper(WIDTH, HEIGHT, item_pixels);
impl DrawTarget<BinaryColor> for Display1in54b {
type Error = core::convert::Infallible;
fn draw_pixel(&mut self, pixel: Pixel<BinaryColor>) -> Result<(), Self::Error> {
self.draw_helper(WIDTH, HEIGHT, pixel)
}
fn size(&self) -> Size {
Size::new(WIDTH, HEIGHT)
}
}
+15 -9
View File
@@ -14,8 +14,11 @@ use crate::traits::{
mod constants;
use crate::epd1in54b::constants::*;
/// Width of epd1in54 in pixels
pub const WIDTH: u32 = 200;
/// Height of epd1in54 in pixels
pub const HEIGHT: u32 = 200;
/// Default Background Color (white)
pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White;
const IS_BUSY_LOW: bool = true;
@@ -100,6 +103,7 @@ where
black: &[u8],
red: &[u8],
) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.send_resolution(spi)?;
self.interface
@@ -113,8 +117,6 @@ where
self.interface
.cmd(spi, Command::DATA_START_TRANSMISSION_2)?;
self.interface.data(spi, red)?;
self.wait_until_idle();
Ok(())
}
}
@@ -147,6 +149,7 @@ where
}
fn sleep(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.interface
.cmd_with_data(spi, Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x17])?; //border floating
@@ -190,6 +193,7 @@ where
}
fn update_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.send_resolution(spi)?;
self.interface
@@ -212,8 +216,6 @@ where
self.interface.data_x_times(spi, color, nbits)?;
//NOTE: Example code has a delay here
self.wait_until_idle();
Ok(())
}
@@ -227,17 +229,23 @@ where
width: u32,
height: u32,
) -> Result<(), SPI::Error> {
Ok(())
unimplemented!()
}
fn display_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.command(spi, Command::DISPLAY_REFRESH)?;
self.wait_until_idle();
self.command(spi, Command::DISPLAY_REFRESH)?;
Ok(())
}
fn update_and_display_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.update_frame(spi, buffer)?;
self.display_frame(spi)?;
Ok(())
}
fn clear_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.send_resolution(spi)?;
let color = DEFAULT_BACKGROUND_COLOR.get_byte_value();
@@ -255,8 +263,6 @@ where
.cmd(spi, Command::DATA_START_TRANSMISSION_2)?;
self.interface
.data_x_times(spi, color, WIDTH * HEIGHT / 8)?;
self.wait_until_idle();
Ok(())
}
+10 -7
View File
@@ -1,6 +1,6 @@
use crate::epd2in9::{DEFAULT_BACKGROUND_COLOR, HEIGHT, WIDTH};
use crate::graphics::{Display, DisplayRotation};
use crate::prelude::*;
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::*;
/// Display with Fullsize buffer for use with the 2in9 EPD
@@ -22,12 +22,15 @@ impl Default for Display2in9 {
}
}
impl Drawing<Color> for Display2in9 {
fn draw<T>(&mut self, item_pixels: T)
where
T: IntoIterator<Item = Pixel<Color>>,
{
self.draw_helper(WIDTH, HEIGHT, item_pixels);
impl DrawTarget<BinaryColor> for Display2in9 {
type Error = core::convert::Infallible;
fn draw_pixel(&mut self, pixel: Pixel<BinaryColor>) -> Result<(), Self::Error> {
self.draw_helper(WIDTH, HEIGHT, pixel)
}
fn size(&self) -> Size {
Size::new(WIDTH, HEIGHT)
}
}
+50 -40
View File
@@ -1,42 +1,51 @@
//! A simple Driver for the Waveshare 2.9" E-Ink Display via SPI
//!
//! Untested!
//!
//! # Example for the 2.9 in E-Ink Display
//!
//! ```rust,ignore
//! use epd_waveshare::{
//! epd2in9::{EPD2in9, Display2in9},
//! graphics::{Display, DisplayRotation},
//! prelude::*,
//! };
//! use embedded_graphics::Drawing;
//!```rust, no_run
//!# use embedded_hal_mock::*;
//!# fn main() -> Result<(), MockError> {
//!use embedded_graphics::{
//! pixelcolor::BinaryColor::On as Black, prelude::*, primitives::Line, style::PrimitiveStyle,
//!};
//!use epd_waveshare::{epd2in9::*, prelude::*};
//!#
//!# let expectations = [];
//!# let mut spi = spi::Mock::new(&expectations);
//!# let expectations = [];
//!# let cs_pin = pin::Mock::new(&expectations);
//!# let busy_in = pin::Mock::new(&expectations);
//!# let dc = pin::Mock::new(&expectations);
//!# let rst = pin::Mock::new(&expectations);
//!# let mut delay = delay::MockNoop::new();
//!
//! // Setup EPD
//! let mut epd = EPD2in9::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay).unwrap();
//!// Setup EPD
//!let mut epd = EPD2in9::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay)?;
//!
//! // Use display graphics
//! let mut display = Display2in9::default();
//!// Use display graphics from embedded-graphics
//!let mut display = Display2in9::default();
//!
//! // Write some hello world in the screenbuffer
//! display.draw(
//! Font6x8::render_str("Hello World!")
//! .stroke(Some(Color::Black))
//! .fill(Some(Color::White))
//! .translate(Coord::new(5, 50))
//! .into_iter(),
//! );
//!// Use embedded graphics for drawing a line
//!let _ = Line::new(Point::new(0, 120), Point::new(0, 295))
//! .into_styled(PrimitiveStyle::with_stroke(Black, 1))
//! .draw(&mut display);
//!
//! // Display updated frame
//! epd.update_frame(&mut spi, &display.buffer()).unwrap();
//! epd.display_frame(&mut spi).expect("display frame new graphics");
//! // Display updated frame
//!epd.update_frame(&mut spi, &display.buffer())?;
//!epd.display_frame(&mut spi)?;
//!
//! // Set the EPD to sleep
//! epd.sleep(&mut spi).expect("sleep");
//! ```
//!// Set the EPD to sleep
//!epd.sleep(&mut spi)?;
//!# Ok(())
//!# }
//!```
/// Width of epd2in9 in pixels
pub const WIDTH: u32 = 128;
/// Height of epd2in9 in pixels
pub const HEIGHT: u32 = 296;
/// Default Background Color (white)
pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White;
const IS_BUSY_LOW: bool = false;
@@ -87,6 +96,8 @@ where
) -> Result<(), SPI::Error> {
self.interface.reset(delay);
self.wait_until_idle();
// 3 Databytes:
// A[7:0]
// 0.. A[8]
@@ -166,12 +177,11 @@ where
}
fn sleep(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.wait_until_idle();
// 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.wait_until_idle();
Ok(())
}
@@ -180,19 +190,17 @@ where
spi: &mut SPI,
delay: &mut DELAY,
) -> Result<(), SPI::Error> {
self.init(spi, delay)?;
self.wait_until_idle();
self.init(spi, delay)?;
Ok(())
}
fn update_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.use_full_frame(spi)?;
self.interface
.cmd_with_data(spi, Command::WRITE_RAM, buffer)?;
self.wait_until_idle();
Ok(())
}
@@ -206,17 +214,17 @@ where
width: u32,
height: u32,
) -> Result<(), SPI::Error> {
self.wait_until_idle();
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.wait_until_idle();
Ok(())
}
fn display_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.wait_until_idle();
// 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
@@ -226,12 +234,17 @@ where
// MASTER Activation should not be interupted to avoid currption of panel images
// therefore a terminate command is send
self.interface.cmd(spi, Command::NOP)?;
Ok(())
}
self.wait_until_idle();
fn update_and_display_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.update_frame(spi, buffer)?;
self.display_frame(spi)?;
Ok(())
}
fn clear_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.use_full_frame(spi)?;
// clear the ram with the background color
@@ -240,8 +253,6 @@ where
self.interface.cmd(spi, Command::WRITE_RAM)?;
self.interface
.data_x_times(spi, color, WIDTH / 8 * HEIGHT)?;
self.wait_until_idle();
Ok(())
}
@@ -325,6 +336,7 @@ where
}
fn set_ram_counter(&mut self, spi: &mut SPI, x: u32, y: u32) -> Result<(), SPI::Error> {
self.wait_until_idle();
// 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
@@ -336,17 +348,15 @@ where
Command::SET_RAM_Y_ADDRESS_COUNTER,
&[y as u8, (y >> 8) as u8],
)?;
self.wait_until_idle();
Ok(())
}
/// Set your own LUT, this function is also used internally for set_lut
fn set_lut_helper(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.wait_until_idle();
assert!(buffer.len() == 30);
self.interface
.cmd_with_data(spi, Command::WRITE_LUT_REGISTER, buffer)?;
self.wait_until_idle();
Ok(())
}
}
+25 -29
View File
@@ -1,6 +1,6 @@
use crate::epd4in2::{DEFAULT_BACKGROUND_COLOR, HEIGHT, WIDTH};
use crate::graphics::{Display, DisplayRotation};
use crate::prelude::*;
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::*;
/// Full size buffer for use with the 4in2 EPD
@@ -22,12 +22,15 @@ impl Default for Display4in2 {
}
}
impl Drawing<Color> for Display4in2 {
fn draw<T>(&mut self, item_pixels: T)
where
T: IntoIterator<Item = Pixel<Color>>,
{
self.draw_helper(WIDTH, HEIGHT, item_pixels);
impl DrawTarget<BinaryColor> for Display4in2 {
type Error = core::convert::Infallible;
fn draw_pixel(&mut self, pixel: Pixel<BinaryColor>) -> Result<(), Self::Error> {
self.draw_helper(WIDTH, HEIGHT, pixel)
}
fn size(&self) -> Size {
Size::new(WIDTH, HEIGHT)
}
}
@@ -52,11 +55,11 @@ impl Display for Display4in2 {
#[cfg(test)]
mod tests {
use super::*;
use crate::color::Black;
use crate::color::Color;
use crate::epd4in2;
use crate::graphics::{Display, DisplayRotation};
use embedded_graphics::coord::Coord;
use embedded_graphics::primitives::Line;
use embedded_graphics::{primitives::Line, style::PrimitiveStyle};
// test buffer length
#[test]
@@ -77,11 +80,9 @@ mod tests {
#[test]
fn graphics_rotation_0() {
let mut display = Display4in2::default();
display.draw(
Line::new(Coord::new(0, 0), Coord::new(7, 0))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(0, 0), Point::new(7, 0))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
@@ -96,11 +97,9 @@ mod tests {
fn graphics_rotation_90() {
let mut display = Display4in2::default();
display.set_rotation(DisplayRotation::Rotate90);
display.draw(
Line::new(Coord::new(0, 392), Coord::new(0, 399))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(0, 392), Point::new(0, 399))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
@@ -115,11 +114,10 @@ mod tests {
fn graphics_rotation_180() {
let mut display = Display4in2::default();
display.set_rotation(DisplayRotation::Rotate180);
display.draw(
Line::new(Coord::new(392, 299), Coord::new(399, 299))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(392, 299), Point::new(399, 299))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
@@ -137,11 +135,9 @@ mod tests {
fn graphics_rotation_270() {
let mut display = Display4in2::default();
display.set_rotation(DisplayRotation::Rotate270);
display.draw(
Line::new(Coord::new(299, 0), Coord::new(299, 7))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(299, 0), Point::new(299, 7))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
+47 -56
View File
@@ -1,46 +1,49 @@
//! A simple Driver for the Waveshare 4.2" E-Ink Display via SPI
//!
//! The other Waveshare E-Ink Displays should be added later on
//!
//! Build with the help of documentation/code from [Waveshare](https://www.waveshare.com/wiki/4.2inch_e-Paper_Module),
//! [Ben Krasnows partial Refresh tips](https://benkrasnow.blogspot.de/2017/10/fast-partial-refresh-on-42-e-paper.html) and
//! the driver documents in the `pdfs`-folder as orientation.
//!
//! This driver was built using [`embedded-hal`] traits.
//!
//! [`embedded-hal`]: https://docs.rs/embedded-hal/~0.1
//!
//! # Requirements
//!
//! ### SPI
//!
//! - MISO is not connected/available
//! - SPI_MODE_0 is used (CPHL = 0, CPOL = 0)
//! - 8 bits per word, MSB first
//! - Max. Speed tested was 8Mhz but more should be possible
//!
//! ### Other....
//!
//! - Buffersize: Wherever a buffer is used it always needs to be of the size: `width / 8 * length`,
//! where width and length being either the full e-ink size or the partial update window size
//!
//! # Examples
//!
//! ```ignore
//! let mut epd4in2 = EPD4in2::new(spi, cs, busy, dc, rst, delay).unwrap();
//!```rust, no_run
//!# use embedded_hal_mock::*;
//!# fn main() -> Result<(), MockError> {
//!use embedded_graphics::{
//! pixelcolor::BinaryColor::On as Black, prelude::*, primitives::Line, style::PrimitiveStyle,
//!};
//!use epd_waveshare::{epd4in2::*, prelude::*};
//!#
//!# let expectations = [];
//!# let mut spi = spi::Mock::new(&expectations);
//!# let expectations = [];
//!# let cs_pin = pin::Mock::new(&expectations);
//!# let busy_in = pin::Mock::new(&expectations);
//!# let dc = pin::Mock::new(&expectations);
//!# let rst = pin::Mock::new(&expectations);
//!# let mut delay = delay::MockNoop::new();
//!
//! let mut buffer = [0u8, epd4in2.get_width() / 8 * epd4in2.get_height()];
//!// Setup EPD
//!let mut epd = EPD4in2::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay)?;
//!
//! // draw something into the buffer
//!// Use display graphics from embedded-graphics
//!let mut display = Display4in2::default();
//!
//! epd4in2.display_and_transfer_buffer(buffer, None);
//!// Use embedded graphics for drawing a line
//!let _ = Line::new(Point::new(0, 120), Point::new(0, 295))
//! .into_styled(PrimitiveStyle::with_stroke(Black, 1))
//! .draw(&mut display);
//!
//! // wait and look at the image
//! // Display updated frame
//!epd.update_frame(&mut spi, &display.buffer())?;
//!epd.display_frame(&mut spi)?;
//!
//! epd4in2.clear_frame(None);
//!
//! epd4in2.sleep();
//! ```
//!// Set the EPD to sleep
//!epd.sleep(&mut spi)?;
//!# Ok(())
//!# }
//!```
//!
//!
//!
@@ -58,8 +61,11 @@ use crate::traits::{InternalWiAdditions, RefreshLUT, WaveshareDisplay};
mod constants;
use crate::epd4in2::constants::*;
/// Width of the display
pub const WIDTH: u32 = 400;
/// Height of the display
pub const HEIGHT: u32 = 300;
/// Default Background Color
pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White;
const IS_BUSY_LOW: bool = true;
@@ -142,21 +148,6 @@ where
DC: OutputPin,
RST: OutputPin,
{
/// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC
///
/// This already initialises the device. That means [init()](init()) isn't needed directly afterwards
///
/// # Example
///
/// ```ignore
/// //buffer = some image data;
///
/// let mut epd4in2 = EPD4in2::new(spi, cs, busy, dc, rst, delay);
///
/// epd4in2.display_and_transfer_frame(buffer, None);
///
/// epd4in2.sleep();
/// ```
fn new<DELAY: DelayMs<u8>>(
spi: &mut SPI,
cs: CS,
@@ -188,6 +179,7 @@ where
}
fn sleep(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.interface
.cmd_with_data(spi, Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x17])?; //border floating
self.command(spi, Command::VCM_DC_SETTING)?; // VCOM to 0V
@@ -202,12 +194,11 @@ where
self.wait_until_idle();
self.interface
.cmd_with_data(spi, Command::DEEP_SLEEP, &[0xA5])?;
self.wait_until_idle();
Ok(())
}
fn update_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.wait_until_idle();
let color_value = self.color.get_byte_value();
self.send_resolution(spi)?;
@@ -226,8 +217,6 @@ where
self.interface
.cmd_with_data(spi, Command::DATA_START_TRANSMISSION_2, buffer)?;
self.wait_until_idle();
Ok(())
}
@@ -240,6 +229,7 @@ where
width: u32,
height: u32,
) -> Result<(), SPI::Error> {
self.wait_until_idle();
if buffer.len() as u32 != width / 8 * height {
//TODO: panic!! or sth like that
//return Err("Wrong buffersize");
@@ -273,19 +263,23 @@ where
self.send_data(spi, buffer)?;
self.command(spi, Command::PARTIAL_OUT)?;
self.wait_until_idle();
Ok(())
}
fn display_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.command(spi, Command::DISPLAY_REFRESH)?;
self.wait_until_idle();
self.command(spi, Command::DISPLAY_REFRESH)?;
Ok(())
}
fn update_and_display_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.update_frame(spi, buffer)?;
self.command(spi, Command::DISPLAY_REFRESH)?;
Ok(())
}
fn clear_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.send_resolution(spi)?;
let color_value = self.color.get_byte_value();
@@ -299,8 +293,6 @@ where
.cmd(spi, Command::DATA_START_TRANSMISSION_2)?;
self.interface
.data_x_times(spi, color_value, WIDTH / 8 * HEIGHT)?;
self.wait_until_idle();
Ok(())
}
@@ -397,6 +389,7 @@ where
lut_wb: &[u8],
lut_bb: &[u8],
) -> Result<(), SPI::Error> {
self.wait_until_idle();
// LUT VCOM
self.cmd_with_data(spi, Command::LUT_FOR_VCOM, lut_vcom)?;
@@ -411,8 +404,6 @@ where
// LUT BLACK to BLACK
self.cmd_with_data(spi, Command::LUT_BLACK_TO_BLACK, lut_bb)?;
self.wait_until_idle();
Ok(())
}
}
+24 -29
View File
@@ -1,6 +1,6 @@
use crate::epd7in5::{DEFAULT_BACKGROUND_COLOR, HEIGHT, WIDTH};
use crate::graphics::{Display, DisplayRotation};
use crate::prelude::*;
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::*;
/// Full size buffer for use with the 7in5 EPD
@@ -22,12 +22,15 @@ impl Default for Display7in5 {
}
}
impl Drawing<Color> for Display7in5 {
fn draw<T>(&mut self, item_pixels: T)
where
T: IntoIterator<Item = Pixel<Color>>,
{
self.draw_helper(WIDTH, HEIGHT, item_pixels);
impl DrawTarget<BinaryColor> for Display7in5 {
type Error = core::convert::Infallible;
fn draw_pixel(&mut self, pixel: Pixel<BinaryColor>) -> Result<(), Self::Error> {
self.draw_helper(WIDTH, HEIGHT, pixel)
}
fn size(&self) -> Size {
Size::new(WIDTH, HEIGHT)
}
}
@@ -52,11 +55,11 @@ impl Display for Display7in5 {
#[cfg(test)]
mod tests {
use super::*;
use crate::color::Black;
use crate::color::Color;
use crate::epd7in5;
use crate::graphics::{Display, DisplayRotation};
use embedded_graphics::coord::Coord;
use embedded_graphics::primitives::Line;
use embedded_graphics::{primitives::Line, style::PrimitiveStyle};
// test buffer length
#[test]
@@ -77,11 +80,9 @@ mod tests {
#[test]
fn graphics_rotation_0() {
let mut display = Display7in5::default();
display.draw(
Line::new(Coord::new(0, 0), Coord::new(7, 0))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(0, 0), Point::new(7, 0))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
@@ -96,11 +97,9 @@ mod tests {
fn graphics_rotation_90() {
let mut display = Display7in5::default();
display.set_rotation(DisplayRotation::Rotate90);
display.draw(
Line::new(Coord::new(0, 632), Coord::new(0, 639))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(0, 632), Point::new(0, 639))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
@@ -115,11 +114,9 @@ mod tests {
fn graphics_rotation_180() {
let mut display = Display7in5::default();
display.set_rotation(DisplayRotation::Rotate180);
display.draw(
Line::new(Coord::new(632, 383), Coord::new(639, 383))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(632, 383), Point::new(639, 383))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
@@ -134,11 +131,9 @@ mod tests {
fn graphics_rotation_270() {
let mut display = Display7in5::default();
display.set_rotation(DisplayRotation::Rotate270);
display.draw(
Line::new(Coord::new(383, 0), Coord::new(383, 7))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(383, 0), Point::new(383, 7))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
+13 -26
View File
@@ -23,8 +23,11 @@ mod graphics;
#[cfg(feature = "graphics")]
pub use self::graphics::Display7in5;
/// Width of the display
pub const WIDTH: u32 = 640;
/// Height of the display
pub const HEIGHT: u32 = 384;
/// Default Background Color
pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White;
const IS_BUSY_LOW: bool = true;
@@ -105,24 +108,6 @@ where
DC: OutputPin,
RST: OutputPin,
{
/// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC
///
/// This already initialises the device. That means [init()] isn't needed
/// directly afterwards.
///
/// [init()]: InternalWiAdditions::init
///
/// # Example
///
/// ```ignore
/// //buffer = some image data;
///
/// let mut epd7in5 = EPD7in5::new(spi, cs, busy, dc, rst, delay);
///
/// epd7in5.display_and_transfer_frame(buffer, None);
///
/// epd7in5.sleep();
/// ```
fn new<DELAY: DelayMs<u8>>(
spi: &mut SPI,
cs: CS,
@@ -150,15 +135,15 @@ where
}
fn sleep(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.command(spi, Command::POWER_OFF)?;
self.wait_until_idle();
self.cmd_with_data(spi, Command::DEEP_SLEEP, &[0xA5])?;
self.wait_until_idle();
Ok(())
}
fn update_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.command(spi, Command::DATA_START_TRANSMISSION_1)?;
for byte in buffer {
let mut temp = *byte;
@@ -171,8 +156,6 @@ where
self.send_data(spi, &[data])?;
}
}
self.wait_until_idle();
Ok(())
}
@@ -189,21 +172,25 @@ where
}
fn display_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.command(spi, Command::DISPLAY_REFRESH)?;
self.wait_until_idle();
self.command(spi, Command::DISPLAY_REFRESH)?;
Ok(())
}
fn update_and_display_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.update_frame(spi, buffer)?;
self.command(spi, Command::DISPLAY_REFRESH)?;
Ok(())
}
fn clear_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.send_resolution(spi)?;
// The Waveshare controllers all implement clear using 0x33
self.command(spi, Command::DATA_START_TRANSMISSION_1)?;
self.interface
.data_x_times(spi, 0x33, WIDTH / 8 * HEIGHT * 4)?;
self.wait_until_idle();
Ok(())
}
+28 -30
View File
@@ -1,6 +1,6 @@
use crate::epd7in5_v2::{DEFAULT_BACKGROUND_COLOR, HEIGHT, WIDTH};
use crate::graphics::{Display, DisplayRotation};
use crate::prelude::*;
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::*;
/// Full size buffer for use with the 7in5 EPD
@@ -22,12 +22,15 @@ impl Default for Display7in5 {
}
}
impl Drawing<Color> for Display7in5 {
fn draw<T>(&mut self, item_pixels: T)
where
T: IntoIterator<Item = Pixel<Color>>,
{
self.draw_helper(WIDTH, HEIGHT, item_pixels);
impl DrawTarget<BinaryColor> for Display7in5 {
type Error = core::convert::Infallible;
fn draw_pixel(&mut self, pixel: Pixel<BinaryColor>) -> Result<(), Self::Error> {
self.draw_helper(WIDTH, HEIGHT, pixel)
}
fn size(&self) -> Size {
Size::new(WIDTH, HEIGHT)
}
}
@@ -52,11 +55,10 @@ impl Display for Display7in5 {
#[cfg(test)]
mod tests {
use super::*;
use crate::color::Color;
use crate::color::{Black, Color};
use crate::epd7in5_v2;
use crate::graphics::{Display, DisplayRotation};
use embedded_graphics::coord::Coord;
use embedded_graphics::primitives::Line;
use embedded_graphics::{primitives::Line, style::PrimitiveStyle};
// test buffer length
#[test]
@@ -77,11 +79,10 @@ mod tests {
#[test]
fn graphics_rotation_0() {
let mut display = Display7in5::default();
display.draw(
Line::new(Coord::new(0, 0), Coord::new(7, 0))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(0, 0), Point::new(7, 0))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
@@ -96,11 +97,10 @@ mod tests {
fn graphics_rotation_90() {
let mut display = Display7in5::default();
display.set_rotation(DisplayRotation::Rotate90);
display.draw(
Line::new(Coord::new(0, 792), Coord::new(0, 799))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(0, 792), Point::new(0, 799))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
@@ -115,11 +115,10 @@ mod tests {
fn graphics_rotation_180() {
let mut display = Display7in5::default();
display.set_rotation(DisplayRotation::Rotate180);
display.draw(
Line::new(Coord::new(792, 479), Coord::new(799, 479))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(792, 479), Point::new(799, 479))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
@@ -134,11 +133,10 @@ mod tests {
fn graphics_rotation_270() {
let mut display = Display7in5::default();
display.set_rotation(DisplayRotation::Rotate270);
display.draw(
Line::new(Coord::new(479, 0), Coord::new(479, 7))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(479, 0), Point::new(479, 7))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
+14 -43
View File
@@ -17,7 +17,7 @@ use embedded_hal::{
use crate::color::Color;
use crate::interface::DisplayInterface;
use crate::traits::{InternalWiAdditions, RefreshLUT, WaveshareDisplay, WaveshareDisplayExt};
use crate::traits::{InternalWiAdditions, RefreshLUT, WaveshareDisplay};
pub(crate) mod command;
use self::command::Command;
@@ -27,8 +27,11 @@ mod graphics;
#[cfg(feature = "graphics")]
pub use self::graphics::Display7in5;
/// Width of the display
pub const WIDTH: u32 = 800;
/// Height of the display
pub const HEIGHT: u32 = 480;
/// Default Background Color
pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White;
const IS_BUSY_LOW: bool = true;
@@ -87,24 +90,6 @@ where
DC: OutputPin,
RST: OutputPin,
{
/// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC
///
/// This already initialises the device. That means [init()] isn't needed
/// directly afterwards.
///
/// [init()]: InternalWiAdditions::init
///
/// # Example
///
/// ```rust,ignore
/// //buffer = some image data;
///
/// let mut epd7in5 = EPD7in5::new(spi, cs, busy, dc, rst, delay);
///
/// epd7in5.update_and_display_frame(&mut spi, &buffer);
///
/// epd7in5.sleep();
/// ```
fn new<DELAY: DelayMs<u8>>(
spi: &mut SPI,
cs: CS,
@@ -132,6 +117,7 @@ where
}
fn sleep(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.command(spi, Command::POWER_OFF)?;
self.wait_until_idle();
self.cmd_with_data(spi, Command::DEEP_SLEEP, &[0xA5])?;
@@ -139,9 +125,8 @@ where
}
fn update_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.command(spi, Command::DATA_START_TRANSMISSION_2)?;
self.send_data(spi, buffer)?;
self.wait_until_idle();
self.cmd_with_data(spi, Command::DATA_START_TRANSMISSION_2, buffer)?;
Ok(())
}
@@ -158,12 +143,19 @@ where
}
fn display_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.command(spi, Command::DISPLAY_REFRESH)?;
self.wait_until_idle();
self.command(spi, Command::DISPLAY_REFRESH)?;
Ok(())
}
fn update_and_display_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.update_frame(spi, buffer)?;
self.command(spi, Command::DISPLAY_REFRESH)?;
Ok(())
}
fn clear_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
self.wait_until_idle();
self.send_resolution(spi)?;
self.command(spi, Command::DATA_START_TRANSMISSION_1)?;
@@ -173,7 +165,6 @@ where
self.interface.data_x_times(spi, 0x00, WIDTH * HEIGHT / 8)?;
self.command(spi, Command::DISPLAY_REFRESH)?;
self.wait_until_idle();
Ok(())
}
@@ -206,26 +197,6 @@ where
}
}
impl<SPI, CS, BUSY, DC, RST> WaveshareDisplayExt<SPI, CS, BUSY, DC, RST>
for EPD7in5<SPI, CS, BUSY, DC, RST>
where
SPI: Write<u8>,
CS: OutputPin,
BUSY: InputPin,
DC: OutputPin,
RST: OutputPin,
{
fn update_and_display_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> {
self.command(spi, Command::DATA_START_TRANSMISSION_2)?;
for b in buffer {
self.send_data(spi, &[255 - b])?;
}
self.command(spi, Command::DISPLAY_REFRESH)?;
self.wait_until_idle();
Ok(())
}
}
impl<SPI, CS, BUSY, DC, RST> EPD7in5<SPI, CS, BUSY, DC, RST>
where
SPI: Write<u8>,
+64 -48
View File
@@ -1,7 +1,7 @@
//! Graphics Support for EPDs
use crate::color::Color;
use embedded_graphics::prelude::*;
use embedded_graphics::{pixelcolor::BinaryColor, prelude::*};
/// Displayrotation
#[derive(Clone, Copy)]
@@ -22,7 +22,13 @@ impl Default for DisplayRotation {
}
}
pub trait Display: Drawing<Color> {
/// Necessary traits for all displays to implement for drawing
///
/// Adds support for:
/// - Drawing (With the help of DrawTarget/Embedded Graphics)
/// - Rotations
/// - Clearing
pub trait Display: DrawTarget<BinaryColor> {
/// Clears the buffer of the display with the chosen background color
fn clear_buffer(&mut self, background_color: Color) {
for elem in self.get_mut_buffer().iter_mut() {
@@ -45,31 +51,36 @@ pub trait Display: Drawing<Color> {
/// Helperfunction for the Embedded Graphics draw trait
///
/// Becomes uneccesary when const_generics become stablised
fn draw_helper<T>(&mut self, width: u32, height: u32, item_pixels: T)
where
T: IntoIterator<Item = Pixel<Color>>,
{
fn draw_helper(
&mut self,
width: u32,
height: u32,
pixel: Pixel<BinaryColor>,
) -> Result<(), Self::Error> {
let rotation = self.rotation();
let buffer = self.get_mut_buffer();
for Pixel(UnsignedCoord(x, y), color) in item_pixels {
if outside_display(x, y, width, height, rotation) {
continue;
let Pixel(point, color) = pixel;
if outside_display(point, width, height, rotation) {
return Ok(());
}
// Give us index inside the buffer and the bit-position in that u8 which needs to be changed
let (index, bit) = find_position(point.x as u32, point.y as u32, width, height, rotation);
let index = index as usize;
// "Draw" the Pixel on that bit
match color {
// Black
BinaryColor::On => {
buffer[index] &= !bit;
}
// Give us index inside the buffer and the bit-position in that u8 which needs to be changed
let (index, bit) = find_position(x, y, width, height, rotation);
let index = index as usize;
// "Draw" the Pixel on that bit
match color {
Color::Black => {
buffer[index] &= !bit;
}
Color::White => {
buffer[index] |= bit;
}
// White
BinaryColor::Off => {
buffer[index] |= bit;
}
}
Ok(())
}
}
@@ -83,8 +94,10 @@ pub trait Display: Drawing<Color> {
/// # use epd_waveshare::epd2in9::DEFAULT_BACKGROUND_COLOR;
/// # use epd_waveshare::prelude::*;
/// # use epd_waveshare::graphics::VarDisplay;
/// # use epd_waveshare::color::Black;
/// # use embedded_graphics::prelude::*;
/// # use embedded_graphics::primitives::{Circle, Line};
/// # use embedded_graphics::style::PrimitiveStyle;
/// let width = 128;
/// let height = 296;
///
@@ -93,11 +106,9 @@ pub trait Display: Drawing<Color> {
///
/// display.set_rotation(DisplayRotation::Rotate90);
///
/// display.draw(
/// Line::new(Coord::new(0, 120), Coord::new(0, 295))
/// .stroke(Some(Color::Black))
/// .into_iter(),
/// );
/// let _ = Line::new(Point::new(0, 120), Point::new(0, 295))
/// .into_styled(PrimitiveStyle::with_stroke(Black, 1))
/// .draw(&mut display);
/// ```
pub struct VarDisplay<'a> {
width: u32,
@@ -107,6 +118,9 @@ pub struct VarDisplay<'a> {
}
impl<'a> VarDisplay<'a> {
/// Create a new variable sized display.
///
/// Buffersize must be at least width / 8 * height bytes.
pub fn new(width: u32, height: u32, buffer: &'a mut [u8]) -> VarDisplay<'a> {
let len = buffer.len() as u32;
assert!(width / 8 * height >= len);
@@ -119,12 +133,15 @@ impl<'a> VarDisplay<'a> {
}
}
impl<'a> Drawing<Color> for VarDisplay<'a> {
fn draw<T>(&mut self, item_pixels: T)
where
T: IntoIterator<Item = Pixel<Color>>,
{
self.draw_helper(self.width, self.height, item_pixels);
impl<'a> DrawTarget<BinaryColor> for VarDisplay<'a> {
type Error = core::convert::Infallible;
fn draw_pixel(&mut self, pixel: Pixel<BinaryColor>) -> Result<(), Self::Error> {
self.draw_helper(self.width, self.height, pixel)
}
fn size(&self) -> Size {
Size::new(self.width, self.height)
}
}
@@ -147,7 +164,11 @@ impl<'a> Display for VarDisplay<'a> {
}
// Checks if a pos is outside the defined display
fn outside_display(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) -> bool {
fn outside_display(p: Point, width: u32, height: u32, rotation: DisplayRotation) -> bool {
if p.x < 0 || p.y < 0 {
return true;
}
let (x, y) = (p.x as u32, p.y as u32);
match rotation {
DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => {
if x >= width || y >= height {
@@ -189,10 +210,9 @@ fn find_position(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotat
#[cfg(test)]
mod tests {
use super::{find_position, outside_display, Display, DisplayRotation, VarDisplay};
use crate::color::Black;
use crate::color::Color;
use embedded_graphics::coord::Coord;
use embedded_graphics::prelude::*;
use embedded_graphics::primitives::Line;
use embedded_graphics::{prelude::*, primitives::Line, style::PrimitiveStyle};
#[test]
fn buffer_clear() {
@@ -228,7 +248,7 @@ mod tests {
for x in 0..(width + height) {
//limit x because it runs too long
for y in 0..(u32::max_value()) {
if outside_display(x, y, width, height, rotation2) {
if outside_display(Point::new(x as i32, y as i32), width, height, rotation2) {
break;
} else {
let (idx, _) = find_position(x, y, width, height, rotation2);
@@ -247,11 +267,9 @@ mod tests {
let mut buffer = [DEFAULT_BACKGROUND_COLOR.get_byte_value(); 128 / 8 * 296];
let mut display = VarDisplay::new(width, height, &mut buffer);
display.draw(
Line::new(Coord::new(0, 0), Coord::new(7, 0))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(0, 0), Point::new(7, 0))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
@@ -273,11 +291,9 @@ mod tests {
display.set_rotation(DisplayRotation::Rotate90);
display.draw(
Line::new(Coord::new(0, 120), Coord::new(0, 295))
.stroke(Some(Color::Black))
.into_iter(),
);
let _ = Line::new(Point::new(0, 120), Point::new(0, 295))
.into_styled(PrimitiveStyle::with_stroke(Black, 1))
.draw(&mut display);
let buffer = display.buffer();
+53 -61
View File
@@ -1,58 +1,66 @@
//! A simple Driver for the Waveshare E-Ink Displays via SPI
//! A simple Driver for the [Waveshare](https://github.com/waveshare/e-Paper) E-Ink Displays via SPI
//!
//! This driver was built using [`embedded-hal`] traits.
//! - Built using [`embedded-hal`] traits.
//! - Graphics support is added through [`embedded-graphics`]
//!
//! [`embedded-hal`]: https://docs.rs/embedded-hal/~0.1
//! [`embedded-graphics`]: https://docs.rs/embedded-graphics/
//! [`embedded-hal`]: https://docs.rs/embedded-hal
//!
//! # Requirements
//!
//! ### SPI
//! # Example
//!
//! - MISO is not connected/available
//! - SPI_MODE_0 is used (CPHL = 0, CPOL = 0)
//! - 8 bits per word, MSB first
//! - Max. Speed tested by myself was 8Mhz but more should be possible (Ben Krasnow used 18Mhz with his implemenation)
//!```rust, no_run
//!# use embedded_hal_mock::*;
//!# fn main() -> Result<(), MockError> {
//!use embedded_graphics::{
//! pixelcolor::BinaryColor::On as Black, prelude::*, primitives::Line, style::PrimitiveStyle,
//!};
//!use epd_waveshare::{epd1in54::*, prelude::*};
//!#
//!# let expectations = [];
//!# let mut spi = spi::Mock::new(&expectations);
//!# let expectations = [];
//!# let cs_pin = pin::Mock::new(&expectations);
//!# let busy_in = pin::Mock::new(&expectations);
//!# let dc = pin::Mock::new(&expectations);
//!# let rst = pin::Mock::new(&expectations);
//!# let mut delay = delay::MockNoop::new();
//!
//! ### Other....
//!// Setup EPD
//!let mut epd = EPD1in54::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay)?;
//!
//!// Use display graphics from embedded-graphics
//!let mut display = Display1in54::default();
//!
//!// Use embedded graphics for drawing a line
//!let _ = Line::new(Point::new(0, 120), Point::new(0, 295))
//! .into_styled(PrimitiveStyle::with_stroke(Black, 1))
//! .draw(&mut display);
//!
//! // Display updated frame
//!epd.update_frame(&mut spi, &display.buffer())?;
//!epd.display_frame(&mut spi)?;
//!
//!// Set the EPD to sleep
//!epd.sleep(&mut spi)?;
//!# Ok(())
//!# }
//!```
//!
//! # Other information and requirements
//!
//! - Buffersize: Wherever a buffer is used it always needs to be of the size: `width / 8 * length`,
//! where width and length being either the full e-ink size or the partial update window size
//!
//! # Examples
//! ### SPI
//!
//! ```rust,ignore
//! use epd_waveshare::{
//! epd2in9::{EPD2in9, Display2in9},
//! graphics::{Display, DisplayRotation},
//! prelude::*,
//! };
//! use embedded_graphics::Drawing;
//!
//! // Setup EPD
//! let mut epd = EPD2in9::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay).unwrap();
//!
//! // Use display graphics
//! let mut display = Display2in9::default();
//!
//! // Write some hello world in the screenbuffer
//! display.draw(
//! Font6x8::render_str("Hello World!")
//! .stroke(Some(Color::Black))
//! .fill(Some(Color::White))
//! .translate(Coord::new(5, 50))
//! .into_iter(),
//! );
//!
//! // 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");
//! ```
//! MISO is not connected/available. SPI_MODE_0 is used (CPHL = 0, CPOL = 0) with 8 bits per word, MSB first.
//!
//! Maximum speed tested by myself was 8Mhz but more should be possible (Ben Krasnow used 18Mhz with his implemenation)
//!
#![no_std]
#![deny(missing_docs)]
#[cfg(feature = "graphics")]
pub mod graphics;
@@ -64,36 +72,20 @@ pub mod color;
/// Interface for the physical connection between display and the controlling device
mod interface;
#[cfg(feature = "epd7in5")]
pub mod epd7in5;
#[cfg(feature = "epd7in5_v2")]
pub mod epd7in5_v2;
#[cfg(feature = "epd4in2")]
pub mod epd4in2;
#[cfg(feature = "epd1in54")]
pub mod epd1in54;
#[cfg(feature = "epd1in54b")]
pub mod epd1in54b;
#[cfg(feature = "epd2in9")]
pub mod epd2in9;
#[cfg(feature = "epd2in9b")]
pub mod epd2in9b;
#[cfg(any(feature = "epd1in54", feature = "epd2in9"))]
pub mod epd4in2;
pub mod epd7in5;
pub mod epd7in5_v2;
pub(crate) mod type_a;
/// Includes everything important besides the chosen Display
pub mod prelude {
pub use crate::color::{Color, TriColor};
pub use crate::traits::{RefreshLUT, WaveshareDisplay, WaveshareThreeColorDisplay};
#[cfg(feature = "epd7in5_v2")]
pub use crate::traits::WaveshareDisplayExt;
pub use crate::SPI_MODE;
#[cfg(feature = "graphics")]
+51 -21
View File
@@ -38,11 +38,11 @@ where
/// This initialises the EPD and powers it up
///
/// This function is already called from
/// - [new()](WaveshareInterface::new())
/// - [new()](WaveshareDisplay::new())
/// - [`wake_up`]
///
///
/// This function calls [reset()](WaveshareInterface::reset()),
/// This function calls [reset](WaveshareDisplay::reset),
/// so you don't need to call reset your self when trying to wake your device up
/// after setting it to sleep.
fn init<DELAY: DelayMs<u8>>(
@@ -75,7 +75,47 @@ where
/// All the functions to interact with the EPDs
///
/// This trait includes all public functions to use the EPDS
/// This trait includes all public functions to use the EPDs
///
/// # Example
///
///```rust, no_run
///# use embedded_hal_mock::*;
///# fn main() -> Result<(), MockError> {
///use embedded_graphics::{
/// pixelcolor::BinaryColor::On as Black, prelude::*, primitives::Line, style::PrimitiveStyle,
///};
///use epd_waveshare::{epd4in2::*, prelude::*};
///#
///# let expectations = [];
///# let mut spi = spi::Mock::new(&expectations);
///# let expectations = [];
///# let cs_pin = pin::Mock::new(&expectations);
///# let busy_in = pin::Mock::new(&expectations);
///# let dc = pin::Mock::new(&expectations);
///# let rst = pin::Mock::new(&expectations);
///# let mut delay = delay::MockNoop::new();
///
///// Setup EPD
///let mut epd = EPD4in2::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay)?;
///
///// Use display graphics from embedded-graphics
///let mut display = Display4in2::default();
///
///// Use embedded graphics for drawing a line
///let _ = Line::new(Point::new(0, 120), Point::new(0, 295))
/// .into_styled(PrimitiveStyle::with_stroke(Black, 1))
/// .draw(&mut display);
///
/// // Display updated frame
///epd.update_frame(&mut spi, &display.buffer())?;
///epd.display_frame(&mut spi)?;
///
///// Set the EPD to sleep
///epd.sleep(&mut spi)?;
///# Ok(())
///# }
///```
pub trait WaveshareDisplay<SPI, CS, BUSY, DC, RST>
where
SPI: Write<u8>,
@@ -86,7 +126,7 @@ where
{
/// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC
///
/// This already initialises the device. That means [init()](WaveshareInterface::init()) isn't needed directly afterwards
/// This already initialises the device.
fn new<DELAY: DelayMs<u8>>(
spi: &mut SPI,
cs: CS,
@@ -101,19 +141,18 @@ where
/// Let the device enter deep-sleep mode to save power.
///
/// The deep sleep mode returns to standby with a hardware reset.
/// But you can also use [wake_up()](WaveshareInterface::wake_up()) to awaken.
/// But as you need to power it up once more anyway you can also just directly use [new()](WaveshareInterface::new()) for resetting
/// and initialising which already contains the reset
fn sleep(&mut self, spi: &mut SPI) -> Result<(), SPI::Error>;
/// Wakes the device up from sleep
///
/// Also reintialises the device if necessary.
fn wake_up<DELAY: DelayMs<u8>>(
&mut self,
spi: &mut SPI,
delay: &mut DELAY,
) -> Result<(), SPI::Error>;
/// Sets the backgroundcolor for various commands like [clear_frame()](WaveshareInterface::clear_frame())
/// Sets the backgroundcolor for various commands like [clear_frame](WaveshareDisplay::clear_frame)
fn set_background_color(&mut self, color: Color);
/// Get current background color
@@ -148,9 +187,12 @@ where
/// This function waits until the device isn`t busy anymore
fn display_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error>;
/// Provide a combined update&display and save some time (skipping a busy check in between)
fn update_and_display_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error>;
/// Clears the frame buffer on the EPD with the declared background color
///
/// The background color can be changed with [`set_background_color`]
/// The background color can be changed with [`WaveshareDisplay::set_background_color`]
fn clear_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error>;
/// Trait for using various Waveforms from different LUTs
@@ -174,15 +216,3 @@ where
/// if the device is still busy
fn is_busy(&self) -> bool;
}
/// Tiny optional extension trait
pub trait WaveshareDisplayExt<SPI, CS, BUSY, DC, RST>
where
SPI: Write<u8>,
CS: OutputPin,
BUSY: InputPin,
DC: OutputPin,
RST: OutputPin,
{
// provide a combined update&display and save some time (skipping a busy check in between)
fn update_and_display_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error>;
}
+15 -15
View File
@@ -12,28 +12,28 @@ use crate::traits;
#[derive(Copy, Clone)]
pub(crate) enum Command {
/// Driver Output control
/// 3 Databytes:
/// A[7:0]
/// 0.. A[8]
/// 0.. B[2:0]
/// Default: Set A[8:0] = 0x127 and B[2:0] = 0x0
/// 3 Databytes:
/// A[7:0]
/// 0.. A[8]
/// 0.. B[2:0]
/// Default: Set A[8:0] = 0x127 and B[2:0] = 0x0
DRIVER_OUTPUT_CONTROL = 0x01,
/// Booster Soft start control
/// 3 Databytes:
/// 1.. A[6:0]
/// 1.. B[6:0]
/// 1.. C[6:0]
/// Default: A[7:0] = 0xCF, B[7:0] = 0xCE, C[7:0] = 0x8D
/// 3 Databytes:
/// 1.. A[6:0]
/// 1.. B[6:0]
/// 1.. C[6:0]
/// Default: A[7:0] = 0xCF, B[7:0] = 0xCE, C[7:0] = 0x8D
BOOSTER_SOFT_START_CONTROL = 0x0C,
GATE_SCAN_START_POSITION = 0x0F,
//TODO: useful?
// GATE_SCAN_START_POSITION = 0x0F,
/// Deep Sleep Mode Control
/// 1 Databyte:
/// 0.. A[0]
/// Values:
/// A[0] = 0: Normal Mode (POR)
/// A[0] = 1: Enter Deep Sleep Mode
/// 1 Databyte:
/// 0.. A[0]
/// Values:
/// A[0] = 0: Normal Mode (POR)
/// A[0] = 1: Enter Deep Sleep Mode
DEEP_SLEEP_MODE = 0x10,
// /// Data Entry mode setting
DATA_ENTRY_MODE_SETTING = 0x11,