Skip to main content

save/
save.rs

1/*
2 * Copyright (c) 2024 Matteo Franceschini
3 * All rights reserved.
4 *
5 * Use of this source code is governed by BSD-3-Clause-Clear
6 * license that can be found in the LICENSE file
7 */
8
9use std::{time::Duration, vec};
10
11use alienrgb::{Action, AnimationSub, Commands, Effects, Elc, Rgb, Zone};
12
13fn main() {
14    let animation = std::env::var("ANIMATION").unwrap_or("SPECIAL".to_owned());
15    let animation_id: u16 = std::env::var("ANIMATION_ID")
16        .unwrap_or("1".to_owned())
17        .parse()
18        .unwrap_or(1);
19    let red: u8 = std::env::var("RED")
20        .unwrap_or("255".to_owned())
21        .parse()
22        .unwrap_or(255);
23    let green: u8 = std::env::var("GREEN")
24        .unwrap_or("0".to_owned())
25        .parse()
26        .unwrap_or(0);
27    let blue: u8 = std::env::var("BLUE")
28        .unwrap_or("0".to_owned())
29        .parse()
30        .unwrap_or(0);
31    let color = Rgb::new(red, green, blue);
32    let duration_millis: u64 = std::env::var("DURATION")
33        .unwrap_or("1000".to_owned())
34        .parse()
35        .unwrap_or(1000);
36    let duration = Duration::from_millis(duration_millis);
37    let mut elc = Elc::new();
38    let commands = match animation.as_str() {
39        "SPECIAL" => special(animation_id),
40        "BREATHE" => breathe(color, animation_id),
41        "SPECTRUM" => spectrum(duration, animation_id),
42        "WAVE" => wave(color, animation_id),
43        "RAINBOW" => rainbow(duration, animation_id),
44        "BACK_AND_FORTH" => back_and_forth(color, animation_id),
45        _ => special(animation_id),
46    };
47    save(&mut elc, commands).unwrap();
48}
49
50fn save(elc: &mut Elc, commands: Vec<Commands>) -> Result<(), rusb::Error> {
51    for command in commands {
52        let r = elc.execute(&command)?;
53        println!("{command:#?}:\n{r:#?}");
54    }
55    Ok(())
56}
57
58fn special(animation_id: u16) -> Vec<Commands> {
59    let zones_1: Vec<Zone> = (4..8).collect();
60    let zones_2: Vec<Zone> = (8..12).collect();
61    let zones_3: Vec<Zone> = (12..16).collect();
62    let colors_1 = vec![
63        Rgb::new(0, 255, 0),
64        Rgb::new(255, 255, 255),
65        Rgb::new(255, 0, 0),
66    ];
67    let actions_1: Vec<Action> = colors_1
68        .into_iter()
69        .map(|c| {
70            Action::new(
71                Effects::Morph,
72                c,
73                Duration::from_millis(1000),
74                Duration::from_millis(100),
75            )
76        })
77        .collect();
78    let colors_2 = vec![
79        Rgb::new(0, 0, 255),
80        Rgb::new(0, 255, 0),
81        Rgb::new(255, 0, 0),
82    ];
83    let actions_2: Vec<Action> = colors_2
84        .into_iter()
85        .map(|c| {
86            Action::new(
87                Effects::Morph,
88                c,
89                Duration::from_millis(1000),
90                Duration::from_millis(100),
91            )
92        })
93        .collect();
94    let colors_3 = vec![
95        Rgb::new(0x2D, 0x9A, 0x77),
96        Rgb::new(0x9A, 0x2D, 0x98),
97        Rgb::new(0xE5, 0xDC, 0x57),
98    ];
99    let actions_3: Vec<Action> = colors_3
100        .into_iter()
101        .map(|c| {
102            Action::new(
103                Effects::Morph,
104                c,
105                Duration::from_millis(1000),
106                Duration::from_millis(100),
107            )
108        })
109        .collect();
110    let commands = vec![
111        Commands::Animation(AnimationSub::Remove, animation_id),
112        Commands::Animation(AnimationSub::StartNew, animation_id),
113        Commands::StartSeries(true, zones_1.to_owned()),
114        Commands::AddActions(actions_1),
115        Commands::StartSeries(true, zones_2.to_owned()),
116        Commands::AddActions(actions_2),
117        Commands::StartSeries(true, zones_3.to_owned()),
118        Commands::AddActions(actions_3),
119        Commands::Animation(AnimationSub::FinishSave, animation_id),
120    ];
121    commands
122}
123
124fn breathe(color: Rgb, animation_id: u16) -> Vec<Commands> {
125    let zones: Vec<Zone> = (4..16).collect();
126    let actions = [
127        Action::new(
128            Effects::Morph,
129            color,
130            Duration::from_millis(500),
131            Duration::from_millis(64),
132        ),
133        Action::new(
134            Effects::Morph,
135            color,
136            Duration::from_millis(2000),
137            Duration::from_millis(64),
138        ),
139        Action::new(
140            Effects::Morph,
141            Rgb::new(0, 0, 0),
142            Duration::from_millis(500),
143            Duration::from_millis(64),
144        ),
145        Action::new(
146            Effects::Morph,
147            Rgb::new(0, 0, 0),
148            Duration::from_millis(2000),
149            Duration::from_millis(64),
150        ),
151    ];
152    let commands = vec![
153        Commands::Animation(AnimationSub::Remove, animation_id),
154        Commands::Animation(AnimationSub::StartNew, animation_id),
155        Commands::StartSeries(true, zones.to_owned()),
156        Commands::AddActions(actions[..3].to_vec()),
157        Commands::AddActions(actions[3..].to_vec()),
158        Commands::Animation(AnimationSub::FinishSave, animation_id),
159    ];
160    commands
161}
162
163fn spectrum(duration: Duration, animation_id: u16) -> Vec<Commands> {
164    let zones: Vec<Zone> = (4..16).collect();
165    let colors = vec![
166        Rgb::new(0xFF, 0, 0),
167        Rgb::new(0xFF, 0xA5, 0),
168        Rgb::new(0xFF, 0xFF, 0),
169        Rgb::new(0, 0x80, 0),
170        Rgb::new(0, 0xBF, 0xFF),
171        Rgb::new(0, 0, 0xFF),
172        Rgb::new(0x80, 0x00, 0x80),
173    ];
174    let actions: Vec<Action> = colors
175        .into_iter()
176        .map(|c| Action::new(Effects::Morph, c, duration, Duration::from_millis(64)))
177        .collect();
178    let commands = vec![
179        Commands::Animation(AnimationSub::Remove, animation_id),
180        Commands::Animation(AnimationSub::StartNew, animation_id),
181        Commands::StartSeries(true, zones.to_owned()),
182        Commands::AddActions(actions[..3].to_vec()),
183        Commands::AddActions(actions[3..6].to_vec()),
184        Commands::AddActions(actions[6..].to_vec()),
185        Commands::Animation(AnimationSub::FinishSave, animation_id),
186    ];
187    commands
188}
189
190fn wave(color: Rgb, animation_id: u16) -> Vec<Commands> {
191    let right: Vec<Zone> = (4..7).collect();
192    let middle_right: Vec<Zone> = (7..10).collect();
193    let middle_left: Vec<Zone> = (10..13).collect();
194    let left: Vec<Zone> = (13..16).collect();
195    let colors_right = vec![
196        color,
197        Rgb::new(0, 0, 0),
198        Rgb::new(0, 0, 0),
199        Rgb::new(0, 0, 0),
200    ];
201    let actions_right: Vec<Action> = colors_right
202        .into_iter()
203        .map(|c| {
204            Action::new(
205                Effects::Morph,
206                c,
207                Duration::from_millis(500),
208                Duration::from_millis(64),
209            )
210        })
211        .collect();
212    let colors_middle_right = vec![
213        Rgb::new(0, 0, 0),
214        color,
215        Rgb::new(0, 0, 0),
216        Rgb::new(0, 0, 0),
217    ];
218    let actions_middle_right: Vec<Action> = colors_middle_right
219        .into_iter()
220        .map(|c| {
221            Action::new(
222                Effects::Morph,
223                c,
224                Duration::from_millis(500),
225                Duration::from_millis(64),
226            )
227        })
228        .collect();
229    let colors_middle_left = vec![
230        Rgb::new(0, 0, 0),
231        Rgb::new(0, 0, 0),
232        color,
233        Rgb::new(0, 0, 0),
234    ];
235    let actions_middle_left: Vec<Action> = colors_middle_left
236        .into_iter()
237        .map(|c| {
238            Action::new(
239                Effects::Morph,
240                c,
241                Duration::from_millis(500),
242                Duration::from_millis(64),
243            )
244        })
245        .collect();
246    let colors_left = vec![
247        Rgb::new(0, 0, 0),
248        Rgb::new(0, 0, 0),
249        Rgb::new(0, 0, 0),
250        color,
251    ];
252    let actions_left: Vec<Action> = colors_left
253        .into_iter()
254        .map(|c| {
255            Action::new(
256                Effects::Morph,
257                c,
258                Duration::from_millis(500),
259                Duration::from_millis(64),
260            )
261        })
262        .collect();
263    let commands = vec![
264        Commands::Animation(AnimationSub::Remove, animation_id),
265        Commands::Animation(AnimationSub::StartNew, animation_id),
266        Commands::StartSeries(true, right.to_owned()),
267        Commands::AddActions(actions_right[..3].to_vec()),
268        Commands::AddActions(actions_right[3..].to_vec()),
269        Commands::StartSeries(true, middle_right.to_owned()),
270        Commands::AddActions(actions_middle_right[..3].to_vec()),
271        Commands::AddActions(actions_middle_right[3..].to_vec()),
272        Commands::StartSeries(true, middle_left.to_owned()),
273        Commands::AddActions(actions_middle_left[..3].to_vec()),
274        Commands::AddActions(actions_middle_left[3..].to_vec()),
275        Commands::StartSeries(true, left.to_owned()),
276        Commands::AddActions(actions_left[..3].to_vec()),
277        Commands::AddActions(actions_left[3..].to_vec()),
278        Commands::Animation(AnimationSub::FinishSave, animation_id),
279    ];
280    commands
281}
282
283fn rainbow(duration: Duration, animation_id: u16) -> Vec<Commands> {
284    let right: Vec<Zone> = (4..7).collect();
285    let middle_right: Vec<Zone> = (7..10).collect();
286    let middle_left: Vec<Zone> = (10..13).collect();
287    let left: Vec<Zone> = (13..16).collect();
288    let colors_right = vec![
289        Rgb::new(0xFF, 0, 0),
290        Rgb::new(0xFF, 0xA5, 0),
291        Rgb::new(0xFF, 0xFF, 0),
292        Rgb::new(0, 0x80, 0),
293        Rgb::new(0, 0xBF, 0xFF),
294        Rgb::new(0, 0, 0xFF),
295        Rgb::new(0x80, 0, 0x80),
296    ];
297    let actions_right: Vec<Action> = colors_right
298        .into_iter()
299        .map(|c| Action::new(Effects::Morph, c, duration, Duration::from_millis(64)))
300        .collect();
301    let colors_middle_right = vec![
302        Rgb::new(0x80, 0, 0x80),
303        Rgb::new(0xFF, 0, 0),
304        Rgb::new(0xFF, 0xA5, 0),
305        Rgb::new(0xFF, 0xFF, 0),
306        Rgb::new(0, 0x80, 0),
307        Rgb::new(0, 0xBF, 0xFF),
308        Rgb::new(0, 0, 0xFF),
309    ];
310    let actions_middle_right: Vec<Action> = colors_middle_right
311        .into_iter()
312        .map(|c| Action::new(Effects::Morph, c, duration, Duration::from_millis(64)))
313        .collect();
314    let colors_middle_left = vec![
315        Rgb::new(0, 0, 0xFF),
316        Rgb::new(0x80, 0, 0x80),
317        Rgb::new(0xFF, 0, 0),
318        Rgb::new(0xFF, 0xA5, 0),
319        Rgb::new(0xFF, 0xFF, 0),
320        Rgb::new(0, 0x80, 0),
321        Rgb::new(0, 0xBF, 0xFF),
322    ];
323    let actions_middle_left: Vec<Action> = colors_middle_left
324        .into_iter()
325        .map(|c| Action::new(Effects::Morph, c, duration, Duration::from_millis(64)))
326        .collect();
327    let colors_left = vec![
328        Rgb::new(0, 0xBF, 0xFF),
329        Rgb::new(0, 0, 0xFF),
330        Rgb::new(0x80, 0, 0x80),
331        Rgb::new(0xFF, 0, 0),
332        Rgb::new(0xFF, 0xA5, 0),
333        Rgb::new(0xFF, 0xFF, 0),
334        Rgb::new(0, 0x80, 0),
335    ];
336    let actions_left: Vec<Action> = colors_left
337        .into_iter()
338        .map(|c| Action::new(Effects::Morph, c, duration, Duration::from_millis(64)))
339        .collect();
340    let commands = vec![
341        Commands::Animation(AnimationSub::Remove, animation_id),
342        Commands::Animation(AnimationSub::StartNew, animation_id),
343        Commands::StartSeries(true, right.to_owned()),
344        Commands::AddActions(actions_right[..3].to_vec()),
345        Commands::AddActions(actions_right[3..6].to_vec()),
346        Commands::AddActions(actions_right[6..].to_vec()),
347        Commands::StartSeries(true, middle_right.to_owned()),
348        Commands::AddActions(actions_middle_right[..3].to_vec()),
349        Commands::AddActions(actions_middle_right[3..6].to_vec()),
350        Commands::AddActions(actions_middle_right[6..].to_vec()),
351        Commands::StartSeries(true, middle_left.to_owned()),
352        Commands::AddActions(actions_middle_left[..3].to_vec()),
353        Commands::AddActions(actions_middle_left[3..6].to_vec()),
354        Commands::AddActions(actions_middle_left[6..].to_vec()),
355        Commands::StartSeries(true, left.to_owned()),
356        Commands::AddActions(actions_left[..3].to_vec()),
357        Commands::AddActions(actions_left[3..6].to_vec()),
358        Commands::AddActions(actions_left[6..].to_vec()),
359        Commands::Animation(AnimationSub::FinishSave, animation_id),
360    ];
361    commands
362}
363
364fn back_and_forth(color: Rgb, animation_id: u16) -> Vec<Commands> {
365    let right: Vec<Zone> = (4..7).collect();
366    let middle_right: Vec<Zone> = (7..10).collect();
367    let middle_left: Vec<Zone> = (10..13).collect();
368    let left: Vec<Zone> = (13..16).collect();
369    let colors_right = vec![
370        color,
371        Rgb::new(0, 0, 0),
372        Rgb::new(0, 0, 0),
373        Rgb::new(0, 0, 0),
374        Rgb::new(0, 0, 0),
375        Rgb::new(0, 0, 0),
376    ];
377    let actions_right: Vec<Action> = colors_right
378        .into_iter()
379        .map(|c| {
380            Action::new(
381                Effects::Morph,
382                c,
383                Duration::from_millis(500),
384                Duration::from_millis(64),
385            )
386        })
387        .collect();
388    let colors_middle_right = vec![
389        Rgb::new(0, 0, 0),
390        color,
391        Rgb::new(0, 0, 0),
392        Rgb::new(0, 0, 0),
393        Rgb::new(0, 0, 0),
394        color,
395    ];
396    let actions_middle_right: Vec<Action> = colors_middle_right
397        .into_iter()
398        .map(|c| {
399            Action::new(
400                Effects::Morph,
401                c,
402                Duration::from_millis(500),
403                Duration::from_millis(64),
404            )
405        })
406        .collect();
407    let colors_middle_left = vec![
408        Rgb::new(0, 0, 0),
409        Rgb::new(0, 0, 0),
410        color,
411        Rgb::new(0, 0, 0),
412        color,
413        Rgb::new(0, 0, 0),
414    ];
415    let actions_middle_left: Vec<Action> = colors_middle_left
416        .into_iter()
417        .map(|c| {
418            Action::new(
419                Effects::Morph,
420                c,
421                Duration::from_millis(500),
422                Duration::from_millis(64),
423            )
424        })
425        .collect();
426    let colors_left = vec![
427        Rgb::new(0, 0, 0),
428        Rgb::new(0, 0, 0),
429        Rgb::new(0, 0, 0),
430        color,
431        Rgb::new(0, 0, 0),
432        Rgb::new(0, 0, 0),
433    ];
434    let actions_left: Vec<Action> = colors_left
435        .into_iter()
436        .map(|c| {
437            Action::new(
438                Effects::Morph,
439                c,
440                Duration::from_millis(500),
441                Duration::from_millis(64),
442            )
443        })
444        .collect();
445    let commands = vec![
446        Commands::Animation(AnimationSub::Remove, animation_id),
447        Commands::Animation(AnimationSub::StartNew, animation_id),
448        Commands::StartSeries(true, right.to_owned()),
449        Commands::AddActions(actions_right[..3].to_vec()),
450        Commands::AddActions(actions_right[3..].to_vec()),
451        Commands::StartSeries(true, middle_right.to_owned()),
452        Commands::AddActions(actions_middle_right[..3].to_vec()),
453        Commands::AddActions(actions_middle_right[3..].to_vec()),
454        Commands::StartSeries(true, middle_left.to_owned()),
455        Commands::AddActions(actions_middle_left[..3].to_vec()),
456        Commands::AddActions(actions_middle_left[3..].to_vec()),
457        Commands::StartSeries(true, left.to_owned()),
458        Commands::AddActions(actions_left[..3].to_vec()),
459        Commands::AddActions(actions_left[3..].to_vec()),
460        Commands::Animation(AnimationSub::FinishSave, animation_id),
461    ];
462    commands
463}