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
455
456
457
458
459
|
#include <iostream>
#include <string>
#include <list>
struct VirtualCoords
{
int16_t x;
int16_t y;
int16_t virt_row; // chain of panels row
int16_t virt_col; // chain of panels col
VirtualCoords() : x(0), y(0)
{
}
};
enum PANEL_SCAN_RATE
{
NORMAL_TWO_SCAN, NORMAL_ONE_SIXTEEN, // treated as the same
FOUR_SCAN_32PX_HIGH,
FOUR_SCAN_16PX_HIGH
};
// Chaining approach... From the perspective of the DISPLAY / LED side of the chain of panels.
enum PANEL_CHAIN_TYPE
{
CHAIN_TOP_LEFT_DOWN,
CHAIN_TOP_RIGHT_DOWN,
CHAIN_BOTTOM_LEFT_UP,
CHAIN_BOTTOM_RIGHT_UP,
CHAIN_TOP_RIGHT_DOWN_ZZ, /// ZigZag chaining. Might need a big ass cable to do this, all panels right way up.
CHAIN_BOTTOM_RIGHT_UP_ZZ
};
class VirtualMatrixPanelTest
{
public:
VirtualMatrixPanelTest(int _vmodule_rows, int _vmodule_cols, int _panelResX, int _panelResY, PANEL_CHAIN_TYPE _panel_chain_type = CHAIN_TOP_RIGHT_DOWN)
{
panelResX = _panelResX;
panelResY = _panelResY;
vmodule_rows = _vmodule_rows;
vmodule_cols = _vmodule_cols;
virtualResX = vmodule_cols * _panelResX;
virtualResY = vmodule_rows * _panelResY;
dmaResX = panelResX * vmodule_rows * vmodule_cols;
panel_chain_type = _panel_chain_type;
/* Virtual Display width() and height() will return a real-world value. For example:
* Virtual Display width: 128
* Virtual Display height: 64
*
* So, not values that at 0 to X-1
*/
coords.x = coords.y = -1; // By default use an invalid co-ordinates that will be rejected by updateMatrixDMABuffer
switch (panel_chain_type) {
case CHAIN_TOP_LEFT_DOWN:
chain_type_str = "CHAIN_TOP_LEFT_DOWN";
break;
case CHAIN_TOP_RIGHT_DOWN:
chain_type_str = "CHAIN_TOP_RIGHT_DOWN";
break;
case CHAIN_TOP_RIGHT_DOWN_ZZ:
chain_type_str = "CHAIN_TOP_RIGHT_DOWN_ZZ";
break;
case CHAIN_BOTTOM_RIGHT_UP:
chain_type_str = "CHAIN_BOTTOM_RIGHT_UP";
break;
case CHAIN_BOTTOM_LEFT_UP:
chain_type_str = "CHAIN_BOTTOM_LEFT_UP";
break;
default:
chain_type_str = "WTF!";
break;
}
std::cout << "\n\n***************************************************************************\n";
std::cout << "Chain type: " << chain_type_str << " ";
std::printf("Testing chain of panels of %d rows, %d columns, %d px by %d px resolution. \n\n", vmodule_rows, vmodule_cols, panelResX, panelResX, panelResY);
}
// equivalent methods of the matrix library so it can be just swapped out.
void drawPixel(int16_t x, int16_t y, int16_t expected_x, int16_t expected_y);
std::string chain_type_str = "UNKNOWN";
// Internal co-ord conversion function
VirtualCoords getCoords_Dev(int16_t x, int16_t y);
VirtualCoords getCoords_WorkingBaslineMarch2023(int16_t x, int16_t y);
VirtualCoords coords;
private:
int16_t virtualResX;
int16_t virtualResY;
int16_t vmodule_rows;
int16_t vmodule_cols;
int16_t panelResX;
int16_t panelResY;
int16_t dmaResX; // The width of the chain in pixels (as the DMA engine sees it)
PANEL_CHAIN_TYPE panel_chain_type;
PANEL_SCAN_RATE panel_scan_rate = NORMAL_TWO_SCAN;
bool _rotate = false;
}; // end Class header
#include "baseline.hpp"
/**
* Development version for testing.
*/
inline VirtualCoords VirtualMatrixPanelTest::getCoords_Dev(int16_t virt_x, int16_t virt_y)
{
coords.x = coords.y = -1; // By defalt use an invalid co-ordinates that will be rejected by updateMatrixDMABuffer
if (virt_x < 0 || virt_x >= virtualResX || virt_y < 0 || virt_y >= virtualResY)
{ // Co-ordinates go from 0 to X-1 remember! otherwise they are out of range!
return coords;
}
// Do we want to rotate?
if (_rotate)
{
int16_t temp_x = virt_x;
virt_x = virt_y;
virt_y = virtualResY - 1 - temp_x;
}
int row = (virt_y / panelResY); // 0 indexed
switch(panel_chain_type)
{
case (CHAIN_TOP_RIGHT_DOWN):
{
if ( (row % 2) == 1 )
{ // upside down panel
//Serial.printf("Condition 1, row %d ", row);
// refersed for the row
coords.x = dmaResX - virt_x - (row*virtualResX);
// y co-ord inverted within the panel
coords.y = panelResY - 1 - (virt_y % panelResY);
}
else
{
//Serial.printf("Condition 2, row %d ", row);
coords.x = ((vmodule_rows - (row+1))*virtualResX)+virt_x;
coords.y = virt_y % panelResY;
}
}
break;
case (CHAIN_TOP_LEFT_DOWN): // OK -> modulus opposite of CHAIN_TOP_RIGHT_DOWN
{
if ( (row % 2) == 0 )
{ // refersed panel
//Serial.printf("Condition 1, row %d ", row);
coords.x = dmaResX - virt_x - (row*virtualResX);
// y co-ord inverted within the panel
coords.y = panelResY - 1 - (virt_y % panelResY);
}
else
{
//Serial.printf("Condition 2, row %d ", row);
coords.x = ((vmodule_rows - (row+1))*virtualResX)+virt_x;
coords.y = virt_y % panelResY;
}
}
break;
case (CHAIN_BOTTOM_LEFT_UP): //
{
row = vmodule_rows - row - 1;
if ( (row % 2) == 1 )
{
// Serial.printf("Condition 1, row %d ", row);
coords.x = ((vmodule_rows - (row+1))*virtualResX)+virt_x;
coords.y = virt_y % panelResY;
}
else
{ // inverted panel
// Serial.printf("Condition 2, row %d ", row);
coords.x = dmaResX - (row*virtualResX) - virt_x;
coords.y = panelResY - 1 - (virt_y % panelResY);
}
}
break;
case (CHAIN_BOTTOM_RIGHT_UP): // OK -> modulus opposite of CHAIN_BOTTOM_LEFT_UP
{
row = vmodule_rows - row - 1;
if ( (row % 2) == 0 )
{ // right side up
// Serial.printf("Condition 1, row %d ", row);
// refersed for the row
coords.x = ((vmodule_rows - (row+1))*virtualResX)+virt_x;
coords.y = virt_y % panelResY;
}
else
{ // inverted panel
// Serial.printf("Condition 2, row %d ", row);
coords.x = dmaResX - (row*virtualResX) - virt_x;
coords.y = panelResY - 1 - (virt_y % panelResY);
}
}
break;
case CHAIN_TOP_RIGHT_DOWN_ZZ:
{
// Right side up. Starting from top left all the way down.
// Connected in a Zig Zag manner = some long ass cables being used potentially
//Serial.printf("Condition 2, row %d ", row);
coords.x = ((vmodule_rows - (row+1))*virtualResX)+virt_x;
coords.y = virt_y % panelResY;
}
case CHAIN_BOTTOM_RIGHT_UP_ZZ:
{
// Right side up. Starting from top left all the way down.
// Connected in a Zig Zag manner = some long ass cables being used potentially
//Serial.printf("Condition 2, row %d ", row);
coords.x = (row*virtualResX)+virt_x;
coords.y = virt_y % panelResY;
}
default:
return coords;
break;
} // end switch
/* START: Pixel remapping AGAIN to convert TWO parallel scanline output that the
* the underlying hardware library is designed for (because
* there's only 2 x RGB pins... and convert this to 1/4 or something
*/
if (panel_scan_rate == FOUR_SCAN_32PX_HIGH)
{
/* Convert Real World 'VirtualMatrixPanel' co-ordinates (i.e. Real World pixel you're looking at
on the panel or chain of panels, per the chaining configuration) to a 1/8 panels
double 'stretched' and 'squished' coordinates which is what needs to be sent from the
DMA buffer.
Note: Look at the FourScanPanel example code and you'll see that the DMA buffer is setup
as if the panel is 2 * W and 0.5 * H !
*/
if ((virt_y & 8) == 0)
{
coords.x += ((coords.x / panelResX) + 1) * panelResX; // 1st, 3rd 'block' of 8 rows of pixels, offset by panel width in DMA buffer
}
else
{
coords.x += (coords.x / panelResX) * panelResX; // 2nd, 4th 'block' of 8 rows of pixels, offset by panel width in DMA buffer
}
// http://cpp.sh/4ak5u
// Real number of DMA y rows is half reality
// coords.y = (y / 16)*8 + (y & 0b00000111);
coords.y = (virt_y >> 4) * 8 + (virt_y & 0b00000111);
}
else if (panel_scan_rate == FOUR_SCAN_16PX_HIGH)
{
if ((virt_y & 8) == 0)
{
coords.x += (panelResX >> 2) * (((coords.x & 0xFFF0) >> 4) + 1); // 1st, 3rd 'block' of 8 rows of pixels, offset by panel width in DMA buffer
}
else
{
coords.x += (panelResX >> 2) * (((coords.x & 0xFFF0) >> 4)); // 2nd, 4th 'block' of 8 rows of pixels, offset by panel width in DMA buffer
}
if (virt_y < 32)
coords.y = (virt_y >> 4) * 8 + (virt_y & 0b00000111);
else
{
coords.y = ((virt_y - 32) >> 4) * 8 + (virt_y & 0b00000111);
coords.x += 256;
}
}
return coords;
}
bool check(VirtualCoords expected, VirtualCoords result, int x = -1, int y = -1)
{
if ( result.x != expected.x || result.y != expected.y )
{
std::printf("Requested (%d, %d) -> expecting physical (%d, %d) got (%d, %d).", x, y, expected.x, expected.y, result.x, result.y);
std::cout << "\t *** FAIL ***\n ";
std::cout << "\n";
return false;
}
else
{
return true;
}
}
main(int argc, char* argv[])
{
std::cout << "Starting Testing...\n";
std::list <PANEL_CHAIN_TYPE> chain_t_test_list { CHAIN_TOP_LEFT_DOWN, CHAIN_TOP_RIGHT_DOWN, CHAIN_BOTTOM_LEFT_UP, CHAIN_BOTTOM_RIGHT_UP };
// Draw pixel at virtual position 70x, 70y =
// x, y x, y
// x == horizontal
// y = vert :-)
// 192 x 192 pixel virtual display
int rows = 3;
int cols = 3;
int panel_width_x = 64;
int panel_height_y = 64;
std::string panel_scan_type = "NORMAL_TWO_SCAN";
for (auto chain_t : chain_t_test_list) {
VirtualMatrixPanelTest test = VirtualMatrixPanelTest(rows,cols,panel_width_x,panel_height_y, chain_t);
int pass_counter = 0;
int fail_counter = 0;
for (int16_t x = 0; x < panel_width_x*cols; x++)
{
for (int16_t y = 0; y < panel_height_y*rows; y++)
{
VirtualCoords expected = test.getCoords_WorkingBaslineMarch2023(x,y);
VirtualCoords result = test.getCoords_Dev(x,y);
bool chk_result = check(expected, result, x, y);
if ( chk_result )
{
fail_counter++;
}
else
{
pass_counter++;
}
}
}
if ( fail_counter > 0) {
std::printf("ERROR: %d tests failed.\n", fail_counter);
} else{
std::printf("SUCCESS: %d coord tests passed.\n", pass_counter);
}
} // end chain type test list
std::cout << "Performing NON-SERPENTINE (ZIG ZAG) TEST";
rows = 3;
cols = 1;
panel_width_x = 64;
panel_height_y = 64;
VirtualMatrixPanelTest test = VirtualMatrixPanelTest(rows,cols,panel_width_x,panel_height_y, CHAIN_TOP_RIGHT_DOWN_ZZ);
// CHAIN_TOP_RIGHT_DOWN_ZZ test 1
// (x,y)
VirtualCoords result = test.getCoords_Dev(0,0);
VirtualCoords expected; expected.x = 64*2; expected.y = 0;
std::printf("Expected physical (%d, %d) got (%d, %d).\n", expected.x, expected.y, result.x, result.y);
// CHAIN_TOP_RIGHT_DOWN_ZZ test 2
result = test.getCoords_Dev(10,64*3-1);
expected.x = 10; expected.y = 63;
std::printf("Expected physical (%d, %d) got (%d, %d).\n", expected.x, expected.y, result.x, result.y);
// CHAIN_TOP_RIGHT_DOWN_ZZ test 3
result = test.getCoords_Dev(16,64*2-1);
expected.x = 80; expected.y = 63;
std::printf("Expected physical (%d, %d) got (%d, %d).\n", expected.x, expected.y, result.x, result.y);
// CHAIN_BOTTOM_RIGHT_UP_ZZ test 4
result = test.getCoords_Dev(0,0);
expected.x = 0; expected.y = 0;
std::printf("Expected physical (%d, %d) got (%d, %d).\n", expected.x, expected.y, result.x, result.y);
// CHAIN_BOTTOM_RIGHT_UP_ZZ test 4
result = test.getCoords_Dev(63,64);
expected.x = 64*2-1; expected.y = 0;
std::printf("Expected physical (%d, %d) got (%d, %d).\n", expected.x, expected.y, result.x, result.y);
std::cout << "\n\n";
return 0;
}
|