Fix VirtualMatrixPanel scan rates

New PR to include changes made by #305 

Fixes #286 and related single display issues

I tried to avoid "fixing" the formatting but it was all over the place so I had to ;)

Would be great if we could get some QC tests, especially for single display VirtualMatrixPanel usage.
This commit is contained in:
Sol Huebner 2022-08-07 23:51:41 +02:00 committed by GitHub
parent 00307a8fbd
commit 4cb2de0921
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -19,22 +19,27 @@
#include "ESP32-HUB75-MatrixPanel-I2S-DMA.h"
#ifndef NO_GFX
#include <Fonts/FreeSansBold12pt7b.h>
#include <Fonts/FreeSansBold12pt7b.h>
#endif
struct VirtualCoords {
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_ONE_SIXTEEN, ONE_EIGHT_32, ONE_EIGHT_16};
enum PANEL_SCAN_RATE
{
NORMAL_ONE_SIXTEEN,
ONE_EIGHT_32,
ONE_EIGHT_16
};
#ifdef USE_GFX_ROOT
class VirtualMatrixPanel : public GFX
@ -45,7 +50,7 @@ class VirtualMatrixPanel
#endif
{
public:
public:
int16_t virtualResX;
int16_t virtualResY;
@ -61,9 +66,9 @@ class VirtualMatrixPanel
VirtualMatrixPanel(MatrixPanel_I2S_DMA &disp, int _vmodule_rows, int _vmodule_cols, int _panelResX, int _panelResY, bool serpentine_chain = true, bool top_down_chain = false)
#ifdef USE_GFX_ROOT
: GFX(_vmodule_cols*_panelResX, _vmodule_rows*_panelResY)
: GFX(_vmodule_cols * _panelResX, _vmodule_rows * _panelResY)
#elif !defined NO_GFX
: Adafruit_GFX(_vmodule_cols*_panelResX, _vmodule_rows*_panelResY)
: Adafruit_GFX(_vmodule_cols * _panelResX, _vmodule_rows * _panelResY)
#endif
{
this->display = &disp;
@ -74,13 +79,11 @@ class VirtualMatrixPanel
vmodule_rows = _vmodule_rows;
vmodule_cols = _vmodule_cols;
virtualResX = vmodule_cols*_panelResX;
virtualResY = vmodule_rows*_panelResY;
virtualResX = vmodule_cols * _panelResX;
virtualResY = vmodule_rows * _panelResY;
dmaResX = panelResX * vmodule_rows * vmodule_cols;
/* Virtual Display width() and height() will return a real-world value. For example:
* Virtual Display width: 128
* Virtual Display height: 64
@ -89,23 +92,25 @@ class VirtualMatrixPanel
*/
_s_chain_party = serpentine_chain; // serpentine, or 'S' chain?
_chain_top_down= top_down_chain;
_chain_top_down = top_down_chain;
coords.x = coords.y = -1; // By default use an invalid co-ordinates that will be rejected by updateMatrixDMABuffer
}
// equivalent methods of the matrix library so it can be just swapped out.
virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
virtual void fillScreen(uint16_t color); // overwrite adafruit implementation
virtual void fillScreenRGB888(uint8_t r, uint8_t g,uint8_t b);
virtual void fillScreenRGB888(uint8_t r, uint8_t g, uint8_t b);
void clearScreen() { display->clearScreen(); }
void drawPixelRGB888(int16_t x, int16_t y, uint8_t r, uint8_t g, uint8_t b);
#ifdef USE_GFX_ROOT
// 24bpp FASTLED CRGB colour struct support
void fillScreen(CRGB color);
void drawPixel(int16_t x, int16_t y, CRGB color);
#endif
uint16_t color444(uint8_t r, uint8_t g, uint8_t b) { return display->color444(r, g, b); }
uint16_t color565(uint8_t r, uint8_t g, uint8_t b) { return display->color565(r, g, b); }
uint16_t color333(uint8_t r, uint8_t g, uint8_t b) { return display->color333(r, g, b); }
@ -116,8 +121,7 @@ class VirtualMatrixPanel
void setPhysicalPanelScanRate(PANEL_SCAN_RATE rate);
protected:
protected:
virtual VirtualCoords getCoords(int16_t &x, int16_t &y);
VirtualCoords coords;
@ -134,34 +138,37 @@ class VirtualMatrixPanel
* Updates the private class member variable 'coords', so no need to use the return value.
* Not thread safe, but not a concern for ESP32 sketch anyway... I think.
*/
inline VirtualCoords VirtualMatrixPanel::getCoords(int16_t &x, int16_t &y) {
//Serial.println("Called Base.");
inline VirtualCoords VirtualMatrixPanel::getCoords(int16_t &x, int16_t &y)
{
// Serial.println("Called Base.");
coords.x = coords.y = -1; // By defalt use an invalid co-ordinates that will be rejected by updateMatrixDMABuffer
// We want to rotate?
if (_rotate){
int16_t temp_x=x;
x=y;
y=virtualResY-1-temp_x;
// Do we want to rotate?
if (_rotate)
{
int16_t temp_x = x;
x = y;
y = virtualResY - 1 - temp_x;
}
if ( x < 0 || x >= virtualResX || y < 0 || y >= virtualResY ) { // Co-ordinates go from 0 to X-1 remember! otherwise they are out of range!
//Serial.printf("VirtualMatrixPanel::getCoords(): Invalid virtual display coordinate. x,y: %d, %d\r\n", x, y);
if (x < 0 || x >= virtualResX || y < 0 || y >= virtualResY)
{ // Co-ordinates go from 0 to X-1 remember! otherwise they are out of range!
// Serial.printf("VirtualMatrixPanel::getCoords(): Invalid virtual display coordinate. x,y: %d, %d\r\n", x, y);
return coords;
}
// Stupidity check
if ( (vmodule_rows == 1) && (vmodule_cols == 1)) // single panel...
if ((vmodule_rows == 1) && (vmodule_cols == 1)) // single panel...
{
coords.x = x;
coords.y = y;
return coords;
}
uint8_t row = (y / panelResY) + 1; //a non indexed 0 row number
if( ( _s_chain_party && !_chain_top_down && (row % 2 == 0) ) // serpentine vertically stacked chain starting from bottom row (i.e. ESP closest to ground), upwards
else
{
uint8_t row = (y / panelResY) + 1; // a non indexed 0 row number
if ((_s_chain_party && !_chain_top_down && (row % 2 == 0)) // serpentine vertically stacked chain starting from bottom row (i.e. ESP closest to ground), upwards
||
( _s_chain_party && _chain_top_down && (row % 2 != 0) ) // serpentine vertically stacked chain starting from the sky downwards
(_s_chain_party && _chain_top_down && (row % 2 != 0)) // serpentine vertically stacked chain starting from the sky downwards
)
{
// First portion gets you to the correct offset for the row you need
@ -174,9 +181,10 @@ inline VirtualCoords VirtualMatrixPanel::getCoords(int16_t &x, int16_t &y) {
else
{
// Normal chain pixel co-ordinate
coords.x = x + ((y / panelResY) * (virtualResX)) ;
coords.x = x + ((y / panelResY) * (virtualResX));
coords.y = y % panelResY;
}
}
// Reverse co-ordinates if panel chain from ESP starts from the TOP RIGHT
if (_chain_top_down)
@ -187,15 +195,14 @@ inline VirtualCoords VirtualMatrixPanel::getCoords(int16_t &x, int16_t &y) {
coords.y = (_cfg.mx_height-1) - coords.y;
*/
coords.x = (dmaResX - 1) - coords.x;
coords.y = (panelResY-1) - coords.y;
coords.y = (panelResY - 1) - coords.y;
}
/* START: Pixel remapping AGAIN to convert 1/16 SCAN output that the
* the underlying hardware library is designed for (because
* there's only 2 x RGB pins... and convert this to 1/8 or something
*/
if ( _panelScanRate == ONE_EIGHT_32)
if (_panelScanRate == ONE_EIGHT_32)
{
/* 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
@ -211,17 +218,19 @@ inline VirtualCoords VirtualMatrixPanel::getCoords(int16_t &x, int16_t &y) {
// to
Serial.print("to ("); Serial.print(coords.x, DEC); Serial.print(","); Serial.print(coords.y, DEC); Serial.println(") ");
*/
if ( (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
if ((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
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 = (y >> 4)*8 + (y & 0b00000111);
coords.y = (y >> 4) * 8 + (y & 0b00000111);
/*
Serial.print("OneEightScanPanel Mapping ("); Serial.print(x, DEC); Serial.print(","); Serial.print(y, DEC); Serial.print(") ");
@ -229,45 +238,50 @@ inline VirtualCoords VirtualMatrixPanel::getCoords(int16_t &x, int16_t &y) {
Serial.print("to ("); Serial.print(coords.x, DEC); Serial.print(","); Serial.print(coords.y, DEC); Serial.println(") ");
*/
}
if ( _panelScanRate == ONE_EIGHT_16)
else if (_panelScanRate == ONE_EIGHT_16)
{
if ((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 ((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 (y < 32)
coords.y = (y >> 4) * 8 + (y & 0b00000111);
else{
coords.y = ((y-32) >> 4) * 8 + (y & 0b00000111);
else
{
coords.y = ((y - 32) >> 4) * 8 + (y & 0b00000111);
coords.x += 256;
}
}
//Serial.print("Mapping to x: "); Serial.print(coords.x, DEC); Serial.print(", y: "); Serial.println(coords.y, DEC);
// Serial.print("Mapping to x: "); Serial.print(coords.x, DEC); Serial.print(", y: "); Serial.println(coords.y, DEC);
return coords;
}
inline void VirtualMatrixPanel::drawPixel(int16_t x, int16_t y, uint16_t color) { // adafruit virtual void override
inline void VirtualMatrixPanel::drawPixel(int16_t x, int16_t y, uint16_t color)
{ // adafruit virtual void override
getCoords(x, y);
this->display->drawPixel(coords.x, coords.y, color);
}
inline void VirtualMatrixPanel::fillScreen(uint16_t color) { // adafruit virtual void override
inline void VirtualMatrixPanel::fillScreen(uint16_t color)
{ // adafruit virtual void override
this->display->fillScreen(color);
}
inline void VirtualMatrixPanel::fillScreenRGB888(uint8_t r, uint8_t g,uint8_t b) {
inline void VirtualMatrixPanel::fillScreenRGB888(uint8_t r, uint8_t g, uint8_t b)
{
this->display->fillScreenRGB888(r, g, b);
}
inline void VirtualMatrixPanel::drawPixelRGB888(int16_t x, int16_t y, uint8_t r, uint8_t g, uint8_t b) {
inline void VirtualMatrixPanel::drawPixelRGB888(int16_t x, int16_t y, uint8_t r, uint8_t g, uint8_t b)
{
getCoords(x, y);
this->display->drawPixelRGB888( coords.x, coords.y, r, g, b);
this->display->drawPixelRGB888(coords.x, coords.y, r, g, b);
}
#ifdef USE_GFX_ROOT
@ -284,21 +298,28 @@ inline void VirtualMatrixPanel::fillScreen(CRGB color)
}
#endif
inline void VirtualMatrixPanel::setRotate(bool rotate) {
_rotate=rotate;
inline void VirtualMatrixPanel::setRotate(bool rotate)
{
_rotate = rotate;
#ifndef NO_GFX
// We don't support rotation by degrees.
if (rotate) { setRotation(1); } else { setRotation(0); }
if (rotate)
{
setRotation(1);
}
else
{
setRotation(0);
}
#endif
}
inline void VirtualMatrixPanel::setPhysicalPanelScanRate(PANEL_SCAN_RATE rate) {
_panelScanRate=rate;
inline void VirtualMatrixPanel::setPhysicalPanelScanRate(PANEL_SCAN_RATE rate)
{
_panelScanRate = rate;
}
#ifndef NO_GFX
inline void VirtualMatrixPanel::drawDisplayTest()
{
@ -306,15 +327,16 @@ inline void VirtualMatrixPanel::drawDisplayTest()
this->display->setTextColor(this->display->color565(255, 255, 0));
this->display->setTextSize(1);
for ( int panel = 0; panel < vmodule_cols*vmodule_rows; panel++ ) {
int top_left_x = (panel == 0) ? 0:(panel*panelResX);
this->display->drawRect( top_left_x, 0, panelResX, panelResY, this->display->color565( 0, 255, 0));
this->display->setCursor(panel*panelResX, panelResY-3);
this->display->print((vmodule_cols*vmodule_rows)-panel);
for (int panel = 0; panel < vmodule_cols * vmodule_rows; panel++)
{
int top_left_x = (panel == 0) ? 0 : (panel * panelResX);
this->display->drawRect(top_left_x, 0, panelResX, panelResY, this->display->color565(0, 255, 0));
this->display->setCursor(panel * panelResX, panelResY - 3);
this->display->print((vmodule_cols * vmodule_rows) - panel);
}
}
#endif
/*
// need to recreate this one, as it wouldn't work to just map where it starts.
inline void VirtualMatrixPanel::drawIcon (int *ico, int16_t x, int16_t y, int16_t icon_cols, int16_t icon_rows) {