Next iteration with backoffs, tasks, basic skeleton for events

This commit is contained in:
2025-09-20 20:50:30 +02:00
parent 0315b4a559
commit 29ba78d5b2
15 changed files with 4926 additions and 211 deletions

68
src/tasks/render.rs Normal file
View File

@@ -0,0 +1,68 @@
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, pubsub::Subscriber};
use embassy_time::{Duration, Instant, Timer};
use esp_hal::{rmt::ChannelCreator, Async, peripherals::GPIO5};
use esp_hal_smartled::{buffer_size_async, SmartLedsAdapterAsync};
use figments::{hardware::Output, liber8tion::trig::sin8, prelude::Rectangle, render::Sample};
use log::{info, warn};
use rgb::Rgb;
use crate::{display::BikeOutput, events::Notification};
#[embassy_executor::task]
pub async fn render(mut events: Subscriber<'static, NoopRawMutex, Notification, 4, 4, 4>, rmt_channel: ChannelCreator<Async, 0>, gpio: GPIO5<'static>) {
let rmt_buffer = [0u32; buffer_size_async(178)];
let target = SmartLedsAdapterAsync::new(rmt_channel, gpio, rmt_buffer);
// Change this number to use a different number of LEDs
//let mut pixbuf = [Default::default(); 16];
// Change this to adjust the power available; the USB spec says 500ma is the standard limit, but sometimes you can draw more from a power brick
const POWER_MA : u32 = 500;
// You probably don't need to change these values, unless your LED strip is somehow not 5 volts
const POWER_VOLTS : u32 = 5;
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
// This value is used as the 'seed' for rendering each frame, allowing us to do things like run the animation backwards, frames for double FPS, or even use system uptime for more human-paced animations
let mut frame = 0;
//let mut target = BrightnessWriter::new(target, MAX_POWER_MW);
let mut output = BikeOutput::new(target, MAX_POWER_MW);
info!("Rendering started!");
loop {
/*if let Some(evt) = events.try_next_message() {
}*/
let start = Instant::now();
//pixbuf.blank();
output.blank().await.expect("Failed to blank framebuf");
// Render the frame to the pixbuf, while also calculating the power consumption as we go
for (coords, pix) in output.sample(&Rectangle::everything()) {
*pix = Rgb::new(sin8(coords.x.wrapping_mul(3).wrapping_add(coords.y.wrapping_mul(3)).wrapping_add(frame)), 0, 0);
}
// Finally, write out the rendered frame
//target.write(pixbuf.iter().cloned()).await.expect("Could not write frame");
//info!("frame");
output.commit().await.expect("Failed to commit frame");
//info!("commit");
let render_duration = Instant::now() - start;
let render_budget = Duration::from_millis(16);
if render_duration < render_budget {
let remaining_budget = render_budget - render_duration;
Timer::after(remaining_budget).await;
} else {
warn!("Render stall! Frame took {}ms", render_duration.as_millis());
}
// Increment the frame counter
frame += 1;
}
}