2024-10-27 11:19:26 +01:00
|
|
|
#![feature(trait_upcasting)]
|
2024-10-20 17:22:27 +02:00
|
|
|
#![allow(arithmetic_overflow)]
|
|
|
|
use esp_idf_svc::hal::prelude::Peripherals;
|
2024-10-28 22:22:03 +01:00
|
|
|
use ws2812_esp32_rmt_driver::lib_embedded_graphics::{LedPixelShape, Ws2812DrawTarget};
|
2024-10-20 17:22:27 +02:00
|
|
|
use embedded_graphics::{
|
|
|
|
prelude::*,
|
|
|
|
};
|
2024-10-20 17:23:13 +02:00
|
|
|
use palette::Hsv;
|
|
|
|
use palette::convert::IntoColorUnclamped;
|
2024-10-20 17:22:27 +02:00
|
|
|
|
2024-10-20 17:23:13 +02:00
|
|
|
mod power;
|
|
|
|
mod lib8;
|
2024-10-27 11:19:26 +01:00
|
|
|
mod render;
|
|
|
|
mod task;
|
|
|
|
mod time;
|
2024-10-27 15:14:28 +01:00
|
|
|
mod geometry;
|
|
|
|
mod embedded_graphics_lib;
|
2024-10-27 11:19:26 +01:00
|
|
|
|
|
|
|
use crate::time::Periodically;
|
2024-10-27 15:14:28 +01:00
|
|
|
use crate::geometry::{Coordinates, VirtualCoordinates};
|
|
|
|
use crate::embedded_graphics_lib::EmbeddedDisplay;
|
2024-10-28 23:31:21 +01:00
|
|
|
use crate::render::{Surfaces, Surface, SimpleSurface, Display};
|
|
|
|
use crate::task::Task;
|
2024-10-27 11:19:26 +01:00
|
|
|
|
2024-10-28 22:22:03 +01:00
|
|
|
struct IdleTask<T: Surface> {
|
2024-10-27 11:19:26 +01:00
|
|
|
frame: u8,
|
2024-10-28 22:22:03 +01:00
|
|
|
surface: T,
|
2024-10-27 11:19:26 +01:00
|
|
|
updater: Periodically
|
|
|
|
}
|
2024-10-20 17:22:27 +02:00
|
|
|
|
2024-10-27 11:19:26 +01:00
|
|
|
struct IdleShader {
|
|
|
|
frame: u8
|
|
|
|
}
|
|
|
|
|
|
|
|
impl render::Shader for IdleShader {
|
2024-10-27 15:14:28 +01:00
|
|
|
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()
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-28 22:22:03 +01:00
|
|
|
impl<T: Surface> IdleTask<T> {
|
|
|
|
fn new(surface: T) -> Self {
|
2024-10-27 11:19:26 +01:00
|
|
|
IdleTask {
|
|
|
|
frame: 0,
|
2024-10-28 22:22:03 +01:00
|
|
|
surface: surface,
|
2024-10-27 11:19:26 +01:00
|
|
|
updater: Periodically::new_every_n_ms(16)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-28 22:22:03 +01:00
|
|
|
impl<T: Surface> task::Task for IdleTask<T> {
|
2024-10-27 11:19:26 +01:00
|
|
|
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 }));
|
|
|
|
})
|
|
|
|
}
|
2024-10-27 15:14:28 +01:00
|
|
|
|
|
|
|
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-27 11:19:26 +01:00
|
|
|
}
|
2024-10-20 17:22:27 +02:00
|
|
|
|
2024-10-28 22:22:03 +01:00
|
|
|
type PonderjarTarget<'a> = Ws2812DrawTarget<'a, PonderjarMatrix>;
|
|
|
|
|
2024-10-28 23:31:21 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2024-10-27 11:19:26 +01:00
|
|
|
log::info!("Setting up display");
|
2024-10-28 23:31:21 +01:00
|
|
|
let mut display = PonderjarTarget::new_display::<SimpleSurface>();
|
2024-10-27 11:19:26 +01:00
|
|
|
|
|
|
|
log::info!("Creating runner");
|
|
|
|
let mut runner = task::Scheduler::new(vec![
|
2024-10-28 22:22:03 +01:00
|
|
|
Box::new(IdleTask::new(display.new_surface().unwrap())),
|
2024-10-27 11:19:26 +01:00
|
|
|
Box::new(display),
|
|
|
|
]);
|
|
|
|
|
|
|
|
log::info!("Ready to rock and roll");
|
2024-10-20 17:22:27 +02:00
|
|
|
loop {
|
2024-10-27 11:19:26 +01:00
|
|
|
runner.tick();
|
2024-10-20 17:22:27 +02:00
|
|
|
}
|
|
|
|
}
|