-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixLed.h
326 lines (254 loc) · 6.31 KB
/
MatrixLed.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#pragma once
#include <inttypes.h>
#include <string.h>
#include "PXLParser.h"
template <uint8_t _max_layers, uint8_t _width, uint8_t _height>
class MatrixLed
{
typedef struct
{
bool active; // только для active = true изображение будет отображаться
bool is_set; // is_set = true только для слоёв, которым присвоены файлы
PXLParser<_width, _height> parser;
} layers_t;
typedef struct __attribute__((__packed__))
{
uint8_t G;
uint8_t R;
uint8_t B;
} color_t;
public:
MatrixLed(uint16_t fps) : _fps(fps), _brightness(64)
{
// CWE-762:Using 'memset' on class that contains a 'std::function'.
//memset(_layers, 0x00, sizeof(_layers));
for(layers_t &layer : _layers)
{
layer.active = false;
layer.is_set = false;
layer.parser.ReInit();
}
_ClearBuffer();
}
bool RegLayer(const char *filename, uint8_t idx)
{
if (idx < _max_layers)
{
if(_layers[idx].is_set == true)
{
_layers[idx].parser.CloseFile();
}
_layers[idx].active = false;
_layers[idx].is_set = true;
return _layers[idx].parser.OpenFile(filename);
}
return false;
}
void ShowLayer(uint8_t idx)
{
if (idx < _max_layers && _layers[idx].is_set == true)
{
_layers[idx].active = true;
}
return;
}
void HideLayer(uint8_t idx)
{
if (idx < _max_layers && _layers[idx].is_set == true)
{
_layers[idx].active = false;
_layers[idx].parser.ReInit();
}
return;
}
void SetBrightness(uint8_t brightness)
{
_brightness = brightness;
return;
}
/*
Рисует пиксель в ручном режиме.
uint8_t x - Координата по оси X;
uint8_t y - Координата по оси Y;
uint32_t color - Цвет пикселя в формате 0xRRGGBBAA
*/
void DrawPixel(uint8_t x, uint8_t y, uint32_t color)
{
if(_manual_mode == false) return;
if(x >= _width || y >= _height) return;
uint16_t idx = (x * _height) + ((x % 2 == 0) ? y : (_height - y - 1));
return DrawPixel(idx, color);
}
/*
Рисует пиксель в ручном режиме.
uint8_t x - Координата по оси X;
uint8_t y - Координата по оси Y;
uint32_t color - Цвет пикселя в формате 0xAABBRRGG;
*/
void DrawPixel(uint16_t idx, uint32_t color)
{
if(_manual_mode == false) return;
if(idx >= (_width * _height)) return;
if((color >> 24) < 255) return;
memcpy(_frame_buff + idx, &color, 3);
return;
}
/*
Запускает ручной вывод картинки, построенной из пикселей.
*/
void ManualDraw()
{
_BrightnessCorrection();
_frame_buff_ready = true;
return;
}
/*
Устанавлиает режим работы: Ручное управление пикселями, или Автоматический парсер PXL файлов.
*/
void ManualMode(bool mode)
{
_manual_mode = mode;
return;
}
#ifndef MATRIX_STEP_BY_STEP
void Processing(uint32_t time)
{
if(_manual_mode == true) return;
if(_frame_buff_ready == true) return;
if(time - _last_screen_time <= _fps) return;
_last_screen_time = time;
for (layers_t &layer : _layers)
{
if (!layer.active)
continue;
layer.parser.GetAutoFrame(time, [&](pixel_data_t &pixel_data)
{
if (pixel_data.color4 < 255)
return;
memcpy(_frame_buff + pixel_data.index, &pixel_data.color1, 3);
});
}
_BrightnessCorrection();
_frame_buff_ready = true;
return;
}
#else
#warning Need optimization
void Processing(uint32_t time)
{
if(_manual_mode == true) return;
if(_frame_buff_ready == true) return;
if(time - _last_screen_time <= _fps) return;
switch(_layers_idx)
{
case 0:
{
_last_screen_time = time;
break;
}
case _max_layers:
{
_BrightnessCorrection();
_frame_buff_ready = true;
return;
}
}
layers_t &layer = _layers[_layers_idx++];
if(!layer.active)
return;
layer.parser.GetAutoFrame(time, [&](pixel_data_t &pixel_data)
{
if (pixel_data.color4 < 255)
return;
memcpy(_frame_buff + pixel_data.index, &pixel_data.color1, 3);
});
return;
}
#endif
/*
Возвращает указатель на кадровый буфер и его размер.
uint8_t *&buffer - Указатель на буфер;
uint16_t &length - Длина массиа.
*/
void GetFrameBuffer(uint8_t *&buffer, uint16_t &length)
{
buffer = (uint8_t *)_frame_buff;
length = sizeof(_frame_buff);
return;
}
/*
Возвращает true если кадровый буфер готов для вывода.
*/
bool IsBufferReady()
{
bool result = false;
if(_frame_buff_ready == true && _frame_is_draw == false)
{
result = true;
}
return result;
}
/*
Вызывается при инициализации передачи данных из буфера.
*/
void SetFrameDrawStart()
{
_frame_is_draw = true;
return;
}
/*
*/
bool GetFrameIsDraw()
{
return _frame_is_draw;
}
/*
Вызывается при завершении передачи данных из буфера.
*/
void SetFrameDrawEnd()
{
_ClearBuffer();
return;
}
private:
void _BrightnessCorrection()
{
uint8_t *buffer = (uint8_t *)_frame_buff;
uint16_t frame_buff_idx = sizeof(_frame_buff);
while(frame_buff_idx > 0)
{
_BrightnessConvert( buffer[--frame_buff_idx] );
//_BrightnessGammaConvert( buffer[--frame_buff_idx] );
}
return;
}
void _ClearBuffer()
{
memset(_frame_buff, 0x00, sizeof(_frame_buff));
_layers_idx = 0;
_frame_buff_ready = false;
_frame_is_draw = false;
return;
}
// Расчёт яркости пикселя
inline void _BrightnessConvert(uint8_t &val)
{
if(val == 0) return;
val = ((val * _brightness) + 255) >> 8;
}
// Расчёт яркости пикселя + гамма-коррекция по параболе x^2
inline void _BrightnessGammaConvert(uint8_t &val)
{
if(val == 0) return;
val = (((uint32_t)val * val * _brightness) + 130305) >> 16;
}
layers_t _layers[_max_layers];
uint8_t _layers_idx;
color_t _frame_buff[(_width * _height)];
bool _frame_buff_ready;
bool _frame_is_draw = false;
uint16_t _fps;
uint8_t _brightness;
uint32_t _last_screen_time = 0;
bool _manual_mode = false;
};