renderbug/src/main.rs

67 lines
2.1 KiB
Rust
Raw Normal View History

2024-10-20 17:22:27 +02:00
#![allow(arithmetic_overflow)]
use esp_idf_svc::hal::prelude::Peripherals;
use ws2812_esp32_rmt_driver::lib_embedded_graphics::{LedPixelStrip, Ws2812DrawTarget};
use embedded_graphics::{
prelude::*,
pixelcolor::Rgb888,
2024-10-20 17:22:27 +02:00
};
use palette::Hsv;
use palette::convert::IntoColorUnclamped;
2024-10-20 17:22:27 +02:00
mod power;
mod lib8;
2024-10-20 17:22:27 +02:00
fn main() {
// It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_svc::sys::link_patches();
// Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default();
log::info!("Hello, world!");
let peripherals = Peripherals::take().unwrap();
let led_pin = peripherals.pins.gpio14;
let channel = peripherals.rmt.channel0;
const NUM_PIXELS : usize = 300;
const POWER_VOLTS : u32 = 5;
const POWER_MA : u32 = 500;
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
2024-10-20 17:22:27 +02:00
let mut draw = Ws2812DrawTarget::<LedPixelStrip<NUM_PIXELS>>::new(channel, led_pin).unwrap();
let mut hue : u8 = 0;
let mut length : usize = NUM_PIXELS;
let mut forwards = false;
loop {
let mut totalMW = 0;
draw.clear(Rgb888::BLACK);
for i in 0..length {
let hsvColor = Hsv::new_srgb(hue.wrapping_add(i as u8), 255, 255);
let rgbColor : lib8::RGB8 = hsvColor.into_color_unclamped();
2024-10-20 17:22:27 +02:00
let color = Rgb888::new(rgbColor.red, rgbColor.green, rgbColor.blue);
totalMW += power::colorToMW(color);
2024-10-20 17:22:27 +02:00
Pixel(Point::new(i as i32, 0), color).draw(&mut draw).unwrap();
}
let brightness = power::brightnessForMW(totalMW, 255, MAX_POWER_MW);
2024-10-20 17:22:27 +02:00
draw.set_brightness(brightness);
draw.flush().unwrap();
log::info!("Frame hue={} power={} brightness={}", hue, totalMW, brightness);
hue = hue.wrapping_add(1);
if forwards {
length += 1
} else {
length -= 1
}
if length <= 1 {
forwards = true;
} else if length >= NUM_PIXELS {
forwards = false;
}
}
}