lib8: drop custom RGB8 struct for rgb crate

This commit is contained in:
Victoria Fischer 2024-10-30 20:48:00 +01:00
parent b7995423d7
commit 1668db2c85
6 changed files with 54 additions and 75 deletions

View File

@ -1,6 +1,6 @@
[x] cfg macros [x] cfg macros
[ ] warnings [ ] warnings
[ ] rgb crate [x] rgb crate
[ ] Layer blending [ ] Layer blending
[ ] Refactor idle pattern into test pattern [ ] Refactor idle pattern into test pattern
[ ] Wifi [ ] Wifi

View File

@ -1,65 +1,41 @@
use palette::convert::FromColorUnclamped;
use palette::encoding::srgb::Srgb; use palette::encoding::srgb::Srgb;
use palette::Hsv;
use crate::power::AsMilliwatts; use rgb::RGB8;
#[derive(PartialEq, Debug, Copy, Clone)] pub trait IntoRgb8 {
pub struct RGB8 { fn into_rgb8(self) -> RGB8;
pub red: u8,
pub green: u8,
pub blue: u8
} }
impl RGB8 { impl IntoRgb8 for RGB8 {
pub const fn new(red : u8, green : u8, blue : u8) -> Self { fn into_rgb8(self) -> RGB8 {
Self { self
red: red,
green: green,
blue: blue
}
} }
} }
impl FromColorUnclamped<Hsv<Srgb, u8>> for RGB8 { impl IntoRgb8 for palette::Hsv<Srgb, u8> {
fn from_color_unclamped(hsv: Hsv<Srgb, u8>) -> RGB8 { fn into_rgb8(self) -> RGB8 {
if hsv.saturation == 0 { if self.saturation == 0 {
return RGB8::new(hsv.value, hsv.value, hsv.value); return RGB8::new(self.value, self.value, self.value);
} }
let region = hsv.hue.into_inner() / 43; let region = self.hue.into_inner() / 43;
let remainder = (hsv.hue.into_inner() - (region * 43)) * 6; let remainder = (self.hue.into_inner() - (region * 43)) * 6;
let p = hsv.value.wrapping_mul(255 - hsv.saturation).wrapping_shr(8); let p = self.value.wrapping_mul(255 - self.saturation).wrapping_shr(8);
let q = (hsv.value.wrapping_mul(255 - ((hsv.saturation.wrapping_mul(remainder)).wrapping_shr(8)))).wrapping_shr(8); let q = (self.value.wrapping_mul(255 - ((self.saturation.wrapping_mul(remainder)).wrapping_shr(8)))).wrapping_shr(8);
let t = (hsv.value.wrapping_mul(255 - ((hsv.saturation.wrapping_mul(255 - remainder)).wrapping_shr(8)))).wrapping_shr(8); let t = (self.value.wrapping_mul(255 - ((self.saturation.wrapping_mul(255 - remainder)).wrapping_shr(8)))).wrapping_shr(8);
match region { match region {
0 => RGB8::new(hsv.value, t, p), 0 => RGB8::new(self.value, t, p),
1 => RGB8::new(q, hsv.value, p), 1 => RGB8::new(q, self.value, p),
2 => RGB8::new(p, hsv.value, t), 2 => RGB8::new(p, self.value, t),
3 => RGB8::new(p, q, hsv.value), 3 => RGB8::new(p, q, self.value),
4 => RGB8::new(t, p, hsv.value), 4 => RGB8::new(t, p, self.value),
_ => RGB8::new(hsv.value, p, q) _ => RGB8::new(self.value, p, q)
} }
} }
} }
impl AsMilliwatts for RGB8 {
fn as_milliwatts(&self) -> u32 {
const RED_MW : u32 = 16 * 5; //< 16mA @ 5v = 80mW
const GREEN_MW : u32 = 11 * 5; //< 11mA @ 5v = 55mW
const BLUE_MW : u32 = 15 * 5; //< 15mA @ 5v = 75mW
const DARK_MW : u32 = 1 * 5; //< 1mA @ 5v = 5mW
let red = (self.red as u32 * RED_MW).wrapping_shr(8);
let green = (self.green as u32 * GREEN_MW).wrapping_shr(8);
let blue = (self.blue as u32 * BLUE_MW).wrapping_shr(8);
return red + green + blue + DARK_MW;
}
}
#[cfg(feature="embedded-graphics")] #[cfg(feature="embedded-graphics")]
mod embedded_graphics { mod embedded_graphics {
use embedded_graphics::pixelcolor::RgbColor; use embedded_graphics::pixelcolor::RgbColor;
@ -90,20 +66,4 @@ mod embedded_graphics {
impl PixelColor for RGB8 { impl PixelColor for RGB8 {
type Raw = RawU8; type Raw = RawU8;
} }
impl<T: RgbColor> AsMilliwatts for T {
fn as_milliwatts(&self) -> u32 {
const RED_MW : u32 = 16 * 5; //< 16mA @ 5v = 80mW
const GREEN_MW : u32 = 11 * 5; //< 11mA @ 5v = 55mW
const BLUE_MW : u32 = 15 * 5; //< 15mA @ 5v = 75mW
const DARK_MW : u32 = 1 * 5; //< 1mA @ 5v = 5mW
let red = (self.r() as u32 * RED_MW).wrapping_shr(8);
let green = (self.g() as u32 * GREEN_MW).wrapping_shr(8);
let blue = (self.b() as u32 * BLUE_MW).wrapping_shr(8);
return red + green + blue + DARK_MW;
}
}
} }

View File

@ -1,5 +1,6 @@
use palette::Hsv; use palette::Hsv;
use palette::convert::IntoColorUnclamped;
use rgb::RGB8;
mod power; mod power;
mod lib8; mod lib8;
@ -9,6 +10,14 @@ mod time;
mod geometry; mod geometry;
mod platform; mod platform;
use crate::time::Periodically;
use crate::geometry::{Coordinates, VirtualCoordinates};
use crate::render::{Shader, Surfaces, Surface};
use crate::task::Task;
use crate::platform::DisplayInit;
use crate::lib8::IntoRgb8;
#[cfg(feature="embedded-graphics")] #[cfg(feature="embedded-graphics")]
mod embedded_graphics_lib; mod embedded_graphics_lib;
@ -27,12 +36,6 @@ use ws2812_esp32_rmt_driver::lib_smart_leds::Ws2812Esp32Rmt;
#[cfg(feature="smart-leds")] #[cfg(feature="smart-leds")]
use crate::smart_leds_lib::spi::SPIDisplay; use crate::smart_leds_lib::spi::SPIDisplay;
use crate::time::Periodically;
use crate::geometry::{Coordinates, VirtualCoordinates};
use crate::render::{Shader, Surfaces, Surface};
use crate::task::Task;
use crate::platform::DisplayInit;
#[cfg(feature="threads")] #[cfg(feature="threads")]
use crate::render::SharedSurface; use crate::render::SharedSurface;
@ -50,8 +53,8 @@ struct IdleShader {
} }
impl Shader for IdleShader { impl Shader for IdleShader {
fn draw(&self, coords: VirtualCoordinates) -> lib8::RGB8 { fn draw(&self, coords: VirtualCoordinates) -> RGB8 {
Hsv::new_srgb(self.frame.wrapping_add(coords.x()).wrapping_add(coords.y()), 255, 255).into_color_unclamped() Hsv::new_srgb(self.frame.wrapping_add(coords.x()).wrapping_add(coords.y()), 255, 255).into_rgb8()
} }
} }

