renderbug: split out power and RGB8 types to separate modules
This commit is contained in:
parent
6fe6f2f74a
commit
5686dca311
44
src/lib8.rs
Normal file
44
src/lib8.rs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
use palette::convert::{IntoColor, IntoColorUnclamped, FromColorUnclamped};
|
||||||
|
use palette::encoding::srgb::Srgb;
|
||||||
|
use palette::Hsv;
|
||||||
|
|
||||||
|
#[derive(PartialEq, Debug)]
|
||||||
|
pub struct RGB8 {
|
||||||
|
pub red: u8,
|
||||||
|
pub green: u8,
|
||||||
|
pub blue: u8
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RGB8 {
|
||||||
|
fn new(red : u8, green : u8, blue : u8) -> Self {
|
||||||
|
Self {
|
||||||
|
red: red,
|
||||||
|
green: green,
|
||||||
|
blue: blue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromColorUnclamped<Hsv<Srgb, u8>> for RGB8 {
|
||||||
|
fn from_color_unclamped(hsv: Hsv<Srgb, u8>) -> RGB8 {
|
||||||
|
if hsv.saturation == 0 {
|
||||||
|
return RGB8::new(hsv.value, hsv.value, hsv.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
let region = hsv.hue.into_inner() / 43;
|
||||||
|
let remainder = (hsv.hue.into_inner() - (region * 43)) * 6;
|
||||||
|
|
||||||
|
let p = (hsv.value.wrapping_mul(255 - hsv.saturation).wrapping_shr(8));
|
||||||
|
let q = (hsv.value.wrapping_mul(255 - ((hsv.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);
|
||||||
|
|
||||||
|
match region {
|
||||||
|
0 => RGB8::new(hsv.value, t, p),
|
||||||
|
1 => RGB8::new(q, hsv.value, p),
|
||||||
|
2 => RGB8::new(p, hsv.value, t),
|
||||||
|
3 => RGB8::new(p, q, hsv.value),
|
||||||
|
4 => RGB8::new(t, p, hsv.value),
|
||||||
|
_ => RGB8::new(hsv.value, p, q)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
84
src/main.rs
84
src/main.rs
@ -3,75 +3,15 @@ use esp_idf_svc::hal::prelude::Peripherals;
|
|||||||
use ws2812_esp32_rmt_driver::lib_embedded_graphics::{LedPixelStrip, Ws2812DrawTarget};
|
use ws2812_esp32_rmt_driver::lib_embedded_graphics::{LedPixelStrip, Ws2812DrawTarget};
|
||||||
use embedded_graphics::{
|
use embedded_graphics::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
pixelcolor::{Rgb888, RgbColor},
|
pixelcolor::Rgb888,
|
||||||
};
|
};
|
||||||
use std::thread::sleep;
|
use palette::Hsv;
|
||||||
use std::time::Duration;
|
use palette::convert::IntoColorUnclamped;
|
||||||
use palette::{Hsv, Srgb};
|
|
||||||
use palette::convert::{IntoColor, IntoColorUnclamped, FromColorUnclamped};
|
|
||||||
|
|
||||||
fn colorToMW(color : impl RgbColor) -> u32 {
|
|
||||||
const gRed_mW : u32 = 16 * 5; //< 16mA @ 5v = 80mW
|
|
||||||
const gGreen_mW : u32 = 11 * 5; //< 11mA @ 5v = 55mW
|
|
||||||
const gBlue_mW : u32 = 15 * 5; //< 15mA @ 5v = 75mW
|
|
||||||
const gDark_mW : u32 = 1 * 5; //< 1mA @ 5v = 5mW
|
|
||||||
|
|
||||||
let redMW = (color.r() as u32 * gRed_mW).wrapping_shr(8);
|
mod power;
|
||||||
let greenMW = (color.g() as u32 * gGreen_mW).wrapping_shr(8);
|
mod lib8;
|
||||||
let blueMW = (color.b() as u32 * gBlue_mW).wrapping_shr(8);
|
|
||||||
|
|
||||||
return redMW + greenMW + blueMW + gDark_mW;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn brightnessForMW(totalMW : u32, target : u8, maxPower: u32) -> u8 {
|
|
||||||
let target32 = target as u32;
|
|
||||||
let requestedMW = (totalMW * target32) / 256;
|
|
||||||
if requestedMW > maxPower {
|
|
||||||
return ((target32 * maxPower) / requestedMW).try_into().unwrap();
|
|
||||||
}
|
|
||||||
return target;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
|
||||||
struct RGB8 {
|
|
||||||
red: u8,
|
|
||||||
green: u8,
|
|
||||||
blue: u8
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RGB8 {
|
|
||||||
fn new(red : u8, green : u8, blue : u8) -> Self {
|
|
||||||
Self {
|
|
||||||
red: red,
|
|
||||||
green: green,
|
|
||||||
blue: blue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromColorUnclamped<Hsv<palette::encoding::srgb::Srgb, u8>> for RGB8 {
|
|
||||||
fn from_color_unclamped(hsv: Hsv<palette::encoding::srgb::Srgb, u8>) -> RGB8 {
|
|
||||||
if hsv.saturation == 0 {
|
|
||||||
return RGB8::new(hsv.value, hsv.value, hsv.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
let region = hsv.hue.into_inner() / 43;
|
|
||||||
let remainder = (hsv.hue.into_inner() - (region * 43)) * 6;
|
|
||||||
|
|
||||||
let p = (hsv.value.wrapping_mul(255 - hsv.saturation).wrapping_shr(8));
|
|
||||||
let q = (hsv.value.wrapping_mul(255 - ((hsv.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);
|
|
||||||
|
|
||||||
match region {
|
|
||||||
0 => RGB8::new(hsv.value, t, p),
|
|
||||||
1 => RGB8::new(q, hsv.value, p),
|
|
||||||
2 => RGB8::new(p, hsv.value, t),
|
|
||||||
3 => RGB8::new(p, q, hsv.value),
|
|
||||||
4 => RGB8::new(t, p, hsv.value),
|
|
||||||
_ => RGB8::new(hsv.value, p, q)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// It is necessary to call this function once. Otherwise some patches to the runtime
|
// It is necessary to call this function once. Otherwise some patches to the runtime
|
||||||
@ -87,8 +27,10 @@ fn main() {
|
|||||||
let led_pin = peripherals.pins.gpio14;
|
let led_pin = peripherals.pins.gpio14;
|
||||||
let channel = peripherals.rmt.channel0;
|
let channel = peripherals.rmt.channel0;
|
||||||
|
|
||||||
const NUM_PIXELS : usize = 255;
|
const NUM_PIXELS : usize = 300;
|
||||||
const MAX_POWER_MW : u32 = 1000;
|
const POWER_VOLTS : u32 = 5;
|
||||||
|
const POWER_MA : u32 = 500;
|
||||||
|
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
|
||||||
|
|
||||||
let mut draw = Ws2812DrawTarget::<LedPixelStrip<NUM_PIXELS>>::new(channel, led_pin).unwrap();
|
let mut draw = Ws2812DrawTarget::<LedPixelStrip<NUM_PIXELS>>::new(channel, led_pin).unwrap();
|
||||||
let mut hue : u8 = 0;
|
let mut hue : u8 = 0;
|
||||||
@ -98,16 +40,14 @@ fn main() {
|
|||||||
let mut totalMW = 0;
|
let mut totalMW = 0;
|
||||||
draw.clear(Rgb888::BLACK);
|
draw.clear(Rgb888::BLACK);
|
||||||
for i in 0..length {
|
for i in 0..length {
|
||||||
//let color = HSV::new(hue.wrapping_add(i as u8), 255, 255);
|
|
||||||
let hsvColor = Hsv::new_srgb(hue.wrapping_add(i as u8), 255, 255);
|
let hsvColor = Hsv::new_srgb(hue.wrapping_add(i as u8), 255, 255);
|
||||||
let rgbColor : RGB8 = hsvColor.into_color_unclamped();
|
let rgbColor : lib8::RGB8 = hsvColor.into_color_unclamped();
|
||||||
//let rgbColor : Srgb = hsvColor.into_color();
|
|
||||||
let color = Rgb888::new(rgbColor.red, rgbColor.green, rgbColor.blue);
|
let color = Rgb888::new(rgbColor.red, rgbColor.green, rgbColor.blue);
|
||||||
|
|
||||||
totalMW += colorToMW(color);
|
totalMW += power::colorToMW(color);
|
||||||
Pixel(Point::new(i as i32, 0), color).draw(&mut draw).unwrap();
|
Pixel(Point::new(i as i32, 0), color).draw(&mut draw).unwrap();
|
||||||
}
|
}
|
||||||
let brightness = brightnessForMW(totalMW, 255, MAX_POWER_MW);
|
let brightness = power::brightnessForMW(totalMW, 255, MAX_POWER_MW);
|
||||||
draw.set_brightness(brightness);
|
draw.set_brightness(brightness);
|
||||||
draw.flush().unwrap();
|
draw.flush().unwrap();
|
||||||
log::info!("Frame hue={} power={} brightness={}", hue, totalMW, brightness);
|
log::info!("Frame hue={} power={} brightness={}", hue, totalMW, brightness);
|
||||||
|
23
src/power.rs
Normal file
23
src/power.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
use embedded_graphics::pixelcolor::RgbColor;
|
||||||
|
|
||||||
|
pub fn colorToMW(color : impl RgbColor) -> u32 {
|
||||||
|
const gRed_mW : u32 = 16 * 5; //< 16mA @ 5v = 80mW
|
||||||
|
const gGreen_mW : u32 = 11 * 5; //< 11mA @ 5v = 55mW
|
||||||
|
const gBlue_mW : u32 = 15 * 5; //< 15mA @ 5v = 75mW
|
||||||
|
const gDark_mW : u32 = 1 * 5; //< 1mA @ 5v = 5mW
|
||||||
|
|
||||||
|
let redMW = (color.r() as u32 * gRed_mW).wrapping_shr(8);
|
||||||
|
let greenMW = (color.g() as u32 * gGreen_mW).wrapping_shr(8);
|
||||||
|
let blueMW = (color.b() as u32 * gBlue_mW).wrapping_shr(8);
|
||||||
|
|
||||||
|
return redMW + greenMW + blueMW + gDark_mW;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn brightnessForMW(totalMW : u32, target : u8, maxPower: u32) -> u8 {
|
||||||
|
let target32 = target as u32;
|
||||||
|
let requestedMW = (totalMW * target32) / 256;
|
||||||
|
if requestedMW > maxPower {
|
||||||
|
return ((target32 * maxPower) / requestedMW).try_into().unwrap();
|
||||||
|
}
|
||||||
|
return target;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user