Browse Source

Merge branch 'add_embedded_graphics' of github.com:Caemor/eink-waveshare-rs into add_embedded_graphics

embedded-hal-1.0
Chris 7 years ago
parent
commit
17abcc74b0
  1. 3
      Cargo.toml
  2. 3
      examples/embedded_linux_epd4in2/Cargo.toml
  3. 121
      examples/embedded_linux_epd4in2/src/main.rs
  4. 238
      src/drawing.rs
  5. 5
      src/epd1in54/mod.rs
  6. 5
      src/epd2in9/mod.rs
  7. 12
      src/interface.rs
  8. 14
      src/type_a/constants.rs
  9. 16
      src/type_a/mod.rs

3
Cargo.toml

@ -29,7 +29,8 @@ epd4in2_fast_update = []
[dependencies] [dependencies]
embedded-graphics = "0.4.1" #embedded-graphics = "0.4.1"
embedded-graphics = {git = "https://github.com/caemor/embedded-graphics", branch = "patch-1"}
[dependencies.embedded-hal] [dependencies.embedded-hal]

3
examples/embedded_linux_epd4in2/Cargo.toml

@ -11,4 +11,7 @@ eink_waveshare_rs = { path = "../../", default-features = false, features = ["ep
linux-embedded-hal = "0.2.0" linux-embedded-hal = "0.2.0"
#embedded-graphics = "0.4.1"
embedded-graphics = {git = "https://github.com/caemor/embedded-graphics", branch = "patch-1"}
embedded-hal = { version = "0.2.1", features = ["unproven"] } embedded-hal = { version = "0.2.1", features = ["unproven"] }

121
examples/embedded_linux_epd4in2/src/main.rs

@ -8,10 +8,18 @@ extern crate eink_waveshare_rs;
use eink_waveshare_rs::{ use eink_waveshare_rs::{
EPD4in2, EPD4in2,
drawing_old::{Graphics}, drawing_old::{Graphics},
drawing::{DisplayEink42BlackWhite, Display, DisplayRotation},
color::Color, color::Color,
WaveshareDisplay, WaveshareDisplay,
}; };
extern crate embedded_graphics;
use embedded_graphics::coord::Coord;
use embedded_graphics::fonts::Font6x8;
use embedded_graphics::prelude::*;
use embedded_graphics::primitives::{Circle, Line};
use embedded_graphics::Drawing;
use lin_hal::spidev::{self, SpidevOptions}; use lin_hal::spidev::{self, SpidevOptions};
use lin_hal::{Pin, Spidev}; use lin_hal::{Pin, Spidev};
use lin_hal::sysfs_gpio::Direction; use lin_hal::sysfs_gpio::Direction;
@ -62,7 +70,7 @@ impl<'a> InputPin for HackInputPin<'a> {
* *
*/ */
fn main() { fn main() {
run().map_err(|e| println!("{}", e.to_string())); run().map_err(|e| println!("{}", e.to_string())).unwrap();
} }
@ -136,6 +144,8 @@ fn run() -> Result<(), std::io::Error> {
epd4in2.clear_frame(&mut spi).expect("clear frame error"); epd4in2.clear_frame(&mut spi).expect("clear frame error");
epd4in2.update_frame(&mut spi, graphics.get_buffer()).expect("update frame error"); epd4in2.update_frame(&mut spi, graphics.get_buffer()).expect("update frame error");
epd4in2.display_frame(&mut spi)?; epd4in2.display_frame(&mut spi)?;
println!("Finished basic old graphics test");
delay.delay_ms(3000u16); delay.delay_ms(3000u16);
@ -158,6 +168,8 @@ fn run() -> Result<(), std::io::Error> {
epd4in2.update_partial_frame(&mut spi, circle_graphics.get_buffer(), 160,240, 32, 32).expect("update partial frame error"); epd4in2.update_partial_frame(&mut spi, circle_graphics.get_buffer(), 160,240, 32, 32).expect("update partial frame error");
epd4in2.display_frame(&mut spi)?; epd4in2.display_frame(&mut spi)?;
println!("Finished partial update test");
delay.delay_ms(3000u16); delay.delay_ms(3000u16);
@ -170,7 +182,112 @@ fn run() -> Result<(), std::io::Error> {
epd4in2.update_frame(&mut spi, graphics.get_buffer())?; epd4in2.update_frame(&mut spi, graphics.get_buffer())?;
epd4in2.display_frame(&mut spi)?; epd4in2.display_frame(&mut spi)?;
println!("Finished draw string test");
delay.delay_ms(3000u16); delay.delay_ms(3000u16);
println!("Now test new graphics:");
println!("Now test new graphics with rotate90:");
let mut display = DisplayEink42BlackWhite::default();
display.set_rotation(DisplayRotation::Rotate90);
display.draw(
Font6x8::render_str("Rotate 90!")
.with_stroke(Some(Color::Black))
.with_fill(Some(Color::White))
.translate(Coord::new(5, 50))
.into_iter(),
);
epd4in2.update_frame(&mut spi, &display.buffer()).unwrap();
epd4in2.display_frame(&mut spi).expect("display frame new graphics");
delay.delay_ms(2000u16);
println!("Now test new graphics with rotate180:");
let mut display = DisplayEink42BlackWhite::default();
display.set_rotation(DisplayRotation::Rotate180);
display.draw(
Font6x8::render_str("Rotate 180!")
.with_stroke(Some(Color::Black))
.with_fill(Some(Color::White))
.translate(Coord::new(5, 50))
.into_iter(),
);
epd4in2.update_frame(&mut spi, &display.buffer()).unwrap();
epd4in2.display_frame(&mut spi).expect("display frame new graphics");
delay.delay_ms(2000u16);
println!("Now test new graphics with rotate270:");
let mut display = DisplayEink42BlackWhite::default();
display.set_rotation(DisplayRotation::Rotate270);
display.draw(
Font6x8::render_str("Rotate 270!")
.with_stroke(Some(Color::Black))
.with_fill(Some(Color::White))
.translate(Coord::new(5, 50))
.into_iter(),
);
epd4in2.update_frame(&mut spi, &display.buffer()).unwrap();
epd4in2.display_frame(&mut spi).expect("display frame new graphics");
delay.delay_ms(2000u16);
println!("Now test new graphics with default rotation and some special stuff:");
let mut display = DisplayEink42BlackWhite::default();
display.draw(
Circle::new(Coord::new(64, 64), 64)
.with_stroke(Some(Color::Black))
.into_iter(),
);
display.draw(
Line::new(Coord::new(64, 64), Coord::new(0, 64))
.with_stroke(Some(Color::Black))
.into_iter(),
);
display.draw(
Line::new(Coord::new(64, 64), Coord::new(80, 80))
.with_stroke(Some(Color::Black))
.into_iter(),
);
display.draw(
Font6x8::render_str("It's working!")
// Using Style here
.with_style(Style {
fill_color: Some(Color::Black),
stroke_color: Some(Color::White),
stroke_width: 0u8, // Has no effect on fonts
})
.translate(Coord::new(175, 250))
.into_iter(),
);
let mut i = 0;
loop {
i += 1;
println!("Moving Hello World. Loop {} from 20", i);
display.draw(
Font6x8::render_str("Hello World!")
.with_style(Style {
fill_color: Some(Color::White),
stroke_color: Some(Color::Black),
stroke_width: 0u8, // Has no effect on fonts
})
.translate(Coord::new(5 + i*10, 50))
.into_iter(),
);
epd4in2.update_frame(&mut spi, &display.buffer()).unwrap();
epd4in2.display_frame(&mut spi).expect("display frame new graphics");
if i > 20 {
break;
}
delay.delay_ms(1_000u16);
}
println!("Finished tests - going to sleep");
epd4in2.sleep(&mut spi) epd4in2.sleep(&mut spi)
} }

238
src/drawing.rs

@ -19,29 +19,18 @@ impl Default for DisplayRotation {
} }
} }
pub enum Display { pub trait Display {
Eink42BlackWhite, fn buffer(&self) -> &[u8];
} fn set_rotation(&mut self, rotation: DisplayRotation);
impl Display { fn rotation(&self) -> DisplayRotation;
/// Gets the Dimensions of a dipslay in the following order:
/// - Width
/// - Height
/// - Neccessary Buffersize
pub fn get_dimensions(&self) -> (u16, u16, u16) {
match self {
Display::Eink42BlackWhite => (400, 300, 15000),
}
}
} }
pub trait Buffer {
fn get_buffer(&self) -> &[u8];
}
pub struct DisplayEink42BlackWhite { pub struct DisplayEink42BlackWhite {
buffer: [u8; 400 * 300 / 8], buffer: [u8; 400 * 300 / 8],
rotation: DisplayRotation, //TODO: check embedded_graphics for orientation rotation: DisplayRotation, //TODO: check embedded_graphics for orientation
} }
impl Default for DisplayEink42BlackWhite { impl Default for DisplayEink42BlackWhite {
fn default() -> Self { fn default() -> Self {
use epd4in2::constants::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT}; use epd4in2::constants::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT};
@ -54,15 +43,23 @@ impl Default for DisplayEink42BlackWhite {
} }
} }
} }
impl Buffer for DisplayEink42BlackWhite {
fn get_buffer(&self) -> &[u8] { impl Display for DisplayEink42BlackWhite {
fn buffer(&self) -> &[u8] {
&self.buffer &self.buffer
} }
fn set_rotation(&mut self, rotation: DisplayRotation) {
self.rotation = rotation;
}
fn rotation(&self) -> DisplayRotation {
self.rotation
}
} }
impl Drawing<u8> for DisplayEink42BlackWhite {
impl Drawing<Color> for DisplayEink42BlackWhite {
fn draw<T>(&mut self, item_pixels: T) fn draw<T>(&mut self, item_pixels: T)
where where
T: Iterator<Item = Pixel<u8>> T: Iterator<Item = Pixel<Color>>
{ {
use epd4in2::constants::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT}; use epd4in2::constants::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT};
for Pixel(UnsignedCoord(x,y), color) in item_pixels { for Pixel(UnsignedCoord(x,y), color) in item_pixels {
@ -81,7 +78,7 @@ impl Drawing<u8> for DisplayEink42BlackWhite {
return; return;
} }
match Color::from(color) { match color {
Color::Black => { Color::Black => {
self.buffer[idx] &= !bit; self.buffer[idx] &= !bit;
} }
@ -93,83 +90,122 @@ impl Drawing<u8> for DisplayEink42BlackWhite {
} }
} }
// impl Drawing<u8> for DisplayRibbonLeft { //TODO: write tests
// fn draw<T>(&mut self, item_pixels: T) #[cfg(test)]
// where mod tests {
// T: Iterator<Item = Pixel<u8>>, use super::*;
// { use epd4in2;
// for Pixel(UnsignedCoord(x, y), color) in item_pixels { use embedded_graphics::coord::Coord;
// if y > 127 || x > 295 { use embedded_graphics::fonts::Font6x8;
// continue; use embedded_graphics::prelude::*;
// } use embedded_graphics::primitives::{Circle, Line};
// let cell = &mut self.0[y as usize / 8 + (295 - x as usize) * 128 / 8];
// let bit = 7 - y % 8; #[test]
// if color != 0 { fn from_u8() {
// *cell &= !(1 << bit); assert_eq!(Color::Black, Color::from(0u8));
// } else { assert_eq!(Color::White, Color::from(1u8));
// *cell |= 1 << bit; }
// }
// } // test all values aside from 0 and 1 which all should panic
// } #[test]
// } fn from_u8_panic() {
for val in 2..=u8::max_value() {
let result = std::panic::catch_unwind(|| Color::from(val));
assert!(result.is_err());
// /// Draw a single Pixel with `color` }
// /// }
// /// limited to i16::max images (buffer_size) at the moment
// pub fn draw_pixel(&mut self, x: u16, y: u16, color: &Color) { // test buffer length
// let (idx, bit) = match self.rotation { #[test]
// Displayorientation::Rotate0 | Displayorientation::Rotate180 => ( fn graphics_4in2_size() {
// (x as usize / 8 + (self.width as usize / 8) * y as usize), let display = DisplayEink42BlackWhite::default();
// 0x80 >> (x % 8), assert_eq!(display.buffer().len(), 15000);
// ), }
// Displayorientation::Rotate90 | Displayorientation::Rotate270 => (
// y as usize / 8 * self.width as usize + x as usize, // test default background color on all bytes
// 0x80 >> (y % 8), #[test]
// ), fn graphics_4in2_default() {
// }; let display = DisplayEink42BlackWhite::default();
use epd4in2;
// if idx >= self.buffer.len() { for &byte in display.buffer() {
// return; assert_eq!(byte, epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value());
// } }
}
// match color {
// Color::Black => { #[test]
// self.buffer[idx] &= !bit; fn graphics_4in2_rotation_0() {
// } let mut display = DisplayEink42BlackWhite::default();
// Color::White => { display.draw(
// self.buffer[idx] |= bit; Line::new(Coord::new(0, 0), Coord::new(7, 0))
// } .with_stroke(Some(Color::Black))
// } .into_iter(),
// } );
// /// Draw a single Pixel with `color` let buffer = display.buffer();
// ///
// /// limited to i16::max images (buffer_size) at the moment assert_eq!(buffer[0], Color::Black.get_byte_value());
// #[allow(dead_code)]
// fn draw_byte(&mut self, x: u16, y: u16, filling: u8, color: &Color) { for &byte in buffer.iter().skip(1) {
// let idx = match self.rotation { assert_eq!(byte, epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value());
// Displayorientation::Rotate0 | Displayorientation::Rotate180 => { }
// x as usize / 8 + (self.width as usize / 8) * y as usize }
// },
// Displayorientation::Rotate90 | Displayorientation::Rotate270 => { #[test]
// y as usize / 8 + (self.width as usize / 8) * x as usize fn graphics_4in2_rotation_90() {
// }, let mut display = DisplayEink42BlackWhite::default();
// }; display.set_rotation(DisplayRotation::Rotate90);
display.draw(
// if idx >= self.buffer.len() { Line::new(Coord::new(0, 392), Coord::new(0, 399))
// return; .with_stroke(Some(Color::Black))
// } .into_iter(),
);
// match color {
// Color::Black => { let buffer = display.buffer();
// self.buffer[idx] = !filling;
// }, assert_eq!(buffer[0], Color::Black.get_byte_value());
// Color::White => {
// self.buffer[idx] = filling; for &byte in buffer.iter().skip(1) {
// } assert_eq!(byte, epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value());
// } }
// } }
//TODO: write tests #[test]
fn graphics_4in2_rotation_180() {
let mut display = DisplayEink42BlackWhite::default();
display.set_rotation(DisplayRotation::Rotate180);
display.draw(
Line::new(Coord::new(392, 299), Coord::new(399, 299))
.with_stroke(Some(Color::Black))
.into_iter(),
);
let buffer = display.buffer();
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());
}
}
#[test]
fn graphics_4in2_rotation_270() {
let mut display = DisplayEink42BlackWhite::default();
display.set_rotation(DisplayRotation::Rotate270);
display.draw(
Line::new(Coord::new(299, 0), Coord::new(299, 0))
.with_stroke(Some(Color::Black))
.into_iter(),
);
let buffer = display.buffer();
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());
}
}
}

