Files
padmapper/src/bin/main.rs
T
2026-06-01 02:27:20 -07:00

91 lines
2.6 KiB
Rust

#![no_std]
#![no_main]
#![deny(
clippy::mem_forget,
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
holding buffers for the duration of a data transfer."
)]
#![deny(clippy::large_stack_frames)]
use defmt::info;
use embassy_executor::Spawner;
use embassy_time::Timer;
use esp_hal::Blocking;
use esp_hal::clock::CpuClock;
use esp_hal::spi::master::{Spi, Config as SpiConfig};
use esp_hal::time::Rate;
use esp_hal::timer::timg::TimerGroup;
#[allow(unused_imports)] use esp_println; // Needed to link global logger
use ws2812_rs::{WS2812SPI, SendColorBySPI, Color};
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {}
}
extern crate alloc;
// This creates a default app-descriptor required by the esp-idf bootloader.
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!();
const LED_SPI_HZ: u32 = 3_200_000;
#[allow(
clippy::large_stack_frames,
reason = "it's not unusual to allocate larger buffers etc. in main"
)]
#[esp_rtos::main]
async fn main(spawner: Spawner) -> ! {
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: 73744);
let timg0 = TimerGroup::new(peripherals.TIMG0);
esp_rtos::start(timg0.timer0);
let spi = Spi::new(peripherals.SPI2,
SpiConfig::default().with_frequency(Rate::from_hz(LED_SPI_HZ)))
.unwrap()
.with_mosi(peripherals.GPIO48);
let led = WS2812SPI::new(spi);
spawner.spawn(heartbeat_task(led)).unwrap();
spawner.spawn(logging_task()).unwrap();
info!("Padmapper initialized!");
// Idle forever
loop {
Timer::after_secs(6 * 60 * 60).await;
}
}
#[embassy_executor::task]
async fn heartbeat_task(mut led: WS2812SPI<Spi<'static, Blocking>>) {
let on_color = Color([8, 0, 0]);
let off_color = Color([0, 0, 0]);
loop {
led.color_send_by_spi(on_color, LED_SPI_HZ).unwrap();
Timer::after_millis(100).await;
led.color_send_by_spi(off_color, LED_SPI_HZ).unwrap();
Timer::after_millis(100).await;
led.color_send_by_spi(on_color, LED_SPI_HZ).unwrap();
Timer::after_millis(100).await;
led.color_send_by_spi(off_color, LED_SPI_HZ).unwrap();
Timer::after_millis(700).await;
}
}
#[embassy_executor::task]
async fn logging_task() {
let mut i = 0u64;
loop {
info!("Hello from Padmapper! {}\r\n", i);
Timer::after_secs(1).await;
i += 1;
}
}