Skip to content

Instantly share code, notes, and snippets.

@eadmaster
Last active July 13, 2024 18:21
Show Gist options
  • Save eadmaster/f7244a526b8e45cfdc79ea9b37e5f17a to your computer and use it in GitHub Desktop.
Save eadmaster/f7244a526b8e45cfdc79ea9b37e5f17a to your computer and use it in GitHub Desktop.
ESP32-HUB75-MatrixPanel-DMA_test.ino
// Example sketch which shows how to display some patterns
// on a 64x32 LED matrix
//
#include <ESP32-HUB75-MatrixPanel-I2S-DMA.h>
#define PANEL_RES_X 128 // Number of pixels wide of each INDIVIDUAL panel module.
#define PANEL_RES_Y 64 // Number of pixels tall of each INDIVIDUAL panel module.
#define PANEL_CHAIN 1 // Total number of panels chained one to another
//MatrixPanel_I2S_DMA dma_display;
MatrixPanel_I2S_DMA *dma_display = nullptr;
uint16_t myBLACK = dma_display->color565(0, 0, 0);
uint16_t myWHITE = dma_display->color565(5, 5, 5);
uint16_t myRED = dma_display->color565(5, 0, 0);
uint16_t myGREEN = dma_display->color565(0, 5, 0);
uint16_t myBLUE = dma_display->color565(0, 0, 5);
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
// From: https://gist.github.com/davidegironi/3144efdc6d67e5df55438cc3cba613c8
uint16_t colorWheel(uint8_t pos) {
if(pos < 85) {
return dma_display->color565(pos * 3, 255 - pos * 3, 0);
} else if(pos < 170) {
pos -= 85;
return dma_display->color565(255 - pos * 3, 0, pos * 3);
} else {
pos -= 170;
return dma_display->color565(0, pos * 3, 255 - pos * 3);
}
}
uint8_t EQUALIZER_STR_SIZE = 12;
uint8_t curr_line = 0;
void drawText(char* text)
{
//dma_display->setTextColor(dma_display->color444(random(1, 5), random(1, 5), random(1, 5)));
//Serial.println(8*curr_line);
dma_display->setCursor(0, 8*curr_line);
dma_display->println(text);
}
#define R1_PIN 2
#define B1_PIN 4
#define R2_PIN 16
#define B2_PIN 17
#define A_PIN 5
#define C_PIN 18
#define CLK_PIN 19
#define OE_PIN 21
#define G1_PIN 13
#define G2_PIN 12
#define E_PIN 25
#define B_PIN 14
#define D_PIN 27
#define LAT_PIN 26
void setup() {
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};
// Module configuration
HUB75_I2S_CFG mxconfig(
PANEL_RES_X, // module width
PANEL_RES_Y, // module height
PANEL_CHAIN, // Chain length
_pins // Pin mapping
);
//mxconfig.double_buff = true;
//mxconfig.gpio.e = E_PIN;
//mxconfig.clkphase = false;
//mxconfig.driver = HUB75_I2S_CFG::FM6126A;
// Display Setup
dma_display = new MatrixPanel_I2S_DMA(mxconfig);
dma_display->begin();
dma_display->setBrightness8(1); //0-255
dma_display->clearScreen();
dma_display->setTextSize(1); // size 1 == 8 pixels high
dma_display->setTextWrap(true);
//dma_display->setTextColor(dma_display->color444(10,10,10));
dma_display->setTextColor(dma_display->color333(5, 5, 5));
drawText("init LED matrix OK");
/*
dma_display->fillScreen(myWHITE);
// fix the screen with green
dma_display->fillRect(0, 0, dma_display->width(), dma_display->height(), dma_display->color444(0, 15, 0));
delay(500);
*/
// draw a box in yellow
dma_display->drawRect(0, 0, dma_display->width(), dma_display->height(), dma_display->color444(5, 5, 0));
delay(500);
// draw an 'X' in red
dma_display->drawLine(0, 0, dma_display->width()-1, dma_display->height()-1, dma_display->color444(5, 0, 0));
dma_display->drawLine(dma_display->width()-1, 0, 0, dma_display->height()-1, dma_display->color444(5, 0, 0));
delay(500);
// draw a blue circle
dma_display->drawCircle(10, 10, 10, dma_display->color444(0, 0, 5));
delay(500);
// fill a violet circle
dma_display->fillCircle(40, 21, 10, dma_display->color444(5, 0, 5));
delay(500);
/*
// fill the screen with 'black'
dma_display->fillScreen(dma_display->color444(0, 0, 0));
//drawText(0);
*/
Serial.begin(115200); // MEMO: 9600 is too slow?
Serial.setTimeout(1); // maximum milliseconds to wait for serial data when using Serial.readBytesUntil(), Serial.readBytes(), Serial.parseInt() or Serial.parseFloat()
}
void draw_equalizer(const char* text)
{
// iterate over the string and detect escaped colors - FORMAT: /RGB
byte i = 0;
char cur_c = ' ';
byte cur_v = 0;
byte r=0;
byte g=0;
byte b=1;
// clear half screen
dma_display->fillRect(0, (PANEL_RES_Y - 9), (PANEL_RES_X), (PANEL_RES_Y/2), dma_display->color333(0, 0, 0));
for(i=0; i<EQUALIZER_STR_SIZE; i++) {
cur_c = text[i];
if(cur_c == 0) {
break; //end of string (truncated?)
} else if(cur_c >= '0' && cur_c <= '9') { // check if it is a valid int
cur_v = (byte)(cur_c - '0');
// change color according to value
// TODO: faster switching using a lut table?
if(cur_v==0) {
continue; // nothing to draw
} else if(cur_v>=7) {
r=1; g=0; b=0; // red
} else if(cur_v>=5) {
r=1; g=1; b=0; // yellow
} else if(cur_v>=3) {
r=0; g=1; b=0; // green
} else if(cur_v>=1) {
r=0; g=0; b=1; // blue
} /*else if(cur_v>=1) {
r=1; g=0; b=1; // purple
}*/
//BAR_WIDTH=PANEL_RES_X/11;
// draw the line
//if(lrc_str[0]==0) {
if(0) {
// full screen equalizer
dma_display->fillRect((i*11), (PANEL_RES_Y/2 - cur_v ), 11, cur_v*11, dma_display->color333(r, g, b));
} else {
// half screen eq.
dma_display->fillRect((i*11), (PANEL_RES_Y - cur_v), 11, cur_v, dma_display->color333(r, g, b));
}
} else {
// invalid char
//continue;
break;
}
} // endfor
return;
}
#include <math.h> // for ceil
void loop() {
// read the equalizer control string
//delay(1); // causes the buffer to overflow?
if (Serial.available() >= EQUALIZER_STR_SIZE ) {
// slower using readStringUntil
String control_str=Serial.readStringUntil('\n');
//Serial.println((control_str)); //Serial.flush();
if(control_str.length()< EQUALIZER_STR_SIZE-1) return; // skip truncated strings
draw_equalizer(control_str.c_str());
String lrc_line = control_str.substring(EQUALIZER_STR_SIZE-1);
if( lrc_line.length() > 1 ) {
if(curr_line == 0) {
dma_display->clearScreen();
}
drawText((char*) lrc_line.c_str());
//curr_line += 1;
curr_line += 1 + (int) ceil( lrc_line.length()/20 ); // compute drawn lines assuming 8-bit height chars
// TODO: scroll lines
//if( curr_line*8 > 64 ) {
if( curr_line*8 > 32 ) {
curr_line = 0;
}
}
}
/*
// animate by going through the colour wheel for the first two lines
drawText(wheelval);
wheelval +=1;
delay(20);
*/
/*
drawText(0);
delay(2000);
dma_display->clearScreen();
dma_display->fillScreen(myBLACK);
delay(2000);
dma_display->fillScreen(myBLUE);
delay(2000);
dma_display->fillScreen(myRED);
delay(2000);
dma_display->fillScreen(myGREEN);
delay(2000);
dma_display->fillScreen(myWHITE);
dma_display->clearScreen();
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment