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

This commit is contained in:
Chris
2018-10-18 16:10:10 +02:00
9 changed files with 286 additions and 113 deletions
+128 -92
View File
@@ -19,29 +19,18 @@ impl Default for DisplayRotation {
}
}
pub enum Display {
Eink42BlackWhite,
}
impl Display {
/// 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 Display {
fn buffer(&self) -> &[u8];
fn set_rotation(&mut self, rotation: DisplayRotation);
fn rotation(&self) -> DisplayRotation;
}
pub trait Buffer {
fn get_buffer(&self) -> &[u8];
}
pub struct DisplayEink42BlackWhite {
buffer: [u8; 400 * 300 / 8],
rotation: DisplayRotation, //TODO: check embedded_graphics for orientation
}
impl Default for DisplayEink42BlackWhite {
fn default() -> Self {
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
}
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)
where
T: Iterator<Item = Pixel<u8>>
T: Iterator<Item = Pixel<Color>>
{
use epd4in2::constants::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT};
for Pixel(UnsignedCoord(x,y), color) in item_pixels {
@@ -81,7 +78,7 @@ impl Drawing<u8> for DisplayEink42BlackWhite {
return;
}
match Color::from(color) {
match color {
Color::Black => {
self.buffer[idx] &= !bit;
}
@@ -93,83 +90,122 @@ impl Drawing<u8> for DisplayEink42BlackWhite {
}
}
// impl Drawing<u8> for DisplayRibbonLeft {
// fn draw<T>(&mut self, item_pixels: T)
// where
// T: Iterator<Item = Pixel<u8>>,
// {
// for Pixel(UnsignedCoord(x, y), color) in item_pixels {
// if y > 127 || x > 295 {
// continue;
// }
// let cell = &mut self.0[y as usize / 8 + (295 - x as usize) * 128 / 8];
// let bit = 7 - y % 8;
// if color != 0 {
// *cell &= !(1 << bit);
// } else {
// *cell |= 1 << bit;
// }
// }
// }
// }
//TODO: write tests
#[cfg(test)]
mod tests {
use super::*;
use epd4in2;
use embedded_graphics::coord::Coord;
use embedded_graphics::fonts::Font6x8;
use embedded_graphics::prelude::*;
use embedded_graphics::primitives::{Circle, Line};
#[test]
fn from_u8() {
assert_eq!(Color::Black, Color::from(0u8));
assert_eq!(Color::White, Color::from(1u8));
}
// 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) {
// let (idx, bit) = match self.rotation {
// Displayorientation::Rotate0 | Displayorientation::Rotate180 => (
// (x as usize / 8 + (self.width as usize / 8) * y as usize),
// 0x80 >> (x % 8),
// ),
// Displayorientation::Rotate90 | Displayorientation::Rotate270 => (
// y as usize / 8 * self.width as usize + x as usize,
// 0x80 >> (y % 8),
// ),
// };
// test buffer length
#[test]
fn graphics_4in2_size() {
let display = DisplayEink42BlackWhite::default();
assert_eq!(display.buffer().len(), 15000);
}
// test default background color on all bytes
#[test]
fn graphics_4in2_default() {
let display = DisplayEink42BlackWhite::default();
use epd4in2;
for &byte in display.buffer() {
assert_eq!(byte, epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value());
}
}
// if idx >= self.buffer.len() {
// return;
// }
#[test]
fn graphics_4in2_rotation_0() {
let mut display = DisplayEink42BlackWhite::default();
display.draw(
Line::new(Coord::new(0, 0), Coord::new(7, 0))
.with_stroke(Some(Color::Black))
.into_iter(),
);
let buffer = display.buffer();
// match color {
// Color::Black => {
// self.buffer[idx] &= !bit;
// }
// Color::White => {
// self.buffer[idx] |= bit;
// }
// }
// }
assert_eq!(buffer[0], Color::Black.get_byte_value());
// /// Draw a single Pixel with `color`
// ///
// /// limited to i16::max images (buffer_size) at the moment
// #[allow(dead_code)]
// fn draw_byte(&mut self, x: u16, y: u16, filling: u8, color: &Color) {
// let idx = match self.rotation {
// Displayorientation::Rotate0 | Displayorientation::Rotate180 => {
// x as usize / 8 + (self.width as usize / 8) * y as usize
// },
// Displayorientation::Rotate90 | Displayorientation::Rotate270 => {
// y as usize / 8 + (self.width as usize / 8) * x as usize
// },
// };
for &byte in buffer.iter().skip(1) {
assert_eq!(byte, epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value());
}
}
// if idx >= self.buffer.len() {
// return;
// }
#[test]
fn graphics_4in2_rotation_90() {
let mut display = DisplayEink42BlackWhite::default();
display.set_rotation(DisplayRotation::Rotate90);
display.draw(
Line::new(Coord::new(0, 392), Coord::new(0, 399))
.with_stroke(Some(Color::Black))
.into_iter(),
);
let buffer = display.buffer();
// match color {
// Color::Black => {
// self.buffer[idx] = !filling;
// },
// Color::White => {
// self.buffer[idx] = filling;
// }
// }
// }
assert_eq!(buffer[0], Color::Black.get_byte_value());
//TODO: write tests
for &byte in buffer.iter().skip(1) {
assert_eq!(byte, epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value());
}
}
#[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());
}
}
}
+4 -1
View File
@@ -29,7 +29,10 @@ use hal::{
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;
+4 -1
View File
@@ -28,7 +28,10 @@ use hal::{
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;
+11 -1
View File
@@ -100,8 +100,18 @@ where
{
// activate spi with cs low
self.cs.set_low();
// 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
self.cs.set_high();
+14
View File
@@ -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
];
+1 -15
View File
@@ -1,16 +1,2 @@
pub(crate) mod command;
// 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
];
pub(crate) mod constants;