Merge pull request #307 from solhuebner/patch-4

Fix VirtualMatrixPanel scan rates
This commit is contained in:
mrfaptastic 2022-08-16 20:43:20 +01:00 committed by GitHub
commit b0f5c6063d
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" #include "ESP32-HUB75-MatrixPanel-I2S-DMA.h"
#ifndef NO_GFX #ifndef NO_GFX
#include <Fonts/FreeSansBold12pt7b.h> #include <Fonts/FreeSansBold12pt7b.h>
#endif #endif
struct VirtualCoords { struct VirtualCoords
{
int16_t x; int16_t x;
int16_t y; int16_t y;
int16_t virt_row; // chain of panels row int16_t virt_row; // chain of panels row
int16_t virt_col; // chain of panels col int16_t virt_col; // chain of panels col
VirtualCoords() : x(0), y(0) 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 #ifdef USE_GFX_ROOT
class VirtualMatrixPanel : public GFX class VirtualMatrixPanel : public GFX
@ -45,7 +50,7 @@ class VirtualMatrixPanel
#endif #endif
{ {
public: public:
int16_t virtualResX; int16_t virtualResX;
int16_t virtualResY; 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) 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 #ifdef USE_GFX_ROOT
: GFX(_vmodule_cols*_panelResX, _vmodule_rows*_panelResY) : GFX(_vmodule_cols * _panelResX, _vmodule_rows * _panelResY)
#elif !defined NO_GFX #elif !defined NO_GFX
: Adafruit_GFX(_vmodule_cols*_panelResX, _vmodule_rows*_panelResY) : Adafruit_GFX(_vmodule_cols * _panelResX, _vmodule_rows * _panelResY)
#endif #endif
{ {
this->display = &disp; this->display = &disp;
@ -74,13 +79,11 @@ class VirtualMatrixPanel
vmodule_rows = _vmodule_rows; vmodule_rows = _vmodule_rows;
vmodule_cols = _vmodule_cols; vmodule_cols = _vmodule_cols;
virtualResX = vmodule_cols*_panelResX; virtualResX = vmodule_cols * _panelResX;
virtualResY = vmodule_rows*_panelResY; virtualResY = vmodule_rows * _panelResY;
dmaResX = panelResX * vmodule_rows * vmodule_cols; dmaResX = panelResX * vmodule_rows * vmodule_cols;
/* Virtual Display width() and height() will return a real-world value. For example: /* Virtual Display width() and height() will return a real-world value. For example:
* Virtual Display width: 128 * Virtual Display width: 128
* Virtual Display height: 64 * Virtual Display height: 64
@ -89,23 +92,25 @@ class VirtualMatrixPanel
*/ */
_s_chain_party = serpentine_chain; // serpentine, or 'S' chain? _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 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. // 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 drawPixel(int16_t x, int16_t y, uint16_t color);
virtual void fillScreen(uint16_t color); // overwrite adafruit implementation 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 clearScreen() { display->clearScreen(); }
void drawPixelRGB888(int16_t x, int16_t y, uint8_t r, uint8_t g, uint8_t b); void drawPixelRGB888(int16_t x, int16_t y, uint8_t r, uint8_t g, uint8_t b);
#ifdef USE_GFX_ROOT #ifdef USE_GFX_ROOT
// 24bpp FASTLED CRGB colour struct support // 24bpp FASTLED CRGB colour struct support
void fillScreen(CRGB color); void fillScreen(CRGB color);
void drawPixel(int16_t x, int16_t y, CRGB color); void drawPixel(int16_t x, int16_t y, CRGB color);
#endif #endif
uint16_t color444(uint8_t r, uint8_t g, uint8_t b) { return display->color444(r, g, b); } 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 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); } 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); void setPhysicalPanelScanRate(PANEL_SCAN_RATE rate);
protected: protected:
virtual VirtualCoords getCoords(int16_t &x, int16_t &y); virtual VirtualCoords getCoords(int16_t &x, int16_t &y);
VirtualCoords coords; VirtualCoords coords;
@ -134,34 +138,37 @@ class VirtualMatrixPanel
* Updates the private class member variable 'coords', so no need to use the return value. * 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. * Not thread safe, but not a concern for ESP32 sketch anyway... I think.
*/ */
inline VirtualCoords VirtualMatrixPanel::getCoords(int16_t &x, int16_t &y) { inline VirtualCoords VirtualMatrixPanel::getCoords(int16_t &x, int16_t &y)
//Serial.println("Called Base."); {
// Serial.println("Called Base.");
coords.x = coords.y = -1; // By defalt use an invalid co-ordinates that will be rejected by updateMatrixDMABuffer coords.x = coords.y = -1; // By defalt use an invalid co-ordinates that will be rejected by updateMatrixDMABuffer
// We want to rotate? // Do we want to rotate?
if (_rotate){ if (_rotate)
int16_t temp_x=x; {
x=y; int16_t temp_x = x;
y=virtualResY-1-temp_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! if (x < 0 || x >= virtualResX || y < 0 || y >= virtualResY)
//Serial.printf("VirtualMatrixPanel::getCoords(): Invalid virtual display coordinate. x,y: %d, %d\r\n", x, y); { // 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; return coords;
} }
// Stupidity check // Stupidity check
if ( (vmodule_rows == 1) && (vmodule_cols == 1)) // single panel... if ((vmodule_rows == 1) && (vmodule_cols == 1)) // single panel...
{ {
coords.x = x; coords.x = x;
coords.y = y; coords.y = y;
return coords;
} }
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 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 // 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 else
{ {
// Normal chain pixel co-ordinate // Normal chain pixel co-ordinate
coords.x = x + ((y / panelResY) * (virtualResX)) ; coords.x = x + ((y / panelResY) * (virtualResX));
coords.y = y % panelResY; coords.y = y % panelResY;
} }
}
// Reverse co-ordinates if panel chain from ESP starts from the TOP RIGHT // Reverse co-ordinates if panel chain from ESP starts from the TOP RIGHT
if (_chain_top_down) 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.y = (_cfg.mx_height-1) - coords.y;
*/ */
coords.x = (dmaResX - 1) - coords.x; 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 /* START: Pixel remapping AGAIN to convert 1/16 SCAN output that the
* the underlying hardware library is designed for (because * the underlying hardware library is designed for (because
* there's only 2 x RGB pins... and convert this to 1/8 or something * 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 /* 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 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 // to
Serial.print("to ("); Serial.print(coords.x, DEC); Serial.print(","); Serial.print(coords.y, DEC); Serial.println(") "); Serial.print("to ("); Serial.print(coords.x, DEC); Serial.print(","); Serial.print(coords.y, DEC); Serial.println(") ");
*/ */
if ( (y & 8) == 0) { 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 {
coords.x += ((coords.x / panelResX) + 1) * panelResX; // 1st, 3rd 'block' of 8 rows of pixels, offset by panel width in DMA buffer
} }
else { else
coords.x += (coords.x / panelResX)*panelResX; // 2nd, 4th 'block' of 8 rows of pixels, offset by panel width in DMA buffer {
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 // http://cpp.sh/4ak5u
// Real number of DMA y rows is half reality // Real number of DMA y rows is half reality
// coords.y = (y / 16)*8 + (y & 0b00000111); // 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(") "); 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(") "); Serial.print("to ("); Serial.print(coords.x, DEC); Serial.print(","); Serial.print(coords.y, DEC); Serial.println(") ");
*/ */
} }
else if (_panelScanRate == ONE_EIGHT_16)
if ( _panelScanRate == ONE_EIGHT_16)
{ {
if ((y & 8) == 0) { 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) + 1); // 1st, 3rd 'block' of 8 rows of pixels, offset by panel width in DMA buffer
coords.x += (panelResX>>2) * (((coords.x & 0xFFF0)>>4)); // 2nd, 4th '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) if (y < 32)
coords.y = (y >> 4) * 8 + (y & 0b00000111); coords.y = (y >> 4) * 8 + (y & 0b00000111);
else{ else
coords.y = ((y-32) >> 4) * 8 + (y & 0b00000111); {
coords.y = ((y - 32) >> 4) * 8 + (y & 0b00000111);
coords.x += 256; 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; 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); getCoords(x, y);
this->display->drawPixel(coords.x, coords.y, color); 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); 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); 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); 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 #ifdef USE_GFX_ROOT
@ -284,21 +298,28 @@ inline void VirtualMatrixPanel::fillScreen(CRGB color)
} }
#endif #endif
inline void VirtualMatrixPanel::setRotate(bool rotate) { inline void VirtualMatrixPanel::setRotate(bool rotate)
_rotate=rotate; {
_rotate = rotate;
#ifndef NO_GFX #ifndef NO_GFX
// We don't support rotation by degrees. // We don't support rotation by degrees.
if (rotate) { setRotation(1); } else { setRotation(0); } if (rotate)
{
setRotation(1);
}
else
{
setRotation(0);
}
#endif #endif
} }
inline void VirtualMatrixPanel::setPhysicalPanelScanRate(PANEL_SCAN_RATE rate) { inline void VirtualMatrixPanel::setPhysicalPanelScanRate(PANEL_SCAN_RATE rate)
_panelScanRate=rate; {
_panelScanRate = rate;
} }
#ifndef NO_GFX #ifndef NO_GFX
inline void VirtualMatrixPanel::drawDisplayTest() inline void VirtualMatrixPanel::drawDisplayTest()
{ {
@ -306,15 +327,16 @@ inline void VirtualMatrixPanel::drawDisplayTest()
this->display->setTextColor(this->display->color565(255, 255, 0)); this->display->setTextColor(this->display->color565(255, 255, 0));
this->display->setTextSize(1); this->display->setTextSize(1);
for ( int panel = 0; panel < 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)); int top_left_x = (panel == 0) ? 0 : (panel * panelResX);
this->display->setCursor(panel*panelResX, panelResY-3); this->display->drawRect(top_left_x, 0, panelResX, panelResY, this->display->color565(0, 255, 0));
this->display->print((vmodule_cols*vmodule_rows)-panel); this->display->setCursor(panel * panelResX, panelResY - 3);
this->display->print((vmodule_cols * vmodule_rows) - panel);
} }
} }
#endif #endif
/* /*
// need to recreate this one, as it wouldn't work to just map where it starts. // 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) { inline void VirtualMatrixPanel::drawIcon (int *ico, int16_t x, int16_t y, int16_t icon_cols, int16_t icon_rows) {