Rename Buffer Trait to Display and add a Rotation Function to it
parent
8904066de6
commit
d2a47eb05c
|
|
@ -8,7 +8,7 @@ extern crate eink_waveshare_rs;
|
||||||
use eink_waveshare_rs::{
|
use eink_waveshare_rs::{
|
||||||
EPD4in2,
|
EPD4in2,
|
||||||
drawing_old::{Graphics},
|
drawing_old::{Graphics},
|
||||||
drawing::{DisplayEink42BlackWhite, Buffer},
|
drawing::{DisplayEink42BlackWhite, Display},
|
||||||
color::Color,
|
color::Color,
|
||||||
WaveshareDisplay,
|
WaveshareDisplay,
|
||||||
};
|
};
|
||||||
|
|
@ -184,12 +184,8 @@ fn run() -> Result<(), std::io::Error> {
|
||||||
println!("Finished draw string test");
|
println!("Finished draw string test");
|
||||||
|
|
||||||
delay.delay_ms(3000u16);
|
delay.delay_ms(3000u16);
|
||||||
println!("Now test new graphics:");
|
|
||||||
|
|
||||||
let mut i = 0;
|
println!("Now test new graphics:");
|
||||||
loop {
|
|
||||||
println!("Loop {}", i);
|
|
||||||
i += 1;
|
|
||||||
let mut display = DisplayEink42BlackWhite::default();
|
let mut display = DisplayEink42BlackWhite::default();
|
||||||
display.draw(
|
display.draw(
|
||||||
Circle::new(Coord::new(64, 64), 64)
|
Circle::new(Coord::new(64, 64), 64)
|
||||||
|
|
@ -206,9 +202,30 @@ fn run() -> Result<(), std::io::Error> {
|
||||||
.with_stroke(Some(Color::Black))
|
.with_stroke(Some(Color::Black))
|
||||||
.into_iter(),
|
.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(),
|
||||||
|
);
|
||||||
|
|
||||||
|
//display.clear();
|
||||||
|
//display.set_rotation(Rotation)
|
||||||
|
|
||||||
|
let mut i = 0;
|
||||||
|
loop {
|
||||||
|
i += 1;
|
||||||
|
println!("Moving Hello World. Loop {} from 20", i);
|
||||||
|
|
||||||
display.draw(
|
display.draw(
|
||||||
Font6x8::render_str("Hello World!")
|
Font6x8::render_str("Hello World!")
|
||||||
.with_stroke(Some(Color::Black))
|
.with_stroke(Some(Color::Black))
|
||||||
|
.with_fill(Some(Color::White))
|
||||||
.translate(Coord::new(5 + i*10, 50))
|
.translate(Coord::new(5 + i*10, 50))
|
||||||
.into_iter(),
|
.into_iter(),
|
||||||
);
|
);
|
||||||
|
|
@ -221,6 +238,8 @@ fn run() -> Result<(), std::io::Error> {
|
||||||
}
|
}
|
||||||
delay.delay_ms(1_000u16);
|
delay.delay_ms(1_000u16);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
println!("Finished tests - going to sleep");
|
println!("Finished tests - going to sleep");
|
||||||
epd4in2.sleep(&mut spi)
|
epd4in2.sleep(&mut spi)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
138
src/drawing.rs
138
src/drawing.rs
|
|
@ -19,29 +19,18 @@ impl Default for DisplayRotation {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Display {
|
pub trait Display {
|
||||||
Eink42BlackWhite,
|
fn get_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,42 +43,16 @@ impl Default for DisplayEink42BlackWhite {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Buffer for DisplayEink42BlackWhite {
|
|
||||||
|
impl Display for DisplayEink42BlackWhite {
|
||||||
fn get_buffer(&self) -> &[u8] {
|
fn get_buffer(&self) -> &[u8] {
|
||||||
&self.buffer
|
&self.buffer
|
||||||
}
|
}
|
||||||
}
|
fn set_rotation(&mut self, rotation: DisplayRotation) {
|
||||||
impl Drawing<u8> for DisplayEink42BlackWhite {
|
self.rotation = rotation;
|
||||||
fn draw<T>(&mut self, item_pixels: T)
|
|
||||||
where
|
|
||||||
T: Iterator<Item = Pixel<u8>>
|
|
||||||
{
|
|
||||||
use epd4in2::constants::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT};
|
|
||||||
for Pixel(UnsignedCoord(x,y), color) in item_pixels {
|
|
||||||
let (idx, bit) = match self.rotation {
|
|
||||||
DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => (
|
|
||||||
(x as usize / 8 + (WIDTH as usize / 8) * y as usize),
|
|
||||||
0x80 >> (x % 8),
|
|
||||||
),
|
|
||||||
DisplayRotation::Rotate90 | DisplayRotation::Rotate270 => (
|
|
||||||
y as usize / 8 * WIDTH as usize + x as usize,
|
|
||||||
0x80 >> (y % 8),
|
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
if idx >= self.buffer.len() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
match Color::from(color) {
|
|
||||||
Color::Black => {
|
|
||||||
self.buffer[idx] &= !bit;
|
|
||||||
}
|
|
||||||
Color::White => {
|
|
||||||
self.buffer[idx] |= bit;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
fn rotation(&self) -> DisplayRotation {
|
||||||
|
self.rotation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -127,83 +90,4 @@ impl Drawing<Color> 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;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// /// 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),
|
|
||||||
// ),
|
|
||||||
// };
|
|
||||||
|
|
||||||
// if idx >= self.buffer.len() {
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// match color {
|
|
||||||
// Color::Black => {
|
|
||||||
// self.buffer[idx] &= !bit;
|
|
||||||
// }
|
|
||||||
// Color::White => {
|
|
||||||
// self.buffer[idx] |= bit;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// /// 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
|
|
||||||
// },
|
|
||||||
// };
|
|
||||||
|
|
||||||
// if idx >= self.buffer.len() {
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// match color {
|
|
||||||
// Color::Black => {
|
|
||||||
// self.buffer[idx] = !filling;
|
|
||||||
// },
|
|
||||||
// Color::White => {
|
|
||||||
// self.buffer[idx] = filling;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
//TODO: write tests
|
//TODO: write tests
|
||||||
Loading…
Reference in New Issue