platform: clean up main.rs and split out hardware specific bits to platform module

This commit is contained in:
Victoria Fischer 2024-10-29 11:51:25 +01:00
parent 8426f0b0e5
commit 62f09ac1f3
4 changed files with 113 additions and 89 deletions

View File

@ -30,7 +30,7 @@ embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-
[dependencies]
log = { version = "0.4", default-features = false }
esp-idf-svc = { version = "0.49", default-features = false }
ws2812-esp32-rmt-driver = { version = "*", features = ["embedded-graphics-core"]}
ws2812-esp32-rmt-driver = { version = "*", features = ["embedded-graphics-core", "smart-leds-trait"]}
embedded-graphics = { version = "0.8.1", features = ["fixed_point", "defmt"] }
hsv = "0.1.1"
palette = { version = "0.7.6" }

View File

@ -1,17 +1,8 @@
#![feature(trait_upcasting)]
#![allow(arithmetic_overflow)]
use ws2812_esp32_rmt_driver::lib_embedded_graphics::{LedPixelShape, Ws2812DrawTarget};
use embedded_graphics::{
prelude::*,
};
use palette::Hsv;
use palette::convert::IntoColorUnclamped;
use esp_idf_svc::hal::{
prelude::*,
spi::{config::{Config, DriverConfig}, Dma, SpiDriver, SpiBusDriver},
gpio::AnyIOPin,
};
use ws2812_esp32_rmt_driver::lib_smart_leds::Ws2812Esp32Rmt;
use ws2812_esp32_rmt_driver::lib_embedded_graphics::Ws2812DrawTarget;
mod power;
mod lib8;
@ -21,17 +12,13 @@ mod time;
mod geometry;
mod embedded_graphics_lib;
mod smart_leds_lib;
mod platform;
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::render::{Shader, Surfaces, Surface, SimpleSurface};
use crate::task::Task;
use ws2812_spi::Ws2812;
use smart_leds_trait::SmartLedsWrite;
use esp_idf_svc::hal::units::MegaHertz;
use crate::platform::{DisplayInit, PonderjarTarget, SPIDisplay};
struct IdleTask<T: Surface> {
frame: u8,
@ -43,7 +30,7 @@ struct IdleShader {
frame: u8
}
impl render::Shader for IdleShader {
impl 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()
}
@ -59,7 +46,7 @@ impl<T: Surface> IdleTask<T> {
}
}
impl<T: Surface> task::Task for IdleTask<T> {
impl<T: Surface> Task for IdleTask<T> {
fn name(&self) -> &'static str { "Idle" }
fn tick(&mut self) {
@ -74,72 +61,6 @@ impl<T: Surface> task::Task for IdleTask<T> {
}
}
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
}
}
}
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)
}
}
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
@ -150,7 +71,8 @@ fn main() {
log::info!("Setting up display");
//let mut display = SPIDisplay::new_display::<SimpleSurface>();
let mut display = PonderjarTarget::new_display::<SimpleSurface>();
//let mut display = PonderjarTarget::new_display::<SimpleSurface>();
let mut display = Ws2812Esp32Rmt::new_display::<SimpleSurface>();
log::info!("Creating runner");
let mut runner = task::Scheduler::new(vec![

102
src/platform.rs Normal file
View File

@ -0,0 +1,102 @@
use esp_idf_svc::hal::{
prelude::*,
gpio::AnyIOPin,
spi::{
config::{Config, DriverConfig},
Dma,
SpiBusDriver,
SpiDriver,
}
};
use ws2812_spi::Ws2812;
use ws2812_esp32_rmt_driver::lib_smart_leds::Ws2812Esp32Rmt;
use ws2812_esp32_rmt_driver::lib_embedded_graphics::{LedPixelShape, Ws2812DrawTarget};
use embedded_graphics::prelude::{Size, Point};
use crate::smart_leds_lib::SmartLedDisplay;
use crate::embedded_graphics_lib::EmbeddedDisplay;
use crate::render::{Surface, Display};
use crate::task::Task;
pub trait DisplayInit {
fn new_display<S: Surface>() -> impl Display<S> + Task;
}
impl DisplayInit for Ws2812Esp32Rmt<'_> {
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 SmartLedDisplay::new(target, MAX_POWER_MW);
}
}
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);
}
}
pub 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)
}
}
pub 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
}
}
}
pub type PonderjarTarget<'a> = Ws2812DrawTarget<'a, PonderjarMatrix>;

View File

@ -76,7 +76,7 @@ impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> Display<S> for SmartLedDisp
}
fn render_frame(&mut self) {
for x in (0..self.pixbuf.len()) {
for x in 0..self.pixbuf.len() {
let virtCoords = VirtualCoordinates::new(x as u8, 0);
let mut pixel = RGB8::new(0, 0, 0);
for surface in self.surfaces.iter() {