Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
witnessmenow committed Feb 3, 2024
2 parents 400b8ab + 975e0ce commit b5449fa
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 149 deletions.
149 changes: 149 additions & 0 deletions examples/BuildingBlocks/Shapes/BouncingSquares/BouncingSquares.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*******************************************************************
Bouncing squares of different colours and sizes
Based on mrfaptastic's example in ESP32-HUB75-MatrixPanel-I2S-DMA
https://github.com/mrfaptastic/ESP32-HUB75-MatrixPanel-I2S-DMA/blob/master/examples/BouncingSquares/BouncingSquares.ino
Parts Used:
ESP32 Trinity - https://github.com/witnessmenow/ESP32-Trinity
*******************************************************************/

// ----------------------------
// Additional Libraries - each one of these will need to be installed.
// ----------------------------

#include <ESP32-HUB75-MatrixPanel-I2S-DMA.h>
// This is the library for interfacing with the display

// Can be installed from the library manager (Search for "ESP32 MATRIX DMA")
// https://github.com/mrfaptastic/ESP32-HUB75-MatrixPanel-I2S-DMA

// ----------------------------
// Dependency Libraries - each one of these will need to be installed.
// ----------------------------

// Adafruit GFX library is a dependency for the matrix Library
// Can be installed from the library manager
// https://github.com/adafruit/Adafruit-GFX-Library

// --------------------------------
// ------- Matrix Config ------
// --------------------------------

const int panelResX = 64; // Number of pixels wide of each INDIVIDUAL panel module.
const int panelResY = 64; // Number of pixels tall of each INDIVIDUAL panel module.
const int panel_chain = 1; // Total number of panels chained one to another.

// -------------------------------
// ------- Other Config ------
// -------------------------------

const int numSquares = 20;
const int minSquareSize = 2;
const int maxSquareSize = 10;

const int refreshDelay = 20;

//------------------------------------------------------------------------------------------------------------------

MatrixPanel_I2S_DMA *dma_display = nullptr;

uint16_t myDARK = dma_display->color565(64, 64, 64);
uint16_t myWHITE = dma_display->color565(192, 192, 192);
uint16_t myRED = dma_display->color565(255, 0, 0);
uint16_t myGREEN = dma_display->color565(0, 255, 0);
uint16_t myBLUE = dma_display->color565(0, 0, 255);
uint16_t myCYAN = dma_display->color565(0, 255, 255);
uint16_t myMAGENTA = dma_display->color565(255, 0, 255);
uint16_t myYELLOW = dma_display->color565(255, 255, 0);

uint16_t colours[8] = { myDARK, myWHITE, myRED, myGREEN, myBLUE, myCYAN, myMAGENTA, myYELLOW };

struct Square
{
float xpos, ypos;
float velocityx;
float velocityy;
uint16_t square_size;
uint16_t colour;
};

Square Squares[numSquares];

void displaySetup() {
HUB75_I2S_CFG mxconfig(
panelResX, // Module width
panelResY, // Module height
panel_chain // Chain length
);

// This is how you enable the double buffer.
// Double buffer can help with animation heavy projects
mxconfig.double_buff = true;

// If you are using a 64x64 matrix you need to pass a value for the E pin
// The trinity connects GPIO 18 to E.
// This can be commented out for any smaller displays (but should work fine with it)
mxconfig.gpio.e = 18;

// May or may not be needed depending on your matrix
// Example of what needing it looks like:
// https://github.com/mrfaptastic/ESP32-HUB75-MatrixPanel-I2S-DMA/issues/134#issuecomment-866367216
mxconfig.clkphase = false;

// Some matrix panels use different ICs for driving them and some of them have strange quirks.
// If the display is not working right, try this.
//mxconfig.driver = HUB75_I2S_CFG::FM6126A;

// Display Setup
dma_display = new MatrixPanel_I2S_DMA(mxconfig);
dma_display->begin();
}

void setup()
{
Serial.begin(115200);
displaySetup();

// Create some Squares
for (int i = 0; i < numSquares; i++)
{
Squares[i].square_size = random(minSquareSize, maxSquareSize);
Squares[i].xpos = random(0, dma_display->width() - Squares[i].square_size);
Squares[i].ypos = random(0, dma_display->height() - Squares[i].square_size);
Squares[i].velocityx = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
Squares[i].velocityy = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
Squares[i].colour = colours[i % 8];
}
}

