ESP_IOT v2.5
IOT ESP Coding
LED_DisPlay.cpp
Go to the documentation of this file.
1#include "../../Defines.h"
2#ifdef USE_FAST_LED
3#include "LED_DisPlay.h"
4
5#define DATA_PIN 27
6
7/**
8 * Code is from M5Atom.h object, but refactored to be it's own object
9 */
11{
12}
13
15{
16}
17
18void LED_DisPlay::begin(uint8_t LEDNumber)
19{
20 FastLED.addLeds<WS2812, DATA_PIN, GRB>(_ledbuff, LEDNumber);
21 _xSemaphore = xSemaphoreCreateMutex();
22 _numberled = LEDNumber;
23
24 registerPinUse_mainModule(DATA_PIN, "DATA_PIN", "LED_DisPlay::begin", false);
25
26}
27
28void LED_DisPlay::run(void *data)
29{
30 data = nullptr;
31
32 for (int num = 0; num < NUM_LEDS; num++)
33 {
34 _ledbuff[num] = 0x000000;
35 }
36 FastLED.show();
37 FastLED.setBrightness(20);
38
39 while (1)
40 {
41 xSemaphoreTake(_xSemaphore, portMAX_DELAY);
42 if (_mode == kAnimation_run)
43 {
44 _displaybuff(_am_buffptr, _count_x, _count_y);
45 FastLED.show();
46 if ((_am_mode & kMoveRight) || (_am_mode & kMoveLeft))
47 {
48 if (_am_mode & kMoveRight)
49 {
50 _count_x++;
51 }
52 else
53 {
54 _count_x--;
55 }
56 }
57 if ((_am_mode & kMoveTop) || (_am_mode & kMoveBottom))
58 {
59 if (_am_mode & kMoveTop)
60 {
61 _count_y--;
62 }
63 else
64 {
65 _count_y++;
66 }
67 }
68 if ((_am_count != -1) && (_am_count != 0))
69 {
70 _am_count--;
71 if (_am_count == 0)
72 {
73 _mode = kAnimation_stop;
74 }
75 }
76 delay(_am_speed);
77 }
78 else if (_mode == kAnimation_frush)
79 {
80 _mode = kAnimation_stop;
81 FastLED.show();
82 FastLED.setBrightness(20);
83 }
84 xSemaphoreGive(_xSemaphore);
85 delay(10);
86 }
87}
88
89void LED_DisPlay::_displaybuff(uint8_t *buffptr, int32_t offsetx, int32_t offsety)
90{
91 uint16_t xsize = 0, ysize = 0;
92 xsize = _xColumns;
93 ysize = _yRows;
94
95 offsetx = offsetx % xsize;
96 offsety = offsety % ysize;
97
98 int16_t setdatax = (offsetx < 0) ? (-offsetx) : (xsize - offsetx);
99 int16_t setdatay = (offsety < 0) ? (-offsety) : (ysize - offsety);
100 for (int x = 0; x < 5; x++)
101 {
102 for (int y = 0; y < 5; y++)
103 {
104 _ledbuff[x + y * 5].raw[1] = buffptr[2 + ((setdatax + x) % xsize + ((setdatay + y) % ysize) * xsize) * 3 + 0];
105 _ledbuff[x + y * 5].raw[0] = buffptr[2 + ((setdatax + x) % xsize + ((setdatay + y) % ysize) * xsize) * 3 + 1];
106 _ledbuff[x + y * 5].raw[2] = buffptr[2 + ((setdatax + x) % xsize + ((setdatay + y) % ysize) * xsize) * 3 + 2];
107 }
108 }
109 FastLED.setBrightness(Brightness);
110}
111
112void LED_DisPlay::animation(uint8_t *buffptr, uint8_t amspeed, uint8_t ammode, int64_t amcount)
113{
114 xSemaphoreTake(_xSemaphore, portMAX_DELAY);
115 if (_mode == kAnimation_run)
116 {
117 _mode = kAnimation_stop;
118 }
119 _am_buffptr = buffptr;
120 _am_speed = amspeed;
121 _am_mode = ammode;
122 _am_count = amcount;
123 _count_x = _count_y = 0;
124 _mode = kAnimation_run;
125 xSemaphoreGive(_xSemaphore);
126}
127
128void LED_DisPlay::displaybuff(uint8_t *buffptr, int32_t offsetx, int32_t offsety)
129{
130 uint16_t xsize = 0, ysize = 0;
131 xsize = _xColumns;
132 ysize = _yRows;
133
134 offsetx = offsetx % xsize;
135 offsety = offsety % ysize;
136
137 int16_t setdatax = (offsetx < 0) ? (-offsetx) : (xsize - offsetx);
138 int16_t setdatay = (offsety < 0) ? (-offsety) : (ysize - offsety);
139 xSemaphoreTake(_xSemaphore, portMAX_DELAY);
140
141 for (int x = 0; x < 5; x++)
142 {
143 for (int y = 0; y < 5; y++)
144 {
145 _ledbuff[x + y * 5].raw[1] = buffptr[2 + ((setdatax + x) % xsize + ((setdatay + y) % ysize) * xsize) * 3 + 0];
146 _ledbuff[x + y * 5].raw[0] = buffptr[2 + ((setdatax + x) % xsize + ((setdatay + y) % ysize) * xsize) * 3 + 1];
147 _ledbuff[x + y * 5].raw[2] = buffptr[2 + ((setdatax + x) % xsize + ((setdatay + y) % ysize) * xsize) * 3 + 2];
148 }
149 }
150
151 _mode = kAnimation_frush;
152
153 xSemaphoreGive(_xSemaphore);
154 FastLED.setBrightness(Brightness);
155}
156
157void LED_DisPlay::setBrightness(uint8_t brightness)
158{
159 xSemaphoreTake(_xSemaphore, portMAX_DELAY);
160 brightness = (brightness > 100) ? 100 : brightness;
161 brightness = (40 * brightness / 100);
162 Brightness = brightness;
163 FastLED.setBrightness(Brightness);
164 xSemaphoreGive(_xSemaphore);
165}
166
167void LED_DisPlay::drawpix(uint8_t xpos, uint8_t ypos, CRGB Color)
168{
169 if ((xpos >= 5) || (ypos >= 5))
170 {
171 return;
172 }
173 xSemaphoreTake(_xSemaphore, portMAX_DELAY);
174 _ledbuff[xpos + ypos * 5] = Color;
175 _mode = kAnimation_frush;
176 xSemaphoreGive(_xSemaphore);
177}
178
179void LED_DisPlay::drawpix(uint8_t Number, CRGB Color)
180{
181 if (Number >= NUM_LEDS)
182 {
183 return;
184 }
185 xSemaphoreTake(_xSemaphore, portMAX_DELAY);
186 _ledbuff[Number] = Color;
187 _mode = kAnimation_frush;
188 xSemaphoreGive(_xSemaphore);
189}
190
191void LED_DisPlay::fillpix(CRGB Color)
192{
193 xSemaphoreTake(_xSemaphore, portMAX_DELAY);
194 for (int i = 0; i < NUM_LEDS; i++)
195 {
196 _ledbuff[i] = Color;
197 }
198 _mode = kAnimation_frush;
199 xSemaphoreGive(_xSemaphore);
200}
201
203{
204 xSemaphoreTake(_xSemaphore, portMAX_DELAY);
205 for (int8_t i = 0; i < NUM_LEDS; i++)
206 {
207 _ledbuff[i] = 0;
208 }
209 _mode = kAnimation_frush;
210 xSemaphoreGive(_xSemaphore);
211}
212
214{
215 if (_mode == kAnimation_run)
216 {
217 return true;
218 }
219 else if (_mode == kAnimation_stop)
220 {
221 return false;
222 }
223 return 0;
224}
225
226void LED_DisPlay::setWidthHeight(uint16_t xColumns, uint16_t yRows)
227{
228 _xColumns = xColumns;
229 _yRows = yRows;
230}
231
232#endif //USE_FAST_LED
#define DATA_PIN
Definition: LED_DisPlay.cpp:5
#define NUM_LEDS
Definition: LED_DisPlay.h:14
void registerPinUse_mainModule(long pin, String pinName, String moduleName, boolean isI2C)
@ kAnimation_frush
Definition: LED_DisPlay.h:40
void begin(uint8_t LEDNumber=NUM_LEDS)
Definition: LED_DisPlay.cpp:18
void displaybuff(uint8_t *buffptr, int32_t offsetx=0, int32_t offsety=0)
uint8_t Brightness
Definition: LED_DisPlay.h:50
void setBrightness(uint8_t brightness)
boolean animationrunning()
void fillpix(CRGB Color)
void run(void *data)
Definition: LED_DisPlay.cpp:28
void drawpix(uint8_t xpos, uint8_t ypos, CRGB Color)
void animation(uint8_t *buffptr, uint8_t amspeed, uint8_t ammode, int64_t amcount=-1)
void setWidthHeight(uint16_t xColumns, uint16_t yRows)
void delay(int ms)
Definition: Task.cpp:58