~ruther/map-led-strip

ref: 8ae60ccfb8f93b1bc11585503f27fc2a501e6ed9 map-led-strip/src/map.rs -rw-r--r-- 2.9 KiB
8ae60ccf — František Boháček feat: add command handling support 1 year, 10 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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(); }
}
Do not follow this link