Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EPD 4in2bc support #144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/epd4in2_variable_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ fn main() -> Result<(), std::io::Error> {
let (x, y, width, height) = (50, 50, 250, 250);

let mut buffer = [epd4in2::DEFAULT_BACKGROUND_COLOR.get_byte_value(); 62500]; //250*250
let mut display = VarDisplay::new(width, height, &mut buffer, false).unwrap();
let mut display =
VarDisplay::new(width, height, &mut buffer, DisplayMode::BwrBitOff as u8).unwrap();
display.set_rotation(DisplayRotation::Rotate0);
draw_text(&mut display, "Rotate 0!", 5, 50);

Expand Down
72 changes: 39 additions & 33 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! EPD representation of multicolor with separate buffers
//! for each bit makes it hard to properly represent colors here

use crate::prelude::DisplayMode;
#[cfg(feature = "graphics")]
use embedded_graphics_core::pixelcolor::BinaryColor;
#[cfg(feature = "graphics")]
Expand Down Expand Up @@ -77,21 +78,21 @@ pub trait ColorType: PixelColor {

/// Return the data used to set a pixel color
///
/// * bwrbit is used to tell the value of the unused bit when a chromatic
/// * MODE is used to tell the value of the unused bit when a chromatic
/// color is set (TriColor only as for now)
/// * pos is the pixel position in the line, used to know which pixels must be set
///
/// Return values are :
/// * .0 is the mask used to exclude this pixel from the byte (eg: 0x7F in BiColor)
/// * .1 are the bits used to set the color in the byte (eg: 0x80 in BiColor)
/// this is u16 because we set 2 bytes in case of split buffer
fn bitmask(&self, bwrbit: bool, pos: u32) -> (u8, u16);
fn bitmask(&self, mode: DisplayMode, pos: u32) -> (u8, u16);
}