5
src/epd1in54/mod.rs

@ -29,7 +29,10 @@ use hal::{
digital::*, digital::*,
}; };
use type_a::{command::Command, LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE}; use type_a::{
command::Command,
constants::{LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE}
};
use color::Color; use color::Color;

5
src/epd2in9/mod.rs

@ -28,7 +28,10 @@ use hal::{
digital::*, digital::*,
}; };
use type_a::{command::Command, LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE}; use type_a::{
command::Command,
constants::{LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE}
};
use color::Color; use color::Color;

12
src/interface.rs

@ -100,8 +100,18 @@ where
{ {
// activate spi with cs low // activate spi with cs low
self.cs.set_low(); self.cs.set_low();
// transfer spi data // transfer spi data
spi.write(data)?; // Be careful!! Linux has a default limit of 4096 bytes per spi transfer
// see https://raspberrypi.stackexchange.com/questions/65595/spi-transfer-fails-with-buffer-size-greater-than-4096
if cfg!(target_os = "linux") {
for data_chunk in data.chunks(4096) {
spi.write(data_chunk)?;
}
} else {
spi.write(data)?;
}
// deativate spi with cs high // deativate spi with cs high
self.cs.set_high(); self.cs.set_high();

14
src/type_a/constants.rs

@ -0,0 +1,14 @@
// Original Waveforms from Waveshare
pub(crate) const LUT_FULL_UPDATE: [u8; 30] =[
0x02, 0x02, 0x01, 0x11, 0x12, 0x12, 0x22, 0x22,
0x66, 0x69, 0x69, 0x59, 0x58, 0x99, 0x99, 0x88,
0x00, 0x00, 0x00, 0x00, 0xF8, 0xB4, 0x13, 0x51,
0x35, 0x51, 0x51, 0x19, 0x01, 0x00
];
pub(crate) const LUT_PARTIAL_UPDATE: [u8; 30] =[
0x10, 0x18, 0x18, 0x08, 0x18, 0x18, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x13, 0x14, 0x44, 0x12,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
];

16
src/type_a/mod.rs

@ -1,16 +1,2 @@
pub(crate) mod command; pub(crate) mod command;
pub(crate) mod constants;
// Original Waveforms from Waveshare
pub(crate) const LUT_FULL_UPDATE: [u8; 30] =[
0x02, 0x02, 0x01, 0x11, 0x12, 0x12, 0x22, 0x22,
0x66, 0x69, 0x69, 0x59, 0x58, 0x99, 0x99, 0x88,
0x00, 0x00, 0x00, 0x00, 0xF8, 0xB4, 0x13, 0x51,
0x35, 0x51, 0x51, 0x19, 0x01, 0x00
];
pub(crate) const LUT_PARTIAL_UPDATE: [u8; 30] =[
0x10, 0x18, 0x18, 0x08, 0x18, 0x18, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x13, 0x14, 0x44, 0x12,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
];
Loading…
Cancel
Save