renderbug/src/main.rs

166 lines
4.6 KiB
Rust
Raw Normal View History

#![feature(trait_upcasting)]
2024-10-20 17:22:27 +02:00
#![allow(arithmetic_overflow)]
use ws2812_esp32_rmt_driver::lib_embedded_graphics::{LedPixelShape, Ws2812DrawTarget};
2024-10-20 17:22:27 +02:00
use embedded_graphics::{
prelude::*,
};
use palette::Hsv;
use palette::convert::IntoColorUnclamped;
2024-10-20 17:22:27 +02:00
use esp_idf_svc::hal::{
prelude::*,
spi::{config::{Config, DriverConfig}, Dma, SpiDriver, SpiBusDriver},
gpio::AnyIOPin,
};
mod power;
mod lib8;
mod render;
mod task;
mod time;
mod geometry;
mod embedded_graphics_lib;
mod smart_leds_lib;
use crate::time::Periodically;
use crate::geometry::{Coordinates, VirtualCoordinates};
use crate::embedded_graphics_lib::EmbeddedDisplay;
use crate::smart_leds_lib::SmartLedDisplay;
use crate::render::{Surfaces, Surface, SimpleSurface, Display};
use crate::task::Task;
use ws2812_spi::Ws2812;
use smart_leds_trait::SmartLedsWrite;
use esp_idf_svc::hal::units::MegaHertz;
struct IdleTask<T: Surface> {
frame: u8,
surface: T,
updater: Periodically
}
2024-10-20 17:22:27 +02:00
struct IdleShader {
frame: u8
}
impl render::Shader for IdleShader {
fn draw(&self, coords: VirtualCoordinates) -> lib8::RGB8 {
Hsv::new_srgb(self.frame.wrapping_add(coords.x()).wrapping_add(coords.y()), 255, 255).into_color_unclamped()
}
}
impl<T: Surface> IdleTask<T> {
fn new(surface: T) -> Self {
IdleTask {
frame: 0,
surface: surface,
updater: Periodically::new_every_n_ms(16)
}
}
}
impl<T: Surface> task::Task for IdleTask<T> {
fn name(&self) -> &'static str { "Idle" }
fn tick(&mut self) {
self.updater.run(|| {
self.frame = self.frame.wrapping_add(1);
self.surface.set_shader(Box::new(IdleShader { frame: self.frame }));
})
}
fn stop(&mut self) {
self.surface.clear_shader();
}
}
struct PonderjarMatrix {}
impl LedPixelShape for PonderjarMatrix {
fn size() -> Size {
Size::new(17, 17)
}
fn pixel_index(point: Point) -> Option<usize> {
if (0..Self::size().width as i32).contains(&point.x) && (0..Self::size().height as i32).contains(&point.y) {
if point.y % 2 == 0 {
Some((point.y as u32 * Self::size().width as u32 + point.x as u32).try_into().unwrap())
} else {
Some((point.y as u32 * Self::size().width as u32 - point.x as u32).try_into().unwrap())
}
} else {
None
}
}
}
2024-10-20 17:22:27 +02:00
type PonderjarTarget<'a> = Ws2812DrawTarget<'a, PonderjarMatrix>;
trait DisplayInit {
fn new_display<S: Surface>() -> impl Display<S> + Task;
}
impl<Shape: LedPixelShape> DisplayInit for Ws2812DrawTarget<'_, Shape> {
fn new_display<S: Surface>() -> impl Display<S> + Task {
let peripherals = Peripherals::take().unwrap();
let led_pin = peripherals.pins.gpio14;
let channel = peripherals.rmt.channel0;
const POWER_VOLTS : u32 = 5;
const POWER_MA : u32 = 500;
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
let target = Self::new(channel, led_pin).unwrap();
return EmbeddedDisplay::<Self, S>::new(target, MAX_POWER_MW);
}
}
struct SPIDisplay {}
impl DisplayInit for SPIDisplay {
fn new_display<S: Surface>() -> impl Display<S> + Task {
let peripherals = Peripherals::take().unwrap();
let driver = SpiDriver::new_without_sclk(
peripherals.spi2,
peripherals.pins.gpio14,
Option::<AnyIOPin>::None,
&DriverConfig::new().dma(Dma::Auto(512))
).unwrap();
let cfg = Config::new().baudrate(3_200.kHz().into());
let spi = SpiBusDriver::new(driver, &cfg).unwrap();
const POWER_VOLTS : u32 = 5;
const POWER_MA : u32 = 500;
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
let target = Ws2812::new(spi);
return SmartLedDisplay::new(target, MAX_POWER_MW)
}
}
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!("Setting up display");
//let mut display = SPIDisplay::new_display::<SimpleSurface>();
let mut display = PonderjarTarget::new_display::<SimpleSurface>();
log::info!("Creating runner");
let mut runner = task::Scheduler::new(vec![
Box::new(IdleTask::new(display.new_surface().unwrap())),
Box::new(display),
]);
log::info!("Ready to rock and roll");
2024-10-20 17:22:27 +02:00
loop {
runner.tick();
2024-10-20 17:22:27 +02:00
}
}