impl ColorType for Color {
const BITS_PER_PIXEL_PER_BUFFER: usize = 1;
const BUFFER_COUNT: usize = 1;
fn bitmask(&self, _bwrbit: bool, pos: u32) -> (u8, u16) {
fn bitmask(&self, _mode: DisplayMode, pos: u32) -> (u8, u16) {
let bit = 0x80 >> (pos % 8);
match self {
Color::Black => (!bit, 0u16),
Expand All @@ -103,27 +104,34 @@ impl ColorType for Color {
impl ColorType for TriColor {
const BITS_PER_PIXEL_PER_BUFFER: usize = 1;
const BUFFER_COUNT: usize = 2;
fn bitmask(&self, bwrbit: bool, pos: u32) -> (u8, u16) {

fn bitmask(&self, mode: DisplayMode, pos: u32) -> (u8, u16) {
let bit = 0x80 >> (pos % 8);
match self {
TriColor::Black => (!bit, 0u16),
TriColor::White => (!bit, bit as u16),
TriColor::Chromatic => (
!bit,
if bwrbit {
(bit as u16) << 8
} else {
(bit as u16) << 8 | bit as u16
},
),
}
let mask = match mode {
DisplayMode::BwrBitOnColorInverted => match self {
TriColor::Black => (bit as u16) << 8,
TriColor::White => (bit as u16) << 8 | bit as u16,
TriColor::Chromatic => 0u16,
},
DisplayMode::BwrBitOn => match self {
TriColor::Black => 0u16,
TriColor::White => bit as u16,
TriColor::Chromatic => (bit as u16) << 8,
},
DisplayMode::BwrBitOff => match self {
TriColor::Black => 0u16,
TriColor::White => bit as u16,
TriColor::Chromatic => (bit as u16) << 8 | bit as u16,
},
};
(!bit, mask)
}
}

impl ColorType for OctColor {
const BITS_PER_PIXEL_PER_BUFFER: usize = 4;
const BUFFER_COUNT: usize = 1;
fn bitmask(&self, _bwrbit: bool, pos: u32) -> (u8, u16) {
fn bitmask(&self, _mode: DisplayMode, pos: u32) -> (u8, u16) {
let mask = !(0xF0 >> (pos % 2));
let bits = self.get_nibble() as u16;
(mask, if pos % 2 == 1 { bits } else { bits << 4 })
Expand Down Expand Up @@ -307,16 +315,16 @@ impl From<BinaryColor> for Color {
impl From<embedded_graphics_core::pixelcolor::Rgb888> for Color {
fn from(rgb: embedded_graphics_core::pixelcolor::Rgb888) -> Self {
use embedded_graphics_core::pixelcolor::RgbColor;
if rgb == RgbColor::BLACK {
Color::Black
} else if rgb == RgbColor::WHITE {
Color::White
} else {
// choose closest color
if (rgb.r() as u16 + rgb.g() as u16 + rgb.b() as u16) > 255 * 3 / 2 {
Color::White
} else {
Color::Black
match rgb {
RgbColor::BLACK => Color::Black,
RgbColor::WHITE => Color::White,
_ => {
// choose closest color
if (rgb.r() as u16 + rgb.g() as u16 + rgb.b() as u16) > 255 * 3 / 2 {
Color::White
} else {
Color::Black
}
}
}
}
Expand Down Expand Up @@ -369,13 +377,11 @@ impl From<BinaryColor> for TriColor {
impl From<embedded_graphics_core::pixelcolor::Rgb888> for TriColor {
fn from(rgb: embedded_graphics_core::pixelcolor::Rgb888) -> Self {
use embedded_graphics_core::pixelcolor::RgbColor;
if rgb == RgbColor::BLACK {
TriColor::Black
} else if rgb == RgbColor::WHITE {
TriColor::White
} else {
match rgb {
RgbColor::BLACK => TriColor::Black,
RgbColor::WHITE => TriColor::White,
// there is no good approximation here since we don't know which color is 'chromatic'
TriColor::Chromatic
_ => TriColor::Chromatic,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/epd1in54/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ use crate::interface::DisplayInterface;
pub type Display1in54 = crate::graphics::Display<
WIDTH,
HEIGHT,
false,
{ crate::graphics::DisplayMode::BwrBitOff as u8 },
{ buffer_len(WIDTH as usize, HEIGHT as usize) },
Color,
>;
Expand Down
2 changes: 1 addition & 1 deletion src/epd1in54b/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::buffer_len;
pub type Display1in54b = crate::graphics::Display<
WIDTH,
HEIGHT,
false,
{ crate::graphics::DisplayMode::BwrBitOff as u8 },
{ buffer_len(WIDTH as usize, HEIGHT as usize) },
Color,
>;
Expand Down
2 changes: 1 addition & 1 deletion src/epd1in54c/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::buffer_len;
pub type Display1in54c = crate::graphics::Display<
WIDTH,
HEIGHT,
false,
{ crate::graphics::DisplayMode::BwrBitOff as u8 },
{ buffer_len(WIDTH as usize, HEIGHT as usize) },
Color,
>;
Expand Down
2 changes: 1 addition & 1 deletion src/epd2in13_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use self::constants::{LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE};
pub type Display2in13 = crate::graphics::Display<
WIDTH,
HEIGHT,
false,
{ crate::graphics::DisplayMode::BwrBitOff as u8 },
{ buffer_len(WIDTH as usize, HEIGHT as usize) },
Color,
>;
Expand Down
2 changes: 1 addition & 1 deletion src/epd2in13bc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ use crate::buffer_len;
pub type Display2in13bc = crate::graphics::Display<
WIDTH,
HEIGHT,
true,
{ crate::graphics::DisplayMode::BwrBitOn as u8 },
{ buffer_len(WIDTH as usize, HEIGHT as usize * 2) },
TriColor,
>;
Expand Down
2 changes: 1 addition & 1 deletion src/epd2in7b/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::buffer_len;
pub type Display2in7b = crate::graphics::Display<
WIDTH,
HEIGHT,
false,
{ crate::graphics::DisplayMode::BwrBitOff as u8 },
{ buffer_len(WIDTH as usize, HEIGHT as usize) },
Color,
>;
Expand Down
2 changes: 1 addition & 1 deletion src/epd2in9/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ use crate::interface::DisplayInterface;
pub type Display2in9 = crate::graphics::Display<
WIDTH,
HEIGHT,
false,
{ crate::graphics::DisplayMode::BwrBitOff as u8 },
{ buffer_len(WIDTH as usize, HEIGHT as usize) },
Color,
>;
Expand Down
2 changes: 1 addition & 1 deletion src/epd2in9_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ use crate::traits::QuickRefresh;
pub type Display2in9 = crate::graphics::Display<
WIDTH,
HEIGHT,
false,
{ crate::graphics::DisplayMode::BwrBitOff as u8 },
{ buffer_len(WIDTH as usize, HEIGHT as usize) },
Color,
>;
Expand Down
2 changes: 1 addition & 1 deletion src/epd2in9bc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ use crate::buffer_len;
pub type Display2in9bc = crate::graphics::Display<
WIDTH,
HEIGHT,
false,
{ crate::graphics::DisplayMode::BwrBitOff as u8 },
{ buffer_len(WIDTH as usize, HEIGHT as usize) },
Color,
>;
Expand Down
2 changes: 1 addition & 1 deletion src/epd3in7/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const IS_BUSY_LOW: bool = false;
pub type Display3in7 = crate::graphics::Display<
WIDTH,
HEIGHT,
false,
{ crate::graphics::DisplayMode::BwrBitOff as u8 },
{ buffer_len(WIDTH as usize, HEIGHT as usize) },
Color,
>;
Expand Down
4 changes: 2 additions & 2 deletions src/epd4in2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use crate::interface::DisplayInterface;
use crate::traits::{InternalWiAdditions, QuickRefresh, RefreshLut, WaveshareDisplay};

//The Lookup Tables for the Display
mod constants;
pub mod constants;
use crate::epd4in2::constants::*;

/// Width of the display
Expand All @@ -80,7 +80,7 @@ use crate::buffer_len;
pub type Display4in2 = crate::graphics::Display<
WIDTH,
HEIGHT,
false,
{ crate::graphics::DisplayMode::BwrBitOff as u8 },
{ buffer_len(WIDTH as usize, HEIGHT as usize) },
Color,
>;
Expand Down
Loading