From ca6d33bf252720b350b50b4dabba7a4a23bdf09f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Wed, 21 Jun 2023 13:56:57 +0200 Subject: [PATCH] feat: add all command --- src/commands.rs | 3 ++- src/commands/all_command.rs | 53 +++++++++++++++++++++++++++++++++++++ src/main.rs | 3 +++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/commands/all_command.rs diff --git a/src/commands.rs b/src/commands.rs index 6b93a443d6c75297d8bb38f6597e80020cbdfeb8..faf438d792c92e64c6d4c36ff2f21fb2bb0d7fec 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -3,4 +3,5 @@ pub mod command_handler; pub mod command; pub mod command_argument; pub mod set_command; -pub mod reset_command; \ No newline at end of file +pub mod reset_command; +pub mod all_command; \ No newline at end of file diff --git a/src/commands/all_command.rs b/src/commands/all_command.rs new file mode 100644 index 0000000000000000000000000000000000000000..4551074283db331bbc4b3b2ac7cfac83308ff44b --- /dev/null +++ b/src/commands/all_command.rs @@ -0,0 +1,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 { + " - light up all LEDs to the given color" + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9447073764610803f1a8542587c959f2efd96e4f..9db640673c865e4a9fbc97e4e63a449f9845ae72 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ use hal::uart::TxRxPins; use nb::block; use nb::Error::{WouldBlock, Other}; use smart_leds::{RGB8, SmartLedsWrite}; +use crate::commands::all_command::AllCommand; use crate::commands::command_handler::{CommandHandler}; use crate::commands::command_handler; use crate::commands::hello_world_command::HelloWorldCommand; @@ -95,11 +96,13 @@ fn main() -> ! { let world_command = HelloWorldCommand::new(); let set_command = SetCommand::new(); let reset_command = ResetCommand::new(); + let all_command = AllCommand::new(); let mut handler = CommandHandler::new( [ ("HELLO_WORLD", &world_command), ("SET", &set_command), ("RESET", &reset_command), + ("ALL", &all_command) ], ['\0'; COMMAND_BUFFER], );