|
|
|
@ -7,84 +7,48 @@ use graphics::{
|
|
|
|
|
use color::Color; |
|
|
|
|
use embedded_graphics::prelude::*; |
|
|
|
|
|
|
|
|
|
use graphics::Graphics; |
|
|
|
|
|
|
|
|
|
use epd4in2::constants::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT}; |
|
|
|
|
|
|
|
|
|
pub struct DisplayEink42BlackWhite {
|
|
|
|
|
buffer: [u8; WIDTH as usize * HEIGHT as usize / 8], |
|
|
|
|
rotation: DisplayRotation, |
|
|
|
|
pub struct DisplayEink4in2BlackWhite { |
|
|
|
|
pub buffer: [u8; WIDTH as usize * HEIGHT as usize / 8], |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Default for DisplayEink42BlackWhite { |
|
|
|
|
impl Default for DisplayEink4in2BlackWhite { |
|
|
|
|
fn default() -> Self { |
|
|
|
|
DisplayEink42BlackWhite { |
|
|
|
|
DisplayEink4in2BlackWhite { |
|
|
|
|
buffer: [ |
|
|
|
|
DEFAULT_BACKGROUND_COLOR.get_byte_value(); |
|
|
|
|
WIDTH as usize * HEIGHT as usize / 8
|
|
|
|
|
], |
|
|
|
|
rotation: DisplayRotation::default() |
|
|
|
|
] |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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<Color> for DisplayEink42BlackWhite { |
|
|
|
|
fn draw<T>(&mut self, item_pixels: T) |
|
|
|
|
where |
|
|
|
|
T: Iterator<Item = Pixel<Color>> |
|
|
|
|
{ |
|
|
|
|
let width = WIDTH as u32; |
|
|
|
|
let height = HEIGHT as u32; |
|
|
|
|
|
|
|
|
|
for Pixel(UnsignedCoord(x,y), color) in item_pixels { |
|
|
|
|
if outside_display(x, y, width, height, self.rotation) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let (idx, bit) = rotation(x, y, width, height, self.rotation); |
|
|
|
|
|
|
|
|
|
let idx = idx as usize; |
|
|
|
|
match color { |
|
|
|
|
Color::Black => { |
|
|
|
|
self.buffer[idx] &= !bit; |
|
|
|
|
} |
|
|
|
|
Color::White => { |
|
|
|
|
self.buffer[idx] |= bit; |
|
|
|
|
} |
|
|
|
|
}
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[cfg(test)] |
|
|
|
|
mod tests { |
|
|
|
|
use super::*; |
|
|
|
|
use epd4in2; |
|
|
|
|
use graphics::Graphics; |
|
|
|
|
use embedded_graphics::coord::Coord; |
|
|
|
|
use embedded_graphics::primitives::Line; |
|
|
|
|
|
|
|
|
|
// test buffer length
|
|
|
|
|
#[test] |
|
|
|
|
fn graphics_size() { |
|
|
|
|
let display = DisplayEink42BlackWhite::default(); |
|
|
|
|
let mut display4in2 = DisplayEink4in2BlackWhite::default(); |
|
|
|
|
let display = Graphics::new(WIDTH, HEIGHT, &mut display4in2.buffer); |
|
|
|
|
assert_eq!(display.buffer().len(), 15000); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// test default background color on all bytes
|
|
|
|
|
#[test] |
|
|
|
|
fn graphics_default() { |
|
|
|
|
let display = DisplayEink42BlackWhite::default(); |
|
|
|
|
let mut display4in2 = DisplayEink4in2BlackWhite::default(); |
|
|
|
|
let mut display = Graphics::new(WIDTH, HEIGHT, &mut display4in2.buffer); |
|
|
|
|
use epd4in2; |
|
|
|
|
for &byte in display.buffer() { |
|
|
|
|
assert_eq!(byte, epd4in2::constants::DEFAULT_BACKGROUND_COLOR.get_byte_value()); |
|
|
|
@ -93,7 +57,8 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn graphics_rotation_0() { |
|
|
|
|
let mut display = DisplayEink42BlackWhite::default(); |
|
|
|
|
let mut display4in2 = DisplayEink4in2BlackWhite::default(); |
|
|
|
|
let mut display = Graphics::new(WIDTH, HEIGHT, &mut display4in2.buffer); |
|
|
|
|
display.draw( |
|
|
|
|
Line::new(Coord::new(0, 0), Coord::new(7, 0)) |
|
|
|
|
.with_stroke(Some(Color::Black)) |
|
|
|
@ -111,7 +76,8 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn graphics_rotation_90() { |
|
|
|
|
let mut display = DisplayEink42BlackWhite::default(); |
|
|
|
|
let mut display4in2 = DisplayEink4in2BlackWhite::default(); |
|
|
|
|
let mut display = Graphics::new(WIDTH, HEIGHT, &mut display4in2.buffer); |
|
|
|
|
display.set_rotation(DisplayRotation::Rotate90); |
|
|
|
|
display.draw( |
|
|
|
|
Line::new(Coord::new(0, 392), Coord::new(0, 399)) |
|
|
|
@ -130,7 +96,8 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn graphics_rotation_180() { |
|
|
|
|
let mut display = DisplayEink42BlackWhite::default(); |
|
|
|
|
let mut display4in2 = DisplayEink4in2BlackWhite::default(); |
|
|
|
|
let mut display = Graphics::new(WIDTH, HEIGHT, &mut display4in2.buffer); |
|
|
|
|
display.set_rotation(DisplayRotation::Rotate180); |
|
|
|
|
display.draw( |
|
|
|
|
Line::new(Coord::new(392, 299), Coord::new(399, 299)) |
|
|
|
@ -153,7 +120,8 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn graphics_rotation_270() { |
|
|
|
|
let mut display = DisplayEink42BlackWhite::default(); |
|
|
|
|
let mut display4in2 = DisplayEink4in2BlackWhite::default(); |
|
|
|
|
let mut display = Graphics::new(WIDTH, HEIGHT, &mut display4in2.buffer); |
|
|
|
|
display.set_rotation(DisplayRotation::Rotate270); |
|
|
|
|
display.draw( |
|
|
|
|
Line::new(Coord::new(299, 0), Coord::new(299, 7)) |
|
|
|
@ -173,4 +141,5 @@ mod tests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|