void loop()
{
dma_display->clearScreen();

for (int i = 0; i < numSquares; i++)
{
// Draw rect and then calculate
dma_display->fillRect(Squares[i].xpos, Squares[i].ypos, Squares[i].square_size, Squares[i].square_size, Squares[i].colour);

if (Squares[i].square_size + Squares[i].xpos >= dma_display->width()) {
Squares[i].velocityx *= -1;
}
else if (Squares[i].xpos <= 0) {
Squares[i].velocityx = abs (Squares[i].velocityx);
}

if (Squares[i].square_size + Squares[i].ypos >= dma_display->height()) {
Squares[i].velocityy *= -1;
}
else if (Squares[i].ypos <= 0) {
Squares[i].velocityy = abs (Squares[i].velocityy);
}

Squares[i].xpos += Squares[i].velocityx;
Squares[i].ypos += Squares[i].velocityy;
}

dma_display->flipDMABuffer();
delay(refreshDelay);
}
116 changes: 68 additions & 48 deletions examples/Projects/SnakeWithNunchuck/SnakeWithNunchuck.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
WIP: Snake game using a 64x64 RGB LED Matrix,
an ESP32 and a Wii Nunchuck.
Parts:
ESP32 D1 Mini * - https://s.click.aliexpress.com/e/_dSi824B
ESP32 I2S Matrix Shield (From my Tindie) = https://www.tindie.com/products/brianlough/esp32-i2s-matrix-shield/
* * = Affilate
Parts Used:
ESP32 Trinity - https://github.com/witnessmenow/ESP32-Trinity
If you find what I do useful and would like to support me,
please consider becoming a sponsor on Github
https://github.com/sponsors/witnessmenow/
Written by Brian Lough
YouTube: https://www.youtube.com/brianlough
Tindie: https://www.tindie.com/stores/brianlough/
Expand All @@ -35,31 +31,35 @@
// Can be installed from the library manager
// https://github.com/dmadison/NintendoExtensionCtrl

#include <ESP32-RGB64x32MatrixPanel-I2S-DMA.h>
#include <ESP32-HUB75-MatrixPanel-I2S-DMA.h>
// This is the library for interfacing with the display

// Can be installed from the library manager (Search for "ESP32 64x32 LED MATRIX")
// https://github.com/mrfaptastic/ESP32-RGB64x32MatrixPanel-I2S-DMA
// Can be installed from the library manager (Search for "ESP32 MATRIX DMA")
// https://github.com/mrfaptastic/ESP32-HUB75-MatrixPanel-I2S-DMA


// Adafruit GFX library is a dependancy for the matrix Library
// Adafruit GFX library is a dependency for the matrix Library
// Can be installed from the library manager
// https://github.com/adafruit/Adafruit-GFX-Library

// --------------------------------
// ------- Matrix Config ------
// --------------------------------

const int panelResX = 64; // Number of pixels wide of each INDIVIDUAL panel module.
const int panelResY = 64; // Number of pixels tall of each INDIVIDUAL panel module.
const int panel_chain = 1; // Total number of panels chained one to another.

//--------------------------------
//Game Config Options:
// Game Config Options:
//--------------------------------

//Display settings
#define SCREEN_X 64
#define SCREEN_Y 64

#define WORLD_TO_PIXEL 1

#define WORLD_X 64
#define WORLD_Y 64

//Snake Settings
// Snake Settings
#define SNAKE_START_LENGTH 10

// Fat Snake - Woohooo
Expand All @@ -68,35 +68,31 @@
//#define WORLD_X 21
//#define WORLD_Y 21

//Snake Settings
// Snake Settings
//#define SNAKE_START_LENGTH 3

//Game Settings
// Game Settings
#define DELAY_BETWEEN_FRAMES 300 // smaller == faster snake
#define BOOSTED_DELAY_BETWEEN_FRAMES 25
//--------------------------------

//--------------------------------
//Pin Definitions:
//--------------------------------

// Pin Definitions:
//--------------------------------

unsigned long delayBetweenFrames = DELAY_BETWEEN_FRAMES;

Nunchuk nchuk; // Controller on bus #1