View File

@ -1,7 +1,24 @@
use rgb::RGB8;
pub trait AsMilliwatts { pub trait AsMilliwatts {
fn as_milliwatts(&self) -> u32; fn as_milliwatts(&self) -> u32;
} }
impl AsMilliwatts for RGB8 {
fn as_milliwatts(&self) -> u32 {
const RED_MW : u32 = 16 * 5; //< 16mA @ 5v = 80mW
const GREEN_MW : u32 = 11 * 5; //< 11mA @ 5v = 55mW
const BLUE_MW : u32 = 15 * 5; //< 15mA @ 5v = 75mW
const DARK_MW : u32 = 1 * 5; //< 1mA @ 5v = 5mW
let red = (self.r as u32 * RED_MW).wrapping_shr(8);
let green = (self.g as u32 * GREEN_MW).wrapping_shr(8);
let blue = (self.b as u32 * BLUE_MW).wrapping_shr(8);
return red + green + blue + DARK_MW;
}
}
pub fn brightness_for_mw(total_mw : u32, target : u8, max_power: u32) -> u8 { pub fn brightness_for_mw(total_mw : u32, target : u8, max_power: u32) -> u8 {
let target32 = target as u32; let target32 = target as u32;
let requested_mw = (total_mw * target32) / 256; let requested_mw = (total_mw * target32) / 256;

View File

@ -1,11 +1,11 @@
use std::rc::Rc; use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use std::io; use std::io;
use rgb::RGB8;
#[cfg(feature="threads")] #[cfg(feature="threads")]
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use crate::lib8::RGB8;
use crate::geometry::*; use crate::geometry::*;
pub trait Shader: Send { pub trait Shader: Send {

View File

@ -5,7 +5,6 @@ use crate::render::{Surface, SurfacePool, Display, Surfaces};
use crate::task::Task; use crate::task::Task;
use crate::power::{brightness_for_mw, AsMilliwatts}; use crate::power::{brightness_for_mw, AsMilliwatts};
use crate::time::Periodically; use crate::time::Periodically;
use crate::lib8::RGB8;
use crate::geometry::*; use crate::geometry::*;
use smart_leds::brightness; use smart_leds::brightness;
@ -77,14 +76,14 @@ impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> Display<S> for SmartLedDisp
fn render_frame(&mut self) { fn render_frame(&mut self) {
for x in 0..self.pixbuf.len() { for x in 0..self.pixbuf.len() {
let virt_coords = VirtualCoordinates::new(x as u8, 0); let virt_coords = VirtualCoordinates::new(x as u8, 0);
let mut pixel = RGB8::new(0, 0, 0); let mut pixel = Rgb::new(0, 0, 0);
for surface in self.surfaces.iter() { for surface in self.surfaces.iter() {
surface.with_shader(|shader| { surface.with_shader(|shader| {
pixel = shader.draw(virt_coords.clone()); pixel = shader.draw(virt_coords.clone());
}) })
} }
self.total_mw += pixel.as_milliwatts(); self.total_mw += pixel.as_milliwatts();
self.pixbuf[x] = Rgb::new(pixel.red, pixel.green, pixel.blue); self.pixbuf[x] = Rgb::new(pixel.r, pixel.g, pixel.b);
}; };
} }
} }