~ruther/map-led-strip

d2155d90f7debabfa4a574dcf64400a82c27dba7 — František Boháček 1 year, 9 months ago 82dcc6f
feat: add Czech map representation with RGB8
2 files changed, 146 insertions(+), 0 deletions(-)

M src/main.rs
A src/map.rs
M src/main.rs => src/main.rs +4 -0
@@ 2,6 2,8 @@
#![no_main]

mod strip;
mod map;

use embedded_hal::serial::{Read, Write};
use esp_backtrace as _;
use esp_println::println;


@@ 11,6 13,7 @@ use hal::uart::TxRxPins;
use nb::block;
use nb::Error::{WouldBlock, Other};
use smart_leds::{RGB8, SmartLedsWrite};
use crate::map::Map;
use crate::strip::StripTiming;
const LEDS_COUNT: usize = 72;
const COMMAND_BUFFER: usize = 200;


@@ 80,6 83,7 @@ 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);
    loop {

    }

A src/map.rs => src/map.rs +142 -0
@@ 0,0 1,142 @@
use core::slice::{Iter, IterMut};
use smart_leds::RGB8;

pub const INDEX_MAP : [&str; 72] = [
    "DECIN",
    "LIBEREC",
    "JABLONEC_NAD_NISOU",
    "ÚSTI_NAD_LABEM",
    "CESKA_LIPA",
    "SEMILY",
    "TEPLICE",
    "TRUTNOV",
    "LITOMERICE",
    "MOST",
    "CHOMUTOV",
    "JICIN",
    "NACHOD",
    "MLADA_BOLESLAV",
    "MELNIK",
    "LOUNY",
    "KARLOVY_VARY",
    "JESENIK",
    "HRADEC_KRALOVE",
    "SOKOLOV",
    "NYMBURK",
    "RYCHNOV_NAD_KNEZNOU",
    "KLADNO",
    "RAKOVNIK",
    "CHEB",
    "BRUNTAL",
    "PRAHA",
    "PARDUBICE",
    "KOLIN",
    "ÚSTI_NAD_ORLICI",
    "OPAVA",
    "SUMPERK",
    "BEROUN",
    "KUTNA_HORA",
    "CHRUDIM",
    "KARVINA",
    "OSTRAVA-MESTO",
    "TACHOV",
    "SVITAVY",
    "BENESOV",
    "PLZEN-MESTO",
    "ROKYCANY",
    "FRYDEK-MISTEK",
    "PRIBRAM",
    "NOVY_JICIN",
    "OLOMOUC",
    "HAVLICKŮV_BROD",
    "ZĎAR_NAD_SAZAVOU",
    "PREROV",
    "PROSTEJOV",
    "DOMAZLICE",
    "PELHRIMOV",
    "TABOR",
    "JIHLAVA",
    "KLATOVY",
    "BLANSKO",
    "VSETIN",
    "KROMERIZ",
    "PISEK",
    "VYSKOV",
    "STRAKONICE",
    "ZLIN",
    "TREBIC",
    "BRNO",
    "JINDRICHUV_HRADEC",
    "UHERSKÉ_HRADISTE",
    "PRACHATICE",
    "CESKE_BUDEJOVICE",
    "HODONIN",
    "ZNOJMO",
    "CESKY_KRUMLOV",
    "BRECLAV",
];

pub struct Map<'d> {
    index_map: &'d [&'d str],
    data: &'d mut [RGB8]
}

pub enum Error {
    NotFound
}

impl<'d> Map<'d> {
    pub fn new(index_map: &'d [&'d str], data: &'d mut [RGB8]) -> Self {
        Map {
            index_map,
            data
        }
    }

    pub fn get_index_by_name(&self, name: &[char]) -> Result<usize, Error> {
        for (i, current) in self.index_map.iter().enumerate() {
            if current.len() != name.len() {
                continue;
            }

            let mut matches = true;
            for (j, c) in current.chars().enumerate() {
                if name[j] != c {
                    matches = false;
                    break;
                }
            }

            if matches {
                return Ok(i);
            }
        }

        return Err(Error::NotFound);
    }

    pub fn set_rgb(&mut self, index: usize, r: Option<u8>, g: Option<u8>, b: Option<u8>) -> Result<(), Error> {
        let original = self.data[index];
        if self.data.len() <= index {
            return Err(Error::NotFound)
        }

        self.data[index] = RGB8 {
            r: r.unwrap_or(original.r),
            g: g.unwrap_or(original.g),
            b: b.unwrap_or(original.b),
        };

        Ok(())
    }

    pub fn set_rgb_by_name(&mut self, name: &[char], r: Option<u8>, g: Option<u8>, b: Option<u8>) -> Result<(), Error> {
        let index = self.get_index_by_name(name)?;
        self.set_rgb(index, r, g, b)
    }

    pub fn get_map(&self) -> Iter<RGB8> {
        return self.data.iter();
    }
    pub fn get_map_mut(&mut self) -> IterMut<RGB8> { return self.data.iter_mut(); }
}
\ No newline at end of file

Do not follow this link