Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
NeoRingTopology.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 NeoRingTopology provides a mapping feature of a 2d polar cordinate to a
3 linear 1d cordinate.
4 It is used to map a series of concentric rings of LumitronixIFlexs to a index on
5 the LumitronixIFlexBus.
6 
7 Written by Michael C. Miller.
8 
9 I invest time and resources providing this open source code,
10 please support me by dontating (see https://github.com/Makuna)
11 
12 -------------------------------------------------------------------------
13 This file is part of the LUMITRONIX_iFlex_Workshop library.
14 
15 LumitronixIFlexBus is free software: you can redistribute it and/or modify
16 it under the terms of the GNU Lesser General Public License as
17 published by the Free Software Foundation, either version 3 of
18 the License, or (at your option) any later version.
19 
20 LumitronixIFlexBus is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU Lesser General Public License for more details.
24 
25 You should have received a copy of the GNU Lesser General Public
26 License along with LumitronixIFlex. If not, see
27 <http://www.gnu.org/licenses/>.
28 -------------------------------------------------------------------------*/
29 #pragma once
30 
31 // NeoRingTopology -
32 //
33 // T_LAYOUT - a user provided class that contains the following members with
34 // the Rings[] initialized with the starting index of each ring and
35 // one extra entry for the total count (example below).
36 // Other methods and members can exist to intialize a dynamic layout as needed.
37 //
38 // class RingsLayout
39 // {
40 // protected:
41 // const uint16_t Rings[] = { 0, 1, 7, 19, 35, 59, PixelCount };
42 //
43 // uint8_t _ringCount() const
44 // {
45 // return sizeof(Rings) / sizeof(Rings[0]);
46 // }
47 // };
48 //
49 
50 template <typename T_LAYOUT> class NeoRingTopology : public T_LAYOUT
51 {
52 public:
54  {
55  }
56 
57  uint16_t Map(uint8_t ring, uint16_t pixel) const
58  {
59  if (pixel >= getPixelCountAtRing(ring))
60  {
61  return 0; // invalid ring and/or pixel argument, always return a valid value, the first one
62  }
63 
64  return _map(ring, pixel);
65  }
66 
67  uint16_t MapProbe(uint8_t ring, uint16_t pixel) const
68  {
69  if (pixel >= getPixelCountAtRing(ring))
70  {
71  return getPixelCount(); // total count, out of bounds
72  }
73 
74  return _map(ring, pixel);
75  }
76 
77  uint16_t RingPixelShift(uint8_t ring, uint16_t pixel, int16_t shift)
78  {
79  int32_t ringPixel = pixel;
80  ringPixel += shift;
81 
82  if (ringPixel < 0)
83  {
84  ringPixel = 0;
85  }
86  else
87  {
88  uint16_t count = getPixelCountAtRing(ring);
89  if (ringPixel >= count)
90  {
91  ringPixel = count - 1;
92  }
93  }
94  return ringPixel;
95  }
96 
97  uint16_t RingPixelRotate(uint8_t ring, uint16_t pixel, int16_t rotate)
98  {
99  int32_t ringPixel = pixel;
100  ringPixel += rotate;
101  return ringPixel % getPixelCountAtRing(ring);
102  }
103 
104  uint8_t getCountOfRings() const
105  {
106  return T_LAYOUT::_ringCount() - 1; // minus one as the Rings includes the extra value
107  }
108 
109  uint16_t getPixelCountAtRing(uint8_t ring) const
110  {
111  if (ring >= getCountOfRings())
112  {
113  return 0; // invalid, no pixels
114  }
115 
116  return T_LAYOUT::Rings[ring + 1] - T_LAYOUT::Rings[ring]; // using the extra value for count calc
117  }
118 
119  uint16_t getPixelCount() const
120  {
121  return T_LAYOUT::Rings[T_LAYOUT::_ringCount() - 1]; // the last entry is the total count
122  }
123 
124 private:
125  uint16_t _map(uint8_t ring, uint16_t pixel) const
126  {
127  return T_LAYOUT::Rings[ring] + pixel;
128  }
129 
130 };
Definition: NeoRingTopology.h:51
uint16_t getPixelCountAtRing(uint8_t ring) const
Definition: NeoRingTopology.h:109
uint16_t MapProbe(uint8_t ring, uint16_t pixel) const
Definition: NeoRingTopology.h:67
uint8_t getCountOfRings() const
Definition: NeoRingTopology.h:104
uint16_t getPixelCount() const
Definition: NeoRingTopology.h:119
NeoRingTopology()
Definition: NeoRingTopology.h:53
uint16_t RingPixelRotate(uint8_t ring, uint16_t pixel, int16_t rotate)
Definition: NeoRingTopology.h:97
uint16_t RingPixelShift(uint8_t ring, uint16_t pixel, int16_t shift)
Definition: NeoRingTopology.h:77
uint16_t Map(uint8_t ring, uint16_t pixel) const
Definition: NeoRingTopology.h:57