Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
NeoMosaic.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 NeoMosaic provides a mapping feature of a 2d cordinate to linear 1d cordinate
3 It is used to map tiles of matricies of LumitronixIFlexs to a index on the LumitronixIFlexBus
4 where the the matricies use a set of prefered topology and the tiles of
5 those matricies use the RowMajorAlternating layout
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 
32 //-----------------------------------------------------------------------------
33 // class NeoMosaic
34 // Complex Tile layout class that reduces distance of the interconnects between
35 // the tiles by using different rotations of the layout at specific locations
36 //
37 // T_LAYOUT = the layout used for matrix panel (rotation is ignored)
38 // One of the following classes and their rotated variants
39 // RowMajorLayout
40 // ColumnMajorLayout
41 // RowMajorAlternatingLayout
42 // ColumnMajorAlternatingLayout
43 //
44 //
45 // NOTE: The tiles in the mosaic are always laid out using RowMajorAlternating
46 //
47 //-----------------------------------------------------------------------------
48 
49 template <typename T_LAYOUT> class NeoMosaic
50 {
51 public:
52  NeoMosaic(uint16_t topoWidth, uint16_t topoHeight,
53  uint16_t mosaicWidth, uint16_t mosaicHeight) :
54  _topoWidth(topoWidth),
55  _topoHeight(topoHeight),
56  _mosaicWidth(mosaicWidth),
57  _mosaicHeight(mosaicHeight)
58  {
59  }
60 
61  uint16_t Map(int16_t x, int16_t y) const
62  {
63  uint16_t totalWidth = getWidth();
64  uint16_t totalHeight = getHeight();
65 
66  if (x >= static_cast<int16_t>(totalWidth))
67  {
68  x = totalWidth - 1;
69  }
70  else if (x < 0)
71  {
72  x = 0;
73  }
74 
75  if (y >= static_cast<int16_t>(totalHeight))
76  {
77  y = totalHeight - 1;
78  }
79  else if (y < 0)
80  {
81  y = 0;
82  }
83 
84  uint16_t localIndex;
85  uint16_t tileOffset;
86 
87  calculate(x, y, &localIndex, &tileOffset);
88 
89  return localIndex + tileOffset;
90  }
91 
92  uint16_t MapProbe(int16_t x, int16_t y) const
93  {
94  uint16_t totalWidth = getWidth();
95  uint16_t totalHeight = getHeight();
96 
97  if (x < 0 || x >= totalWidth || y < 0 || y >= totalHeight)
98  {
99  return totalWidth * totalHeight; // count, out of bounds
100  }
101 
102  uint16_t localIndex;
103  uint16_t tileOffset;
104 
105  calculate(x, y, &localIndex, &tileOffset);
106 
107  return localIndex + tileOffset;
108  }
109 
110  NeoTopologyHint TopologyHint(int16_t x, int16_t y) const
111  {
112  uint16_t totalWidth = getWidth();
113  uint16_t totalHeight = getHeight();
114 
115  if (x < 0 || x >= static_cast<int16_t>(totalWidth) ||
116  y < 0 || y >= static_cast<int16_t>(totalHeight))
117  {
119  }
120 
121  uint16_t localIndex;
122  uint16_t tileOffset;
123  NeoTopologyHint result;
124 
125  calculate(x, y, &localIndex, &tileOffset);
126 
127  if (localIndex == 0)
128  {
130  }
131  else if (localIndex == (_topoWidth * _topoHeight - 1))
132  {
134  }
135  else
136  {
137  result = NeoTopologyHint_InPanel;
138  }
139 
140  return result;
141  }
142 
143  uint16_t getWidth() const
144  {
145  return _topoWidth * _mosaicWidth;
146  }
147 
148  uint16_t getHeight() const
149  {
150  return _topoHeight * _mosaicHeight;
151  }
152 
153 private:
154  const uint16_t _topoWidth;
155  const uint16_t _topoHeight;
156  const uint16_t _mosaicWidth;
157  const uint16_t _mosaicHeight;
158 
159  void calculate(uint16_t x, uint16_t y, uint16_t* pLocalIndex, uint16_t* pTileOffset) const
160  {
161  uint16_t tileX = x / _topoWidth;
162  uint16_t topoX = x % _topoWidth;
163 
164  uint16_t tileY = y / _topoHeight;
165  uint16_t topoY = y % _topoHeight;
166 
167  *pTileOffset = RowMajorAlternatingLayout::Map(_mosaicWidth,
168  _mosaicHeight,
169  tileX,
170  tileY) * _topoWidth * _topoHeight;
171 
172  if (tileX & 0x0001)
173  {
174  // odd columns
175  if (tileY & 0x0001)
176  {
177  *pLocalIndex = T_LAYOUT::OddRowOddColumnLayout::Map(_topoWidth, _topoHeight, topoX, topoY);
178  }
179  else
180  {
181  *pLocalIndex = T_LAYOUT::EvenRowOddColumnLayout::Map(_topoWidth, _topoHeight, topoX, topoY);
182  }
183  }
184  else
185  {
186  // even columns
187  if (tileY & 0x0001)
188  {
189  *pLocalIndex = T_LAYOUT::OddRowEvenColumnLayout::Map(_topoWidth, _topoHeight, topoX, topoY);
190  }
191  else
192  {
193  *pLocalIndex = T_LAYOUT::EvenRowEvenColumnLayout::Map(_topoWidth, _topoHeight, topoX, topoY);
194  }
195  }
196  }
197 };
NeoTopologyHint
Definition: NeoTopology.h:30
@ NeoTopologyHint_LastOnPanel
Definition: NeoTopology.h:33
@ NeoTopologyHint_OutOfBounds
Definition: NeoTopology.h:34
@ NeoTopologyHint_InPanel
Definition: NeoTopology.h:32
@ NeoTopologyHint_FirstOnPanel
Definition: NeoTopology.h:31
Definition: NeoMosaic.h:50
uint16_t Map(int16_t x, int16_t y) const
Definition: NeoMosaic.h:61
NeoMosaic(uint16_t topoWidth, uint16_t topoHeight, uint16_t mosaicWidth, uint16_t mosaicHeight)
Definition: NeoMosaic.h:52
uint16_t getWidth() const
Definition: NeoMosaic.h:143
NeoTopologyHint TopologyHint(int16_t x, int16_t y) const
Definition: NeoMosaic.h:110
uint16_t MapProbe(int16_t x, int16_t y) const
Definition: NeoMosaic.h:92
uint16_t getHeight() const
Definition: NeoMosaic.h:148
static uint16_t Map(uint16_t width, uint16_t, uint16_t x, uint16_t y)
Definition: RowMajorAlternatingLayout.h:53