~ruther/map-led-strip

d9a635d176b9adb3957a4b3cbed0ec50d339a5b0 — František Boháček 1 year, 9 months ago 3004833
feat: add allocator, Box command handlers
5 files changed, 58 insertions(+), 14 deletions(-)

M .cargo/config.toml
M Cargo.lock
M Cargo.toml
M src/commands/command_handler.rs
M src/main.rs
M .cargo/config.toml => .cargo/config.toml +1 -1
@@ 10,4 10,4 @@ rustflags = [
target = "xtensa-esp32-none-elf"

[unstable]
build-std = ["core"]
build-std = ["core", "alloc"]

M Cargo.lock => Cargo.lock +24 -0
@@ 142,6 142,16 @@ dependencies = [
]

[[package]]
name = "esp-alloc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83792eb7261a375bb838679fea2b45654b8f4a48da6bef10a96da5054aa81c7d"
dependencies = [
 "critical-section",
 "linked_list_allocator",
]

[[package]]
name = "esp-backtrace"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 269,6 279,18 @@ dependencies = [
]

[[package]]
name = "libm"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7012b1bbb0719e1097c47611d3898568c546d597c2e74d66f6087edd5233ff4"

[[package]]
name = "linked_list_allocator"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286"

[[package]]
name = "lock_api"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 283,10 305,12 @@ name = "map_test"
version = "0.1.0"
dependencies = [
 "embedded-hal",
 "esp-alloc",
 "esp-backtrace",
 "esp-println",
 "esp32-hal",
 "fugit",
 "libm",
 "nb 1.1.0",
 "smart-leds",
 "smart-leds-trait",

M Cargo.toml => Cargo.toml +1 -0
@@ 14,3 14,4 @@ nb = "1.1.0"
smart-leds = "0.3.0"
smart-leds-trait = "0.2.1"
fugit = "0.3.7"
esp-alloc = "0.3.0"

M src/commands/command_handler.rs => src/commands/command_handler.rs +5 -4
@@ 1,3 1,4 @@
use alloc::boxed::Box;
use embedded_hal::serial::{Read, Write};
use esp_println::println;
use nb::block;


@@ 15,7 16,7 @@ pub struct CommandHandler<'d, const BUFFER_SIZE: usize, const HANDLERS_COUNT: us
    buffer_position: usize,
    command_loaded: bool,
    buffer: [char; BUFFER_SIZE],
    handlers: [(&'d str, &'d dyn SpecificCommandHandler); HANDLERS_COUNT],
    handlers: [(&'d str, Box<dyn SpecificCommandHandler>); HANDLERS_COUNT],
    handling_special: u8
}



@@ 34,7 35,7 @@ pub enum CommandHandleError {
}

impl<'d, const BUFFER_SIZE: usize, const HANDLERS_COUNT: usize> CommandHandler<'d, BUFFER_SIZE, HANDLERS_COUNT> {
    pub fn new(handlers: [(&'d str, &'d dyn SpecificCommandHandler); HANDLERS_COUNT], buffer: [char; BUFFER_SIZE]) -> Self
    pub fn new(handlers: [(&'d str, Box<dyn SpecificCommandHandler>); HANDLERS_COUNT], buffer: [char; BUFFER_SIZE]) -> Self
    {
        Self {
            command_loaded: false,


@@ 156,7 157,7 @@ impl<'d, const BUFFER_SIZE: usize, const HANDLERS_COUNT: usize> CommandHandler<'
    fn handle_help(&self) -> Result<(), CommandHandleError>
    {
        println!("Available commands:\r");
        for (cmd, handler) in self.handlers {
        for (cmd, handler) in &self.handlers {
            println!("  {0} {1}\r", cmd, handler.help());
        }



@@ 187,7 188,7 @@ impl<'d, const BUFFER_SIZE: usize, const HANDLERS_COUNT: usize> CommandHandler<'
            return help_handled;
        }

        for (handler_command, handler) in self.handlers {
        for (handler_command, handler) in &self.handlers {
            if !first_argument.compare(handler_command) {
                continue;
            }

M src/main.rs => src/main.rs +27 -9
@@ 1,11 1,14 @@
#![no_std]
#![no_main]

extern crate alloc;

mod strip;
mod map;
mod commands;

use embedded_hal::serial::{Write};
use alloc::boxed::Box;
use esp_backtrace as _;
use esp_println::println;
use hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, timer::{TimerGroup}, Rtc, IO, Delay, PulseControl, Uart};


@@ 24,6 27,21 @@ use crate::strip::StripTiming;

const LEDS_COUNT: usize = 72;
const COMMAND_BUFFER: usize = 200;
#[global_allocator]
static ALLOCATOR: EspHeap = EspHeap::empty();

fn init_heap() {
    extern "C" {
        static mut _heap_start: u32;
        static mut _heap_end: u32;
    }

    unsafe {
        let heap_start = &_heap_start as *const _ as usize;
        let heap_end = &_heap_end as *const _ as usize;
        ALLOCATOR.init(heap_start as *mut u8, heap_end - heap_start);
    }
}

#[entry]
fn main() -> ! {


@@ 32,6 50,8 @@ fn main() -> ! {
    let mut system = peripherals.DPORT.split();
    let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

    init_heap();

    // Disable the RTC and TIMG watchdog timers
    let mut rtc = Rtc::new(peripherals.RTC_CNTL);
    let timer_group0 = TimerGroup::new(


@@ 92,18 112,16 @@ 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::default();
    let set_command = SetCommand::default();
    let reset_command = ResetCommand::default();
    let all_command = AllCommand::default();
    // Init commands
    let mut handler = CommandHandler::new(
        [
            ("HELLO_WORLD", &world_command),
            ("SET", &set_command),
            ("RESET", &reset_command),
            ("ALL", &all_command)
            ("HELLO_WORLD", Box::new(HelloWorldCommand::default())),
            ("SET", Box::new(SetCommand::default())),
            ("RESET", Box::new(ResetCommand::default())),
            ("ALL", Box::new(AllCommand::default())),
            ("SNAKE", Box::new(SnakeCommand::default()))
        ],
        ['\0'; COMMAND_BUFFER],
        ['\0'; constants::COMMAND_BUFFER],
    );

    block!(serial.write(b'>')).ok().unwrap();

Do not follow this link