~ruther/map-led-strip

ref: ca6d33bf252720b350b50b4dabba7a4a23bdf09f map-led-strip/src/commands/all_command.rs -rw-r--r-- 1.4 KiB
ca6d33bf — František Boháček feat: add all command 1 year, 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use esp_println::println;
use crate::commands::command_handler::{CommandData, CommandHandleError, SpecificCommandHandler};
use crate::commands::command_handler::CommandHandleError::WrongArguments;

pub struct AllCommand {
}

impl AllCommand {
    pub fn new() -> Self {
        Self {}
    }
}

impl SpecificCommandHandler for AllCommand {
    fn handle(&self, command: &mut CommandData) -> Result<(), CommandHandleError> {
        let cmd = &command.command;
        let map = &mut command.map;

        if cmd.parsed_arguments.len() < 4 {
            println!("Less than 4 args.");
            return Err(WrongArguments);
        }

        let r = cmd.parsed_arguments[1].try_to_integer();
        let g = cmd.parsed_arguments[2].try_to_integer();
        let b = cmd.parsed_arguments[3].try_to_integer();

        if r.is_none() || g.is_none() || b.is_none() {
            println!("Cold not parse r, g, b.");
            return Err(WrongArguments);
        }

        let r = r.unwrap();
        let g = g.unwrap();
        let b = b.unwrap();

        if r > 255 || g > 255 || b > 255 {
            return Err(WrongArguments);
        }

        for led in command.map.get_map_mut() {
            led.r = r as u8;
            led.g = g as u8;
            led.b = b as u8;
        }

        Ok(())
    }

    fn help(&self) -> &'static str {
        "<R> <G> <B> - light up all LEDs to the given color"
    }
}
Do not follow this link