forked from pixelmatix/SmartMatrix
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMatrixForeground.cpp
446 lines (363 loc) · 15.3 KB
/
MatrixForeground.cpp
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* SmartMatrix Library - Methods for interacting with foreground layer
*
* Copyright (c) 2014 Louis Beaudoin (Pixelmatix)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include "SmartMatrix.h"
#ifndef DISABLE_FOREGROUND_FUNCTIONS
// FIXME: Most of these variable should be in smartmatrix class (i.e. one per instance of smart matrix, rather than being global)
// options
static char text[textLayerMaxStringLength];
static unsigned char textlen;
static int scrollcounter = 0;
static rgb24 textcolor = {0xff, 0xff, 0xff};
static int fontTopOffset = 1;
static int fontLeftOffset = 1;
static bool majorScrollFontChange = false;
bool hasForeground = false;
// Scrolling
static ScrollMode scrollmode = bounceForward;
static unsigned char framesperscroll = 4;
// foreground bitmap, one uint32_t stores 32 bits
// double buffered to prevent flicker while drawing
#if DRAWING_HEIGHT >= 32
static uint32_t foregroundBitmap[2][ DRAWING_HEIGHT * DRAWING_WIDTH / 32 ];
#else
// safety space for height = 16 and rotation = 90 or 270
static uint32_t foregroundBitmap[2][ DRAWING_HEIGHT * 2 * DRAWING_WIDTH / 32 ];
#endif
const unsigned char foregroundDrawBuffer = 0;
const unsigned char foregroundRefreshBuffer = 1;
volatile bool SmartMatrix::foregroundCopyPending = false;
static const bitmap_font *scrollFont = &apple5x7;
// these variables describe the text bitmap: size, location on the screen, and bounds of where it moves
static unsigned int textWidth;
static int scrollMin, scrollMax;
static int scrollPosition;
// shift and dimension values required for addressing and limits
// initialised or changed in handleForegroundDrawingCopy()
// (because this is the first foreground method called during a loop-cycle)
// these variables try to minimise the need for time consuming calculations
static rotationDegrees currRotation = rotation0;
static uint16_t currLocalWidth = 0; // 0 forces initialisation in first call of handleForegroundDrawingCopy()
static uint16_t currLocalHeight = 0;
static uint16_t arrayYmult = currLocalWidth / 32;
// stops the scrolling text on the next refresh
void SmartMatrix::stopScrollText(void) {
// setup conditions for ending scrolling:
// scrollcounter is next to zero
scrollcounter = 1;
// position text at the end of the cycle
scrollPosition = scrollMin;
}
void SmartMatrix::clearForeground(void) {
memset(foregroundBitmap[foregroundDrawBuffer], 0x00, sizeof(foregroundBitmap[0]));
}
void SmartMatrix::displayForegroundDrawing(bool waitUntilComplete) {
hasForeground = true;
while (foregroundCopyPending);
foregroundCopyPending = true;
while (waitUntilComplete && foregroundCopyPending);
}
void SmartMatrix::handleForegroundDrawingCopy(void) {
// check if first call or if rotation has changed. if so: correct some values required for addressing
if ((currLocalWidth == 0) || (currRotation != screenConfig.rotation)) {
currRotation = screenConfig.rotation;
currLocalWidth = (screenConfig.rotation == 0 || screenConfig.rotation == 180)
? screenConfig.localWidth : screenConfig.localHeight;
currLocalHeight = (screenConfig.rotation == 0 || screenConfig.rotation == 180)
? screenConfig.localHeight : screenConfig.localWidth;
arrayYmult = currLocalWidth / 32;
}
if (!foregroundCopyPending)
return;
memcpy(foregroundBitmap[foregroundRefreshBuffer], foregroundBitmap[foregroundDrawBuffer], sizeof(foregroundBitmap[0]));
redrawForeground();
foregroundCopyPending = false;
}
void SmartMatrix::drawForegroundPixel(int16_t x, int16_t y, bool opaque) {
uint32_t tempBitmask;
if(opaque) {
tempBitmask = 0x80000000 >> (x % 32);
foregroundBitmap[foregroundDrawBuffer][y * arrayYmult + (x / 32) ] |= tempBitmask;
} else {
tempBitmask = ~(0x80000000 >> (x % 32));
foregroundBitmap[foregroundDrawBuffer][y * arrayYmult + (x / 32) ] &= tempBitmask;
}
}
bitmap_font *foregroundfont = (bitmap_font *) &apple3x5;
void SmartMatrix::setForegroundFont(fontChoices newFont) {
foregroundfont = (bitmap_font *)fontLookup(newFont);
majorScrollFontChange = true;
}
void SmartMatrix::drawForegroundChar(int16_t x, int16_t y, char character, bool opaque) {
uint32_t tempBitmask;
int k;
for (k = y; k < y+foregroundfont->Height; k++) {
// ignore rows that are not on the screen
if(k < 0) continue;
if (k >= currLocalHeight) return;
// read in uint8, shift it to be in MSB (font is in the top bits of the uint32)
tempBitmask = getBitmapFontRowAtXY(character, k - y, foregroundfont) << 24;
if (x < 0)
foregroundBitmap[foregroundDrawBuffer][k * arrayYmult + 0 ] |= tempBitmask << -x;
else
foregroundBitmap[foregroundDrawBuffer][k * arrayYmult + (x / 32) ] |= tempBitmask >> (x % 32);
}
}
void SmartMatrix::drawForegroundString(int16_t x, int16_t y, const char text [], bool opaque) {
// limit text to 10 chars, why?
for (int i = 0; i < 30; i++) {
char character = text[i];
if (character == '\0')
return;
drawForegroundChar(i * foregroundfont->Width + x, y, character, opaque);
}
}
void SmartMatrix::drawForegroundMonoBitmap(int16_t x, int16_t y, uint8_t width, uint8_t height, uint8_t *bitmap, bool opaque) {
int xcnt, ycnt;
for (ycnt = 0; ycnt < height; ycnt++) {
for (xcnt = 0; xcnt < width; xcnt++) {
if (getBitmapPixelAtXY(xcnt, ycnt, width, height, bitmap)) {
drawForegroundPixel(x + xcnt, y + ycnt, opaque);
}
}
}
}
// returns 0 if stopped
// returns positive number indicating number of loops left if running
// returns -1 if continuously scrolling
int SmartMatrix::getScrollStatus(void) const {
return scrollcounter;
}
void SmartMatrix::setScrollMinMax(void) {
switch (scrollmode) {
case wrapForward:
case bounceForward:
case bounceReverse:
case wrapForwardFromLeft:
scrollMin = -textWidth;
scrollMax = currLocalWidth;
scrollPosition = scrollMax;
if (scrollmode == bounceReverse)
scrollPosition = scrollMin;
else if(scrollmode == wrapForwardFromLeft)
scrollPosition = fontLeftOffset;
// TODO: handle special case - put content in fixed location if wider than window
break;
case stopped:
case off:
scrollMin = scrollMax = scrollPosition = 0;
break;
}
}
void SmartMatrix::scrollText(const char inputtext[], int numScrolls) {
int length = strlen((const char *)inputtext);
if (length > textLayerMaxStringLength)
length = textLayerMaxStringLength;
strncpy(text, (const char *)inputtext, length);
textlen = length;
scrollcounter = numScrolls;
textWidth = (textlen * scrollFont->Width) - 1;
setScrollMinMax();
}
//Updates the text that is currently scrolling to the new value
//Useful for a clock display where the time changes.
void SmartMatrix::updateScrollText(const char inputtext[]){
int length = strlen((const char *)inputtext);
if (length > textLayerMaxStringLength)
length = textLayerMaxStringLength;
strncpy(text, (const char *)inputtext, length);
textlen = length;
textWidth = (textlen * scrollFont->Width) - 1;
setScrollMinMax();
}
// TODO: recompute stuff after changing mode, font, etc
void SmartMatrix::setScrollMode(ScrollMode mode) {
scrollmode = mode;
}
void SmartMatrix::setScrollSpeed(unsigned char pixels_per_second) {
framesperscroll = (MATRIX_REFRESH_RATE * 1.0) / pixels_per_second;
}
void SmartMatrix::setScrollFont(fontChoices newFont) {
scrollFont = fontLookup(newFont);
}
void SmartMatrix::setScrollColor(const rgb24 & newColor) {
copyRgb24(textcolor, newColor);
}
void SmartMatrix::setScrollOffsetFromTop(int offset) {
fontTopOffset = offset;
majorScrollFontChange = true;
}
void SmartMatrix::setScrollStartOffsetFromLeft(int offset) {
fontLeftOffset = offset;
}
// if font size or position changed since the last call, redraw the whole frame
void SmartMatrix::redrawForeground(void) {
int j, k, l;
int charPosition, textPosition;
uint8_t charY0, charY1;
for (j = 0; j < currLocalHeight; j++) {
// skip rows without text
if (j < fontTopOffset || j >= fontTopOffset + scrollFont->Height)
continue;
hasForeground = true;
// now in row with text
// find the position of the first char
charPosition = scrollPosition;
textPosition = 0;
// move to first character at least partially on screen
while (charPosition + scrollFont->Width < 0 ) {
charPosition += scrollFont->Width;
textPosition++;
}
// find rows within character bitmap that will be drawn (0-font->height unless text is partially off screen)
charY0 = j - fontTopOffset;
if (currLocalHeight < fontTopOffset + scrollFont->Height) {
charY1 = currLocalHeight - fontTopOffset;
} else {
charY1 = scrollFont->Height;
}
/* TODO: some edge cases could end up with unwanted drawing to the screen, e.g. foregrounddrawing call,
* then scrolling text change before displayForegroundDrawing() call would show drawing before intended
*/
if(majorScrollFontChange) {
// clear full refresh buffer and copy background over
memset(foregroundBitmap[foregroundRefreshBuffer], 0x00, sizeof(foregroundBitmap[0]));
memcpy(foregroundBitmap[foregroundRefreshBuffer], foregroundBitmap[foregroundDrawBuffer], sizeof(foregroundBitmap[0]));
majorScrollFontChange = false;
}
// clear rows used by font before drawing on top
for (k = 0; k < charY1 - charY0; k++) {
//foregroundBitmap[foregroundRefreshBuffer][j + k][0] = 0x00;
for (l = 0; l < arrayYmult; l++) {
foregroundBitmap[foregroundRefreshBuffer][ (j + k) * arrayYmult + l] = 0x00;
}
}
while (textPosition < textlen && charPosition < currLocalWidth) {
uint32_t tempBitmask;
// draw character from top to bottom
for (k = charY0; k < charY1; k++) {
// read in uint8, shift it to be in MSB (font is in the top bits of the uint32)
tempBitmask = getBitmapFontRowAtXY(text[textPosition], k, scrollFont) << 24;
//if (charPosition < 0)
// foregroundBitmap[foregroundRefreshBuffer][j + k - charY0][0] |= tempBitmask << -charPosition;
//else
// foregroundBitmap[foregroundRefreshBuffer][j + k - charY0][0] |= tempBitmask >> charPosition;
//for (l = 0; l < panels_per_row /*SmartMatrix::screenConfig.localWidth / 32*/; ++l) {
for (l = 0; l < arrayYmult; l++) {
// character position relative to panel l
int panelPosition = charPosition - l * 32;
if (panelPosition > -8 && panelPosition < 0)
foregroundBitmap[foregroundRefreshBuffer][(j + k - charY0) * arrayYmult + l] |= tempBitmask << -panelPosition;
else if (panelPosition >= 0 && panelPosition < 32)
foregroundBitmap[foregroundRefreshBuffer][(j + k - charY0) * arrayYmult + l] |= tempBitmask >> panelPosition;
}
}
// get set up for next character
charPosition += scrollFont->Width;
textPosition++;
}
j += (charY1 - charY0) - 1;
}
}
// called once per frame to update foreground (virtual) bitmap
// function needs major efficiency improvments
void SmartMatrix::updateForeground(void) {
bool resetScrolls = false;
static unsigned char currentframe = 0;
// return if not ready to update
if (!scrollcounter || ++currentframe <= framesperscroll)
return;
currentframe = 0;
switch (scrollmode) {
case wrapForward:
case wrapForwardFromLeft:
scrollPosition--;
if (scrollPosition <= scrollMin) {
scrollPosition = scrollMax;
if (scrollcounter > 0) scrollcounter--;
}
break;
case bounceForward:
scrollPosition--;
if (scrollPosition <= scrollMin) {
scrollmode = bounceReverse;
if (scrollcounter > 0) scrollcounter--;
}
break;
case bounceReverse:
scrollPosition++;
if (scrollPosition >= scrollMax) {
scrollmode = bounceForward;
if (scrollcounter > 0) scrollcounter--;
}
break;
default:
case stopped:
scrollPosition = fontLeftOffset;
resetScrolls = true;
break;
}
// done scrolling - move text off screen and disable
if (!scrollcounter) {
resetScrolls = true;
}
// for now, fill the bitmap fresh with each update
// TODO: reset only when necessary, and update just the pixels that need it
resetScrolls = true;
if (resetScrolls) {
redrawForeground();
}
}
// returns true and copies color to xyPixel if pixel is opaque, returns false if not
bool SmartMatrix::getForegroundPixel(uint8_t hardwareX, uint8_t hardwareY, rgb24 *xyPixel) {
uint8_t localScreenX = hardwareX, localScreenY = hardwareY; // initialise to avoid compiler-warnings
// convert hardware x/y to the pixel in the local screen
switch( SmartMatrix::screenConfig.rotation ) {
case rotation0 :
localScreenX = hardwareX;
localScreenY = hardwareY;
break;
case rotation180 :
localScreenX = (DRAWING_WIDTH - 1) - hardwareX;
localScreenY = (DRAWING_HEIGHT - 1) - hardwareY;
break;
case rotation90 :
localScreenX = hardwareY;
localScreenY = (DRAWING_WIDTH - 1) - hardwareX;
break;
case rotation270 :
localScreenX = (DRAWING_HEIGHT - 1) - hardwareY;
localScreenY = hardwareX;
break;
default:
// TODO: Should throw an error
return false;
};
uint32_t bitmask = 0x80000000 >> (localScreenX % 32);
if (foregroundBitmap[foregroundRefreshBuffer][localScreenY * arrayYmult + (localScreenX / 32) ] & bitmask) {
copyRgb24(*xyPixel, textcolor);
return true;
}
return false;
}
#endif // DISABLE_FOREGROUND_FUNCTIONS