RGB64x32MatrixPanel_I2S_DMA dma_display;

MatrixPanel_I2S_DMA *dma_display = nullptr;

uint16_t myRED = dma_display.color565(255, 0, 0);
uint16_t myGREEN = dma_display.color565(0, 255, 0);
uint16_t myBLUE = dma_display.color565(0, 0, 255);
uint16_t myWHITE = dma_display.color565(255, 255, 255);
uint16_t myYELLOW = dma_display.color565(255, 255, 0);
uint16_t myCYAN = dma_display.color565(0, 255, 255);
uint16_t myMAGENTA = dma_display.color565(255, 0, 255);
uint16_t myBLACK = dma_display.color565(0, 0, 0);
uint16_t myRED = dma_display->color565(255, 0, 0);
uint16_t myGREEN = dma_display->color565(0, 255, 0);
uint16_t myBLUE = dma_display->color565(0, 0, 255);
uint16_t myWHITE = dma_display->color565(255, 255, 255);
uint16_t myYELLOW = dma_display->color565(255, 255, 0);
uint16_t myCYAN = dma_display->color565(0, 255, 255);
uint16_t myMAGENTA = dma_display->color565(255, 0, 255);
uint16_t myBLACK = dma_display->color565(0, 0, 0);

uint16_t snakeBodyColour = myMAGENTA;
uint16_t snakeHeadColour = myGREEN;
Expand Down Expand Up @@ -150,26 +146,50 @@ void initSnake(int snakeLength, int x, int y) {
player->head = current;
}

void displaySetup() {
HUB75_I2S_CFG mxconfig(
panelResX, // Module width
panelResY, // Module height
panel_chain // Chain length
);

// If you are using a 64x64 matrix you need to pass a value for the E pin
// The trinity connects GPIO 18 to E.
// This can be commented out for any smaller displays (but should work fine with it)
mxconfig.gpio.e = 18;

// May or may not be needed depending on your matrix
// Example of what needing it looks like:
// https://github.com/mrfaptastic/ESP32-HUB75-MatrixPanel-I2S-DMA/issues/134#issuecomment-866367216
mxconfig.clkphase = false;

// Some matrix panels use different ICs for driving them and some of them have strange quirks.
// If the display is not working right, try this.
//mxconfig.driver = HUB75_I2S_CFG::FM6126A;

dma_display = new MatrixPanel_I2S_DMA(mxconfig);
dma_display->begin();
}

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
dma_display.begin();


displaySetup();
dma_display->fillScreen(myBLACK);

nchuk.begin();

while (!nchuk.connect()) {
Serial.println("Nunchuk on bus #1 not detected!");
dma_display.setCursor(5, 0);
dma_display.setTextColor(myBLUE);
dma_display.println("No");
dma_display.setCursor(5, 9);
dma_display.setTextColor(myRED);
dma_display.println("Nunchuck");
dma_display.setCursor(5, 18);
dma_display.setTextColor(myGREEN);
dma_display.println("detected!");
dma_display->setCursor(5, 0);
dma_display->setTextColor(myBLUE);
dma_display->println("No");
dma_display->setCursor(5, 9);
dma_display->setTextColor(myRED);
dma_display->println("Nunchuck");
dma_display->setCursor(5, 18);
dma_display->setTextColor(myGREEN);
dma_display->println("detected!");
delay(1000);
}

Expand Down Expand Up @@ -369,11 +389,11 @@ void processApple() {

void drawBodyPart(int x, int y, uint16_t colour) {
int realX = (x * WORLD_TO_PIXEL);
int realy = (y * WORLD_TO_PIXEL);
int realY = (y * WORLD_TO_PIXEL);
if (WORLD_TO_PIXEL > 1) {
dma_display.drawRect(realX, realy, 3, 3, colour);
dma_display->drawRect(realX, realY, 3, 3, colour);
} else {
dma_display.drawPixel(realX, realy, colour);
dma_display->drawPixel(realX, realY, colour);
}
}

Expand Down Expand Up @@ -404,7 +424,7 @@ void loop() {
processApple();
}
// display.clearDisplay();
dma_display.fillScreen(myBLACK);
dma_display->fillScreen(myBLACK);

drawSnake();
drawApple();
Expand Down
Loading

0 comments on commit b5449fa

Please sign in to comment.