Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
NeoTiles.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 NeoTiles 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 one topology and the tiles of those matricies can
5 use another
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 // class NeoTiles
33 // Simple template Tile layout class
34 // T_MATRIX_LAYOUT = the layout used on the pixel matrix panel (a tile)
35 // T_TILE_LAYOUT = the layout used for the tiles.
36 // one of the following classes and their rotated variants
37 // RowMajorLayout
38 // ColumnMajorLayout
39 // RowMajorAlternatingLayout
40 // ColumnMajorAlternatingLayout
41 //-----------------------------------------------------------------------------
42 template <typename T_MATRIX_LAYOUT, typename T_TILE_LAYOUT> class NeoTiles
43 {
44 public:
45  NeoTiles(uint16_t topoWidth, uint16_t topoHeight,
46  uint16_t tilesWidth, uint16_t tilesHeight) :
47  _topo(topoWidth, topoHeight),
48  _width(tilesWidth),
49  _height(tilesHeight)
50  {
51  }
52 
53  uint16_t Map(int16_t x, int16_t y) const
54  {
55  uint16_t totalWidth = getWidth();
56  uint16_t totalHeight = getHeight();
57 
58  if (x >= static_cast<int16_t>(totalWidth))
59  {
60  x = totalWidth - 1;
61  }
62  else if (x < 0)
63  {
64  x = 0;
65  }
66 
67  if (y >= static_cast<int16_t>(totalHeight))
68  {
69  y = totalHeight - 1;
70  }
71  else if (y < 0)
72  {
73  y = 0;
74  }
75 
76  uint16_t localIndex;
77  uint16_t tileOffset;
78 
79  calculate(x, y, &localIndex, &tileOffset);
80 
81  return localIndex + tileOffset;
82  }
83 
84  uint16_t MapProbe(int16_t x, int16_t y) const
85  {
86  uint16_t totalWidth = getWidth();
87  uint16_t totalHeight = getHeight();
88 
89  if (x < 0 || x >= totalWidth || y < 0 || y >= totalHeight)
90  {
91  return totalWidth * totalHeight; // count, out of bounds
92  }
93 
94  uint16_t localIndex;
95  uint16_t tileOffset;
96 
97  calculate(x, y, &localIndex, &tileOffset);
98 
99  return localIndex + tileOffset;
100  }
101 
102  NeoTopologyHint TopologyHint(int16_t x, int16_t y) const
103  {
104  uint16_t totalWidth = getWidth();
105  uint16_t totalHeight = getHeight();
106 
107  if (x < 0 || x >= static_cast<int16_t>(totalWidth) ||
108  y < 0 || y >= static_cast<int16_t>(totalHeight))
109  {
111  }
112 
113  uint16_t localIndex;
114  uint16_t tileOffset;
115  NeoTopologyHint result;
116 
117  calculate(x, y, &localIndex, &tileOffset);
118 
119  if (localIndex == 0)
120  {
122  }
123  else if (localIndex == (_topo.getWidth() * _topo.getHeight() - 1))
124  {
126  }
127  else
128  {
129  result = NeoTopologyHint_InPanel;
130  }
131 
132  return result;
133  }
134 
135  uint16_t getWidth() const
136  {
137  return _width * _topo.getWidth();
138  }
139 
140  uint16_t getHeight() const
141  {
142  return _height * _topo.getHeight();
143  }
144 
145 private:
146  const NeoTopology<T_MATRIX_LAYOUT> _topo;
147  const uint16_t _width;
148  const uint16_t _height;
149 
150  void calculate(uint16_t x, uint16_t y, uint16_t* pLocalIndex, uint16_t* pTileOffset) const
151  {
152  uint16_t tileX = x / _topo.getWidth();
153  uint16_t topoX = x % _topo.getWidth();
154 
155  uint16_t tileY = y / _topo.getHeight();
156  uint16_t topoY = y % _topo.getHeight();
157 
158  *pTileOffset = T_TILE_LAYOUT::Map(_width, _height, tileX, tileY) * _topo.getWidth() * _topo.getHeight();
159  *pLocalIndex = _topo.Map(topoX, topoY);
160  }
161 };
162 
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: NeoTiles.h:43
NeoTopologyHint TopologyHint(int16_t x, int16_t y) const
Definition: NeoTiles.h:102
uint16_t getWidth() const
Definition: NeoTiles.h:135
NeoTiles(uint16_t topoWidth, uint16_t topoHeight, uint16_t tilesWidth, uint16_t tilesHeight)
Definition: NeoTiles.h:45
uint16_t getHeight() const
Definition: NeoTiles.h:140
uint16_t Map(int16_t x, int16_t y) const
Definition: NeoTiles.h:53
uint16_t MapProbe(int16_t x, int16_t y) const
Definition: NeoTiles.h:84
uint16_t Map(int16_t x, int16_t y) const
Definition: NeoTopology.h:55
uint16_t getWidth() const
Definition: NeoTopology.h:85
uint16_t getHeight() const
Definition: NeoTopology.h:90