Browse Source

Add from_u8 to color, finish drawing for 4in2

embedded-hal-1.0
Chris 7 years ago
parent
commit
29b7986848
  1. 53
      src/color.rs
  2. 24
      src/drawing.rs
  3. 2
      src/epd4in2/mod.rs
  4. 3
      src/lib.rs

53
src/color.rs

@ -1,3 +1,6 @@
use embedded_graphics::prelude::*;
/// Only for the B/W Displays atm /// Only for the B/W Displays atm
#[derive(Clone, Copy, PartialEq, Debug)] #[derive(Clone, Copy, PartialEq, Debug)]
pub enum Color { pub enum Color {
@ -22,6 +25,14 @@ impl Color {
} }
} }
fn from_u8(val: u8) -> Self {
match val {
0 => Color::Black,
1 => Color::White,
e => panic!("DisplayColor only parses 0 and 1 (Black and White) and not `{}`", e),
}
}
/// Get the color encoding of a specific bit in a byte /// Get the color encoding of a specific bit in a byte
/// ///
/// input is the byte where one bit is gonna be selected /// input is the byte where one bit is gonna be selected
@ -73,3 +84,45 @@ impl Color {
} }
} }
} }
impl PixelColor for Color {}
impl From<u8> for Color {
fn from(value: u8) -> Self {
Color::from_u8(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[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());
}
}
#[test]
fn u8_conversion_black() {
assert_eq!(Color::from(Color::Black.get_bit_value()), Color::Black);
assert_eq!(Color::from(0u8).get_bit_value(), 0u8);
}
#[test]
fn u8_conversion_white() {
assert_eq!(Color::from(Color::White.get_bit_value()), Color::White);
assert_eq!(Color::from(1u8).get_bit_value(), 1u8);
}
}

24
src/drawing.rs

@ -1,4 +1,5 @@
use color::Color; use color::Color;
use embedded_graphics::prelude::*;
/// Displayrotation /// Displayrotation
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
@ -12,9 +13,9 @@ pub enum DisplayRotation {
/// Rotate 270 degrees clockwise /// Rotate 270 degrees clockwise
Rotate270, Rotate270,
} }
impl Default for Displayorientation { impl Default for DisplayRotation {
fn default() -> Self { fn default() -> Self {
Displayorientation::Rotate0 DisplayRotation::Rotate0
} }
} }
@ -39,15 +40,15 @@ pub trait Buffer {
pub struct DisplayEink42BlackWhite { pub struct DisplayEink42BlackWhite {
buffer: [u8; 400 * 300 / 8], buffer: [u8; 400 * 300 / 8],
rotation: Displayorientation, //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::*; use epd4in2::constants::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT};
DisplayEink42BlackWhite { DisplayEink42BlackWhite {
buffer: [ buffer: [
DEFAULT_BACKGROUND_COLOR.get_full_byte(), DEFAULT_BACKGROUND_COLOR.get_byte_value();
WIDTH * HEIGHT / 8 WIDTH as usize * HEIGHT as usize / 8
], ],
rotation: DisplayRotation::default() rotation: DisplayRotation::default()
} }
@ -63,14 +64,15 @@ impl Drawing<u8> for DisplayEink42BlackWhite {
where where
T: Iterator<Item = Pixel<u8>> T: Iterator<Item = Pixel<u8>>
{ {
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 {
let (idx, bit) = match self.rotation { let (idx, bit) = match self.rotation {
Displayorientation::Rotate0 | Displayorientation::Rotate180 => ( DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => (
(x as usize / 8 + (self.width as usize / 8) * y as usize), (x as usize / 8 + (WIDTH as usize / 8) * y as usize),
0x80 >> (x % 8), 0x80 >> (x % 8),
), ),
Displayorientation::Rotate90 | Displayorientation::Rotate270 => ( DisplayRotation::Rotate90 | DisplayRotation::Rotate270 => (
y as usize / 8 * self.width as usize + x as usize, y as usize / 8 * WIDTH as usize + x as usize,
0x80 >> (y % 8), 0x80 >> (y % 8),
), ),
}; };
@ -79,7 +81,7 @@ impl Drawing<u8> for DisplayEink42BlackWhite {
return; return;
} }
match color { match Color::from(color) {
Color::Black => { Color::Black => {
self.buffer[idx] &= !bit; self.buffer[idx] &= !bit;
} }

2
src/epd4in2/mod.rs

@ -55,7 +55,7 @@ use traits::{WaveshareDisplay, InternalWiAdditions};
use interface::DisplayInterface; use interface::DisplayInterface;
//The Lookup Tables for the Display //The Lookup Tables for the Display
mod constants; pub(crate) mod constants; //TODO: Limit to crate::drawing
use self::constants::*; use self::constants::*;
use color::Color; use color::Color;

3
src/lib.rs

@ -50,6 +50,8 @@ use hal::spi::{Mode, Phase, Polarity};
#[cfg(feature = "graphics")] #[cfg(feature = "graphics")]
pub mod drawing; pub mod drawing;
pub(crate) mod drawing_old;
mod traits; mod traits;
pub use traits::{WaveshareDisplay}; pub use traits::{WaveshareDisplay};
@ -79,7 +81,6 @@ pub use epd2in9::EPD2in9;
pub(crate) mod type_a; pub(crate) mod type_a;
extern crate embedded_graphics; extern crate embedded_graphics;
use embedded_graphics;
//TODO: test spi mode //TODO: test spi mode
/// SPI mode - /// SPI mode -

Loading…
Cancel
Save