aboutsummaryrefslogtreecommitdiff
path: root/examples/PIO_TestPatterns/src/main.cpp
blob: 803435f7e89a6c25730f00d8b6eadb1800456622 (plain)
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
// How to use this library with a FM6126 panel, thanks goes to:
// https://github.com/hzeller/rpi-rgb-led-matrix/issues/746

#ifdef IDF_BUILD
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <driver/gpio.h>
#include "sdkconfig.h"
#endif

#include <Arduino.h>
#include "xtensa/core-macros.h"
#ifdef VIRTUAL_PANE
#include <ESP32-VirtualMatrixPanel-I2S-DMA.h>
#else
#include <ESP32-HUB75-MatrixPanel-I2S-DMA.h>
#endif
#include "main.h"

#define R1_PIN   1
#define G1_PIN  40
#define B1_PIN   2
#define R2_PIN   4
#define G2_PIN  36
#define B2_PIN   6
#define A_PIN    8
#define B_PIN   21
#define C_PIN   10
#define D_PIN   17
#define E_PIN   34 // required for 1/32 scan panels, like 64x64px. Any available pin would do, i.e. IO32
#define LAT_PIN 15
#define OE_PIN  14
#define CLK_PIN 13

HUB75_I2S_CFG::i2s_pins _pins={R1_PIN, G1_PIN, B1_PIN, R2_PIN, G2_PIN, B2_PIN, A_PIN, B_PIN, C_PIN, D_PIN, E_PIN, LAT_PIN, OE_PIN, CLK_PIN};

// Configure for your panel(s) as appropriate!
#define E_PIN 34
#define PANEL_WIDTH 64
#define PANEL_HEIGHT 64         // Panel height of 64 will required E_PIN to be defined.

#ifdef VIRTUAL_PANE
 #define PANELS_NUMBER 1         // Number of chained panels, if just a single panel, obviously set to 1
#else
 #define PANELS_NUMBER 1         // Number of chained panels, if just a single panel, obviously set to 1
#endif

#define PANE_WIDTH PANEL_WIDTH * PANELS_NUMBER
#define PANE_HEIGHT PANEL_HEIGHT
#define NUM_LEDS PANE_WIDTH*PANE_HEIGHT

#ifdef VIRTUAL_PANE
 #define NUM_ROWS 1 // Number of rows of chained INDIVIDUAL PANELS
 #define NUM_COLS 1 // Number of INDIVIDUAL PANELS per ROW
 #define PANEL_CHAIN NUM_ROWS*NUM_COLS    // total number of panels chained one to another
 // Change this to your needs, for details on VirtualPanel pls read the PDF!
 #define SERPENT false
 #define TOPDOWN true
#endif


#ifdef VIRTUAL_PANE
VirtualMatrixPanel *matrix = nullptr;
MatrixPanel_I2S_DMA *chain = nullptr;
#else
MatrixPanel_I2S_DMA *matrix = nullptr;
#endif
// patten change delay
#define PATTERN_DELAY 2000

uint16_t time_counter = 0, cycles = 0, fps = 0;
unsigned long fps_timer;

// gradient buffer
CRGB *ledbuff;
//

unsigned long t1, t2, s1=0, s2=0, s3=0;
uint32_t ccount1, ccount2;

uint8_t color1 = 0, color2 = 0, color3 = 0;
uint16_t x,y;

const char *str = "* ESP32 I2S DMA *";

void setup(){

  Serial.begin(BAUD_RATE);
  Serial.println("Starting pattern test...");

  // redefine pins if required
  //HUB75_I2S_CFG::i2s_pins _pins={R1, G1, BL1, R2, G2, BL2, CH_A, CH_B, CH_C, CH_D, CH_E, LAT, OE, CLK};
  HUB75_I2S_CFG mxconfig(PANEL_WIDTH, PANEL_HEIGHT, PANELS_NUMBER, _pins);

  mxconfig.gpio.e = E_PIN;
  mxconfig.driver = HUB75_I2S_CFG::MBI5124;   // for panels using FM6126A chips
  mxconfig.i2sspeed = HUB75_I2S_CFG::HZ_8M;

#ifndef VIRTUAL_PANE
  matrix = new MatrixPanel_I2S_DMA(mxconfig);
  matrix->begin();
  matrix->setBrightness8(128);
#else
  chain = new MatrixPanel_I2S_DMA(mxconfig);
  chain->begin();
  chain->setBrightness8(128);
  // create VirtualDisplay object based on our newly created dma_display object
  matrix = new VirtualMatrixPanel((*chain), NUM_ROWS, NUM_COLS, PANEL_WIDTH, PANEL_HEIGHT, CHAIN_TOP_LEFT_DOWN);
#endif

  ledbuff = (CRGB *)malloc(NUM_LEDS * sizeof(CRGB));  // allocate buffer for some tests
  buffclear(ledbuff);
}

