Merge pull request #87 from crzysdrs/main
Add Color Conversions, Allow Changing Display Background Color thanks to @crzysdrsmain
commit
b3f9bc810a
63
src/color.rs
63
src/color.rs
|
|
@ -72,9 +72,70 @@ impl From<()> for OctColor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "graphics")]
|
||||||
|
impl From<BinaryColor> for OctColor {
|
||||||
|
fn from(b: BinaryColor) -> OctColor {
|
||||||
|
match b {
|
||||||
|
BinaryColor::On => OctColor::Black,
|
||||||
|
BinaryColor::Off => OctColor::White,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "graphics")]
|
||||||
|
impl From<OctColor> for embedded_graphics_core::pixelcolor::Rgb888 {
|
||||||
|
fn from(b: OctColor) -> Self {
|
||||||
|
let (r, b, g) = b.rgb();
|
||||||
|
Self::new(r, b, g)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "graphics")]
|
||||||
|
impl From<embedded_graphics_core::pixelcolor::Rgb888> for OctColor {
|
||||||
|
fn from(p: embedded_graphics_core::pixelcolor::Rgb888) -> OctColor {
|
||||||
|
use embedded_graphics_core::prelude::RgbColor;
|
||||||
|
let colors = [
|
||||||
|
OctColor::Black,
|
||||||
|
OctColor::White,
|
||||||
|
OctColor::Green,
|
||||||
|
OctColor::Blue,
|
||||||
|
OctColor::Red,
|
||||||
|
OctColor::Yellow,
|
||||||
|
OctColor::Orange,
|
||||||
|
OctColor::HiZ,
|
||||||
|
];
|
||||||
|
// 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(&OctColor::White)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "graphics")]
|
||||||
|
impl From<embedded_graphics_core::pixelcolor::raw::RawU4> for OctColor {
|
||||||
|
fn from(b: embedded_graphics_core::pixelcolor::raw::RawU4) -> Self {
|
||||||
|
use embedded_graphics_core::prelude::RawData;
|
||||||
|
OctColor::from_nibble(b.into_inner()).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "graphics")]
|
#[cfg(feature = "graphics")]
|
||||||
impl PixelColor for OctColor {
|
impl PixelColor for OctColor {
|
||||||
type Raw = ();
|
type Raw = embedded_graphics_core::pixelcolor::raw::RawU4;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OctColor {
|
impl OctColor {
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ where
|
||||||
self.cmd_with_data(spi, Command::BoosterSoftStart, &[0xC7, 0xC7, 0x1D])?;
|
self.cmd_with_data(spi, Command::BoosterSoftStart, &[0xC7, 0xC7, 0x1D])?;
|
||||||
self.cmd_with_data(spi, Command::PllControl, &[0x3C])?;
|
self.cmd_with_data(spi, Command::PllControl, &[0x3C])?;
|
||||||
self.cmd_with_data(spi, Command::TemperatureSensor, &[0x00])?;
|
self.cmd_with_data(spi, Command::TemperatureSensor, &[0x00])?;
|
||||||
self.cmd_with_data(spi, Command::VcomAndDataIntervalSetting, &[0x37])?;
|
self.update_vcom(spi)?;
|
||||||
self.cmd_with_data(spi, Command::TconSetting, &[0x22])?;
|
self.cmd_with_data(spi, Command::TconSetting, &[0x22])?;
|
||||||
self.send_resolution(spi)?;
|
self.send_resolution(spi)?;
|
||||||
|
|
||||||
|
|
@ -68,7 +68,7 @@ where
|
||||||
|
|
||||||
delay.delay_ms(100);
|
delay.delay_ms(100);
|
||||||
|
|
||||||
self.cmd_with_data(spi, Command::VcomAndDataIntervalSetting, &[0x37])?;
|
self.update_vcom(spi)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -118,6 +118,7 @@ where
|
||||||
_delay: &mut DELAY,
|
_delay: &mut DELAY,
|
||||||
) -> Result<(), SPI::Error> {
|
) -> Result<(), SPI::Error> {
|
||||||
self.wait_busy_high();
|
self.wait_busy_high();
|
||||||
|
self.update_vcom(spi)?;
|
||||||
self.send_resolution(spi)?;
|
self.send_resolution(spi)?;
|
||||||
self.cmd_with_data(spi, Command::DataStartTransmission1, buffer)?;
|
self.cmd_with_data(spi, Command::DataStartTransmission1, buffer)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -160,6 +161,7 @@ where
|
||||||
fn clear_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
|
fn clear_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
|
||||||
let bg = OctColor::colors_byte(self.color, self.color);
|
let bg = OctColor::colors_byte(self.color, self.color);
|
||||||
self.wait_busy_high();
|
self.wait_busy_high();
|
||||||
|
self.update_vcom(spi)?;
|
||||||
self.send_resolution(spi)?;
|
self.send_resolution(spi)?;
|
||||||
self.command(spi, Command::DataStartTransmission1)?;
|
self.command(spi, Command::DataStartTransmission1)?;
|
||||||
self.interface.data_x_times(spi, bg, WIDTH * HEIGHT / 2)?;
|
self.interface.data_x_times(spi, bg, WIDTH * HEIGHT / 2)?;
|
||||||
|
|
@ -238,6 +240,12 @@ where
|
||||||
self.send_data(spi, &[(h >> 8) as u8])?;
|
self.send_data(spi, &[(h >> 8) as u8])?;
|
||||||
self.send_data(spi, &[h as u8])
|
self.send_data(spi, &[h as u8])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_vcom(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
|
||||||
|
let bg_color = (self.color.get_nibble() & 0b111) << 5;
|
||||||
|
self.cmd_with_data(spi, Command::VcomAndDataIntervalSetting, &[0x17 | bg_color])?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue