mirror of
https://github.com/Anshorei/epd-waveshare.git
synced 2026-10-03 22:06:43 +00:00
Various improvements (#25)
- Added is_busy to Waveshare_Interface - Added IS_BUSY_LOW const for all supported epds - Added is_busy to DisplayInterface - moved width, height and default_background_color directly to epd4in2 module - Added VarDisplay (a variable buffersize display/graphic driver) - Removed all Buffers (Buffer1in54,...) and instead made specialised Displays (Display1in54,...) with included Buffers - Updated and added more examples - Cargo fmt/clippy - Improved Docs/Readmes
This commit is contained in:
+42
-17
@@ -1,44 +1,73 @@
|
||||
use crate::epd1in54::{DEFAULT_BACKGROUND_COLOR, HEIGHT, WIDTH};
|
||||
use crate::graphics::{Display, DisplayRotation};
|
||||
use crate::prelude::*;
|
||||
use embedded_graphics::prelude::*;
|
||||
|
||||
/// Full size buffer for use with the 1in54 EPD
|
||||
///
|
||||
/// Can also be manuall constructed:
|
||||
/// `buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value(); WIDTH / 8 * HEIGHT]`
|
||||
pub struct Buffer1in54BlackWhite {
|
||||
pub buffer: [u8; WIDTH as usize * HEIGHT as usize / 8],
|
||||
pub struct Display1in54 {
|
||||
buffer: [u8; WIDTH as usize * HEIGHT as usize / 8],
|
||||
rotation: DisplayRotation,
|
||||
}
|
||||
|
||||
impl Default for Buffer1in54BlackWhite {
|
||||
impl Default for Display1in54 {
|
||||
fn default() -> Self {
|
||||
Buffer1in54BlackWhite {
|
||||
Display1in54 {
|
||||
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value();
|
||||
WIDTH as usize * HEIGHT as usize / 8],
|
||||
rotation: DisplayRotation::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drawing<Color> for Display1in54 {
|
||||
fn draw<T>(&mut self, item_pixels: T)
|
||||
where
|
||||
T: Iterator<Item = Pixel<Color>>,
|
||||
{
|
||||
self.draw_helper(WIDTH, HEIGHT, item_pixels);
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Display1in54 {
|
||||
fn buffer(&self) -> &[u8] {
|
||||
&self.buffer
|
||||
}
|
||||
|
||||
fn get_mut_buffer(&mut self) -> &mut [u8] {
|
||||
&mut self.buffer
|
||||
}
|
||||
|
||||
fn set_rotation(&mut self, rotation: DisplayRotation) {
|
||||
self.rotation = rotation;
|
||||
}
|
||||
|
||||
fn rotation(&self) -> DisplayRotation {
|
||||
self.rotation
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::color::Color;
|
||||
use crate::graphics::{Display, DisplayRotation};
|
||||
use embedded_graphics::coord::Coord;
|
||||
use embedded_graphics::prelude::*;
|
||||
use embedded_graphics::primitives::Line;
|
||||
|
||||
// test buffer length
|
||||
#[test]
|
||||
fn graphics_size() {
|
||||
let mut display1in54 = Buffer1in54BlackWhite::default();
|
||||
let display = Display::new(WIDTH, HEIGHT, &mut display1in54.buffer);
|
||||
let display = Display1in54::default();
|
||||
assert_eq!(display.buffer().len(), 5000);
|
||||
}
|
||||
|
||||
// test default background color on all bytes
|
||||
#[test]
|
||||
fn graphics_default() {
|
||||
let mut display1in54 = Buffer1in54BlackWhite::default();
|
||||
let display = Display::new(WIDTH, HEIGHT, &mut display1in54.buffer);
|
||||
let display = Display1in54::default();
|
||||
for &byte in display.buffer() {
|
||||
assert_eq!(byte, DEFAULT_BACKGROUND_COLOR.get_byte_value());
|
||||
}
|
||||
@@ -46,8 +75,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn graphics_rotation_0() {
|
||||
let mut display1in54 = Buffer1in54BlackWhite::default();
|
||||
let mut display = Display::new(WIDTH, HEIGHT, &mut display1in54.buffer);
|
||||
let mut display = Display1in54::default();
|
||||
display.draw(
|
||||
Line::new(Coord::new(0, 0), Coord::new(7, 0))
|
||||
.with_stroke(Some(Color::Black))
|
||||
@@ -65,8 +93,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn graphics_rotation_90() {
|
||||
let mut display1in54 = Buffer1in54BlackWhite::default();
|
||||
let mut display = Display::new(WIDTH, HEIGHT, &mut display1in54.buffer);
|
||||
let mut display = Display1in54::default();
|
||||
display.set_rotation(DisplayRotation::Rotate90);
|
||||
display.draw(
|
||||
Line::new(Coord::new(0, 192), Coord::new(0, 199))
|
||||
@@ -85,8 +112,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn graphics_rotation_180() {
|
||||
let mut display1in54 = Buffer1in54BlackWhite::default();
|
||||
let mut display = Display::new(WIDTH, HEIGHT, &mut display1in54.buffer);
|
||||
let mut display = Display1in54::default();
|
||||
display.set_rotation(DisplayRotation::Rotate180);
|
||||
display.draw(
|
||||
Line::new(Coord::new(192, 199), Coord::new(199, 199))
|
||||
@@ -108,8 +134,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn graphics_rotation_270() {
|
||||
let mut display1in54 = Buffer1in54BlackWhite::default();
|
||||
let mut display = Display::new(WIDTH, HEIGHT, &mut display1in54.buffer);
|
||||
let mut display = Display1in54::default();
|
||||
display.set_rotation(DisplayRotation::Rotate270);
|
||||
display.draw(
|
||||
Line::new(Coord::new(199, 0), Coord::new(199, 7))
|
||||
|
||||
+14
-7
@@ -2,19 +2,19 @@
|
||||
//!
|
||||
//! # Example for the 1.54 in E-Ink Display
|
||||
//!
|
||||
//! ```ignore
|
||||
//! ```rust,ignore
|
||||
//! use epd_waveshare::{
|
||||
//! epd1in54::{EPD1in54, Buffer1in54},
|
||||
//! epd1in54::{EPD1in54, Display1in54},
|
||||
//! graphics::{Display, DisplayRotation},
|
||||
//! prelude::*,
|
||||
//! };
|
||||
//! use embedded_graphics::Drawing;
|
||||
//!
|
||||
//! // Setup EPD
|
||||
//! let mut epd = EPD1in54::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay)?;
|
||||
//! let mut epd = EPD1in54::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay).unwrap();
|
||||
//!
|
||||
//! // Use display graphics
|
||||
//! let mut buffer = Buffer1in54::default();
|
||||
//! let mut display = Display::new(epd.width(), epd.height(), &mut buffer.buffer);
|
||||
//! let mut display = Display1in54::default();
|
||||
//!
|
||||
//! // Write some hello world in the screenbuffer
|
||||
//! display.draw(
|
||||
@@ -37,6 +37,7 @@ pub const WIDTH: u32 = 200;
|
||||
pub const HEIGHT: u32 = 200;
|
||||
//const DPI: u16 = 184;
|
||||
pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White;
|
||||
const IS_BUSY_LOW: bool = false;
|
||||
|
||||
use embedded_hal::{
|
||||
blocking::{delay::*, spi::Write},
|
||||
@@ -54,8 +55,10 @@ use crate::traits::{RefreshLUT, WaveshareDisplay};
|
||||
|
||||
use crate::interface::DisplayInterface;
|
||||
|
||||
#[cfg(feature = "graphics")]
|
||||
mod graphics;
|
||||
pub use crate::epd1in54::graphics::Buffer1in54BlackWhite as Buffer1in54;
|
||||
#[cfg(feature = "graphics")]
|
||||
pub use crate::epd1in54::graphics::Display1in54;
|
||||
|
||||
/// EPD1in54 driver
|
||||
///
|
||||
@@ -248,6 +251,10 @@ where
|
||||
RefreshLUT::QUICK => self.set_lut_helper(spi, &LUT_PARTIAL_UPDATE),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_busy(&self) -> bool {
|
||||
self.interface.is_busy(IS_BUSY_LOW)
|
||||
}
|
||||
}
|
||||
|
||||
impl<SPI, CS, BUSY, DC, RST> EPD1in54<SPI, CS, BUSY, DC, RST>
|
||||
@@ -259,7 +266,7 @@ where
|
||||
RST: OutputPin,
|
||||
{
|
||||
fn wait_until_idle(&mut self) {
|
||||
self.interface.wait_until_idle(false);
|
||||
self.interface.wait_until_idle(IS_BUSY_LOW);
|
||||
}
|
||||
|
||||
pub(crate) fn use_full_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
|
||||
|
||||
+39
-10
@@ -1,40 +1,69 @@
|
||||
use crate::epd2in9::{DEFAULT_BACKGROUND_COLOR, HEIGHT, WIDTH};
|
||||
use crate::graphics::{Display, DisplayRotation};
|
||||
use crate::prelude::*;
|
||||
use embedded_graphics::prelude::*;
|
||||
|
||||
/// Full size buffer for use with the 2in9 EPD
|
||||
/// Display with Fullsize buffer for use with the 2in9 EPD
|
||||
///
|
||||
/// Can also be manuall constructed:
|
||||
/// `buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value(); WIDTH / 8 * HEIGHT]`
|
||||
pub struct Buffer2in9 {
|
||||
pub buffer: [u8; WIDTH as usize * HEIGHT as usize / 8],
|
||||
pub struct Display2in9 {
|
||||
buffer: [u8; WIDTH as usize * HEIGHT as usize / 8],
|
||||
rotation: DisplayRotation,
|
||||
}
|
||||
|
||||
impl Default for Buffer2in9 {
|
||||
impl Default for Display2in9 {
|
||||
fn default() -> Self {
|
||||
Buffer2in9 {
|
||||
Display2in9 {
|
||||
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value();
|
||||
WIDTH as usize * HEIGHT as usize / 8],
|
||||
rotation: DisplayRotation::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drawing<Color> for Display2in9 {
|
||||
fn draw<T>(&mut self, item_pixels: T)
|
||||
where
|
||||
T: Iterator<Item = Pixel<Color>>,
|
||||
{
|
||||
self.draw_helper(WIDTH, HEIGHT, item_pixels);
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Display2in9 {
|
||||
fn buffer(&self) -> &[u8] {
|
||||
&self.buffer
|
||||
}
|
||||
|
||||
fn get_mut_buffer(&mut self) -> &mut [u8] {
|
||||
&mut self.buffer
|
||||
}
|
||||
|
||||
fn set_rotation(&mut self, rotation: DisplayRotation) {
|
||||
self.rotation = rotation;
|
||||
}
|
||||
|
||||
fn rotation(&self) -> DisplayRotation {
|
||||
self.rotation
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::graphics::Display;
|
||||
|
||||
// test buffer length
|
||||
#[test]
|
||||
fn graphics_size() {
|
||||
let mut buffer = Buffer2in9::default();
|
||||
let display = Display::new(WIDTH, HEIGHT, &mut buffer.buffer);
|
||||
let display = Display2in9::default();
|
||||
assert_eq!(display.buffer().len(), 4736);
|
||||
}
|
||||
|
||||
// test default background color on all bytes
|
||||
#[test]
|
||||
fn graphics_default() {
|
||||
let mut buffer = Buffer2in9::default();
|
||||
let display = Display::new(WIDTH, HEIGHT, &mut buffer.buffer);
|
||||
let display = Display2in9::default();
|
||||
for &byte in display.buffer() {
|
||||
assert_eq!(byte, DEFAULT_BACKGROUND_COLOR.get_byte_value());
|
||||
}
|
||||
|
||||
+14
-7
@@ -4,19 +4,19 @@
|
||||
//!
|
||||
//! # Example for the 2.9 in E-Ink Display
|
||||
//!
|
||||
//! ```ignore
|
||||
//! ```rust,ignore
|
||||
//! use epd_waveshare::{
|
||||
//! epd2in9::{EPD2in9, Buffer2in9},
|
||||
//! 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)?;
|
||||
//! let mut epd = EPD2in9::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay).unwrap();
|
||||
//!
|
||||
//! // Use display graphics
|
||||
//! let mut buffer = Buffer2in9::default();
|
||||
//! let mut display = Display::new(epd.width(), epd.height(), &mut buffer.buffer);
|
||||
//! let mut display = Display2in9::default();
|
||||
//!
|
||||
//! // Write some hello world in the screenbuffer
|
||||
//! display.draw(
|
||||
@@ -38,6 +38,7 @@
|
||||
pub const WIDTH: u32 = 128;
|
||||
pub const HEIGHT: u32 = 296;
|
||||
pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White;
|
||||
const IS_BUSY_LOW: bool = false;
|
||||
|
||||
use embedded_hal::{
|
||||
blocking::{delay::*, spi::Write},
|
||||
@@ -55,8 +56,10 @@ use crate::traits::*;
|
||||
|
||||
use crate::interface::DisplayInterface;
|
||||
|
||||
#[cfg(feature = "graphics")]
|
||||
mod graphics;
|
||||
pub use crate::epd2in9::graphics::Buffer2in9;
|
||||
#[cfg(feature = "graphics")]
|
||||
pub use crate::epd2in9::graphics::Display2in9;
|
||||
|
||||
/// EPD2in9 driver
|
||||
///
|
||||
@@ -247,6 +250,10 @@ where
|
||||
RefreshLUT::QUICK => self.set_lut_helper(spi, &LUT_PARTIAL_UPDATE),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_busy(&self) -> bool {
|
||||
self.interface.is_busy(IS_BUSY_LOW)
|
||||
}
|
||||
}
|
||||
|
||||
impl<SPI, CS, BUSY, DC, RST> EPD2in9<SPI, CS, BUSY, DC, RST>
|
||||
@@ -258,7 +265,7 @@ where
|
||||
RST: OutputPin,
|
||||
{
|
||||
fn wait_until_idle(&mut self) {
|
||||
self.interface.wait_until_idle(false);
|
||||
self.interface.wait_until_idle(IS_BUSY_LOW);
|
||||
}
|
||||
|
||||
fn use_full_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
|
||||
|
||||
@@ -1,9 +1,3 @@
|
||||
use crate::color::Color;
|
||||
|
||||
pub const WIDTH: u32 = 400;
|
||||
pub const HEIGHT: u32 = 300;
|
||||
pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White;
|
||||
|
||||
#[rustfmt::skip]
|
||||
pub(crate) const LUT_VCOM0: [u8; 44] = [
|
||||
0x00, 0x17, 0x00, 0x00, 0x00, 0x02,
|
||||
|
||||
+48
-39
@@ -1,22 +1,54 @@
|
||||
use crate::epd4in2::constants::{DEFAULT_BACKGROUND_COLOR, HEIGHT, WIDTH};
|
||||
use crate::epd4in2::{DEFAULT_BACKGROUND_COLOR, HEIGHT, WIDTH};
|
||||
use crate::graphics::{Display, DisplayRotation};
|
||||
use crate::prelude::*;
|
||||
use embedded_graphics::prelude::*;
|
||||
|
||||
/// Full size buffer for use with the 4in2 EPD
|
||||
///
|
||||
/// Can also be manuall constructed:
|
||||
/// `buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value(); WIDTH / 8 * HEIGHT]`
|
||||
pub struct Buffer4in2 {
|
||||
pub buffer: [u8; WIDTH as usize * HEIGHT as usize / 8],
|
||||
pub struct Display4in2 {
|
||||
buffer: [u8; WIDTH as usize * HEIGHT as usize / 8],
|
||||
rotation: DisplayRotation,
|
||||
}
|
||||
|
||||
impl Default for Buffer4in2 {
|
||||
impl Default for Display4in2 {
|
||||
fn default() -> Self {
|
||||
Buffer4in2 {
|
||||
Display4in2 {
|
||||
buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value();
|
||||
WIDTH as usize * HEIGHT as usize / 8],
|
||||
rotation: DisplayRotation::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drawing<Color> for Display4in2 {
|
||||
fn draw<T>(&mut self, item_pixels: T)
|
||||
where
|
||||
T: Iterator<Item = Pixel<Color>>,
|
||||
{
|
||||
self.draw_helper(WIDTH, HEIGHT, item_pixels);
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Display4in2 {
|
||||
fn buffer(&self) -> &[u8] {
|
||||
&self.buffer
|
||||
}
|
||||
|
||||
fn get_mut_buffer(&mut self) -> &mut [u8] {
|
||||
&mut self.buffer
|
||||
}
|
||||
|
||||
fn set_rotation(&mut self, rotation: DisplayRotation) {
|
||||
self.rotation = rotation;
|
||||
}
|
||||
|
||||
fn rotation(&self) -> DisplayRotation {
|
||||
self.rotation
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -24,35 +56,27 @@ mod tests {
|
||||
use crate::epd4in2;
|
||||
use crate::graphics::{Display, DisplayRotation};
|
||||
use embedded_graphics::coord::Coord;
|
||||
use embedded_graphics::prelude::*;
|
||||
use embedded_graphics::primitives::Line;
|
||||
|
||||
// test buffer length
|
||||
#[test]
|
||||
fn graphics_size() {
|
||||
let mut display4in2 = Buffer4in2::default();
|
||||
let display = Display::new(WIDTH, HEIGHT, &mut display4in2.buffer);
|
||||
let display = Display4in2::default();
|
||||
assert_eq!(display.buffer().len(), 15000);
|
||||
}
|
||||
|
||||
// test default background color on all bytes
|
||||
#[test]
|
||||
fn graphics_default() {
|
||||
let mut display4in2 = Buffer4in2::default();
|
||||
let display = Display::new(WIDTH, HEIGHT, &mut display4in2.buffer);
|
||||
use crate::epd4in2;
|
||||
let display = Display4in2::default();
|
||||
for &byte in display.buffer() {
|
||||
assert_eq!(
|
||||
byte,
|
||||
epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value()
|
||||
);
|
||||
assert_eq!(byte, epd4in2::DEFAULT_BACKGROUND_COLOR.get_byte_value());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn graphics_rotation_0() {
|
||||
let mut display4in2 = Buffer4in2::default();
|
||||
let mut display = Display::new(WIDTH, HEIGHT, &mut display4in2.buffer);
|
||||
let mut display = Display4in2::default();
|
||||
display.draw(
|
||||
Line::new(Coord::new(0, 0), Coord::new(7, 0))
|
||||
.with_stroke(Some(Color::Black))
|
||||
@@ -64,17 +88,13 @@ mod tests {
|
||||
assert_eq!(buffer[0], Color::Black.get_byte_value());
|
||||
|
||||
for &byte in buffer.iter().skip(1) {
|
||||
assert_eq!(
|
||||
byte,
|
||||
epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value()
|
||||
);
|
||||
assert_eq!(byte, epd4in2::DEFAULT_BACKGROUND_COLOR.get_byte_value());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn graphics_rotation_90() {
|
||||
let mut display4in2 = Buffer4in2::default();
|
||||
let mut display = Display::new(WIDTH, HEIGHT, &mut display4in2.buffer);
|
||||
let mut display = Display4in2::default();
|
||||
display.set_rotation(DisplayRotation::Rotate90);
|
||||
display.draw(
|
||||
Line::new(Coord::new(0, 392), Coord::new(0, 399))
|
||||
@@ -87,17 +107,13 @@ mod tests {
|
||||
assert_eq!(buffer[0], Color::Black.get_byte_value());
|
||||
|
||||
for &byte in buffer.iter().skip(1) {
|
||||
assert_eq!(
|
||||
byte,
|
||||
epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value()
|
||||
);
|
||||
assert_eq!(byte, epd4in2::DEFAULT_BACKGROUND_COLOR.get_byte_value());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn graphics_rotation_180() {
|
||||
let mut display4in2 = Buffer4in2::default();
|
||||
let mut display = Display::new(WIDTH, HEIGHT, &mut display4in2.buffer);
|
||||
let mut display = Display4in2::default();
|
||||
display.set_rotation(DisplayRotation::Rotate180);
|
||||
display.draw(
|
||||
Line::new(Coord::new(392, 299), Coord::new(399, 299))
|
||||
@@ -113,17 +129,13 @@ mod tests {
|
||||
assert_eq!(buffer[0], Color::Black.get_byte_value());
|
||||
|
||||
for &byte in buffer.iter().skip(1) {
|
||||
assert_eq!(
|
||||
byte,
|
||||
epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value()
|
||||
);
|
||||
assert_eq!(byte, epd4in2::DEFAULT_BACKGROUND_COLOR.get_byte_value());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn graphics_rotation_270() {
|
||||
let mut display4in2 = Buffer4in2::default();
|
||||
let mut display = Display::new(WIDTH, HEIGHT, &mut display4in2.buffer);
|
||||
let mut display = Display4in2::default();
|
||||
display.set_rotation(DisplayRotation::Rotate270);
|
||||
display.draw(
|
||||
Line::new(Coord::new(299, 0), Coord::new(299, 7))
|
||||
@@ -139,10 +151,7 @@ mod tests {
|
||||
assert_eq!(buffer[0], Color::Black.get_byte_value());
|
||||
|
||||
for &byte in buffer.iter().skip(1) {
|
||||
assert_eq!(
|
||||
byte,
|
||||
epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value()
|
||||
);
|
||||
assert_eq!(byte, epd4in2::DEFAULT_BACKGROUND_COLOR.get_byte_value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+14
-3
@@ -56,15 +56,22 @@ use crate::traits::{InternalWiAdditions, RefreshLUT, WaveshareDisplay};
|
||||
|
||||
//The Lookup Tables for the Display
|
||||
mod constants;
|
||||
pub use self::constants::*;
|
||||
use crate::epd4in2::constants::*;
|
||||
|
||||
pub const WIDTH: u32 = 400;
|
||||
pub const HEIGHT: u32 = 300;
|
||||
pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White;
|
||||
const IS_BUSY_LOW: bool = true;
|
||||
|
||||
use crate::color::Color;
|
||||
|
||||
pub(crate) mod command;
|
||||
use self::command::Command;
|
||||
|
||||
#[cfg(feature = "graphics")]
|
||||
mod graphics;
|
||||
pub use self::graphics::Buffer4in2;
|
||||
#[cfg(feature = "graphics")]
|
||||
pub use self::graphics::Display4in2;
|
||||
|
||||
/// EPD4in2 driver
|
||||
///
|
||||
@@ -322,6 +329,10 @@ where
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_busy(&self) -> bool {
|
||||
self.interface.is_busy(IS_BUSY_LOW)
|
||||
}
|
||||
}
|
||||
|
||||
impl<SPI, CS, BUSY, DC, RST> EPD4in2<SPI, CS, BUSY, DC, RST>
|
||||
@@ -350,7 +361,7 @@ where
|
||||
}
|
||||
|
||||
fn wait_until_idle(&mut self) {
|
||||
self.interface.wait_until_idle(true)
|
||||
self.interface.wait_until_idle(IS_BUSY_LOW)
|
||||
}
|
||||
|
||||
fn send_resolution(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
|
||||
|
||||
+108
-48
@@ -22,71 +22,131 @@ impl Default for DisplayRotation {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Display<'a> {
|
||||
pub trait Display: Drawing<Color> {
|
||||
/// 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() {
|
||||
*elem = background_color.get_byte_value();
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the buffer
|
||||
fn buffer(&self) -> &[u8];
|
||||
|
||||
/// Returns a mutable buffer
|
||||
fn get_mut_buffer(&mut self) -> &mut [u8];
|
||||
|
||||
/// Sets the rotation of the display
|
||||
fn set_rotation(&mut self, rotation: DisplayRotation);
|
||||
|
||||
/// Get the current rotation of the display
|
||||
fn rotation(&self) -> DisplayRotation;
|
||||
|
||||
/// 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: Iterator<Item = Pixel<Color>>,
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A variable Display without a predefined buffer
|
||||
///
|
||||
/// The buffer can be created as following:
|
||||
/// buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value(); WIDTH / 8 * HEIGHT]
|
||||
///
|
||||
/// Example:
|
||||
/// ```rust,no_run
|
||||
/// # use epd_waveshare::epd2in9::DEFAULT_BACKGROUND_COLOR;
|
||||
/// # use epd_waveshare::prelude::*;
|
||||
/// # use epd_waveshare::graphics::VarDisplay;
|
||||
/// # use embedded_graphics::prelude::*;
|
||||
/// # use embedded_graphics::primitives::{Circle, Line};
|
||||
/// let width = 128;
|
||||
/// let height = 296;
|
||||
///
|
||||
/// let mut buffer = [DEFAULT_BACKGROUND_COLOR.get_byte_value(); 128 / 8 * 296];
|
||||
/// let mut display = VarDisplay::new(width, height, &mut buffer);
|
||||
///
|
||||
/// display.set_rotation(DisplayRotation::Rotate90);
|
||||
///
|
||||
/// display.draw(
|
||||
/// Line::new(Coord::new(0, 120), Coord::new(0, 295))
|
||||
/// .with_stroke(Some(Color::Black))
|
||||
/// .into_iter(),
|
||||
/// );
|
||||
/// ```
|
||||
pub struct VarDisplay<'a> {
|
||||
width: u32,
|
||||
height: u32,
|
||||
rotation: DisplayRotation,
|
||||
buffer: &'a mut [u8], //buffer: Box<u8>//[u8; 15000]
|
||||
}
|
||||
|
||||
impl<'a> Display<'a> {
|
||||
pub fn new(width: u32, height: u32, buffer: &'a mut [u8]) -> Display<'a> {
|
||||
impl<'a> VarDisplay<'a> {
|
||||
pub fn new(width: u32, height: u32, buffer: &'a mut [u8]) -> VarDisplay<'a> {
|
||||
let len = buffer.len() as u32;
|
||||
assert!(width / 8 * height >= len);
|
||||
Display {
|
||||
VarDisplay {
|
||||
width,
|
||||
height,
|
||||
rotation: DisplayRotation::default(),
|
||||
buffer,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn buffer(&self) -> &[u8] {
|
||||
&self.buffer
|
||||
}
|
||||
|
||||
pub fn clear_buffer(&mut self, background_color: Color) {
|
||||
for elem in &mut self.buffer.iter_mut() {
|
||||
*elem = background_color.get_byte_value();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_rotation(&mut self, rotation: DisplayRotation) {
|
||||
self.rotation = rotation;
|
||||
}
|
||||
|
||||
pub fn rotation(&self) -> DisplayRotation {
|
||||
self.rotation
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drawing<Color> for Display<'a> {
|
||||
impl<'a> Drawing<Color> for VarDisplay<'a> {
|
||||
fn draw<T>(&mut self, item_pixels: T)
|
||||
where
|
||||
T: Iterator<Item = Pixel<Color>>,
|
||||
{
|
||||
for Pixel(UnsignedCoord(x, y), color) in item_pixels {
|
||||
if outside_display(x, y, self.width, self.height, self.rotation) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Give us index inside the buffer and the bit-position in that u8 which needs to be changed
|
||||
let (index, bit) = rotation(x, y, self.width, self.height, self.rotation);
|
||||
let index = index as usize;
|
||||
|
||||
// "Draw" the Pixel on that bit
|
||||
match color {
|
||||
Color::Black => {
|
||||
self.buffer[index] &= !bit;
|
||||
}
|
||||
Color::White => {
|
||||
self.buffer[index] |= bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
self.draw_helper(self.width, self.height, item_pixels);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Display for VarDisplay<'a> {
|
||||
fn buffer(&self) -> &[u8] {
|
||||
&self.buffer
|
||||
}
|
||||
|
||||
fn get_mut_buffer(&mut self) -> &mut [u8] {
|
||||
&mut self.buffer
|
||||
}
|
||||
|
||||
fn set_rotation(&mut self, rotation: DisplayRotation) {
|
||||
self.rotation = rotation;
|
||||
}
|
||||
|
||||
fn rotation(&self) -> DisplayRotation {
|
||||
self.rotation
|
||||
}
|
||||
}
|
||||
|
||||
// Checks if a pos is outside the defined display
|
||||
fn outside_display(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) -> bool {
|
||||
match rotation {
|
||||
DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => {
|
||||
@@ -105,7 +165,7 @@ fn outside_display(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRot
|
||||
|
||||
#[rustfmt::skip]
|
||||
//returns index position in the u8-slice and the bit-position inside that u8
|
||||
fn rotation(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) -> (u32, u8) {
|
||||
fn find_position(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) -> (u32, u8) {
|
||||
match rotation {
|
||||
DisplayRotation::Rotate0 => (
|
||||
x / 8 + (width / 8) * y,
|
||||
@@ -128,7 +188,7 @@ fn rotation(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation)
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{outside_display, rotation, Display, DisplayRotation};
|
||||
use super::{find_position, outside_display, Display, DisplayRotation, VarDisplay};
|
||||
use crate::color::Color;
|
||||
use embedded_graphics::coord::Coord;
|
||||
use embedded_graphics::prelude::*;
|
||||
@@ -139,7 +199,7 @@ mod tests {
|
||||
use crate::epd4in2::{HEIGHT, WIDTH};
|
||||
|
||||
let mut buffer = [Color::Black.get_byte_value(); WIDTH as usize / 8 * HEIGHT as usize];
|
||||
let mut display = Display::new(WIDTH, HEIGHT, &mut buffer);
|
||||
let mut display = VarDisplay::new(WIDTH, HEIGHT, &mut buffer);
|
||||
|
||||
for &byte in display.buffer.iter() {
|
||||
assert_eq!(byte, Color::Black.get_byte_value());
|
||||
@@ -171,7 +231,7 @@ mod tests {
|
||||
if outside_display(x, y, width, height, rotation2) {
|
||||
break;
|
||||
} else {
|
||||
let (idx, _) = rotation(x, y, width, height, rotation2);
|
||||
let (idx, _) = find_position(x, y, width, height, rotation2);
|
||||
assert!(idx < max_value);
|
||||
}
|
||||
}
|
||||
@@ -185,7 +245,7 @@ mod tests {
|
||||
let height = 296;
|
||||
|
||||
let mut buffer = [DEFAULT_BACKGROUND_COLOR.get_byte_value(); 128 / 8 * 296];
|
||||
let mut display = Display::new(width, height, &mut buffer);
|
||||
let mut display = VarDisplay::new(width, height, &mut buffer);
|
||||
|
||||
display.draw(
|
||||
Line::new(Coord::new(0, 0), Coord::new(7, 0))
|
||||
@@ -209,7 +269,7 @@ mod tests {
|
||||
let height = 296;
|
||||
|
||||
let mut buffer = [DEFAULT_BACKGROUND_COLOR.get_byte_value(); 128 / 8 * 296];
|
||||
let mut display = Display::new(width, height, &mut buffer);
|
||||
let mut display = VarDisplay::new(width, height, &mut buffer);
|
||||
|
||||
display.set_rotation(DisplayRotation::Rotate90);
|
||||
|
||||
|
||||
+21
-4
@@ -127,16 +127,33 @@ where
|
||||
/// Most likely there was a mistake with the 2in9 busy connection
|
||||
/// //TODO: use the #cfg feature to make this compile the right way for the certain types
|
||||
pub(crate) fn wait_until_idle(&mut self, is_busy_low: bool) {
|
||||
// TODO: removal of delay. TEST!
|
||||
//tested: worked without the delay for all tested devices
|
||||
//self.delay_ms(1);
|
||||
//low: busy, high: idle
|
||||
while (is_busy_low && self.busy.is_low()) || (!is_busy_low && self.busy.is_high()) {
|
||||
//TODO: REMOVAL of DELAY: it's only waiting for the signal anyway and should continue work asap
|
||||
|
||||
while self.is_busy(is_busy_low) {
|
||||
//tested: REMOVAL of DELAY: it's only waiting for the signal anyway and should continue work asap
|
||||
//old: shorten the time? it was 100 in the beginning
|
||||
//self.delay_ms(5);
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if device is still busy
|
||||
///
|
||||
/// This is normally handled by the more complicated commands themselves,
|
||||
/// but in the case you send data and commands directly you might need to check
|
||||
/// if the device is still busy
|
||||
///
|
||||
/// is_busy_low
|
||||
///
|
||||
/// - TRUE for epd4in2, epd2in13, epd2in7, epd5in83, epd7in5
|
||||
/// - FALSE for epd2in9, epd1in54 (for all Display Type A ones?)
|
||||
///
|
||||
/// Most likely there was a mistake with the 2in9 busy connection
|
||||
/// //TODO: use the #cfg feature to make this compile the right way for the certain types
|
||||
pub(crate) fn is_busy(&self, is_busy_low: bool) -> bool {
|
||||
(is_busy_low && self.busy.is_low()) || (!is_busy_low && self.busy.is_high())
|
||||
}
|
||||
|
||||
/// Resets the device.
|
||||
///
|
||||
/// Often used to awake the module from deep sleep. See [EPD4in2::sleep()](EPD4in2::sleep())
|
||||
|
||||
+27
-11
@@ -20,22 +20,35 @@
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ```ignore
|
||||
//! use eink-waveshare-rs::epd4in2::EPD4in2;
|
||||
//! ```rust,ignore
|
||||
//! use epd_waveshare::{
|
||||
//! epd2in9::{EPD2in9, Display2in9},
|
||||
//! graphics::{Display, DisplayRotation},
|
||||
//! prelude::*,
|
||||
//! };
|
||||
//! use embedded_graphics::Drawing;
|
||||
//!
|
||||
//! let mut epd4in2 = EPD4in2::new(spi, cs, busy, dc, rst, delay).unwrap();
|
||||
//! // Setup EPD
|
||||
//! let mut epd = EPD2in9::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay).unwrap();
|
||||
//!
|
||||
//! let mut buffer = [0u8, epd4in2.get_width() / 8 * epd4in2.get_height()];
|
||||
//! // Use display graphics
|
||||
//! let mut display = Display2in9::default();
|
||||
//!
|
||||
//! // draw something into the buffer
|
||||
//! // Write some hello world in the screenbuffer
|
||||
//! display.draw(
|
||||
//! Font6x8::render_str("Hello World!")
|
||||
//! .with_stroke(Some(Color::Black))
|
||||
//! .with_fill(Some(Color::White))
|
||||
//! .translate(Coord::new(5, 50))
|
||||
//! .into_iter(),
|
||||
//! );
|
||||
//!
|
||||
//! epd4in2.display_and_transfer_buffer(buffer, None);
|
||||
//! // Display updated frame
|
||||
//! epd.update_frame(&mut spi, &display.buffer()).unwrap();
|
||||
//! epd.display_frame(&mut spi).expect("display frame new graphics");
|
||||
//!
|
||||
//! // wait and look at the image
|
||||
//!
|
||||
//! epd4in2.clear_frame(None);
|
||||
//!
|
||||
//! epd4in2.sleep();
|
||||
//! // Set the EPD to sleep
|
||||
//! epd.sleep(&mut spi).expect("sleep");
|
||||
//! ```
|
||||
//!
|
||||
//!
|
||||
@@ -67,6 +80,9 @@ pub mod prelude {
|
||||
pub use crate::color::Color;
|
||||
pub use crate::traits::{RefreshLUT, WaveshareDisplay};
|
||||
pub use crate::SPI_MODE;
|
||||
|
||||
#[cfg(feature = "graphics")]
|
||||
pub use crate::graphics::{Display, DisplayRotation};
|
||||
}
|
||||
|
||||
use embedded_hal::spi::{Mode, Phase, Polarity};
|
||||
|
||||
@@ -125,6 +125,7 @@ where
|
||||
fn display_frame(&mut self, spi: &mut SPI) -> 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`]
|
||||
fn clear_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error>;
|
||||
|
||||
@@ -141,4 +142,11 @@ where
|
||||
spi: &mut SPI,
|
||||
refresh_rate: Option<RefreshLUT>,
|
||||
) -> Result<(), SPI::Error>;
|
||||
|
||||
/// Checks if the display is busy transmitting data
|
||||
///
|
||||
/// This is normally handled by the more complicated commands themselves,
|
||||
/// but in the case you send data and commands directly you might need to check
|
||||
/// if the device is still busy
|
||||
fn is_busy(&self) -> bool;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user