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