Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
NeoBufferMethods.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 NeoBufferMethod
3 
4 Written by Michael C. Miller.
5 
6 I invest time and resources providing this open source code,
7 please support me by dontating (see https://github.com/Makuna)
8 
9 -------------------------------------------------------------------------
10 This file is part of the LUMITRONIX_iFlex_Workshop library.
11 
12 LumitronixIFlexBus is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as
14 published by the Free Software Foundation, either version 3 of
15 the License, or (at your option) any later version.
16 
17 LumitronixIFlexBus is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Lesser General Public License for more details.
21 
22 You should have received a copy of the GNU Lesser General Public
23 License along with LumitronixIFlex. If not, see
24 <http://www.gnu.org/licenses/>.
25 -------------------------------------------------------------------------*/
26 
27 #pragma once
28 
29 template<typename T_COLOR_FEATURE> class NeoBufferMethod
30 {
31 public:
32  NeoBufferMethod(uint16_t width, uint16_t height, PGM_VOID_P pixels = nullptr) :
33  _width(width),
34  _height(height)
35  {
36  _pixels = (uint8_t*)malloc(PixelsSize());
37  if (pixels)
38  {
39  // copy from progmem to initialize
40  T_COLOR_FEATURE::movePixelsInc_P(_pixels, pixels, PixelCount());
41  }
42  }
43 
45  {
46  free(_pixels);
47  _pixels = nullptr;
48  }
49 
51  {
53  }
54 
55  uint8_t* Pixels() const
56  {
57  return _pixels;
58  };
59 
60  size_t PixelsSize() const
61  {
62  return PixelSize() * PixelCount();
63  };
64 
65  size_t PixelSize() const
66  {
67  return T_COLOR_FEATURE::PixelSize;
68  };
69 
70  uint16_t PixelCount() const
71  {
72  return _width * _height;
73  };
74 
75  uint16_t Width() const
76  {
77  return _width;
78  };
79 
80  uint16_t Height() const
81  {
82  return _height;
83  };
84 
85  void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color)
86  {
87  if (indexPixel < PixelCount())
88  {
89  T_COLOR_FEATURE::applyPixelColor(_pixels, indexPixel, color);
90  }
91  };
92 
93  void SetPixelColor(int16_t x, int16_t y, typename T_COLOR_FEATURE::ColorObject color)
94  {
95  if (x < 0 || x >= _width || y < 0 || y >= _height)
96  {
97  return;
98  }
99 
100  uint16_t indexPixel = x + y * _width;
101  T_COLOR_FEATURE::applyPixelColor(_pixels, indexPixel, color);
102  };
103 
104  typename T_COLOR_FEATURE::ColorObject GetPixelColor(uint16_t indexPixel) const
105  {
106  if (indexPixel >= PixelCount())
107  {
108  // Pixel # is out of bounds, this will get converted to a
109  // color object type initialized to 0 (black)
110  return 0;
111  }
112 
113  return T_COLOR_FEATURE::retrievePixelColor(_pixels, indexPixel);
114  };
115 
116  typename T_COLOR_FEATURE::ColorObject GetPixelColor(int16_t x, int16_t y) const
117  {
118  if (x < 0 || x >= _width || y < 0 || y >= _height)
119  {
120  // Pixel # is out of bounds, this will get converted to a
121  // color object type initialized to 0 (black)
122  return 0;
123  }
124 
125  uint16_t indexPixel = x + y * _width;
126  return T_COLOR_FEATURE::retrievePixelColor(_pixels, indexPixel);
127  };
128 
129  void ClearTo(typename T_COLOR_FEATURE::ColorObject color)
130  {
131  uint8_t temp[T_COLOR_FEATURE::PixelSize];
132 
133  T_COLOR_FEATURE::applyPixelColor(temp, 0, color);
134 
135  T_COLOR_FEATURE::replicatePixel(_pixels, temp, PixelCount());
136  };
137 
138  void CopyPixels(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count)
139  {
140  T_COLOR_FEATURE::movePixelsInc(pPixelDest, pPixelSrc, count);
141  }
142 
143  typedef typename T_COLOR_FEATURE::ColorObject ColorObject;
144  typedef T_COLOR_FEATURE ColorFeature;
145 
146 private:
147  const uint16_t _width;
148  const uint16_t _height;
149  uint8_t* _pixels;
150 };
151 
152 
153 
#define PGM_VOID_P
Definition: NeoUtil.h:42
Definition: NeoBufferMethods.h:30
T_COLOR_FEATURE::ColorObject ColorObject
Definition: NeoBufferMethods.h:143
void ClearTo(typename T_COLOR_FEATURE::ColorObject color)
Definition: NeoBufferMethods.h:129
void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color)
Definition: NeoBufferMethods.h:85
size_t PixelsSize() const
Definition: NeoBufferMethods.h:60
void CopyPixels(uint8_t *pPixelDest, const uint8_t *pPixelSrc, uint16_t count)
Definition: NeoBufferMethods.h:138
T_COLOR_FEATURE ColorFeature
Definition: NeoBufferMethods.h:144
uint16_t Height() const
Definition: NeoBufferMethods.h:80
uint16_t PixelCount() const
Definition: NeoBufferMethods.h:70
~NeoBufferMethod()
Definition: NeoBufferMethods.h:44
void SetPixelColor(int16_t x, int16_t y, typename T_COLOR_FEATURE::ColorObject color)
Definition: NeoBufferMethods.h:93
T_COLOR_FEATURE::ColorObject GetPixelColor(int16_t x, int16_t y) const
Definition: NeoBufferMethods.h:116
NeoBufferMethod(uint16_t width, uint16_t height, PGM_VOID_P pixels=nullptr)
Definition: NeoBufferMethods.h:32
uint8_t * Pixels() const
Definition: NeoBufferMethods.h:55
T_COLOR_FEATURE::ColorObject GetPixelColor(uint16_t indexPixel) const
Definition: NeoBufferMethods.h:104
size_t PixelSize() const
Definition: NeoBufferMethods.h:65
uint16_t Width() const
Definition: NeoBufferMethods.h:75
Definition: NeoBufferContext.h:32