Skip to content
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
133 changes: 133 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ pub enum TriColor {
Chromatic,
}

/// For the 6 Color Displays
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum HexColor {
/// Black Color
Black = 0x00,
/// White Color
#[default]
White = 0x01,
/// Yellow Color
Yellow = 0x02,
/// Red Color
Red = 0x03,
/// Blue Color
Blue = 0x05,
/// Green Color
Green = 0x06,
}

/// For the 7 Color Displays
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum OctColor {
Expand Down Expand Up @@ -123,6 +141,121 @@ impl ColorType for TriColor {
}
}

impl ColorType for HexColor {
const BITS_PER_PIXEL_PER_BUFFER: usize = 4;
const BUFFER_COUNT: usize = 1;
fn bitmask(&self, _bwrbit: bool, pos: u32) -> (u8, u16) {
let mask = !(0xF0 >> ((pos % 2) * 4));
let bits = self.get_nibble() as u16;
(mask, if pos % 2 == 1 { bits } else { bits << 4 })
}
}

#[cfg(feature = "graphics")]
impl From<BinaryColor> for HexColor {
fn from(b: BinaryColor) -> HexColor {
match b {
BinaryColor::On => HexColor::Black,
BinaryColor::Off => HexColor::White,
}
}
}

#[cfg(feature = "graphics")]
impl From<HexColor> for embedded_graphics_core::pixelcolor::Rgb888 {
fn from(b: HexColor) -> Self {
let (r, g, b) = b.rgb();
Self::new(r, g, b)
}
}

#[cfg(feature = "graphics")]
impl From<embedded_graphics_core::pixelcolor::Rgb888> for HexColor {
fn from(p: embedded_graphics_core::pixelcolor::Rgb888) -> HexColor {
use embedded_graphics_core::prelude::RgbColor;
let colors = [
HexColor::Black,
HexColor::White,
HexColor::Green,
HexColor::Blue,
HexColor::Red,
HexColor::Yellow,
];
// if the user has already mapped to the right color space, it will just be in the list
if let Some(found) = colors.iter().find(|c| c.rgb() == (p.r(), p.g(), p.b())) {
return *found;
}

// This is not ideal but just pick the nearest color
*colors
.iter()
.map(|c| (c, c.rgb()))
.map(|(c, (r, g, b))| {
let dist = (i32::from(r) - i32::from(p.r())).pow(2)
+ (i32::from(g) - i32::from(p.g())).pow(2)
+ (i32::from(b) - i32::from(p.b())).pow(2);
(c, dist)
})
.min_by_key(|(_c, dist)| *dist)
.map(|(c, _)| c)
.unwrap_or(&HexColor::White)
}
}

#[cfg(feature = "graphics")]
impl From<embedded_graphics_core::pixelcolor::raw::RawU4> for HexColor {
fn from(b: embedded_graphics_core::pixelcolor::raw::RawU4) -> Self {
use embedded_graphics_core::prelude::RawData;
HexColor::from_nibble(b.into_inner()).unwrap()
}
}

#[cfg(feature = "graphics")]
impl PixelColor for HexColor {
type Raw = embedded_graphics_core::pixelcolor::raw::RawU4;
}

impl HexColor {
/// Gets the Nibble representation of the Color as needed by the display
pub fn get_nibble(self) -> u8 {
self as u8
}
/// Converts two colors into a single byte for the Display
pub fn colors_byte(a: HexColor, b: HexColor) -> u8 {
a.get_nibble() << 4 | b.get_nibble()
}

///Take the nibble (lower 4 bits) and convert to an HexColor if possible
pub fn from_nibble(nibble: u8) -> Result<HexColor, OutOfColorRangeParseError> {
match nibble & 0xf {
0x00 => Ok(HexColor::Black),
0x01 => Ok(HexColor::White),
0x02 => Ok(HexColor::Yellow),
0x03 => Ok(HexColor::Red),
0x05 => Ok(HexColor::Blue),
0x06 => Ok(HexColor::Green),
e => Err(OutOfColorRangeParseError(e)),
}
}
///Split the nibbles of a single byte and convert both to an HexColor if possible
pub fn split_byte(byte: u8) -> Result<(HexColor, HexColor), OutOfColorRangeParseError> {
let low = HexColor::from_nibble(byte & 0xf)?;
let high = HexColor::from_nibble((byte >> 4) & 0xf)?;
Ok((high, low))
}
/// Converts to limited range of RGB values.
pub fn rgb(self) -> (u8, u8, u8) {
match self {
HexColor::White => (0xff, 0xff, 0xff),
HexColor::Black => (0x00, 0x00, 0x00),
HexColor::Green => (0x00, 0xff, 0x00),
HexColor::Blue => (0x00, 0x00, 0xff),
HexColor::Red => (0xff, 0x00, 0x00),
HexColor::Yellow => (0xff, 0xff, 0x00),
}
}
}

impl ColorType for OctColor {
const BITS_PER_PIXEL_PER_BUFFER: usize = 4;
const BUFFER_COUNT: usize = 1;
Expand Down
44 changes: 44 additions & 0 deletions src/epd7in3e/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::traits;

#[allow(dead_code, clippy::upper_case_acronyms)]
#[derive(Clone, Copy, Debug)]
pub(crate) enum Command {
Ox00 = 0x00,
Ox01 = 0x01,

PowerOff = 0x02,

Ox03 = 0x03,

PowerOn = 0x04,

Ox05 = 0x05,
Ox06 = 0x06,

DeepSleep = 0x07,

Ox08 = 0x08,

DataStartTransmission = 0x10,

DataFresh = 0x12,

Ox30 = 0x30,

Ox50 = 0x50,
Ox60 = 0x60,
Ox61 = 0x61,

Ox84 = 0x84,

CMDH = 0xAA,

OxE3 = 0xE3,
}

impl traits::Command for Command {
/// Returns the address of the command
fn address(self) -> u8 {
self as u8
}
}
Loading
Loading