-
Notifications
You must be signed in to change notification settings - Fork 13
/
P10_matrix.cpp
454 lines (359 loc) · 10.6 KB
/
P10_matrix.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
447
448
449
450
451
452
453
454
/*********************************************************************
This is a library for Chinese P10 32x16 LED matrix displays
It is based on the 5110 library by Adafruit
Written by Dominic Buchstaller.
BSD license, check license.txt for more information
*********************************************************************/
//#include <Wire.h>
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#ifdef __AVR__
#include <util/delay.h>
#endif
#ifndef _BV
#define _BV(x) (1 << (x))
#endif
#include <stdlib.h>
#include <Adafruit_GFX.h>
#include "P10_matrix.h"
#define matrix_width 32
#define matrix_height 16
#define color_step 256 / color_depth
#define color_half_step color_step / 2
#define buffer_size matrix_width * matrix_height * 3 / 8
// the memory buffer for the LCD
uint8_t P10_MATRIX_buffer[color_depth][buffer_size] = {0x00 };
#ifdef PATTERN4
uint8_t P10_MATRIX_send_buffer[buffer_size/4] = {0x00 };
#endif
#ifdef PATTERN8
uint8_t P10_MATRIX_send_buffer[buffer_size/8] = {0x00 };
#endif
#ifdef P5_PATTERN16
uint8_t P10_MATRIX_send_buffer[buffer_size/16] = {0x00 };
#endif
// Pass 8-bit (each) R,G,B, get back 16-bit packed color
uint16_t P10_MATRIX::color565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
// Init code common to both constructors
void P10_MATRIX::init(uint8_t LATCH, uint8_t OE, uint8_t A,uint8_t B,uint8_t C){
_LATCH_PIN = LATCH;
_OE_PIN = OE;
_A_PIN= A;
_B_PIN = B;
_C_PIN = C;
_width=matrix_width;
_height=matrix_height;
_display_color=0;
_test_last_call=0;
_test_pixel_counter=0;
_test_line_counter=0;
_rotate=0;
}
void P10_MATRIX::setRotate(bool rotate) {
_rotate=rotate;
}
P10_MATRIX::P10_MATRIX(uint8_t LATCH, uint8_t OE, uint8_t A,uint8_t B,uint8_t C) : Adafruit_GFX(matrix_width+10, matrix_height) {
init(LATCH, OE, A, B, C);
}
P10_MATRIX::P10_MATRIX(uint8_t LATCH, uint8_t OE, uint8_t A,uint8_t B,uint8_t C,uint8_t D) : Adafruit_GFX(matrix_width+10, matrix_height) {
init(LATCH, OE, A, B, C);
_D_PIN = D;
}
void P10_MATRIX::drawPixel(int16_t x, int16_t y, uint16_t color) {
drawPixelRGB565( x, y, color);
}
void P10_MATRIX::fillMatrixBuffer(int16_t x, int16_t y, uint8_t r, uint8_t g,uint8_t b)
{
if (_rotate){
uint16_t temp_x=x;
x=y;
y=_height-1-temp_x;
}
if ((x < 0) || (x > _width) || (y < 0) || (y > _height))
return;
x =matrix_width - 1 -x;
#ifdef PATTERN4
// Shift register length: 48 bytes, one color: 16 bytes
uint16_t base_offset=(y%4)*48+(x/8)*2;
uint8_t total_offset_r=0;
uint8_t total_offset_g=0;
uint8_t total_offset_b=0;
// Weird shit access pattern
if (y<4)
total_offset_r=base_offset;
if ((y>=4) && (y<8))
total_offset_r=base_offset+1;
if ((y>=8) && (y<12))
total_offset_r=base_offset+8;
if (y>=12)
total_offset_r=base_offset+9;
total_offset_g=total_offset_r+16;
total_offset_b=total_offset_g+16;
//Color interlacing
for (int this_color=0; this_color<color_depth; this_color++)
{
if (r > this_color*color_step+color_half_step)
P10_MATRIX_buffer[this_color][total_offset_r] |=_BV(x%8);
else
P10_MATRIX_buffer[this_color][total_offset_r] &= ~_BV(x%8);
if (g > this_color*color_step+color_half_step)
P10_MATRIX_buffer[(this_color+3)%8][total_offset_g] |=_BV(x%8);
else
P10_MATRIX_buffer[(this_color+3)%8][total_offset_g] &= ~_BV(x%8);
if (b > this_color*color_step+color_half_step)
P10_MATRIX_buffer[(this_color+6)%8][total_offset_b] |=_BV(x%8);
else
P10_MATRIX_buffer[(this_color+6)%8][total_offset_b] &= ~_BV(x%8);
}
#endif
#ifdef PATTERN8
// Shift register length: 24 bytes, one color: 8 bytes
uint16_t base_offset=(y%8)*24+(x/8);
uint8_t total_offset_r=0;
uint8_t total_offset_g=0;
uint8_t total_offset_b=0;
// (A bit less) weird shit access pattern
if (y<8)
total_offset_r=base_offset;
else
total_offset_r=base_offset+4;
total_offset_g=total_offset_r+8;
total_offset_b=total_offset_g+8;
//Simple counting up
for (int this_color=0; this_color<color_depth; this_color++)
{
if (r > this_color*color_step+color_half_step)
P10_MATRIX_buffer[this_color][total_offset_r] |=_BV(x%8);
else
P10_MATRIX_buffer[this_color][total_offset_r] &= ~_BV(x%8);
if (g > this_color*color_step+color_half_step)
P10_MATRIX_buffer[(this_color+3)%8][total_offset_g] |=_BV(x%8);
else
P10_MATRIX_buffer[(this_color+3)%8][total_offset_g] &= ~_BV(x%8);
if (b > this_color*color_step+color_half_step)
P10_MATRIX_buffer[(this_color+6)%8][total_offset_b] |=_BV(x%8);
else
P10_MATRIX_buffer[(this_color+6)%8][total_offset_b] &= ~_BV(x%8);
}
#endif
#ifdef P5_PATTERN16
// Shift register length: 48 bytes, one color: 16 bytes
uint16_t base_offset=(y%16)*48+(x/8);
uint16_t total_offset_r=0;
uint16_t total_offset_g=0;
uint16_t total_offset_b=0;
// (A bit less) weird shit access pattern
if (y<16){
total_offset_r=base_offset;
}else{
total_offset_r=base_offset+8;
}
total_offset_g=total_offset_r+16;
total_offset_b=total_offset_g+16;
//Simple counting up
for (int this_color=0; this_color<color_depth; this_color++)
{
if (r > this_color*color_step+color_half_step)
P10_MATRIX_buffer[this_color][total_offset_r] |=_BV(x%8);
else
P10_MATRIX_buffer[this_color][total_offset_r] &= ~_BV(x%8);
if (g > this_color*color_step+color_half_step)
P10_MATRIX_buffer[(this_color+4)%8][total_offset_g] |=_BV(x%8);
else
P10_MATRIX_buffer[(this_color+4)%8][total_offset_g] &= ~_BV(x%8);
if (b > this_color*color_step+color_half_step)
P10_MATRIX_buffer[(this_color+8)%8][total_offset_b] |=_BV(x%8);
else
P10_MATRIX_buffer[(this_color+8)%8][total_offset_b] &= ~_BV(x%8);
}
# endif
}
void P10_MATRIX::drawPixelRGB565(int16_t x, int16_t y, uint16_t color) {
uint8_t r = ((((color >> 11) & 0x1F) * 527) + 23) >> 6;
uint8_t g = ((((color >> 5) & 0x3F) * 259) + 33) >> 6;
uint8_t b = (((color & 0x1F) * 527) + 23) >> 6;
if (_rotate)
fillMatrixBuffer( x, y, r, g,b);
else
fillMatrixBuffer( x, y, r, g,b);
}
void P10_MATRIX::drawPixelRGB888(int16_t x, int16_t y, uint8_t r, uint8_t g,uint8_t b) {
fillMatrixBuffer(x, y, r, g,b);
}
// the most basic function, get a single pixel
uint8_t P10_MATRIX::getPixel(int8_t x, int8_t y) {
return (0);//P10_MATRIX_buffer[x+ (y/8)*LCDWIDTH] >> (y%8)) & 0x1;
}
void P10_MATRIX::begin() {
SPI.begin();
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.setFrequency(20000000);
pinMode(_OE_PIN, OUTPUT);
pinMode(_LATCH_PIN, OUTPUT);
pinMode(_A_PIN, OUTPUT);
pinMode(_B_PIN, OUTPUT);
pinMode(_C_PIN, OUTPUT);
#ifdef P5_PATTERN16
pinMode(_D_PIN, OUTPUT);
#endif
digitalWrite(_A_PIN, LOW);
digitalWrite(_B_PIN, LOW);
digitalWrite(_C_PIN, LOW);
//for P5
#ifdef P5_PATTERN16
digitalWrite(_D_PIN, LOW);
#endif
digitalWrite(_OE_PIN, HIGH);
}
void P10_MATRIX::display(uint16_t show_time) {
#ifdef PATTERN4
for (uint8_t i=0;i<4;i++)
#endif
#ifdef PATTERN8
for (uint8_t i=0;i<8;i++)
#endif
#ifdef P5_PATTERN16
for (uint8_t i=0;i<16;i++)
#endif
{
// digitalWrite(_A_PIN,HIGH);
// digitalWrite(_B_PIN,HIGH);
// digitalWrite(_C_PIN,LOW);
// digitalWrite(_A_PIN,LOW);
// digitalWrite(_B_PIN,LOW);
// digitalWrite(_C_PIN,LOW);
if (i & 0x01)
digitalWrite(_A_PIN,HIGH);
else
digitalWrite(_A_PIN,LOW);
if (i & 0x02)
digitalWrite(_B_PIN,HIGH);
else
digitalWrite(_B_PIN,LOW);
if (i & 0x04)
digitalWrite(_C_PIN,HIGH);
else
digitalWrite(_C_PIN,LOW);
#ifdef P5_PATTERN16
if (i & 0x08)
digitalWrite(_D_PIN,HIGH);
else
digitalWrite(_D_PIN,LOW);
#endif
#ifdef PATTERN4
for (uint8_t j=0;j<48;j++)
P10_MATRIX_send_buffer[j]= P10_MATRIX_buffer[_display_color][47-j+i*48];
SPI.writeBytes(P10_MATRIX_send_buffer,48);
#endif
#ifdef PATTERN8
for (uint8_t j=0;j<24;j++)
P10_MATRIX_send_buffer[j]= P10_MATRIX_buffer[_display_color][23-j+i*24];
SPI.writeBytes(P10_MATRIX_send_buffer,24);
#endif
#ifdef P5_PATTERN16
for (uint8_t j=0;j<48;j++){
P10_MATRIX_send_buffer[j]= P10_MATRIX_buffer[_display_color][47-j+i*48];
}
SPI.writeBytes(P10_MATRIX_send_buffer,48);
#endif
digitalWrite(_LATCH_PIN,HIGH);
//delayMicroseconds(10);
digitalWrite(_LATCH_PIN,LOW);
//delayMicroseconds(10);
digitalWrite(_OE_PIN,0);
delayMicroseconds(show_time);
digitalWrite(_OE_PIN,1);
}
_display_color++;
if (_display_color>=color_depth)
_display_color=0;
}
void P10_MATRIX::flushDisplay(void) {
// pinMode(13,OUTPUT);
// pinMode(14,OUTPUT);
// digitalWrite(13,0);
// digitalWrite(14,0);
#ifdef PATTERN4
for (int ii=0;ii<48;ii++)
SPI.write(0x00);
#endif
#ifdef PATTERN8
for (int ii=0;ii<24;ii++)
SPI.write(0x00);
#endif
#ifdef P5_PATTERN16
for (int ii=0;ii<48;ii++)
SPI.write(0x00);
#endif
}
void P10_MATRIX::displayTestPattern(uint16_t show_time) {
if ((millis()-_test_last_call)>100)
{
//digitalWrite(13,HIGH);
//digitalWrite(15,HIGH);
//digitalWrite(15,LOW);
SPI.write(0xFF);
_test_last_call=millis();
_test_pixel_counter++;
}
#ifdef PATTERN4
if (_test_pixel_counter>48)
#endif
#ifdef PATTERN8
if (_test_pixel_counter>24)
#endif
#ifdef P5_PATTERN16
if (_test_pixel_counter>48)
#endif
{
_test_pixel_counter=0;
_test_line_counter++;
flushDisplay();
}
if (_test_line_counter> (matrix_height/2))
_test_line_counter=0;
digitalWrite(_A_PIN,HIGH);
digitalWrite(_B_PIN,HIGH);
digitalWrite(_C_PIN,LOW);
digitalWrite(_D_PIN,HIGH);
digitalWrite(_A_PIN,LOW);
digitalWrite(_B_PIN,LOW);
digitalWrite(_C_PIN,LOW);
digitalWrite(_D_PIN,LOW);
// digitalWrite(_C_PIN,HIGH);
if (_test_line_counter & 0x01)
digitalWrite(_A_PIN,HIGH);
else
digitalWrite(_A_PIN,LOW);
if (_test_line_counter & 0x02)
digitalWrite(_B_PIN,HIGH);
else
digitalWrite(_B_PIN,LOW);
if (_test_line_counter & 0x04)
digitalWrite(_C_PIN,HIGH);
else
digitalWrite(_C_PIN,LOW);
#ifdef P5_PATTERN16
if (_test_line_counter & 0x08)
digitalWrite(_D_PIN,HIGH);
else
digitalWrite(_D_PIN,LOW);
#endif
digitalWrite(_LATCH_PIN,HIGH);
digitalWrite(_LATCH_PIN,LOW);
digitalWrite(_OE_PIN,0);
delayMicroseconds(show_time);
digitalWrite(_OE_PIN,1);
}
// clear everything
void P10_MATRIX::clearDisplay(void) {
for(int this_color=0;this_color<color_depth;this_color++)
for (int j=0;j<(buffer_size);j++)
P10_MATRIX_buffer[this_color][j]=0;
}