use std::{time::Duration, vec};
use alienrgb::{Action, AnimationSub, Commands, Effects, Elc, Rgb, Zone};
fn main() {
let animation = std::env::var("ANIMATION").unwrap_or("SPECIAL".to_owned());
let animation_id: u16 = std::env::var("ANIMATION_ID")
.unwrap_or("1".to_owned())
.parse()
.unwrap_or(1);
let red: u8 = std::env::var("RED")
.unwrap_or("255".to_owned())
.parse()
.unwrap_or(255);
let green: u8 = std::env::var("GREEN")
.unwrap_or("0".to_owned())
.parse()
.unwrap_or(0);
let blue: u8 = std::env::var("BLUE")
.unwrap_or("0".to_owned())
.parse()
.unwrap_or(0);
let color = Rgb::new(red, green, blue);
let duration_millis: u64 = std::env::var("DURATION")
.unwrap_or("1000".to_owned())
.parse()
.unwrap_or(1000);
let duration = Duration::from_millis(duration_millis);
let mut elc = Elc::new();
let commands = match animation.as_str() {
"SPECIAL" => special(animation_id),
"BREATHE" => breathe(color, animation_id),
"SPECTRUM" => spectrum(duration, animation_id),
"WAVE" => wave(color, animation_id),
"RAINBOW" => rainbow(duration, animation_id),
"BACK_AND_FORTH" => back_and_forth(color, animation_id),
_ => special(animation_id),
};
save(&mut elc, commands).unwrap();
}
fn save(elc: &mut Elc, commands: Vec<Commands>) -> Result<(), rusb::Error> {
for command in commands {
let r = elc.execute(&command)?;
println!("{command:#?}:\n{r:#?}");
}
Ok(())
}
fn special(animation_id: u16) -> Vec<Commands> {
let zones_1: Vec<Zone> = (4..8).collect();
let zones_2: Vec<Zone> = (8..12).collect();
let zones_3: Vec<Zone> = (12..16).collect();
let colors_1 = vec![
Rgb::new(0, 255, 0),
Rgb::new(255, 255, 255),
Rgb::new(255, 0, 0),
];
let actions_1: Vec<Action> = colors_1
.into_iter()
.map(|c| {
Action::new(
Effects::Morph,
c,
Duration::from_millis(1000),
Duration::from_millis(100),
)
})
.collect();
let colors_2 = vec![
Rgb::new(0, 0, 255),
Rgb::new(0, 255, 0),
Rgb::new(255, 0, 0),
];
let actions_2: Vec<Action> = colors_2
.into_iter()
.map(|c| {
Action::new(
Effects::Morph,
c,
Duration::from_millis(1000),
Duration::from_millis(100),
)
})
.collect();
let colors_3 = vec![
Rgb::new(0x2D, 0x9A, 0x77),
Rgb::new(0x9A, 0x2D, 0x98),
Rgb::new(0xE5, 0xDC, 0x57),
];
let actions_3: Vec<Action> = colors_3
.into_iter()
.map(|c| {
Action::new(
Effects::Morph,
c,
Duration::from_millis(1000),
Duration::from_millis(100),
)
})
.collect();
let commands = vec![
Commands::Animation(AnimationSub::Remove, animation_id),
Commands::Animation(AnimationSub::StartNew, animation_id),
Commands::StartSeries(true, zones_1.to_owned()),
Commands::AddActions(actions_1),
Commands::StartSeries(true, zones_2.to_owned()),
Commands::AddActions(actions_2),
Commands::StartSeries(true, zones_3.to_owned()),
Commands::AddActions(actions_3),
Commands::Animation(AnimationSub::FinishSave, animation_id),
];
commands
}
fn breathe(color: Rgb, animation_id: u16) -> Vec<Commands> {
let zones: Vec<Zone> = (4..16).collect();
let actions = [
Action::new(
Effects::Morph,
color,
Duration::from_millis(500),
Duration::from_millis(64),
),
Action::new(
Effects::Morph,
color,
Duration::from_millis(2000),
Duration::from_millis(64),
),
Action::new(
Effects::Morph,
Rgb::new(0, 0, 0),
Duration::from_millis(500),
Duration::from_millis(64),
),
Action::new(
Effects::Morph,
Rgb::new(0, 0, 0),
Duration::from_millis(2000),
Duration::from_millis(64),
),
];
let commands = vec![
Commands::Animation(AnimationSub::Remove, animation_id),
Commands::Animation(AnimationSub::StartNew, animation_id),
Commands::StartSeries(true, zones.to_owned()),
Commands::AddActions(actions[..3].to_vec()),
Commands::AddActions(actions[3..].to_vec()),
Commands::Animation(AnimationSub::FinishSave, animation_id),
];
commands
}
fn spectrum(duration: Duration, animation_id: u16) -> Vec<Commands> {
let zones: Vec<Zone> = (4..16).collect();
let colors = vec![
Rgb::new(0xFF, 0, 0),
Rgb::new(0xFF, 0xA5, 0),
Rgb::new(0xFF, 0xFF, 0),
Rgb::new(0, 0x80, 0),
Rgb::new(0, 0xBF, 0xFF),
Rgb::new(0, 0, 0xFF),
Rgb::new(0x80, 0x00, 0x80),
];
let actions: Vec<Action> = colors
.into_iter()
.map(|c| Action::new(Effects::Morph, c, duration, Duration::from_millis(64)))
.collect();
let commands = vec![
Commands::Animation(AnimationSub::Remove, animation_id),
Commands::Animation(AnimationSub::StartNew, animation_id),
Commands::StartSeries(true, zones.to_owned()),
Commands::AddActions(actions[..3].to_vec()),
Commands::AddActions(actions[3..6].to_vec()),
Commands::AddActions(actions[6..].to_vec()),
Commands::Animation(AnimationSub::FinishSave, animation_id),
];
commands
}
fn wave(color: Rgb, animation_id: u16) -> Vec<Commands> {
let right: Vec<Zone> = (4..7).collect();
let middle_right: Vec<Zone> = (7..10).collect();
let middle_left: Vec<Zone> = (10..13).collect();
let left: Vec<Zone> = (13..16).collect();
let colors_right = vec![
color,
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
];
let actions_right: Vec<Action> = colors_right
.into_iter()
.map(|c| {
Action::new(
Effects::Morph,
c,
Duration::from_millis(500),
Duration::from_millis(64),
)
})
.collect();
let colors_middle_right = vec![
Rgb::new(0, 0, 0),
color,
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
];
let actions_middle_right: Vec<Action> = colors_middle_right
.into_iter()
.map(|c| {
Action::new(
Effects::Morph,
c,
Duration::from_millis(500),
Duration::from_millis(64),
)
})
.collect();
let colors_middle_left = vec![
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
color,
Rgb::new(0, 0, 0),
];
let actions_middle_left: Vec<Action> = colors_middle_left
.into_iter()
.map(|c| {
Action::new(
Effects::Morph,
c,
Duration::from_millis(500),
Duration::from_millis(64),
)
})
.collect();
let colors_left = vec![
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
color,
];
let actions_left: Vec<Action> = colors_left
.into_iter()
.map(|c| {
Action::new(
Effects::Morph,
c,
Duration::from_millis(500),
Duration::from_millis(64),
)
})
.collect();
let commands = vec![
Commands::Animation(AnimationSub::Remove, animation_id),
Commands::Animation(AnimationSub::StartNew, animation_id),
Commands::StartSeries(true, right.to_owned()),
Commands::AddActions(actions_right[..3].to_vec()),
Commands::AddActions(actions_right[3..].to_vec()),
Commands::StartSeries(true, middle_right.to_owned()),
Commands::AddActions(actions_middle_right[..3].to_vec()),
Commands::AddActions(actions_middle_right[3..].to_vec()),
Commands::StartSeries(true, middle_left.to_owned()),
Commands::AddActions(actions_middle_left[..3].to_vec()),
Commands::AddActions(actions_middle_left[3..].to_vec()),
Commands::StartSeries(true, left.to_owned()),
Commands::AddActions(actions_left[..3].to_vec()),
Commands::AddActions(actions_left[3..].to_vec()),
Commands::Animation(AnimationSub::FinishSave, animation_id),
];
commands
}
fn rainbow(duration: Duration, animation_id: u16) -> Vec<Commands> {
let right: Vec<Zone> = (4..7).collect();
let middle_right: Vec<Zone> = (7..10).collect();
let middle_left: Vec<Zone> = (10..13).collect();
let left: Vec<Zone> = (13..16).collect();
let colors_right = vec![
Rgb::new(0xFF, 0, 0),
Rgb::new(0xFF, 0xA5, 0),
Rgb::new(0xFF, 0xFF, 0),
Rgb::new(0, 0x80, 0),
Rgb::new(0, 0xBF, 0xFF),
Rgb::new(0, 0, 0xFF),
Rgb::new(0x80, 0, 0x80),
];
let actions_right: Vec<Action> = colors_right
.into_iter()
.map(|c| Action::new(Effects::Morph, c, duration, Duration::from_millis(64)))
.collect();
let colors_middle_right = vec![
Rgb::new(0x80, 0, 0x80),
Rgb::new(0xFF, 0, 0),
Rgb::new(0xFF, 0xA5, 0),
Rgb::new(0xFF, 0xFF, 0),
Rgb::new(0, 0x80, 0),
Rgb::new(0, 0xBF, 0xFF),
Rgb::new(0, 0, 0xFF),
];
let actions_middle_right: Vec<Action> = colors_middle_right
.into_iter()
.map(|c| Action::new(Effects::Morph, c, duration, Duration::from_millis(64)))
.collect();
let colors_middle_left = vec![
Rgb::new(0, 0, 0xFF),
Rgb::new(0x80, 0, 0x80),
Rgb::new(0xFF, 0, 0),
Rgb::new(0xFF, 0xA5, 0),
Rgb::new(0xFF, 0xFF, 0),
Rgb::new(0, 0x80, 0),
Rgb::new(0, 0xBF, 0xFF),
];
let actions_middle_left: Vec<Action> = colors_middle_left
.into_iter()
.map(|c| Action::new(Effects::Morph, c, duration, Duration::from_millis(64)))
.collect();
let colors_left = vec![
Rgb::new(0, 0xBF, 0xFF),
Rgb::new(0, 0, 0xFF),
Rgb::new(0x80, 0, 0x80),
Rgb::new(0xFF, 0, 0),
Rgb::new(0xFF, 0xA5, 0),
Rgb::new(0xFF, 0xFF, 0),
Rgb::new(0, 0x80, 0),
];
let actions_left: Vec<Action> = colors_left
.into_iter()
.map(|c| Action::new(Effects::Morph, c, duration, Duration::from_millis(64)))
.collect();
let commands = vec![
Commands::Animation(AnimationSub::Remove, animation_id),
Commands::Animation(AnimationSub::StartNew, animation_id),
Commands::StartSeries(true, right.to_owned()),
Commands::AddActions(actions_right[..3].to_vec()),
Commands::AddActions(actions_right[3..6].to_vec()),
Commands::AddActions(actions_right[6..].to_vec()),
Commands::StartSeries(true, middle_right.to_owned()),
Commands::AddActions(actions_middle_right[..3].to_vec()),
Commands::AddActions(actions_middle_right[3..6].to_vec()),
Commands::AddActions(actions_middle_right[6..].to_vec()),
Commands::StartSeries(true, middle_left.to_owned()),
Commands::AddActions(actions_middle_left[..3].to_vec()),
Commands::AddActions(actions_middle_left[3..6].to_vec()),
Commands::AddActions(actions_middle_left[6..].to_vec()),
Commands::StartSeries(true, left.to_owned()),
Commands::AddActions(actions_left[..3].to_vec()),
Commands::AddActions(actions_left[3..6].to_vec()),
Commands::AddActions(actions_left[6..].to_vec()),
Commands::Animation(AnimationSub::FinishSave, animation_id),
];
commands
}
fn back_and_forth(color: Rgb, animation_id: u16) -> Vec<Commands> {
let right: Vec<Zone> = (4..7).collect();
let middle_right: Vec<Zone> = (7..10).collect();
let middle_left: Vec<Zone> = (10..13).collect();
let left: Vec<Zone> = (13..16).collect();
let colors_right = vec![
color,
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
];
let actions_right: Vec<Action> = colors_right
.into_iter()
.map(|c| {
Action::new(
Effects::Morph,
c,
Duration::from_millis(500),
Duration::from_millis(64),
)
})
.collect();
let colors_middle_right = vec![
Rgb::new(0, 0, 0),
color,
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
color,
];
let actions_middle_right: Vec<Action> = colors_middle_right
.into_iter()
.map(|c| {
Action::new(
Effects::Morph,
c,
Duration::from_millis(500),
Duration::from_millis(64),
)
})
.collect();
let colors_middle_left = vec![
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
color,
Rgb::new(0, 0, 0),
color,
Rgb::new(0, 0, 0),
];
let actions_middle_left: Vec<Action> = colors_middle_left
.into_iter()
.map(|c| {
Action::new(
Effects::Morph,
c,
Duration::from_millis(500),
Duration::from_millis(64),
)
})
.collect();
let colors_left = vec![
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
color,
Rgb::new(0, 0, 0),
Rgb::new(0, 0, 0),
];
let actions_left: Vec<Action> = colors_left
.into_iter()
.map(|c| {
Action::new(
Effects::Morph,
c,
Duration::from_millis(500),
Duration::from_millis(64),
)
})
.collect();
let commands = vec![
Commands::Animation(AnimationSub::Remove, animation_id),
Commands::Animation(AnimationSub::StartNew, animation_id),
Commands::StartSeries(true, right.to_owned()),
Commands::AddActions(actions_right[..3].to_vec()),
Commands::AddActions(actions_right[3..].to_vec()),
Commands::StartSeries(true, middle_right.to_owned()),
Commands::AddActions(actions_middle_right[..3].to_vec()),
Commands::AddActions(actions_middle_right[3..].to_vec()),
Commands::StartSeries(true, middle_left.to_owned()),
Commands::AddActions(actions_middle_left[..3].to_vec()),
Commands::AddActions(actions_middle_left[3..].to_vec()),
Commands::StartSeries(true, left.to_owned()),
Commands::AddActions(actions_left[..3].to_vec()),
Commands::AddActions(actions_left[3..].to_vec()),
Commands::Animation(AnimationSub::FinishSave, animation_id),
];
commands
}