uint8_t wheelval = 0;
void loop(){

  Serial.printf("Cycle: %d\n", ++cycles);

#ifndef NO_GFX
  drawText(wheelval++);
#endif

  Serial.print("Estimating clearScreen() - ");
  ccount1 = XTHAL_GET_CCOUNT();
  matrix->clearScreen();
  ccount1 = XTHAL_GET_CCOUNT() - ccount1;
  Serial.printf("%d ticks\n", ccount1);
  delay(PATTERN_DELAY);


// Power supply tester
// slowly fills matrix with white, stressing PSU
  for (int y=0; y!=PANE_HEIGHT; ++y){
    for (int x=0; x!=PANE_WIDTH; ++x){
      matrix->drawPixelRGB888(x, y, 255,255,255);
      //matrix->drawPixelRGB888(x, y-1, 255,0,0);       // pls, be gentle :)
      delay(2);
    }
  }
  delay(1000);


#ifndef VIRTUAL_PANE
  // simple solid colors
  Serial.println("Fill screen: RED");
  matrix->fillScreenRGB888(255, 0, 0);
  delay(PATTERN_DELAY);
  Serial.println("Fill screen: GREEN");
  matrix->fillScreenRGB888(0, 255, 0);
  delay(PATTERN_DELAY);
  Serial.println("Fill screen: BLUE");
  matrix->fillScreenRGB888(0, 0, 255);
  delay(PATTERN_DELAY);
#endif

  for (uint8_t i=5; i; --i){
    Serial.print("Estimating single drawPixelRGB888(r, g, b) ticks: ");
    color1 = random8();
    ccount1 = XTHAL_GET_CCOUNT();
    matrix->drawPixelRGB888(i,i, color1, color1, color1);
    ccount1 = XTHAL_GET_CCOUNT() - ccount1;
    Serial.printf("%d ticks\n", ccount1);
  }

// Clearing CRGB ledbuff
  Serial.print("Estimating ledbuff clear time: ");
  t1 = micros();
  ccount1 = XTHAL_GET_CCOUNT();
  buffclear(ledbuff);
  ccount1 = XTHAL_GET_CCOUNT() - ccount1;
  t2 = micros()-t1;
  Serial.printf("%lu us, %u ticks\n\n", t2, ccount1);

#ifndef VIRTUAL_PANE
  // Bare fillscreen(r, g, b)
  Serial.print("Estimating fillscreenRGB888(r, g, b) time: ");
  t1 = micros();
  ccount1 = XTHAL_GET_CCOUNT();
  matrix->fillScreenRGB888(64, 64, 64);   // white
  ccount2 = XTHAL_GET_CCOUNT() - ccount1;
  t2 = micros()-t1;
  s1+=t2;
  Serial.printf("%lu us, avg: %lu, ccnt: %d\n", t2, s1/cycles, ccount2);
  delay(PATTERN_DELAY);
#endif

  Serial.print("Estimating full-screen fillrate with looped drawPixelRGB888(): ");
  y = PANE_HEIGHT;
  t1 = micros();
  ccount1 = XTHAL_GET_CCOUNT();
  do {
    --y;
    uint16_t x = PANE_WIDTH;
    do {
      --x;
        matrix->drawPixelRGB888( x, y, 0, 0, 0);
    } while(x);
  } while(y);
  ccount1 = XTHAL_GET_CCOUNT() - ccount1;
  t2 = micros()-t1;
  Serial.printf("%lu us, %u ticks\n", t2, ccount1);



// created random color gradient in ledbuff
  uint8_t color1 = 0;
  uint8_t color2 = random8();
  uint8_t color3 = 0;

  for (uint16_t i = 0; i<NUM_LEDS; ++i){
    ledbuff[i].r=color1++;
    ledbuff[i].g=color2;
    if (i%PANE_WIDTH==0)
      color3+=255/PANE_HEIGHT;

    ledbuff[i].b=color3;
  }
//

//
  Serial.print("Estimating ledbuff-to-matrix fillrate with drawPixelRGB888(), time: ");
  t1 = micros();
  ccount1 = XTHAL_GET_CCOUNT();
  mxfill(ledbuff);
  ccount1 = XTHAL_GET_CCOUNT() - ccount1;
  t2 = micros()-t1;
  s2+=t2;
  Serial.printf("%lu us, avg: %lu, %d ticks:\n", t2, s2/cycles, ccount1);
  delay(PATTERN_DELAY);
//

#ifndef NO_FAST_FUNCTIONS
  // Fillrate for fillRect() function
  Serial.print("Estimating fullscreen fillrate with fillRect() time: ");
  t1 = micros();
  matrix->fillRect(0, 0, PANE_WIDTH, PANE_HEIGHT, 0, 224, 0);
  t2 = micros()-t1;
  Serial.printf("%lu us\n", t2);
  delay(PATTERN_DELAY);


  Serial.print("Chessboard with fillRect(): ");  // шахматка
  matrix->fillScreen(0);
  x =0, y = 0;
  color1 = random8();
  color2 = random8();
  color3 = random8();
  bool toggle=0;
  t1 = micros();
  do {
    do{
      matrix->fillRect(x, y, 8, 8, color1, color2, color3);
      x+=16;
    }while(x < PANE_WIDTH);
    y+=8;
    toggle = !toggle;
    x = toggle ? 8 : 0;
  }while(y < PANE_HEIGHT);
  t2 = micros()-t1;
  Serial.printf("%lu us\n", t2);
  delay(PATTERN_DELAY);
#endif

// ======== V-Lines ==========
  Serial.println("Estimating V-lines with drawPixelRGB888(): ");  //
  matrix->fillScreen(0);
  color1 = random8();
  color2 = random8();
  x = y = 0;
  t1 = micros();
  ccount1 = XTHAL_GET_CCOUNT();
  do {
    y=0;
    do{
      matrix->drawPixelRGB888(x, y, color1, color2, color3);
    } while(++y != PANE_HEIGHT);
    x+=2;
  } while(x != PANE_WIDTH);
  ccount1 = XTHAL_GET_CCOUNT() - ccount1;
  t2 = micros()-t1;
  Serial.printf("%lu us, %u ticks\n", t2, ccount1);
  delay(PATTERN_DELAY);

#ifndef NO_FAST_FUNCTIONS
  Serial.println("Estimating V-lines with vlineDMA(): ");  //
  matrix->fillScreen(0);
  color2 = random8();
  x = y = 0;
  t1 = micros();
  ccount1 = XTHAL_GET_CCOUNT();
  do {
    matrix->drawFastVLine(x, y, PANE_HEIGHT, color1, color2, color3);
    x+=2;
  } while(x != PANE_WIDTH);
  ccount1 = XTHAL_GET_CCOUNT() - ccount1;
  t2 = micros()-t1;
  Serial.printf("%lu us, %u ticks\n", t2, ccount1);
  delay(PATTERN_DELAY);

  Serial.println("Estimating V-lines with fillRect(): ");  //
  matrix->fillScreen(0);
  color1 = random8();
  color2 = random8();
  x = y = 0;
  t1 = micros();
  ccount1 = XTHAL_GET_CCOUNT();
  do {
    matrix->fillRect(x, y, 1, PANE_HEIGHT, color1, color2, color3);
    x+=2;
  } while(x != PANE_WIDTH);
  ccount1 = XTHAL_GET_CCOUNT() - ccount1;
  t2 = micros()-t1;
  Serial.printf("%lu us, %u ticks\n", t2, ccount1);
  delay(PATTERN_DELAY);
#endif



// ======== H-Lines ==========
  Serial.println("Estimating H-lines with drawPixelRGB888(): ");  //
  matrix->fillScreen(0);
  color2 = random8();
  x = y = 0;
  t1 = micros();
  ccount1 = XTHAL_GET_CCOUNT();
  do {
    x=0;
    do{
      matrix->drawPixelRGB888(x, y, color1, color2, color3);
    } while(++x != PANE_WIDTH);
    y+=2;
  } while(y != PANE_HEIGHT);
  ccount1 = XTHAL_GET_CCOUNT() - ccount1;
  t2 = micros()-t1;
  Serial.printf("%lu us, %u ticks\n", t2, ccount1);
  delay(PATTERN_DELAY);

#ifndef NO_FAST_FUNCTIONS
  Serial.println("Estimating H-lines with hlineDMA(): ");
  matrix->fillScreen(0);
  color2 = random8();
  color3 = random8();
  x = y = 0;
  t1 = micros();
  ccount1 = XTHAL_GET_CCOUNT();
  do {
    matrix->drawFastHLine(x, y, PANE_WIDTH, color1, color2, color3);
    y+=2;
  } while(y != PANE_HEIGHT);
  ccount1 = XTHAL_GET_CCOUNT() - ccount1;
  t2 = micros()-t1;
  Serial.printf("%lu us, %u ticks\n", t2, ccount1);
  delay(PATTERN_DELAY);

  Serial.println("Estimating H-lines with fillRect(): ");  //
  matrix->fillScreen(0);
  color2 = random8();
  color3 = random8();
  x = y = 0;
  t1 = micros();
  ccount1 = XTHAL_GET_CCOUNT();
  do {
    matrix->fillRect(x, y, PANE_WIDTH, 1, color1, color2, color3);
    y+=2;
  } while(y != PANE_HEIGHT);
  ccount1 = XTHAL_GET_CCOUNT() - ccount1;
  t2 = micros()-t1;
  Serial.printf("%lu us, %u ticks\n", t2, ccount1);
  delay(PATTERN_DELAY);
#endif




  Serial.println("\n====\n");

  // take a rest for a while
  delay(1000);
}


