Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
NeoDib.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 NeoDib - Device Independant Bitmap, interal data stored in RGB/RGBW format
3 rather than the ColorFeature format
4 
5 Written by Michael C. Miller.
6 
7 I invest time and resources providing this open source code,
8 please support me by dontating (see https://github.com/Makuna)
9 
10 -------------------------------------------------------------------------
11 This file is part of the LUMITRONIX_iFlex_Workshop library.
12 
13 LumitronixIFlexBus is free software: you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as
15 published by the Free Software Foundation, either version 3 of
16 the License, or (at your option) any later version.
17 
18 LumitronixIFlexBus is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU Lesser General Public License for more details.
22 
23 You should have received a copy of the GNU Lesser General Public
24 License along with LumitronixIFlex. If not, see
25 <http://www.gnu.org/licenses/>.
26 -------------------------------------------------------------------------*/
27 #pragma once
28 
29 // T_COLOR_OBJECT - one of the color objects
30 // RgbColor
31 // RgbwColor
32 // Rgb16Color
33 // Rgb48Color
34 // Rgbw64Color
35 // SevenSegDigit
36 //
37 template<typename T_COLOR_OBJECT> class NeoDib
38 {
39 public:
40  NeoDib(uint16_t countPixels) :
41  _countPixels(countPixels),
42  _state(0)
43  {
44  _pixels = (T_COLOR_OBJECT*)malloc(PixelsSize());
45  ResetDirty();
46  }
47 
49  {
50  free((uint8_t*)_pixels);
51  }
52 
53  NeoDib& operator=(const NeoDib& other)
54  {
55  // check for self-assignment
56  if (&other == this)
57  {
58  return *this;
59  }
60 
61  uint16_t copyCount = other.PixelCount() < PixelCount() ? other.PixelCount() : PixelCount();
62 
63  for (uint16_t pixel = 0; pixel < copyCount; pixel++)
64  {
65  _pixels[pixel] = other.Pixels()[pixel];
66  }
67 
68  Dirty();
69  return *this;
70  }
71 
72  T_COLOR_OBJECT* Pixels() const
73  {
74  return _pixels;
75  };
76 
77  uint16_t PixelCount() const
78  {
79  return _countPixels;
80  };
81 
82  size_t PixelsSize() const
83  {
84  return _countPixels * PixelSize();
85  };
86 
87  size_t PixelSize() const
88  {
89  return sizeof(T_COLOR_OBJECT);
90  };
91 
93  uint16_t indexPixel,
94  T_COLOR_OBJECT color)
95  {
96  if (indexPixel < PixelCount())
97  {
98  _pixels[indexPixel] = color;
99  Dirty();
100  }
101  };
102 
103  T_COLOR_OBJECT GetPixelColor(
104  uint16_t indexPixel) const
105  {
106  if (indexPixel >= PixelCount())
107  {
108  return 0;
109  }
110  return _pixels[indexPixel];
111  };
112 
113  void ClearTo(T_COLOR_OBJECT color)
114  {
115  for (uint16_t pixel = 0; pixel < PixelCount(); pixel++)
116  {
117  _pixels[pixel] = color;
118  }
119  Dirty();
120  };
121 
122  template <typename T_COLOR_FEATURE, typename T_SHADER>
123  void Render(NeoBufferContext<T_COLOR_FEATURE> destBuffer, T_SHADER& shader, uint16_t destIndexPixel = 0)
124  {
125  if (IsDirty() || shader.IsDirty())
126  {
127  uint16_t countPixels = destBuffer.PixelCount();
128 
129  if (countPixels > _countPixels)
130  {
131  countPixels = _countPixels;
132  }
133 
134  for (uint16_t indexPixel = 0; indexPixel < countPixels; indexPixel++)
135  {
136  T_COLOR_OBJECT color = shader.Apply(indexPixel, _pixels[indexPixel]);
137  T_COLOR_FEATURE::applyPixelColor(destBuffer.Pixels, destIndexPixel + indexPixel, color);
138  }
139 
140  shader.ResetDirty();
141  ResetDirty();
142  }
143  }
144 
145  bool IsDirty() const
146  {
147  return (_state & NEO_DIRTY);
148  };
149 
150  void Dirty()
151  {
152  _state |= NEO_DIRTY;
153  };
154 
155  void ResetDirty()
156  {
157  _state &= ~NEO_DIRTY;
158  };
159 
160 private:
161  const uint16_t _countPixels; // Number of RGB LEDs in strip
162  T_COLOR_OBJECT* _pixels;
163  uint8_t _state; // internal state
164 };
const uint8_t NEO_DIRTY
Definition: LumitronixIFlexBus.h:32
Definition: NeoDib.h:38
void ResetDirty()
Definition: NeoDib.h:155
size_t PixelSize() const
Definition: NeoDib.h:87
T_COLOR_OBJECT * Pixels() const
Definition: NeoDib.h:72
void ClearTo(T_COLOR_OBJECT color)
Definition: NeoDib.h:113
T_COLOR_OBJECT GetPixelColor(uint16_t indexPixel) const
Definition: NeoDib.h:103
size_t PixelsSize() const
Definition: NeoDib.h:82
void Render(NeoBufferContext< T_COLOR_FEATURE > destBuffer, T_SHADER &shader, uint16_t destIndexPixel=0)
Definition: NeoDib.h:123
NeoDib(uint16_t countPixels)
Definition: NeoDib.h:40
bool IsDirty() const
Definition: NeoDib.h:145
void Dirty()
Definition: NeoDib.h:150
void SetPixelColor(uint16_t indexPixel, T_COLOR_OBJECT color)
Definition: NeoDib.h:92
uint16_t PixelCount() const
Definition: NeoDib.h:77
~NeoDib()
Definition: NeoDib.h:48
NeoDib & operator=(const NeoDib &other)
Definition: NeoDib.h:53
Definition: NeoBufferContext.h:32
uint8_t * Pixels
Definition: NeoBufferContext.h:43
uint16_t PixelCount() const
Definition: NeoBufferContext.h:40