-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFramebuffer_GFX.cpp
More file actions
353 lines (292 loc) · 11 KB
/
Framebuffer_GFX.cpp
File metadata and controls
353 lines (292 loc) · 11 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*-------------------------------------------------------------------------
Arduino library based on Adafruit_Neomatrix but modified to work with SmartMatrix
by Marc MERLIN <marc_soft@merlins.org>
Original notice and license from Adafruit_Neomatrix:
------------------------------------------------------------------------
Arduino library to control single and tiled matrices of WS2811- and
WS2812-based RGB LED devices such as the Adafruit NeoPixel Shield or
displays assembled from NeoPixel strips, making them compatible with
the Adafruit_GFX graphics library. Requires both the FastLED_NeoPixel
and Adafruit_GFX libraries.
Written by Phil Burgess / Paint Your Dragon for Adafruit Industries.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing products
from Adafruit!
-------------------------------------------------------------------------
This file is part of the Adafruit NeoMatrix library.
NeoMatrix is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
NeoMatrix is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with NeoMatrix. If not, see
<http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------*/
#include <Adafruit_GFX.h>
#include <Framebuffer_GFX.h>
#include "gamma.h"
#ifdef __AVR__
#include <avr/pgmspace.h>
#elif defined(ESP8266)
#include <pgmspace.h>
#else
#ifndef pgm_read_byte
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#endif
#endif
#ifndef _swap_uint16_t
#define _swap_uint16_t(a, b) { uint16_t t = a; a = b; b = t; }
#endif
#include "FastLED.h"
Framebuffer_GFX::Framebuffer_GFX(CRGB *fb, const uint16_t w, const uint16_t h, void (* showptr)()):
Adafruit_GFX(w, h), matrixWidth(w), matrixHeight(h), remapFn(NULL){
_fb = fb;
_show = showptr;
type = 0;
tilesX = 0;
tilesY = 0;
// WARNING: Serial.print seems to crash in the constructor,
// but works in begin()
numpix = matrixWidth * matrixHeight;
}
void Framebuffer_GFX::begin() {
Serial.print("Framebuffer_GFX::begin Width: ");
Serial.print(matrixWidth);
Serial.print(" Height: ");
Serial.print(matrixHeight);
Serial.print(" Num Pixels: ");
Serial.println(numpix);
#if 0
Serial.println(matrixWidth);
Serial.println(matrixHeight);
Serial.println(tilesX);
Serial.println(tilesY);
Serial.println(type);
#endif
}
void Framebuffer_GFX::newLedsPtr(CRGB *new_fb_ptr) {
_fb = new_fb_ptr;
}
// You can change frequency, or turn off serial display setfpsfreq
// Note that this computes the number of fps generated by the caller,
// in other words, how many frames are generated per second.
// Depending on the backend, the number of frames displayed could be
// entirely different (faster or slower), which is true for any backend
// that renders the framebuffer asynchronously in an ISR or 2nd core
// like ESP32.
void Framebuffer_GFX::showfps() {
static uint32_t time_last = 0;
static uint32_t last_framecount = 0;
uint32_t time_now = millis();
framecount++;
if (!fpsfreq) return;
if (time_now - time_last > fpsfreq) {
time_last = time_now;
Serial.print("FrameBuffer::GFX ");
Serial.print((framecount - last_framecount) / (fpsfreq/1000));
Serial.println("fps");
last_framecount = framecount;
}
}
// See showfps() for the true meaning for FPS
uint32_t Framebuffer_GFX::fps() {
static uint32_t time_last = 0;
static uint32_t last_framecount = 0;
static uint32_t fps = 0;
uint32_t time_now = millis();
// Can't divide by 0, return the last value computed
if (time_now == time_last) return fps;
fps = 1000 * (framecount - last_framecount) / (time_now - time_last);
time_last = time_now;
last_framecount = framecount;
return fps;
}
// Expand 16-bit input color (Adafruit_GFX colorspace) to 24-bit (NeoPixel)
// (w/gamma adjustment)
uint32_t Framebuffer_GFX::expandColor(uint16_t color) {
return ((uint32_t)pgm_read_byte(&gamma5[ color >> 11 ]) << 16) |
((uint32_t)pgm_read_byte(&gamma6[(color >> 5) & 0x3F]) << 8) |
pgm_read_byte(&gamma5[ color & 0x1F]);
}
// Downgrade 24-bit color to 16-bit (add reverse gamma lookup here?)
uint16_t Framebuffer_GFX::Color(uint8_t r, uint8_t g, uint8_t b) {
return ((uint16_t)(r & 0xF8) << 8) |
((uint16_t)(g & 0xFC) << 3) |
(b >> 3);
}
uint16_t Framebuffer_GFX::Color24to16(uint32_t color) {
return ((uint16_t)(((color & 0xFF0000) >> 16) & 0xF8) << 8) |
((uint16_t)(((color & 0x00FF00) >> 8) & 0xFC) << 3) |
(((color & 0x0000FF) >> 0) >> 3);
}
uint32_t Framebuffer_GFX::CRGBtoint32(CRGB c) {
return (((uint32_t)c.r)<<16) + (((uint32_t)c.g)<<8) + (uint32_t)c.b;
}
// Pass-through is a kludge that lets you override the current drawing
// color with a 'raw' RGB (or RGBW) value that's issued directly to
// pixel(s), side-stepping the 16-bit color limitation of Adafruit_GFX.
// This is not without some limitations of its own -- for example, it
// won't work in conjunction with the background color feature when
// drawing text or bitmaps (you'll just get a solid rect of color),
// only 'transparent' text/bitmaps. Also, no gamma correction.
// Remember to UNSET the passthrough color immediately when done with
// it (call with no value)!
// Pass raw color value to set/enable passthrough
void Framebuffer_GFX::setPassThruColor(CRGB c) {
passThruColor = CRGBtoint32(c);
passThruFlag = true;
}
void Framebuffer_GFX::setPassThruColor(uint32_t c) {
passThruColor = c;
passThruFlag = true;
}
// Call without a value to reset (disable passthrough)
void Framebuffer_GFX::setPassThruColor(void) {
passThruFlag = false;
}
int Framebuffer_GFX::XY(int16_t x, int16_t y) {
// If you send an out of bounds value, you get an special result
// pointing to the last pixel. It doesn't look great, but better
// than crashing. Still, fix the upstream code.
// DrawPixel is able to reject the write, but here we have to return an index
// which has to be inbounds.
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return numpix-1;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
int tileOffset = 0, pixelOffset;
if(remapFn) { // Custom X/Y remapping function
pixelOffset = (*remapFn)(x, y);
} else { // Standard single matrix or tiled matrices
uint8_t corner = type & NEO_MATRIX_CORNER;
uint16_t minor, major, majorScale;
if(tilesX) { // Tiled display, multiple matrices
uint16_t tile;
minor = x / matrixWidth; // Tile # X/Y; presume row major to
major = y / matrixHeight, // start (will swap later if needed)
x = x - (minor * matrixWidth); // Pixel X/Y within tile
y = y - (major * matrixHeight); // (-* is less math than modulo)
// Determine corner of entry, flip axes if needed
if(type & NEO_TILE_RIGHT) minor = tilesX - 1 - minor;
if(type & NEO_TILE_BOTTOM) major = tilesY - 1 - major;
// Determine actual major axis of tiling
if((type & NEO_TILE_AXIS) == NEO_TILE_ROWS) {
majorScale = tilesX;
} else {
_swap_uint16_t(major, minor);
majorScale = tilesY;
}
// Determine tile number
if((type & NEO_TILE_SEQUENCE) == NEO_TILE_PROGRESSIVE) {
// All tiles in same order
tile = major * majorScale + minor;
} else {
// Zigzag; alternate rows change direction. On these rows,
// this also flips the starting corner of the matrix for the
// pixel math later.
if(major & 1) {
corner ^= NEO_MATRIX_CORNER;
tile = (major + 1) * majorScale - 1 - minor;
} else {
tile = major * majorScale + minor;
}
}
// Index of first pixel in tile
tileOffset = tile * matrixWidth * matrixHeight;
} // else no tiling (handle as single tile)
// Find pixel number within tile
minor = x; // Presume row major to start (will swap later if needed)
major = y;
// Determine corner of entry, flip axes if needed
if(corner & NEO_MATRIX_RIGHT) minor = matrixWidth - 1 - minor;
if(corner & NEO_MATRIX_BOTTOM) major = matrixHeight - 1 - major;
// Determine actual major axis of matrix
if((type & NEO_MATRIX_AXIS) == NEO_MATRIX_ROWS) {
majorScale = matrixWidth;
} else {
_swap_uint16_t(major, minor);
majorScale = matrixHeight;
}
// Determine pixel number within tile/matrix
if((type & NEO_MATRIX_SEQUENCE) == NEO_MATRIX_PROGRESSIVE) {
// All lines in same order
pixelOffset = major * majorScale + minor;
} else {
// Zigzag; alternate rows change direction.
if(major & 1) pixelOffset = (major + 1) * majorScale - 1 - minor;
else pixelOffset = major * majorScale + minor;
}
}
#if 0
Serial.print(x);
Serial.print(" ");
Serial.print(y);
Serial.print(" ");
Serial.println(tileOffset + pixelOffset);
#endif
return(tileOffset + pixelOffset);
}
void Framebuffer_GFX::drawPixel(int16_t x, int16_t y, uint16_t color) {
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
int32_t idx = XY(x,y);
if (idx<0 || idx>(int32_t)numpix) return;
_fb[idx] = passThruFlag ? passThruColor : expandColor(color);
}
void Framebuffer_GFX::drawPixel(int16_t x, int16_t y, uint32_t color) {
#if 0
Serial.print(x);
Serial.print(" ");
Serial.print(y);
Serial.print(" ");
Serial.println(color, HEX);
#endif
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
#if 0
Serial.print("Not skipped: ");
Serial.print(x);
Serial.print(" ");
Serial.print(y);
Serial.print(" ");
Serial.println(color, HEX);
#endif
int32_t idx = XY(x,y);
if (idx<0 || idx>(int32_t)numpix) return;
_fb[idx] = color;
}
void Framebuffer_GFX::drawPixel(int16_t x, int16_t y, CRGB c) {
drawPixel(x, y, CRGBtoint32(c));
}
void Framebuffer_GFX::fillScreen(uint16_t color) {
uint32_t c;
c = passThruFlag ? passThruColor : expandColor(color);
for (uint32_t i=0; i<numpix; i++) { _fb[i]=c; }
}
void Framebuffer_GFX::setRemapFunction(uint16_t (*fn)(uint16_t, uint16_t)) {
remapFn = fn;
}
// After setting gamma, this is used with
// CRGB color = CRGB(matrix->gamma[red], matrix->gamma[green], matrix->gamma[blue]);
void Framebuffer_GFX::precal_gamma(float gam) {
for (uint16_t i = 0; i<=255; i++) {
gamma[i] = applyGamma_video(i, gam);
}
}
// vim:sts=2:sw=2