forked from fornellas/SFRGBLEDMatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSFRGBLEDMatrix.cpp
More file actions
334 lines (297 loc) · 7.36 KB
/
SFRGBLEDMatrix.cpp
File metadata and controls
334 lines (297 loc) · 7.36 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
extern "C" {
#include <inttypes.h>
#include <stdlib.h>
#include <math.h>
}
#include <Arduino.h>
#include <SFRGBLEDMatrix.h>
#include <SPI.h>
#include <SFRGBLEDMatrixFonts.h>
// Display properties
#define DISPLAY_PIXELS 64
#define DISPLAY_BUFFER_SIZE (DISPLAY_PIXELS*BITS_PER_COLOR*3>>3)
#define SWAP(a, b) { int16_t t = a; a = b; b = t; }
#define SPECTRUM_LEN 90
//
// Functions
//
SFRGBLEDMatrix::SFRGBLEDMatrix(const uint8_t pinSS, const uint8_t numDispHoriz, const uint8_t numDispVert) {
this->dispCount=numDispHoriz*numDispVert;
this->width=DISP_LEN*numDispHoriz;
this->height=DISP_LEN*numDispVert;
this->recAdjStart=int((( ((height>>3)-1) * (width>>3) )<<3));
this->recAdjIncr=((width>>3)+1)<<3;
this->useGamma=false;
this->pinSS=pinSS;
frameBuffSize=DISPLAY_BUFFER_SIZE*dispCount;
frameBuff=(byte *)calloc((size_t)(frameBuffSize), sizeof(byte)); // FIXME validate if NULL
this->pixels=this->width*this->height;
setupSPI();
setupPINs();
}
SFRGBLEDMatrix::~SFRGBLEDMatrix() {
free(frameBuff);
};
void SFRGBLEDMatrix::setupSPI() {
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV4);
SPI.setBitOrder(MSBFIRST);
SPI.begin();
}
void SFRGBLEDMatrix::setupPINs() {
pinMode(MOSI, OUTPUT);
pinMode(pinSS, OUTPUT);
pinMode(SCK, OUTPUT);
}
void SFRGBLEDMatrix::show() {
digitalWrite(pinSS, LOW);
for(uint16_t p=0;p<frameBuffSize;p++){
SPI.transfer(*(frameBuff + p));
delayMicroseconds(64);
}
digitalWrite(pinSS, HIGH);
delayMicroseconds(297);
};
#define X_MAX (byte)(pgm_read_word_near(coffset+c-CHAR_MIN+1)-(byte)pgm_read_word_near(coffset+c-CHAR_MIN))
void SFRGBLEDMatrix::print(const Color color, const int xOffset, const int yOffset, const uint8_t size, const char c){
const uint16_t *coffset;
const unsigned char *const *line;
switch(size){
case 4:
coffset=coffset_4p;
line=line_4p;
break;
case 5:
coffset=coffset_5p;
line=line_5p;
break;
default:
return;
};
if(c<CHAR_MIN||c>CHAR_MAX)
return;
for(int y=0;y<size;y++){
if(y+yOffset>=height)
continue;
uint8_t x_max=X_MAX;
for(int x=0;x<x_max;x++){
unsigned int bitOffset;
byte charData;
byte pixel;
if(x+yOffset>=width)
continue;
bitOffset=(unsigned int)pgm_read_word_near(coffset+c-CHAR_MIN)+x;
charData=(byte)pgm_read_word_near((byte *)pgm_read_word_near(&line[y])+bitOffset/8);
pixel=(charData>>(7-bitOffset%8)) & B00000001;
if(pixel)
paintPixel(color, x+xOffset, y+yOffset);
}
}
}
void SFRGBLEDMatrix::print(const Color color, int x, int y, const uint8_t size, const char *s){
const uint16_t *coffset;
switch(size){
case 4:
coffset=coffset_4p;
break;
case 5:
coffset=coffset_5p;
break;
default:
return;
}
for(uint16_t p=0;s[p]!='\0';p++){
char c;
c=s[p];
print(color, x, y, size, c);
x+=X_MAX+1;
}
}
void SFRGBLEDMatrix::print_PF(const Color color, int x, int y, const uint8_t size, PGM_P s){
const uint16_t *coffset;
char c;
switch(size){
case 4:
coffset=coffset_4p;
break;
case 5:
coffset=coffset_5p;
break;
default:
return;
}
for(uint16_t p=0;c!=0;p++){
c=pgm_read_byte(s+p);
print(color, x, y, size, c);
x+=X_MAX+1;
}
}
void SFRGBLEDMatrix::paintPixel(Color color, int x, int y) {
uint16_t startPixel;
uint16_t startByte;
// Out of boundaries
if(x>=width||y>=height||y<0||y<0)
return;
// Gamma
if(useGamma){
color=RGB_GAMMA(GET_RED(color), GET_GREEN(color), GET_BLUE(color));
}
// Adjust coordinates, can be disabled for single row of matrices
x+=recAdjStart - int(y>>3)*recAdjIncr;
// print pixel
startPixel=(dispCount-1-(x>>3))*DISPLAY_PIXELS + ((7-y)<<3) + (x&7);
startByte=(startPixel*12)>>3;
// odd pixels
if(startPixel&0x01) {
// XXXX RRRR
frameBuff[startByte]=(frameBuff[startByte]&0xF0)|(color>>8);
// GGGG BBBB
frameBuff[startByte+1]=color&0xFF;
// even pixels
}else{
// RRRR GGGG
frameBuff[startByte]=color>>4;
// BBBB XXXX
frameBuff[startByte+1]=(frameBuff[startByte+1]&0x0F)|(color&0x0F)<<4;
}
}
void SFRGBLEDMatrix::fill(Color color){
for(int x=0;x<width;x++)
for(int y=0;y<height;y++)
paintPixel(color, x, y);
}
void SFRGBLEDMatrix::clear(){
fill(BLACK);
}
void SFRGBLEDMatrix::line(const Color color, int x0, int y0, int x1, int y1){
// http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
// https://github.com/adafruit/Adafruit-GFX-Library/blob/master/Adafruit_GFX.cpp
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
if(steep){
SWAP(x0, y0);
SWAP(x1, y1);
}
if(x0>x1){
SWAP(x0, x1);
SWAP(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int16_t err = dx / 2;
int16_t ystep;
if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;
}
for (; x0<=x1; x0++) {
if (steep) {
paintPixel(color, y0, x0);
} else {
paintPixel(color, x0, y0);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
}
}
void SFRGBLEDMatrix::box(const Color color, int x0, int y0, int x1, int y1){
line(color, x0, y0, x1, y0);
line(color, x1, y0, x1, y1);
line(color, x0, y1, x1, y1);
line(color, x0, y1, x0, y0);
}
void SFRGBLEDMatrix::gamma(boolean state){
useGamma=state;
}
uint8_t SFRGBLEDMatrix::spectrumLen(){
return SPECTRUM_LEN;
}
Color SFRGBLEDMatrix::spectrum(uint16_t value, uint16_t max){
uint8_t p;
p=round((double)value/(double)max*(double)(SPECTRUM_LEN-1));
// RED
if(p<15){
return RGB(15, p+1, 0);
}
// YELLOW
if(p<30){
p-=15;
return RGB(14-p, 15, 0);
}
// GREEN
if(p<45){
p-=30;
return RGB(0, 15, p+1);
}
// CYAN
if(p<60){
p-=45;
return RGB(0, 14-p, 15);
}
// BLUE
if(p<75){
p-=60;
return RGB(p+1, 0, 15);
}
// MAGENTA
if(p<90){
p-=75;
return RGB(15, 0, 14-p);
}
return spectrum(value-max, max);
}
void SFRGBLEDMatrix::progressBarInit(Color borderColor){
clear();
box(borderColor, 0, height/2-2, width-1, height/2+1);
show();
}
void SFRGBLEDMatrix::progressBarUpdate(Color barColor, uint16_t vaule, uint16_t max){
box(barColor, 1, height/2-1, round((double)vaule*((double)width-2.0)/(double)max), height/2);
show();
}
void SFRGBLEDMatrix::CRT(Color bgColor){
for(byte p=0;p<height/2-1;p++){
if(p)
box(bgColor, p-1, p-1, width-1-p+1, height-1-p+1);
show();
}
for(byte p=height/2-1;p<width/2;p++){
box(bgColor, p-1, height/2-2-1, width-1-p+1, height/2+1+1);
show();
}
for(byte p=height/2-2;p<height/2;p++){
box(bgColor, width/2-2, p, width/2+1, height-p-1);
show();
}
}
Color SFRGBLEDMatrix::getPixel(int x, int y){
uint16_t startPixel;
uint16_t startByte;
Color color=BLACK;
// Out of boundaries
if(x>=width||y>=height||y<0||y<0)
return BLACK;
// Adjust coordinates, can be disabled for single row of matrices
x+=recAdjStart - int(y>>3)*recAdjIncr;
// print pixel
startPixel=(dispCount-1-(x>>3))*DISPLAY_PIXELS + ((7-y)<<3) + (x&7);
startByte=(startPixel*12)>>3;
// odd pixels
if(startPixel&0x01) {
// XXXX RRRR
color=(frameBuff[startByte]&0x0F)<<8;
// GGGG BBBB
color=color|frameBuff[startByte+1];
// even pixels
}else{
// RRRR GGGG
color=frameBuff[startByte]<<4;
// BBBB XXXX
color=color|(frameBuff[startByte+1]>>4);
}
return color;
}