~ruther/map-led-strip

b96d6d8db3856bf4056a9092185fb9ffbb69a6af — František Boháček 1 year, 9 months ago 85077ba
refactor: remove body of specific commands, do not expose fields of structs publicly...
M src/commands.rs => src/commands.rs +2 -1
@@ 4,4 4,5 @@ pub mod command;
pub mod command_argument;
pub mod set_command;
pub mod reset_command;
pub mod all_command;
\ No newline at end of file
pub mod all_command;
pub mod command_data;
\ No newline at end of file

M src/commands/all_command.rs => src/commands/all_command.rs +11 -17
@@ 1,29 1,23 @@
use esp_println::println;
use crate::commands::command_handler::{CommandData, CommandHandleError, SpecificCommandHandler};
use crate::commands::command_handler::{CommandHandleError, SpecificCommandHandler};
use crate::commands::command_handler::CommandHandleError::WrongArguments;
use crate::commands::command_data::CommandData;

pub struct AllCommand {
}

impl AllCommand {
    pub fn new() -> Self {
        Self {}
    }
}
#[derive(Default)]
pub struct AllCommand;

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

        if cmd.parsed_arguments.len() < 4 {
        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();
        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.");


@@ 38,7 32,7 @@ impl SpecificCommandHandler for AllCommand {
            return Err(WrongArguments);
        }

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

M src/commands/command.rs => src/commands/command.rs +18 -2
@@ 2,6 2,22 @@ use crate::commands::command_argument::CommandArgument;

pub struct Command<'d>
{
    pub full: &'d [char],
    pub parsed_arguments: &'d [CommandArgument<'d>],
    full: &'d [char],
    parsed_arguments: &'d [CommandArgument<'d>],
}

impl<'d> Command<'d> {
    pub fn new(full: &'d [char], parsed_arguments: &'d [CommandArgument<'d>]) -> Self {
        Self {
            full,
            parsed_arguments
        }
    }
    pub fn full(&self) -> &'d [char] {
        self.full
    }

    pub fn parsed_arguments(&self) -> &'d [CommandArgument<'d>] {
        self.parsed_arguments
    }
}
\ No newline at end of file

M src/commands/command_argument.rs => src/commands/command_argument.rs +0 -2
@@ 1,5 1,3 @@
use esp_println::{print, println};

#[derive(Clone, Copy, Eq, PartialEq)]
pub struct CommandArgument<'d>
{

M src/commands/command_handler.rs => src/commands/command_handler.rs +9 -24
@@ 1,24 1,16 @@
use core::slice::IterMut;
use embedded_hal::serial::{Read, Write};
use esp_println::{print, println};
use esp_println::println;
use nb::block;
use nb::Error::{Other, WouldBlock};
use crate::command_handler::CommandHandleError::{CommandNotRead, NotFound};
use crate::command_handler::CommandReadError::{BufferOverflowed, CommandLoadedAlready, UnexpectedEndOfLine};
use crate::commands::command::Command;
use crate::commands::command_argument::CommandArgument;
use crate::command_handler::{CommandHandleError::{CommandNotRead, NotFound}, CommandReadError::{BufferOverflowed, CommandLoadedAlready, UnexpectedEndOfLine}};
use crate::commands::{command::Command, command_argument::CommandArgument, command_data::CommandData};
use crate::map::Map;

pub trait SpecificCommandHandler {
    fn handle(&self, command: &mut CommandData) -> Result<(), CommandHandleError>;
    fn handle(&self, command: CommandData) -> Result<(), CommandHandleError>;
    fn help(&self) -> &'static str;
}

pub struct CommandData<'d, 'a> {
    pub command: Command<'d>,
    pub map: &'d mut Map<'a>,
}

pub struct CommandHandler<'d, const BUFFER_SIZE: usize, const HANDLERS_COUNT: usize> {
    buffer_position: usize,
    command_loaded: bool,


@@ 158,10 150,7 @@ impl<'d, const BUFFER_SIZE: usize, const HANDLERS_COUNT: usize> CommandHandler<'
            }
        }

        Command {
            full: buffer,
            parsed_arguments: &args[0..length],
        }
        Command::new(buffer, &args[0..length])
    }

    fn handle_help(&self) -> Result<(), CommandHandleError>


@@ 185,12 174,12 @@ impl<'d, const BUFFER_SIZE: usize, const HANDLERS_COUNT: usize> CommandHandler<'

        let command = self.parse_command(buffer, &mut args);

        if command.parsed_arguments.len() == 0 {
        if command.parsed_arguments().len() == 0 {
            self.reset();
            return Err(NotFound);
        }

        let first_argument = command.parsed_arguments[0];
        let first_argument = command.parsed_arguments()[0];

        if first_argument.compare("HELP") {
            let help_handled = self.handle_help();


@@ 203,12 192,8 @@ impl<'d, const BUFFER_SIZE: usize, const HANDLERS_COUNT: usize> CommandHandler<'
                continue;
            }

            let mut command_data = CommandData {
                command,
                map,
            };

            let handled = handler.handle(&mut command_data);
            let command_data = CommandData::new(&command, map);
            let handled = handler.handle(command_data);
            self.reset();
            return handled;
        }

