Renamed drawing to graphics and extracted display-specific stuff into the display folders
parent
b7275198e1
commit
8922707bac
|
|
@ -6,9 +6,9 @@ extern crate eink_waveshare_rs;
|
||||||
|
|
||||||
|
|
||||||
use eink_waveshare_rs::{
|
use eink_waveshare_rs::{
|
||||||
EPD4in2,
|
EPD4in2,
|
||||||
drawing_old::{Graphics},
|
DisplayEink42BlackWhite,
|
||||||
drawing::{DisplayEink42BlackWhite, Display, DisplayRotation},
|
graphics::{Display, DisplayRotation},
|
||||||
color::Color,
|
color::Color,
|
||||||
WaveshareDisplay,
|
WaveshareDisplay,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,39 +1,21 @@
|
||||||
|
use graphics::{
|
||||||
|
outside_display,
|
||||||
|
rotation,
|
||||||
|
DisplayRotation,
|
||||||
|
Display
|
||||||
|
};
|
||||||
use color::Color;
|
use color::Color;
|
||||||
use embedded_graphics::prelude::*;
|
use embedded_graphics::prelude::*;
|
||||||
|
|
||||||
/// Displayrotation
|
use epd4in2::constants::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT};
|
||||||
#[derive(Clone, Copy)]
|
|
||||||
pub enum DisplayRotation {
|
|
||||||
/// No rotation
|
|
||||||
Rotate0,
|
|
||||||
/// Rotate by 90 degrees clockwise
|
|
||||||
Rotate90,
|
|
||||||
/// Rotate by 180 degrees clockwise
|
|
||||||
Rotate180,
|
|
||||||
/// Rotate 270 degrees clockwise
|
|
||||||
Rotate270,
|
|
||||||
}
|
|
||||||
impl Default for DisplayRotation {
|
|
||||||
fn default() -> Self {
|
|
||||||
DisplayRotation::Rotate0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Display {
|
pub struct DisplayEink42BlackWhite {
|
||||||
fn buffer(&self) -> &[u8];
|
buffer: [u8; WIDTH as usize * HEIGHT as usize / 8],
|
||||||
fn set_rotation(&mut self, rotation: DisplayRotation);
|
|
||||||
fn rotation(&self) -> DisplayRotation;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub struct DisplayEink42BlackWhite {
|
|
||||||
buffer: [u8; 400 * 300 / 8],
|
|
||||||
rotation: DisplayRotation,
|
rotation: DisplayRotation,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for DisplayEink42BlackWhite {
|
impl Default for DisplayEink42BlackWhite {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
use epd4in2::constants::{DEFAULT_BACKGROUND_COLOR, WIDTH, HEIGHT};
|
|
||||||
DisplayEink42BlackWhite {
|
DisplayEink42BlackWhite {
|
||||||
buffer: [
|
buffer: [
|
||||||
DEFAULT_BACKGROUND_COLOR.get_byte_value();
|
DEFAULT_BACKGROUND_COLOR.get_byte_value();
|
||||||
|
|
@ -62,8 +44,6 @@ impl Drawing<Color> for DisplayEink42BlackWhite {
|
||||||
where
|
where
|
||||||
T: Iterator<Item = Pixel<Color>>
|
T: Iterator<Item = Pixel<Color>>
|
||||||
{
|
{
|
||||||
use epd4in2::constants::{WIDTH, HEIGHT};
|
|
||||||
|
|
||||||
let width = WIDTH as u32;
|
let width = WIDTH as u32;
|
||||||
let height = HEIGHT as u32;
|
let height = HEIGHT as u32;
|
||||||
|
|
||||||
|
|
@ -87,45 +67,6 @@ impl Drawing<Color> for DisplayEink42BlackWhite {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn outside_display(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) -> bool {
|
|
||||||
match rotation {
|
|
||||||
DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => {
|
|
||||||
if x >= width || y >= height {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
DisplayRotation::Rotate90 | DisplayRotation::Rotate270 => {
|
|
||||||
if y >= width || x >= height {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rotation(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) -> (u32, u8) {
|
|
||||||
match rotation {
|
|
||||||
DisplayRotation::Rotate0 => (
|
|
||||||
x / 8 + (width / 8) * y,
|
|
||||||
0x80 >> (x % 8),
|
|
||||||
),
|
|
||||||
DisplayRotation::Rotate90 => (
|
|
||||||
(width - 1 - y) / 8 + (width / 8) * x,
|
|
||||||
0x01 << (y % 8),
|
|
||||||
),
|
|
||||||
DisplayRotation::Rotate180 => (
|
|
||||||
((width / 8) * height - 1) - (x / 8 + (width / 8) * y),
|
|
||||||
0x01 << (x % 8),
|
|
||||||
),
|
|
||||||
DisplayRotation::Rotate270 => (
|
|
||||||
y / 8 + (height - 1 - x) * (width / 8),
|
|
||||||
0x80 >> (y % 8),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
@ -133,42 +74,16 @@ mod tests {
|
||||||
use embedded_graphics::coord::Coord;
|
use embedded_graphics::coord::Coord;
|
||||||
use embedded_graphics::primitives::Line;
|
use embedded_graphics::primitives::Line;
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn rotation_overflow() {
|
|
||||||
use epd4in2::constants::{WIDTH, HEIGHT};
|
|
||||||
let width = WIDTH as u32;
|
|
||||||
let height = HEIGHT as u32;
|
|
||||||
test_rotation_overflow(width, height, DisplayRotation::Rotate0);
|
|
||||||
test_rotation_overflow(width, height, DisplayRotation::Rotate90);
|
|
||||||
test_rotation_overflow(width, height, DisplayRotation::Rotate180);
|
|
||||||
test_rotation_overflow(width, height, DisplayRotation::Rotate270);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
fn test_rotation_overflow(width: u32, height: u32, rotation2: DisplayRotation) {
|
|
||||||
let max_value = width / 8 * height;
|
|
||||||
for x in 0..(width + height) { //limit x because it runs too long
|
|
||||||
for y in 0..(u32::max_value()) {
|
|
||||||
if outside_display(x, y, width, height, rotation2) {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
let (idx, _) = rotation(x, y, width, height, rotation2);
|
|
||||||
assert!(idx < max_value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// test buffer length
|
// test buffer length
|
||||||
#[test]
|
#[test]
|
||||||
fn graphics_4in2_size() {
|
fn graphics_size() {
|
||||||
let display = DisplayEink42BlackWhite::default();
|
let display = DisplayEink42BlackWhite::default();
|
||||||
assert_eq!(display.buffer().len(), 15000);
|
assert_eq!(display.buffer().len(), 15000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// test default background color on all bytes
|
// test default background color on all bytes
|
||||||
#[test]
|
#[test]
|
||||||
fn graphics_4in2_default() {
|
fn graphics_default() {
|
||||||
let display = DisplayEink42BlackWhite::default();
|
let display = DisplayEink42BlackWhite::default();
|
||||||
use epd4in2;
|
use epd4in2;
|
||||||
for &byte in display.buffer() {
|
for &byte in display.buffer() {
|
||||||
|
|
@ -177,7 +92,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn graphics_4in2_rotation_0() {
|
fn graphics_rotation_0() {
|
||||||
let mut display = DisplayEink42BlackWhite::default();
|
let mut display = DisplayEink42BlackWhite::default();
|
||||||
display.draw(
|
display.draw(
|
||||||
Line::new(Coord::new(0, 0), Coord::new(7, 0))
|
Line::new(Coord::new(0, 0), Coord::new(7, 0))
|
||||||
|
|
@ -195,7 +110,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn graphics_4in2_rotation_90() {
|
fn graphics_rotation_90() {
|
||||||
let mut display = DisplayEink42BlackWhite::default();
|
let mut display = DisplayEink42BlackWhite::default();
|
||||||
display.set_rotation(DisplayRotation::Rotate90);
|
display.set_rotation(DisplayRotation::Rotate90);
|
||||||
display.draw(
|
display.draw(
|
||||||
|
|
@ -214,7 +129,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn graphics_4in2_rotation_180() {
|
fn graphics_rotation_180() {
|
||||||
let mut display = DisplayEink42BlackWhite::default();
|
let mut display = DisplayEink42BlackWhite::default();
|
||||||
display.set_rotation(DisplayRotation::Rotate180);
|
display.set_rotation(DisplayRotation::Rotate180);
|
||||||
display.draw(
|
display.draw(
|
||||||
|
|
@ -237,7 +152,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn graphics_4in2_rotation_270() {
|
fn graphics_rotation_270() {
|
||||||
let mut display = DisplayEink42BlackWhite::default();
|
let mut display = DisplayEink42BlackWhite::default();
|
||||||
display.set_rotation(DisplayRotation::Rotate270);
|
display.set_rotation(DisplayRotation::Rotate270);
|
||||||
display.draw(
|
display.draw(
|
||||||
|
|
@ -60,9 +60,12 @@ use self::constants::*;
|
||||||
|
|
||||||
use color::Color;
|
use color::Color;
|
||||||
|
|
||||||
pub mod command;
|
pub(crate) mod command;
|
||||||
use self::command::Command;
|
use self::command::Command;
|
||||||
|
|
||||||
|
pub mod graphics;
|
||||||
|
|
||||||
|
|
||||||
/// EPD4in2 driver
|
/// EPD4in2 driver
|
||||||
///
|
///
|
||||||
pub struct EPD4in2<SPI, CS, BUSY, DC, RST> {
|
pub struct EPD4in2<SPI, CS, BUSY, DC, RST> {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,95 @@
|
||||||
|
/// Displayrotation
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub enum DisplayRotation {
|
||||||
|
/// No rotation
|
||||||
|
Rotate0,
|
||||||
|
/// Rotate by 90 degrees clockwise
|
||||||
|
Rotate90,
|
||||||
|
/// Rotate by 180 degrees clockwise
|
||||||
|
Rotate180,
|
||||||
|
/// Rotate 270 degrees clockwise
|
||||||
|
Rotate270,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for DisplayRotation {
|
||||||
|
fn default() -> Self {
|
||||||
|
DisplayRotation::Rotate0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Display {
|
||||||
|
fn buffer(&self) -> &[u8];
|
||||||
|
fn set_rotation(&mut self, rotation: DisplayRotation);
|
||||||
|
fn rotation(&self) -> DisplayRotation;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub(crate) fn outside_display(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) -> bool {
|
||||||
|
match rotation {
|
||||||
|
DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => {
|
||||||
|
if x >= width || y >= height {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
DisplayRotation::Rotate90 | DisplayRotation::Rotate270 => {
|
||||||
|
if y >= width || x >= height {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn rotation(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) -> (u32, u8) {
|
||||||
|
match rotation {
|
||||||
|
DisplayRotation::Rotate0 => (
|
||||||
|
x / 8 + (width / 8) * y,
|
||||||
|
0x80 >> (x % 8),
|
||||||
|
),
|
||||||
|
DisplayRotation::Rotate90 => (
|
||||||
|
(width - 1 - y) / 8 + (width / 8) * x,
|
||||||
|
0x01 << (y % 8),
|
||||||
|
),
|
||||||
|
DisplayRotation::Rotate180 => (
|
||||||
|
((width / 8) * height - 1) - (x / 8 + (width / 8) * y),
|
||||||
|
0x01 << (x % 8),
|
||||||
|
),
|
||||||
|
DisplayRotation::Rotate270 => (
|
||||||
|
y / 8 + (height - 1 - x) * (width / 8),
|
||||||
|
0x80 >> (y % 8),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{DisplayRotation, outside_display, rotation};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rotation_overflow() {
|
||||||
|
use epd4in2::constants::{WIDTH, HEIGHT};
|
||||||
|
let width = WIDTH as u32;
|
||||||
|
let height = HEIGHT as u32;
|
||||||
|
test_rotation_overflow(width, height, DisplayRotation::Rotate0);
|
||||||
|
test_rotation_overflow(width, height, DisplayRotation::Rotate90);
|
||||||
|
test_rotation_overflow(width, height, DisplayRotation::Rotate180);
|
||||||
|
test_rotation_overflow(width, height, DisplayRotation::Rotate270);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_rotation_overflow(width: u32, height: u32, rotation2: DisplayRotation) {
|
||||||
|
let max_value = width / 8 * height;
|
||||||
|
for x in 0..(width + height) { //limit x because it runs too long
|
||||||
|
for y in 0..(u32::max_value()) {
|
||||||
|
if outside_display(x, y, width, height, rotation2) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
let (idx, _) = rotation(x, y, width, height, rotation2);
|
||||||
|
assert!(idx < max_value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -48,7 +48,7 @@ extern crate embedded_hal as hal;
|
||||||
use hal::spi::{Mode, Phase, Polarity};
|
use hal::spi::{Mode, Phase, Polarity};
|
||||||
|
|
||||||
#[cfg(feature = "graphics")]
|
#[cfg(feature = "graphics")]
|
||||||
pub mod drawing;
|
pub mod graphics;
|
||||||
|
|
||||||
//TODO: remove old drawing support
|
//TODO: remove old drawing support
|
||||||
#[cfg(feature = "graphics")]
|
#[cfg(feature = "graphics")]
|
||||||
|
|
@ -66,6 +66,8 @@ mod interface;
|
||||||
mod epd4in2;
|
mod epd4in2;
|
||||||
#[cfg(feature = "epd4in2")]
|
#[cfg(feature = "epd4in2")]
|
||||||
pub use epd4in2::EPD4in2;
|
pub use epd4in2::EPD4in2;
|
||||||
|
#[cfg(feature = "epd4in2")]
|
||||||
|
pub use epd4in2::graphics::DisplayEink42BlackWhite;
|
||||||
|
|
||||||
#[cfg(feature = "epd1in54")]
|
#[cfg(feature = "epd1in54")]
|
||||||
mod epd1in54;
|
mod epd1in54;
|
||||||
|
|
|
||||||
|
|
@ -56,8 +56,6 @@ where
|
||||||
DC: OutputPin,
|
DC: OutputPin,
|
||||||
RST: OutputPin,
|
RST: OutputPin,
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC
|
/// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC
|
||||||
///
|
///
|
||||||
/// This already initialises the device. That means [init()](WaveshareInterface::init()) isn't needed directly afterwards
|
/// This already initialises the device. That means [init()](WaveshareInterface::init()) isn't needed directly afterwards
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue