2020-08-02 23:03:16 +02:00
|
|
|
#ifndef _ESP32_VIRTUAL_MATRIX_PANEL_I2S_DMA
|
|
|
|
#define _ESP32_VIRTUAL_MATRIX_PANEL_I2S_DMA
|
|
|
|
|
|
|
|
/*******************************************************************
|
2020-08-09 20:13:13 +02:00
|
|
|
Class contributed by Brian Lough, and expanded by Faptastic.
|
2021-12-20 21:58:55 +01:00
|
|
|
|
|
|
|
Originally designed to allow CHAINING of panels together to create
|
|
|
|
a 'bigger' display of panels. i.e. Chaining 4 panels into a 2x2
|
|
|
|
grid.
|
|
|
|
|
|
|
|
However, the function of this class has expanded now to also manage
|
|
|
|
the output for 1/16 scan panels, as the core DMA library is designed
|
|
|
|
ONLY FOR 1/16 scan matrix panels.
|
2020-08-09 20:13:13 +02:00
|
|
|
|
2020-08-02 23:03:16 +02:00
|
|
|
YouTube: https://www.youtube.com/brianlough
|
|
|
|
Tindie: https://www.tindie.com/stores/brianlough/
|
|
|
|
Twitter: https://twitter.com/witnessmenow
|
|
|
|
*******************************************************************/
|
|
|
|
|
2020-11-28 09:39:35 +01:00
|
|
|
#include "ESP32-HUB75-MatrixPanel-I2S-DMA.h"
|
2021-02-10 16:49:19 +01:00
|
|
|
#ifndef NO_GFX
|
|
|
|
#include <Fonts/FreeSansBold12pt7b.h>
|
|
|
|
#endif
|
2020-08-02 23:03:16 +02:00
|
|
|
|
|
|
|
struct VirtualCoords {
|
2021-11-22 08:43:07 +01:00
|
|
|
|
2020-08-02 23:03:16 +02:00
|
|
|
int16_t x;
|
|
|
|
int16_t y;
|
2021-11-22 08:43:07 +01:00
|
|
|
int16_t virt_row; // chain of panels row
|
|
|
|
int16_t virt_col; // chain of panels col
|
|
|
|
|
|
|
|
VirtualCoords() : x(0), y(0)
|
|
|
|
{ }
|
|
|
|
|
2020-08-02 23:03:16 +02:00
|
|
|
};
|
|
|
|
|
2022-01-10 20:26:01 +01:00
|
|
|
enum PANEL_SCAN_RATE {NORMAL_ONE_SIXTEEN, ONE_EIGHT_32, ONE_EIGHT_16};
|
2021-12-20 22:35:16 +01:00
|
|
|
|
2020-08-02 23:03:16 +02:00
|
|
|
#ifdef USE_GFX_ROOT
|
|
|
|
class VirtualMatrixPanel : public GFX
|
2021-02-10 16:49:19 +01:00
|
|
|
#elif !defined NO_GFX
|
2020-08-02 23:03:16 +02:00
|
|
|
class VirtualMatrixPanel : public Adafruit_GFX
|
2021-02-10 16:49:19 +01:00
|
|
|
#else
|
|
|
|
class VirtualMatrixPanel
|
2020-08-02 23:03:16 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
int16_t virtualResX;
|
|
|
|
int16_t virtualResY;
|
|
|
|
|
2021-06-12 18:19:37 +02:00
|
|
|
int16_t vmodule_rows;
|
|
|
|
int16_t vmodule_cols;
|
2020-08-02 23:03:16 +02:00
|
|
|
|
2020-08-09 20:13:13 +02:00
|
|
|
int16_t panelResX;
|
|
|
|
int16_t panelResY;
|
2021-11-22 08:43:07 +01:00
|
|
|
|
|
|
|
int16_t dmaResX; // The width of the chain in pixels (as the DMA engine sees it)
|
2020-08-02 23:03:16 +02:00
|
|
|
|
2020-11-28 10:45:30 +01:00
|
|
|
MatrixPanel_I2S_DMA *display;
|
2020-08-02 23:03:16 +02:00
|
|
|
|
2021-06-12 18:19:37 +02:00
|
|
|
VirtualMatrixPanel(MatrixPanel_I2S_DMA &disp, int _vmodule_rows, int _vmodule_cols, int _panelResX, int _panelResY, bool serpentine_chain = true, bool top_down_chain = false)
|
2021-02-10 16:49:19 +01:00
|
|
|
#ifdef USE_GFX_ROOT
|
2021-06-12 18:19:37 +02:00
|
|
|
: GFX(_vmodule_cols*_panelResX, _vmodule_rows*_panelResY)
|
2021-02-10 16:49:19 +01:00
|
|
|
#elif !defined NO_GFX
|
2021-06-12 18:19:37 +02:00
|
|
|
: Adafruit_GFX(_vmodule_cols*_panelResX, _vmodule_rows*_panelResY)
|
2020-08-02 23:03:16 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
this->display = &disp;
|
2020-08-09 20:13:13 +02:00
|
|
|
|
2021-06-12 18:19:37 +02:00
|
|
|
panelResX = _panelResX;
|
|
|
|
panelResY = _panelResY;
|
2020-08-09 20:13:13 +02:00
|
|
|
|
2021-06-12 18:19:37 +02:00
|
|
|
vmodule_rows = _vmodule_rows;
|
|
|
|
vmodule_cols = _vmodule_cols;
|
2020-08-02 23:03:16 +02:00
|
|
|
|
2021-06-12 18:19:37 +02:00
|
|
|
virtualResX = vmodule_cols*_panelResX;
|
2021-11-22 08:43:07 +01:00
|
|
|
virtualResY = vmodule_rows*_panelResY;
|
|
|
|
|
|
|
|
dmaResX = panelResX * vmodule_rows * vmodule_cols;
|
|
|
|
|
|
|
|
|
2021-06-10 21:49:28 +02:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
2020-08-09 20:13:13 +02:00
|
|
|
_s_chain_party = serpentine_chain; // serpentine, or 'S' chain?
|
|
|
|
_chain_top_down= top_down_chain;
|
2021-06-12 18:19:37 +02:00
|
|
|
|
|
|
|
coords.x = coords.y = -1; // By default use an invalid co-ordinates that will be rejected by updateMatrixDMABuffer
|
2020-08-09 20:13:13 +02:00
|
|
|
|
2020-08-02 23:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2020-08-09 20:13:13 +02:00
|
|
|
virtual void fillScreen(uint16_t color); // overwrite adafruit implementation
|
2022-01-10 23:31:22 +01:00
|
|
|
virtual void fillScreenRGB888(uint8_t r, uint8_t g,uint8_t b);
|
2021-11-22 08:43:07 +01:00
|
|
|
void clearScreen() { display->clearScreen(); }
|
2020-08-02 23:03:16 +02:00
|
|
|
void drawPixelRGB888(int16_t x, int16_t y, uint8_t r, uint8_t g, uint8_t b);
|
|
|
|
|
2021-11-22 08:43:07 +01:00
|
|
|
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); }
|
2020-09-20 17:14:35 +02:00
|
|
|
|
|
|
|
void flipDMABuffer() { display->flipDMABuffer(); }
|
2021-11-22 08:43:07 +01:00
|
|
|
void drawDisplayTest();
|
|
|
|
void setRotate(bool rotate);
|
2021-12-20 22:35:16 +01:00
|
|
|
|
|
|
|
void setPhysicalPanelScanRate(PANEL_SCAN_RATE rate);
|
2020-08-09 20:13:13 +02:00
|
|
|
|
2021-11-22 08:43:07 +01:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual VirtualCoords getCoords(int16_t &x, int16_t &y);
|
2020-08-02 23:03:16 +02:00
|
|
|
VirtualCoords coords;
|
2021-11-22 08:43:07 +01:00
|
|
|
|
2020-08-09 20:13:13 +02:00
|
|
|
bool _s_chain_party = true; // Are we chained? Ain't no party like a...
|
|
|
|
bool _chain_top_down = false; // is the ESP at the top or bottom of the matrix of devices?
|
2021-06-12 18:19:37 +02:00
|
|
|
bool _rotate = false;
|
2021-12-20 22:35:16 +01:00
|
|
|
|
|
|
|
PANEL_SCAN_RATE _panelScanRate = NORMAL_ONE_SIXTEEN;
|
2020-08-02 23:03:16 +02:00
|
|
|
|
|
|
|
}; // end Class header
|
|
|
|
|
2021-06-12 18:19:37 +02:00
|
|
|
/**
|
|
|
|
* Calculate virtual->real co-ordinate mapping to underlying single chain of panels connected to ESP32.
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-11-22 08:43:07 +01:00
|
|
|
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
|
2020-08-09 20:13:13 +02:00
|
|
|
|
2021-04-05 14:25:46 +02:00
|
|
|
// We want to rotate?
|
|
|
|
if (_rotate){
|
2022-01-11 00:09:06 +01:00
|
|
|
int16_t temp_x=x;
|
2021-04-05 14:25:46 +02:00
|
|
|
x=y;
|
|
|
|
y=virtualResY-1-temp_x;
|
2022-01-11 00:09:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2021-11-22 08:43:07 +01:00
|
|
|
|
2022-01-11 00:09:06 +01:00
|
|
|
// Stupidity check
|
2021-12-20 21:56:14 +01:00
|
|
|
if ( (vmodule_rows == 1) && (vmodule_cols == 1)) // single panel...
|
2021-11-22 08:43:07 +01:00
|
|
|
{
|
|
|
|
coords.x = x;
|
|
|
|
coords.y = y;
|
|
|
|
return coords;
|
|
|
|
}
|
2020-08-09 20:13:13 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// First portion gets you to the correct offset for the row you need
|
|
|
|
// Second portion inverts the x on the row
|
2021-06-12 18:19:37 +02:00
|
|
|
coords.x = ((y / panelResY) * (virtualResX)) + (virtualResX - x) - 1;
|
2021-06-10 21:49:28 +02:00
|
|
|
|
2020-08-09 20:13:13 +02:00
|
|
|
// inverts the y the row
|
2021-06-10 21:49:28 +02:00
|
|
|
coords.y = panelResY - 1 - (y % panelResY);
|
2020-08-09 20:13:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-10 21:49:28 +02:00
|
|
|
// Normal chain pixel co-ordinate
|
2021-06-12 18:19:37 +02:00
|
|
|
coords.x = x + ((y / panelResY) * (virtualResX)) ;
|
2020-08-09 20:13:13 +02:00
|
|
|
coords.y = y % panelResY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reverse co-ordinates if panel chain from ESP starts from the TOP RIGHT
|
|
|
|
if (_chain_top_down)
|
|
|
|
{
|
2021-11-22 08:43:07 +01:00
|
|
|
/*
|
|
|
|
const HUB75_I2S_CFG _cfg = this->display->getCfg();
|
|
|
|
coords.x = (_cfg.mx_width * _cfg.chain_length - 1) - coords.x;
|
|
|
|
coords.y = (_cfg.mx_height-1) - coords.y;
|
|
|
|
*/
|
|
|
|
coords.x = (dmaResX - 1) - coords.x;
|
|
|
|
coords.y = (panelResY-1) - coords.y;
|
2020-08-09 20:13:13 +02:00
|
|
|
}
|
2021-12-20 22:35:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
2022-01-10 20:26:01 +01:00
|
|
|
if ( _panelScanRate == ONE_EIGHT_32)
|
2021-12-20 22:35:16 +01:00
|
|
|
{
|
|
|
|
/* 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 One_Eight_1_8_ScanPanel code and you'll see that the DMA buffer is setup
|
|
|
|
as if the panel is 2 * W and 0.5 * H !
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Serial.print("VirtualMatrixPanel Mapping ("); Serial.print(x, DEC); Serial.print(","); Serial.print(y, DEC); Serial.print(") ");
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Serial.print("OneEightScanPanel Mapping ("); Serial.print(x, DEC); Serial.print(","); Serial.print(y, DEC); Serial.print(") ");
|
|
|
|
// to
|
|
|
|
Serial.print("to ("); Serial.print(coords.x, DEC); Serial.print(","); Serial.print(coords.y, DEC); Serial.println(") ");
|
|
|
|
*/
|
|
|
|
}
|
2022-01-10 20:26:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
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 < 32)
|
|
|
|
coords.y = (y >> 4) * 8 + (y & 0b00000111);
|
|
|
|
else{
|
|
|
|
coords.y = ((y-32) >> 4) * 8 + (y & 0b00000111);
|
|
|
|
coords.x += 256;
|
|
|
|
}
|
|
|
|
}
|
2021-12-20 22:35:16 +01:00
|
|
|
|
2020-08-09 20:13:13 +02:00
|
|
|
|
2021-06-10 21:49:28 +02:00
|
|
|
//Serial.print("Mapping to x: "); Serial.print(coords.x, DEC); Serial.print(", y: "); Serial.println(coords.y, DEC);
|
2021-11-22 08:43:07 +01:00
|
|
|
return coords;
|
2020-08-02 23:03:16 +02:00
|
|
|
}
|
|
|
|
|
2021-11-22 08:43:07 +01:00
|
|
|
inline void VirtualMatrixPanel::drawPixel(int16_t x, int16_t y, uint16_t color) { // adafruit virtual void override
|
2021-06-12 18:19:37 +02:00
|
|
|
getCoords(x, y);
|
2020-08-02 23:03:16 +02:00
|
|
|
this->display->drawPixel(coords.x, coords.y, color);
|
|
|
|
}
|
|
|
|
|
2021-11-22 08:43:07 +01:00
|
|
|
inline void VirtualMatrixPanel::fillScreen(uint16_t color) { // adafruit virtual void override
|
2020-08-02 23:03:16 +02:00
|
|
|
this->display->fillScreen(color);
|
|
|
|
}
|
|
|
|
|
2022-01-10 23:31:22 +01:00
|
|
|
inline void VirtualMatrixPanel::fillScreenRGB888(uint8_t r, uint8_t g,uint8_t b) {
|
|
|
|
this->display->fillScreenRGB888(r, g, b);
|
|
|
|
}
|
|
|
|
|
2021-11-22 08:43:07 +01:00
|
|
|
inline void VirtualMatrixPanel::drawPixelRGB888(int16_t x, int16_t y, uint8_t r, uint8_t g, uint8_t b) {
|
2021-06-12 18:19:37 +02:00
|
|
|
getCoords(x, y);
|
2020-08-02 23:03:16 +02:00
|
|
|
this->display->drawPixelRGB888( coords.x, coords.y, r, g, b);
|
|
|
|
}
|
|
|
|
|
2021-04-05 13:33:21 +02:00
|
|
|
inline void VirtualMatrixPanel::setRotate(bool rotate) {
|
2021-04-06 11:20:22 +02:00
|
|
|
_rotate=rotate;
|
2022-01-09 11:20:14 +01:00
|
|
|
|
|
|
|
#ifndef NO_GFX
|
2021-04-06 11:20:22 +02:00
|
|
|
// We don't support rotation by degrees.
|
|
|
|
if (rotate) { setRotation(1); } else { setRotation(0); }
|
2022-01-09 11:20:14 +01:00
|
|
|
#endif
|
2020-08-02 23:03:16 +02:00
|
|
|
}
|
2021-04-05 13:33:21 +02:00
|
|
|
|
2021-12-20 22:35:16 +01:00
|
|
|
inline void VirtualMatrixPanel::setPhysicalPanelScanRate(PANEL_SCAN_RATE rate) {
|
|
|
|
_panelScanRate=rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-02 23:03:16 +02:00
|
|
|
|
2021-02-10 16:49:19 +01:00
|
|
|
#ifndef NO_GFX
|
2020-08-09 20:13:13 +02:00
|
|
|
inline void VirtualMatrixPanel::drawDisplayTest()
|
|
|
|
{
|
|
|
|
this->display->setFont(&FreeSansBold12pt7b);
|
|
|
|
this->display->setTextColor(this->display->color565(255, 255, 0));
|
|
|
|
this->display->setTextSize(1);
|
|
|
|
|
2021-06-12 18:19:37 +02:00
|
|
|
for ( int panel = 0; panel < vmodule_cols*vmodule_rows; panel++ ) {
|
2020-08-09 20:13:13 +02:00
|
|
|
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);
|
2021-06-12 18:19:37 +02:00
|
|
|
this->display->print((vmodule_cols*vmodule_rows)-panel);
|
2020-08-09 20:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-02-10 16:49:19 +01:00
|
|
|
#endif
|
2021-11-22 08:43:07 +01:00
|
|
|
/*
|
2021-10-16 16:00:43 +02:00
|
|
|
// need to recreate this one, as it wouldn't work to just map where it starts.
|
2021-06-12 18:19:37 +02:00
|
|
|
inline void VirtualMatrixPanel::drawIcon (int *ico, int16_t x, int16_t y, int16_t icon_cols, int16_t icon_rows) {
|
2020-08-02 23:03:16 +02:00
|
|
|
int i, j;
|
2021-06-12 18:19:37 +02:00
|
|
|
for (i = 0; i < icon_rows; i++) {
|
|
|
|
for (j = 0; j < icon_cols; j++) {
|
2020-08-02 23:03:16 +02:00
|
|
|
// This is a call to this libraries version of drawPixel
|
|
|
|
// which will map each pixel, which is what we want.
|
2021-03-31 19:29:03 +02:00
|
|
|
//drawPixelRGB565 (x + j, y + i, ico[i * module_cols + j]);
|
2021-06-12 18:19:37 +02:00
|
|
|
drawPixel (x + j, y + i, ico[i * icon_cols + j]);
|
2020-08-02 23:03:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-22 08:43:07 +01:00
|
|
|
*/
|
2020-08-02 23:03:16 +02:00
|
|
|
|
2021-05-16 16:08:18 +02:00
|
|
|
#endif
|