M src/commands/hello_world_command.rs => src/commands/hello_world_command.rs +8 -14
@@ 1,24 1,18 @@
use embedded_hal::serial::{Read, Write};
use esp_println::{print, println};
use crate::command_handler::{CommandData, CommandHandleError, SpecificCommandHandler};
use crate::map::Map;
use crate::command_handler::{CommandHandleError, SpecificCommandHandler};
use crate::commands::command_data::CommandData;

pub struct HelloWorldCommand {

}

impl HelloWorldCommand {
    pub fn new() -> Self {
        Self {}
    }
}
#[derive(Default)]
pub struct HelloWorldCommand;

impl SpecificCommandHandler for HelloWorldCommand
{
    fn handle(&self, command: &mut CommandData) -> Result<(), CommandHandleError>
    fn handle(&self, command: CommandData) -> Result<(), CommandHandleError>
    {
        let command = command.command();
        let full = command.full();
        print!("Hello world!");
        for c in &command.command.full[command.command.parsed_arguments[0].data.len()..] {
        for c in &full[command.parsed_arguments()[0].data.len()..] {
            print!("{}", c);
        }
        println!("\r");

M src/commands/reset_command.rs => src/commands/reset_command.rs +6 -11
@@ 1,17 1,12 @@
use crate::commands::command_handler::{CommandData, CommandHandleError, SpecificCommandHandler};
use crate::commands::command_handler::CommandHandleError::WrongArguments;
use crate::commands::command_handler::{CommandHandleError, SpecificCommandHandler};
use crate::commands::command_data::CommandData;

pub struct ResetCommand {}

impl ResetCommand {
    pub fn new() -> Self {
        Self {}
    }
}
#[derive(Default)]
pub struct ResetCommand;

impl SpecificCommandHandler for ResetCommand {
    fn handle(&self, command: &mut CommandData) -> Result<(), CommandHandleError> {
        for led in command.map.get_map_mut() {
    fn handle(&self, command: CommandData) -> Result<(), CommandHandleError> {
        for led in command.map().get_map_mut() {
            led.r = 0;
            led.g = 0;
            led.b = 0;

M src/commands/set_command.rs => src/commands/set_command.rs +11 -16
@@ 1,26 1,21 @@
use esp_println::println;
use crate::commands::command_handler::{CommandData, CommandHandleError, SpecificCommandHandler};
use crate::commands::command_handler::{CommandHandleError, SpecificCommandHandler};
use crate::commands::command_handler::CommandHandleError::WrongArguments;
use crate::commands::command_data::CommandData;

pub struct SetCommand {}

impl SetCommand {
    pub fn new() -> Self {
        Self {}
    }
}
#[derive(Default)]
pub struct SetCommand;

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

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

        let led_id = cmd.parsed_arguments[1];
        let led_id = cmd.parsed_arguments()[1];
        let led_id = if let Some(id) = led_id.try_to_integer() {
            Some(id as usize)
        } else {


@@ 32,9 27,9 @@ impl SpecificCommandHandler for SetCommand {
            return Err(WrongArguments);
        }

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

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

M src/main.rs => src/main.rs +7 -8
@@ 5,14 5,14 @@ mod strip;
mod map;
mod commands;

use embedded_hal::serial::{Read, Write};
use embedded_hal::serial::{Write};
use esp_backtrace as _;
use esp_println::println;
use hal::{clock::ClockControl, pulse_control::{ClockSource}, peripherals::Peripherals, prelude::*, timer::{TimerGroup, Timer, Timer0}, Rtc, IO, Delay, interrupt, PulseControl, Uart};
use hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, timer::{TimerGroup}, Rtc, IO, Delay, PulseControl, Uart};
use hal::uart::config::{Config, DataBits, Parity, StopBits};
use hal::uart::TxRxPins;
use nb::block;
use nb::Error::{WouldBlock, Other};
use nb::Error::{Other};
use smart_leds::{RGB8, SmartLedsWrite};
use crate::commands::all_command::AllCommand;
use crate::commands::command_handler::{CommandHandler};


@@ 20,7 20,6 @@ use crate::commands::command_handler;
use crate::commands::hello_world_command::HelloWorldCommand;
use crate::commands::reset_command::ResetCommand;
use crate::commands::set_command::SetCommand;
use crate::map::Map;
use crate::strip::StripTiming;

const LEDS_COUNT: usize = 72;


@@ 93,10 92,10 @@ fn main() -> ! {
    let mut rgb_data: [RGB8; 72] = [RGB8 { r: 0, g: 0, b: 0 }; 72];
    let mut map = map::Map::new(&map::INDEX_MAP, &mut rgb_data);

    let world_command = HelloWorldCommand::new();
    let set_command = SetCommand::new();
    let reset_command = ResetCommand::new();
    let all_command = AllCommand::new();
    let world_command = HelloWorldCommand::default();
    let set_command = SetCommand::default();
    let reset_command = ResetCommand::default();
    let all_command = AllCommand::default();
    let mut handler = CommandHandler::new(
        [
            ("HELLO_WORLD", &world_command),

Do not follow this link