mod player;
use alloc::borrow::Cow;
use alloc::collections::TryReserveError;
use alloc::vec::Vec;
use core::{error, fmt};
pub use player::LightPlayer;
use crate::colors::RgbColor;
use crate::formats::binary::{BinaryBlockType, BinaryFile, BinaryFileError};
mod opcodes {
pub const END: u8 = 0x00;
pub const NOP: u8 = 0x01;
pub const SLEEP: u8 = 0x02;
pub const WAIT_UNTIL: u8 = 0x03;
pub const SET_COLOR: u8 = 0x04;
pub const SET_GRAY: u8 = 0x05;
pub const SET_BLACK: u8 = 0x06;
pub const SET_WHITE: u8 = 0x07;
pub const FADE_TO_COLOR: u8 = 0x08;
pub const FADE_TO_GRAY: u8 = 0x09;
pub const FADE_TO_BLACK: u8 = 0x0a;
pub const FADE_TO_WHITE: u8 = 0x0b;
pub const LOOP_BEGIN: u8 = 0x0c;
pub const LOOP_END: u8 = 0x0d;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum LightProgramError {
File(BinaryFileError),
OutOfMemory,
}
impl fmt::Display for LightProgramError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::File(_) => f.write_str("the show file could not be read"),
Self::OutOfMemory => f.write_str("out of memory"),
}
}
}
impl error::Error for LightProgramError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Self::File(err) => Some(err),
Self::OutOfMemory => None,
}
}
}
impl From<BinaryFileError> for LightProgramError {
fn from(err: BinaryFileError) -> Self {
Self::File(err)
}
}
impl From<TryReserveError> for LightProgramError {
fn from(_: TryReserveError) -> Self {
Self::OutOfMemory
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct LightProgram<'a> {
bytes: Cow<'a, [u8]>,
}
impl LightProgram<'static> {
#[must_use]
pub const fn new() -> Self {
Self {
bytes: Cow::Borrowed(&[]),
}
}
#[must_use]
pub const fn from_vec(bytes: Vec<u8>) -> Self {
Self {
bytes: Cow::Owned(bytes),
}
}
}
impl<'a> LightProgram<'a> {
#[must_use]
pub const fn from_buffer(bytes: &'a [u8]) -> Self {
Self {
bytes: Cow::Borrowed(bytes),
}
}
pub fn from_binary_file(file: &BinaryFile<'a>) -> Result<Option<Self>, LightProgramError> {
Ok(file
.find_first_block_by_type(BinaryBlockType::LIGHT_PROGRAM)?
.map(|block| Self::from_buffer(block.body)))
}
#[must_use]
pub fn bytes(&self) -> &[u8] {
&self.bytes
}
pub fn clear(&mut self) {
*self = LightProgram::new();
}
pub fn set_constant_color(&mut self, color: RgbColor) -> Result<(), LightProgramError> {
let bytecode = [
opcodes::SET_COLOR,
color.red,
color.green,
color.blue,
opcodes::END,
];
let mut bytes = Vec::new();
bytes.try_reserve_exact(bytecode.len())?;
bytes.extend_from_slice(&bytecode);
self.bytes = Cow::Owned(bytes);
Ok(())
}
}