use core::ops::Deref;
use super::{LightProgram, opcodes};
use crate::colors::RgbColor;
const MAX_LOOP_DEPTH: usize = 4;
const SLEEP_AFTER_END_MSEC: u64 = 60_000;
const FRAME_MSEC: u64 = 20;
#[derive(Clone, Debug)]
pub struct LightPlayer<T> {
program: T,
executor: Executor,
current_timestamp: u64,
next_timestamp: u64,
}
impl<'a, T: Deref<Target = LightProgram<'a>>> LightPlayer<T> {
pub fn new(program: T) -> Self {
let mut executor = Executor::default();
executor.step(&[], 0);
executor.rewind(program.bytes());
Self {
program,
executor,
current_timestamp: 0,
next_timestamp: 0,
}
}
pub fn program(&self) -> &LightProgram<'a> {
&self.program
}
pub fn color_at(&mut self, timestamp: u64) -> RgbColor {
self.seek(timestamp);
self.executor.current_color
}
pub fn seek(&mut self, timestamp: u64) -> bool {
let bytes = self.program.bytes();
if timestamp < self.current_timestamp {
self.executor.rewind(bytes);
self.current_timestamp = 0;
self.next_timestamp = 0;
}
while timestamp > self.next_timestamp {
let mut proposal = self.executor.step(bytes, self.next_timestamp);
if proposal < self.next_timestamp {
proposal = self.next_timestamp.saturating_add(1);
}
self.current_timestamp = self.next_timestamp;
self.next_timestamp = proposal;
}
loop {
self.next_timestamp = self.executor.step(bytes, timestamp);
if self.next_timestamp > timestamp {
break;
}
}
self.current_timestamp = timestamp;
self.executor.ended
}
#[must_use]
pub const fn next_timestamp(&self) -> u64 {
self.next_timestamp
}
}
#[derive(Clone, Debug)]
struct Executor {
position: usize,
ended: bool,
loops: LoopStack,
cumulative_duration: u64,
command_start_time: u64,
clock_reset_time: u64,
next_wakeup_time: u64,
reset_clock: bool,
transition: Transition,
current_color: RgbColor,
}
impl Default for Executor {
fn default() -> Self {
Self {
position: 0,
ended: true,
loops: LoopStack::default(),
cumulative_duration: 0,
command_start_time: 0,
clock_reset_time: 0,
next_wakeup_time: 0,
reset_clock: true,
transition: Transition::default(),
current_color: RgbColor::BLACK,
}
}
}
impl Executor {
fn rewind(&mut self, bytes: &[u8]) {
self.position = 0;
self.ended = bytes.is_empty();
self.loops = LoopStack::default();
self.transition.active = false;
self.current_color = RgbColor::BLACK;
self.reset_clock = true;
}
fn step(&mut self, bytes: &[u8], now: u64) -> u64 {
if self.reset_clock {
self.clock_reset_time = now;
self.cumulative_duration = 0;
self.set_color_and_reset_transition(RgbColor::BLACK);
self.reset_clock = false;
self.next_wakeup_time = now;
}
if self.ended {
self.next_wakeup_time = now.wrapping_add(SLEEP_AFTER_END_MSEC);
return self.next_wakeup_time;
}
if self.transition.active {
self.step_transition(now);
}
if now >= self.next_wakeup_time {
self.command_start_time = now;
self.execute_next_command(bytes);
}
self.next_wakeup_time
}
fn execute_next_command(&mut self, bytes: &[u8]) {
match self.next_byte(bytes) {
opcodes::NOP => {}
opcodes::SLEEP => {
self.handle_delay(bytes);
}
opcodes::WAIT_UNTIL => {
let deadline = u64::from(self.next_varint(bytes)) * FRAME_MSEC;
self.delay_until(deadline);
self.cumulative_duration =
self.next_wakeup_time.wrapping_sub(self.clock_reset_time);
}
opcodes::SET_COLOR => {
let color = self.next_color(bytes);
self.handle_delay(bytes);
self.set_color_and_reset_transition(color);
}
opcodes::SET_GRAY => {
let color = self.next_gray(bytes);
self.handle_delay(bytes);
self.set_color_and_reset_transition(color);
}
opcodes::SET_BLACK => {
self.handle_delay(bytes);
self.set_color_and_reset_transition(RgbColor::BLACK);
}
opcodes::SET_WHITE => {
self.handle_delay(bytes);
self.set_color_and_reset_transition(RgbColor::WHITE);
}
opcodes::FADE_TO_COLOR => {
let color = self.next_color(bytes);
self.fade_to(bytes, color);
}
opcodes::FADE_TO_GRAY => {
let color = self.next_gray(bytes);
self.fade_to(bytes, color);
}
opcodes::FADE_TO_BLACK => self.fade_to(bytes, RgbColor::BLACK),
opcodes::FADE_TO_WHITE => self.fade_to(bytes, RgbColor::WHITE),
opcodes::LOOP_BEGIN => {
let iterations = self.next_byte(bytes);
if !self
.loops
.begin(self.position, iterations, self.cumulative_duration)
{
self.ended = true;
}
}
opcodes::LOOP_END => {
if self
.loops
.current_iteration_has_no_duration(self.cumulative_duration)
{
if self.loops.innermost_is_infinite() {
self.ended = true;
} else {
self.loops.leave();
}
} else if let Some(start) = self.loops.end(self.cumulative_duration) {
self.position = start;
}
}
_ => self.ended = true,
}
}
fn next_byte(&mut self, bytes: &[u8]) -> u8 {
match bytes.get(self.position) {
Some(&byte) => {
self.position += 1;
byte
}
None => opcodes::END,
}
}
fn next_color(&mut self, bytes: &[u8]) -> RgbColor {
let red = self.next_byte(bytes);
let green = self.next_byte(bytes);
let blue = self.next_byte(bytes);
RgbColor::new(red, green, blue)
}
fn next_gray(&mut self, bytes: &[u8]) -> RgbColor {
let value = self.next_byte(bytes);
RgbColor::new(value, value, value)
}
fn next_varint(&mut self, bytes: &[u8]) -> u32 {
let mut result: u32 = 0;
let mut index: u32 = 0;
let mut overflow = false;
loop {
let byte = self.next_byte(bytes);
let value = u32::from(byte & 0x7f);
if index < 4 || (index == 4 && value < 0x10) {
result |= value << (7 * index);
index += 1;
} else {
overflow = true;
}
if byte & 0x80 == 0 {
break;
}
}
if overflow { u32::MAX } else { result }
}
fn handle_delay(&mut self, bytes: &[u8]) {
let duration = u64::from(self.next_varint(bytes)) * FRAME_MSEC;
self.cumulative_duration = self.cumulative_duration.wrapping_add(duration);
self.delay_until(self.cumulative_duration);
}
fn delay_until(&mut self, internal_time: u64) {
let time = self.clock_reset_time.wrapping_add(internal_time);
self.next_wakeup_time = self.next_wakeup_time.max(time);
}
fn fade_to(&mut self, bytes: &[u8], color: RgbColor) {
let now = self.command_start_time;
self.handle_delay(bytes);
let duration = self.next_wakeup_time.wrapping_sub(now);
self.transition.end_color = color;
self.transition.active = true;
self.transition.start_time = self.command_start_time;
self.transition.duration = duration;
self.step_transition(now);
}
fn step_transition(&mut self, now: u64) {
let progress = self.transition.progress(now);
self.current_color = self
.transition
.start_color
.linear_interpolation(self.transition.end_color, progress);
self.transition.active = progress < 1.0;
if !self.transition.active {
self.transition.start_color = self.transition.end_color;
}
}
fn set_color_and_reset_transition(&mut self, color: RgbColor) {
self.current_color = color;
self.transition.start_color = color;
}
}
#[derive(Clone, Copy, Debug, Default)]
struct Transition {
active: bool,
start_time: u64,
duration: u64,
start_color: RgbColor,
end_color: RgbColor,
}
impl Transition {
#[expect(
clippy::cast_precision_loss,
reason = "the progress is a float, as in the C library"
)]
fn progress(&self, now: u64) -> f32 {
if now < self.start_time {
0.0
} else if self.duration == 0 {
1.0
} else {
let progress = (now - self.start_time) as f32 / self.duration as f32;
progress.min(1.0)
}
}
}
#[derive(Clone, Copy, Debug, Default)]
struct LoopStack {
items: [LoopStackItem; MAX_LOOP_DEPTH],
depth: usize,
}
#[derive(Clone, Copy, Debug, Default)]
struct LoopStackItem {
start: usize,
iterations_left_plus_one: u8,
iteration_start_time: u64,
}
impl LoopStack {
fn begin(&mut self, start: usize, iterations: u8, time: u64) -> bool {
let Some(item) = self.items.get_mut(self.depth) else {
return false;
};
*item = LoopStackItem {
start,
iterations_left_plus_one: iterations,
iteration_start_time: time,
};
self.depth += 1;
true
}
fn end(&mut self, time: u64) -> Option<usize> {
let index = self.depth.checked_sub(1)?;
let item = self.items.get_mut(index)?;
match item.iterations_left_plus_one {
0 => {
item.iteration_start_time = time;
Some(item.start)
}
1 => {
item.iterations_left_plus_one = 0;
self.depth = index;
None
}
_ => {
item.iterations_left_plus_one -= 1;
item.iteration_start_time = time;
Some(item.start)
}
}
}
fn leave(&mut self) {
self.depth = self.depth.saturating_sub(1);
}
fn innermost(&self) -> Option<&LoopStackItem> {
self.items.get(self.depth.checked_sub(1)?)
}
fn innermost_is_infinite(&self) -> bool {
self.innermost()
.is_some_and(|item| item.iterations_left_plus_one == 0)
}
fn current_iteration_has_no_duration(&self, time: u64) -> bool {
self.innermost()
.is_some_and(|item| item.iteration_start_time == time)
}
}