void buffclear(CRGB *buf){
  memset(buf, 0x00, NUM_LEDS * sizeof(CRGB)); // flush buffer to black  
}

void IRAM_ATTR mxfill(CRGB *leds){
  uint16_t y = PANE_HEIGHT;
  do {
    --y;
    uint16_t x = PANE_WIDTH;
    do {
      --x;
        uint16_t _pixel = y * PANE_WIDTH + x;
        matrix->drawPixelRGB888( x, y, leds[_pixel].r, leds[_pixel].g, leds[_pixel].b);
    } while(x);
  } while(y);
}
//

/**
 *  The one for 256+ matrices
 *  otherwise this:
 *    for (uint8_t i = 0; i < MATRIX_WIDTH; i++) {}
 *  turns into an infinite loop
 */
uint16_t XY16( uint16_t x, uint16_t y)
{ 
  if (x<PANE_WIDTH && y < PANE_HEIGHT){
    return (y * PANE_WIDTH) + x;
  } else {
    return 0;
  }
}

#ifdef NO_GFX
void drawText(int colorWheelOffset){}
#else
void drawText(int colorWheelOffset){
  // draw some text
  matrix->setTextSize(1);     // size 1 == 8 pixels high
  matrix->setTextWrap(false); // Don't wrap at end of line - will do ourselves

  matrix->setCursor(5, 5);    // start at top left, with 5,5 pixel of spacing
  uint8_t w = 0;

  for (w=0; w<strlen(str); w++) {
    matrix->setTextColor(colorWheel((w*32)+colorWheelOffset));
    matrix->print(str[w]);
  }
}
#endif

uint16_t colorWheel(uint8_t pos) {
  if(pos < 85) {
    return matrix->color565(pos * 3, 255 - pos * 3, 0);
  } else if(pos < 170) {
    pos -= 85;
    return matrix->color565(255 - pos * 3, 0, pos * 3);
  } else {
    pos -= 170;
    return matrix->color565(0, pos * 3, 255 - pos * 3);
  }
}