@ Sisor: Compilierung sagt: 15% Programmspeicher belegt, 36% dyn. Speicher belegt. Die Matrizen sind einmal 200 Stk. WS2811 und für das Menü die fertige 16x32 RGB von Adafruit. Diese beiden werden ja nur per Befehl angesprochen (je nachdem welche LED in welcher Farbe), keine bzw ganz wenige vordefinierten "Muster"...
@ shedepe: Ich werde gleich mal ein paar Teile des Codes hier rein stellen für einen Überblick. Die LED-Matrix von adafruit ist für das Menü und muss in jedem "Unterprogramm" angesprochen werden können, das geht bisher nur als matrix1 matrix2 matrix3 etc....
main.ino - Hauptmenü, Matrix für das Menü als "matrix" definiert:
Code:
#include "Rainbow.h"
#include "MegaMaze.h"
#include "Tetris.h"
#include "Snake.h"
#include "ConnectFour.h"
////*********************** MATRIX
#include "Adafruit_GFX.h" // Core graphics library
#include "RGBmatrixPanel.h" // Hardware-specific library
#define CLK 12 // MUST be on PORTB!
#define LAT A3
#define OE 9
#define A A0
#define B A1
#define C A2
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);
char str1[] = "GAME";
char str2[] = "table";
char str3[] = "tetris";
char str4[] = "Pause";
char str5[] = "w";
char str6[] = "a";
char str7[] = "ROW";
long hue = 0;
// ********************** MATRIX end
//......
Adafruit_WS2801 *strip;
NESController *controller;
int curGame=0;
int displayedGame=0;
Game *currentGame = NULL;
bool paused = true;
String gameNames[] = {"Tetris","Mega Maze","Snake","Connect Four","Rainbow"};
int main(void) {
init();
setup();
while (true) {
loop();
}
}
//****************************************************** SETUP ***
// The setup() method runs once, when the sketch starts
void setup() {
// Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row
strip = new Adafruit_WS2801(10,20, pixelsDataPin, pixelsClockPin,0);
controller = new NESController(controllerLatchPin,controllerClockPin,controllerDataPin);
pinMode(controllerLatchPin, OUTPUT);
pinMode(controllerClockPin, OUTPUT);
pinMode(controllerDataPin, INPUT);
digitalWrite(controllerLatchPin, HIGH);
digitalWrite(controllerClockPin, HIGH);
strip->begin();
strip->show();
// *** MATRIX ***
matrix.begin();
matrix.setTextWrap(false); // Allow text to run off right edge
matrix.setTextSize(1);
matrix.fillScreen(0); //CLEAR
matrix.drawRect(0, 0, 32, 16, matrix.Color333(7, 7, 0));
matrix.setCursor(5, 1);
matrix.setTextColor(matrix.Color333(1,5,0));
matrix.print(str1);
delay(200);
matrix.setCursor(2, 8);
matrix.setTextColor(matrix.Color333(2,0,7));
matrix.print(str2);
for (int thisNote = 0; thisNote < 64; thisNote++) {
int noteDuration1 = 2000/noteDurations1[thisNote];
tone(8, melody1[thisNote],noteDuration1);
int pauseBetweenNotes = noteDuration1 * 0.70;
delay(pauseBetweenNotes);
noTone(8);
int randX;
int randY;
int randR;int randG;int randB;
randX = random(10);
randY = random(20);
randR = random(0,255);
randG = random(0,255);
randB = random(0,255);
strip->setPixelColor(randX, randY ,randR,randG,randB);
strip->show();
}
}
void loop() {
controller->controllerRead();
if(paused && controller->buttonPressed(controller->BUTTON_LEFT) &&
!controller->buttonHandled(controller->BUTTON_LEFT)){
controller->handleButton(controller->BUTTON_LEFT);
if(displayedGame==0){
displayedGame = (sizeof(gameNames)/sizeof(String)) - 1;
}else{
displayedGame=(displayedGame-1)%(sizeof(gameNames)/sizeof(String));
}
matrix.fillScreen(0); //CLEAR
matrix.setCursor(5, 5);
matrix.setTextColor(matrix.Color333(1,1,4));
matrix.print(displayedGame);
int Note_C4;
tone(8, Note_C4, 32);
int pauseBetweenNotes = 32 * 1.70;
delay(pauseBetweenNotes);
noTone(8);
}
if(paused && controller->buttonPressed(controller->BUTTON_RIGHT) &&
!controller->buttonHandled(controller->BUTTON_RIGHT)){
controller->handleButton(controller->BUTTON_RIGHT);
int Note_C4;
tone(8, Note_C4, 32);
int pauseBetweenNotes = 32 * 1.70;
delay(pauseBetweenNotes);
noTone(8);
displayedGame=(displayedGame+1)%(sizeof(gameNames)/sizeof(String));
matrix.fillScreen(0); //CLEAR
matrix.setCursor(5, 5);
matrix.setTextColor(matrix.Color333(1,4,1));
matrix.print(displayedGame);
}
if(controller->buttonPressed(controller->BUTTON_START) && !controller->buttonHandled(controller->BUTTON_START)){
controller->handleButton(controller->BUTTON_START);
paused = !paused;
if(!paused){
if(displayedGame!=curGame || currentGame==NULL){
curGame = displayedGame;
delete currentGame;
switch(displayedGame){
case 0:
currentGame = new Tetris(controller,strip);
break;
case 1:
currentGame = new MegaMaze(controller,strip);
break;
case 2:
currentGame = new Snake(controller,strip);
break;
case 3:
currentGame = new ConnectFour(controller,strip);
break;
case 4:
currentGame = new Rainbow(controller,strip);
break;
}
}else{
currentGame->resume();
}
}
if(paused){
matrix.fillScreen(0);
matrix.drawRect(0, 0, 32, 16, matrix.Color333(7, 0, 7));
matrix.setCursor(1, 5);
matrix.setTextColor(matrix.Color333(1,5,7));
matrix.print(str4);
}
}
if(paused){
for(int i=0;i<200;i++){
strip->setPixelColor(i, 0x0);
}
int redPixels[] = {11,12,13,14,15,27,33,47,51,52,53,54,55};
for(int i=0;i<((int)sizeof(redPixels)/2);i++){
strip->setPixelColor(redPixels[i],0xFF0000);
}
int greenPixels[] = {64,65,66,67,68,71,73,75,84,86,88,91,93,95};
for(int i=0;i<((int)sizeof(greenPixels)/2);i++){
strip->setPixelColor(greenPixels[i],0x00FF00);
}
int bluePixels[] = {104,105,106,107,108,112,126,134,144,145,146,147,148};
for(int i=0;i<((int)sizeof(bluePixels)/2);i++){
strip->setPixelColor(bluePixels[i],0x0000FF);
}
int yellowPixels[] = {151,152,153,154,164,175,185,186,187,188};
for(int i=0;i<((int)sizeof(yellowPixels)/2);i++){
strip->setPixelColor(yellowPixels[i],0xFFFF00);
}
int whitePixels[] = {17,22,37,42,57,62,77,82,97,98,99,100,101,102,117,122,137,142,157,162,177,182};
for(int i=0;i<((int)sizeof(whitePixels)/2);i++){
strip->setPixelColor(whitePixels[i],0xFFFFFF);
}
strip->show();
matrix.fillScreen(0); //CLEAR
matrix.drawRect(0, 0, 32, 16, matrix.Color333(7, 0, 7));
matrix.setCursor(5, 1);
matrix.setTextColor(matrix.Color333(1,5,7));
matrix.print(str4);
}else{
currentGame->run();
}
}
eins der Spiele, hier das Menü als "matrix1" definiert:
Code:
#include "Rainbow.h"
////*********************** MATRIX
#include "Adafruit_GFX.h" // Core graphics library
#include "RGBmatrixPanel.h" // Hardware-specific library
#define CLK 12 // MUST be on PORTB!
#define LAT A3
#define OE 9
#define A A0
#define B A1
#define C A2
RGBmatrixPanel matrix1(A, B, C, CLK, LAT, OE, false);
char str11[] = "PRG";
char str12[] = "Mode";
// ********************** MATRIX end
int r_color=0;
int g_color=0;
int b_color=255;
int r_background=120;
int g_background=80;
int b_background=0;
Rainbow::Rainbow(NESController *controller, Adafruit_WS2801 *strip):Game(controller, strip){
resume();
red=128;
green=255;
blue=0;
colorMode=1;
geschw=10;
matrix1.begin();
matrix1.setTextSize(1);
matrix1.fillScreen(0);
matrix1.setCursor(1, 1);
matrix1.setTextColor(matrix1.Color333(2,3,0));
matrix1.print(str11);
matrix1.setCursor(2, 9);
matrix1.print(str12);
matrix1.setCursor(20, 1);
matrix1.setTextColor(matrix1.Color333(1,1,3));
matrix1.print(geschw);
matrix1.setCursor(27, 9);
matrix1.setTextColor(matrix1.Color333(1,1,3));
matrix1.print(colorMode);
}
Rainbow::~Rainbow(){
}
void Rainbow::run(){
if(controller->buttonPressed(controller->BUTTON_UP) &&
!controller->buttonHandled(controller->BUTTON_UP))
{
controller->handleButton(controller->BUTTON_UP);
geschw++;
if(geschw>50){geschw=1;}
matrix1.setCursor(20, 1);
matrix1.fillRect(20, 1, 32, 8, matrix1.Color333(0, 0, 0));
matrix1.setTextColor(matrix1.Color333(1,1,4));
matrix1.print(geschw);
}
if(controller->buttonPressed(controller->BUTTON_DOWN) &&
!controller->buttonHandled(controller->BUTTON_DOWN))
{
controller->handleButton(controller->BUTTON_DOWN);
geschw--;
if(geschw<1){geschw=50;}
matrix1.setCursor(20, 1);
matrix1.fillRect(20, 1, 32, 8, matrix1.Color333(0, 0, 0));
matrix1.setTextColor(matrix1.Color333(1,1,4));
matrix1.print(geschw);
}
if(controller->buttonPressed(controller->BUTTON_A) &&
!controller->buttonHandled(controller->BUTTON_A))
{
controller->handleButton(controller->BUTTON_A);
TIMSK1 &= ~_BV(TOIE1); // Disable Timer1 interrupt
digitalWrite(OE, HIGH); // Disable LED output
}
if(controller->buttonPressed(controller->BUTTON_B) &&
!controller->buttonHandled(controller->BUTTON_B))
{
controller->handleButton(controller->BUTTON_B);
TIMSK1 |= _BV(TOIE1); // Enable Timer1 interrupt
}
if(controller->buttonPressed(controller->BUTTON_LEFT) && !controller->buttonHandled(controller->BUTTON_LEFT)){
controller->handleButton(controller->BUTTON_LEFT);
colorMode--;
if(colorMode<1){
colorMode=6;}
matrix1.setCursor(27, 9);
matrix1.fillRect(27, 9, 32, 16, matrix1.Color333(0, 0, 0));
matrix1.setTextColor(matrix1.Color333(1,1,4));
matrix1.print(colorMode);
}
if(controller->buttonPressed(controller->BUTTON_RIGHT) && !controller->buttonHandled(controller->BUTTON_RIGHT)){
controller->handleButton(controller->BUTTON_RIGHT);
colorMode++;
if(colorMode>6){
colorMode=1;}
matrix1.setCursor(27, 9);
matrix1.fillRect(27, 9, 32, 16, matrix1.Color333(0, 0, 0));
matrix1.setTextColor(matrix1.Color333(1,1,4));
matrix1.print(colorMode);
/*
usw usw
*/
Hier nächstes Spiel/ Unterprogramm, das Menü wird hier als "matrix2" definiert..:
Code:
#include "Tetris.h"
#include "pitches.h"
////*********************** MATRIX
#include "Adafruit_GFX.h" // Core graphics library
#include "RGBmatrixPanel.h" // Hardware-specific library
#define CLK 12 // MUST be on PORTB!
#define LAT A3
#define OE 9
#define A A0
#define B A1
#define C A2
RGBmatrixPanel matrix2(A, B, C, CLK, LAT, OE, false);
char str21[] = "GAME";
char str22[] = "over";
char str23[] = "score";
char str24[] = "WIN";
char str25[] = "Name";
char str26[] = "BEST";
char str27[] = "RAINBOW";
char str28 = EEPROM.read(4);
char str29 = EEPROM.read(5);
char str30 = EEPROM.read(6);
int text2X = matrix2.width(),
text2Min = sizeof(str26) * -12;
long hue2 = 0;
// ********************** MATRIX end
// Melodien:
// notes in the melody:
int melody2[] = {
NOTE_G4, NOTE_A4,NOTE_C4, NOTE_A4, NOTE_C4, NOTE_G4};
int melody3[] = {
NOTE_A4, NOTE_C4,NOTE_G4};
int melody5[] = {
NOTE_E3, NOTE_DS3,NOTE_AS3, NOTE_E3, NOTE_DS3,NOTE_AS3 };
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations2[] = {
32,32,32,16,16,8 };
int noteDurations3[] = {
32,32,32};
int noteDurations5[] = {
32,32,32,32,32,32 };
Tetris::Tetris(NESController *controller, Adafruit_WS2801 *strip):Game(controller, strip){
// myGLCD3.setColor(0, 0, 0);
// myGLCD3.fillRect(1, 110, 318, 224); // Clear
matrix2.begin();
matrix2.setTextSize(1);
matrix2.fillScreen(0); //CLEAR
matrix2.fillRect(0, 1, 32, 3, matrix2.Color333(2, 0, 0));
matrix2.fillRect(0, 13, 32, 15, matrix2.Color333(2, 0, 0));
matrix2.setTextColor(matrix2.Color333(2, 5, 3));
matrix2.setCursor(1, 4);
matrix2.print("T");
matrix2.setTextColor(matrix2.Color333(0, 5, 0));
matrix2.setCursor(6, 6);
matrix2.print("e");matrix2.setTextColor(matrix2.Color333(2, 0, 3));
matrix2.setCursor(11, 4);
matrix2.print("t");matrix2.setTextColor(matrix2.Color333(0, 5, 3));
matrix2.setCursor(17, 6);
matrix2.print("r");matrix2.setTextColor(matrix2.Color333(2, 5, 0));
matrix2.setCursor(22, 6);
matrix2.print("i");matrix2.setTextColor(matrix2.Color333(0, 0, 3));
matrix2.setCursor(26, 4);
matrix2.print("s");
delay(2000);
matrix2.fillScreen(0);
matrix2.setCursor(6, 0);
matrix2.setTextColor(matrix2.Color333(7,5,0));
matrix2.print(str26);
matrix2.setCursor(2, 8);
matrix2.setTextColor(matrix2.Color333(4,5,7));
matrix2.print(highScore);
delay(2000);
highScore = ((unsigned long)EEPROM.read(0) << 24)+
((unsigned long)EEPROM.read(1) << 16)+
((unsigned long)EEPROM.read(2) << 8)+
((unsigned long)EEPROM.read(3));
highInitials[0] = EEPROM.read(4);
highInitials[1] = EEPROM.read(5);
highInitials[2] = EEPROM.read(6);
curPiece=0;
curRotation=0;
lastDropTime=0;
clearingRows=false;
animateClearRowTimer=0;
initials[0]='A';
initials[1]='A';
initials[2]='A';
initialsPos=0;
newGame();
/* //Reset high score
EEPROM.write(0,0);
EEPROM.write(1,0);
EEPROM.write(2,0);
EEPROM.write(3,1);
EEPROM.write(4,'A');
EEPROM.write(5,'A');
EEPROM.write(6,'A'); */
}
Tetris::~Tetris(){
}
void Tetris::newGame(){
randomSeed(analogRead(0));
score=0;
curLevel=0;
lines=0;
gameOver=0;
gameOverAnimation=0;
for (int i = 0; i < boardWidth; i++) {
for (int j = 0; j < boardHeight; j++) {
board[i][j].setColor(0x0);
}
}
setPiece();
}
void Tetris::run(){
if(controller->buttonPressed(controller->BUTTON_SELECT)
&& !controller->buttonHandled(controller->BUTTON_SELECT))
{
controller->handleButton(controller->BUTTON_SELECT);
newGame();
}
if(gameOver){
if(gameOverAnimation<200){
if(board[gameOverAnimation%boardWidth]
[gameOverAnimation/boardWidth].getColor()==0)
{
board[gameOverAnimation%boardWidth]
[gameOverAnimation/boardWidth].setColor
(random(0,255)*255*255+random(0,255)*255+random(0,255));
}
gameOverAnimation++;
}
if(score>highScore){
if(initialsPos>2)
{
saveHighScore();
}
else{
if(controller->buttonPressed(controller->BUTTON_DOWN) &&
!controller->buttonHandled(controller->BUTTON_DOWN))
{
controller->handleButton(controller->BUTTON_DOWN);
if(initials[initialsPos]>'A')
{
initials[initialsPos]--;
}
else {
initials[initialsPos]='Z';
}
}
if(controller->buttonPressed(controller->BUTTON_UP) &&
!controller->buttonHandled(controller->BUTTON_UP))
{
controller->handleButton(controller->BUTTON_UP);
if(initials[initialsPos]<'Z')
{
initials[initialsPos]++;
}else {
initials[initialsPos]='A';
}
}
if(controller->buttonPressed(controller->BUTTON_A) &&
!controller->buttonHandled(controller->BUTTON_A))
{
controller->handleButton(controller->BUTTON_A);
initialsPos++;
}
// LCD-Menue
//lcd->write(157+initialsPos)
//lcd->print(initials[initialsPos])
//lcd->write(157+initialsPos)
// TFT-Menue
/* myGLCD3.setColor(0, 0, 0);
myGLCD3.fillRect(100+(initialsPos*20), 60, 120+(initialsPos*20), 100); // Clear
myGLCD3.setColor(0,0,255);
myGLCD3.setFont(arial_bold);
myGLCD3.print(initials, 100+(initialsPos*20), 60);*/
// Matrix-LED-Menue
matrix2.fillRect(5+(initialsPos*8), 8, 13+(initialsPos*8), 16, matrix2.Color333(0, 0, 0));
matrix2.setCursor(5+(initialsPos*8), 8);
matrix2.setTextColor(matrix2.Color333(1,5,0));
matrix2.print(initials[initialsPos]);
// matrix2.setCursor(12+initialsPos, 5);
/*
etc....
etc....
etc....
*/
gameOver=true;
if(score<=highScore){
/* myGLCD3.setColor(0, 0, 0);
myGLCD3.fillRect(1, 110, 318, 224); // Clear
myGLCD3.setColor(245,150,50);
myGLCD3.setFont(arial_bold);
myGLCD3.print("GAME OVER", CENTER, 60);
myGLCD3.setColor(255,255,0);
myGLCD3.printNumI(score, CENTER, 120);*/
matrix2.fillScreen(0);
matrix2.setCursor(2, 0);
matrix2.setTextColor(matrix2.Color333(7,5,0));
matrix2.print(str21);
matrix2.setCursor(2, 8);
matrix2.setTextColor(matrix2.Color333(7,5,0));
matrix2.print(str22);
delay(3000);
matrix2.fillScreen(0);
matrix2.setCursor(2, 0);
matrix2.setTextColor(matrix2.Color333(1,5,7));
matrix2.print(str23);
matrix2.setCursor(2, 8);
matrix2.setTextColor(matrix2.Color333(1,5,0));
matrix2.print(score);
}
else{
/* myGLCD3.setColor(0, 0, 0);
myGLCD3.fillRect(1, 110, 318, 224); // Clear
myGLCD3.setColor(245,150,50);
myGLCD3.setFont(arial_bold);
myGLCD3.print("RECORD", CENTER, 60);
myGLCD3.setColor(255,255,0);
myGLCD3.print("Name: ", 14, 120);*/
matrix2.fillScreen(0);
matrix2.setCursor(6, 1);
matrix2.setTextColor(matrix2.Color333(7,5,0));
matrix2.print(str24);
matrix2.setCursor(2, 8);
matrix2.setTextColor(matrix2.Color333(4,5,7));
matrix2.print(score);
delay(3000);
matrix2.fillScreen(0);
matrix2.setCursor(5, 0);
matrix2.setTextColor(matrix2.Color333(1,5,7));
matrix2.print(str25);
matrix2.setCursor(2, 10);
matrix2.setTextColor(matrix2.Color333(1,5,0));
}
initialsPos=0;
initials[0] = 'A';
initials[1] = 'A';
initials[2] = 'A';
for (int thisNote = 0; thisNote < 6; thisNote++) {
int noteDuration5 = 3000/noteDurations5[thisNote];
tone(8, melody5[thisNote],noteDuration5);
int pauseBetweenNotes = noteDuration5 * 1.70;
delay(pauseBetweenNotes);
noTone(8); }
}
board[pixelX][pixelY].setColor(pieceColors[curPiece]);
}
}
if(!clearRows()){
setPiece();
int Note_A3;
tone(8, Note_A3, 8);
int pauseBetweenNotes = 8 * 1.70;
delay(pauseBetweenNotes);
noTone(8);
}
}
}
void Tetris::setPiece(){
if(!gameOver){
curPos.setPosition(4,0);
lastDropTime = millis();
random(7);
curPiece=random(7);
curRotation=0;
updateDisplay();
}
}
void Tetris::increaseScore(unsigned long amount){
score+=amount;
updateDisplay();
}
void Tetris::resume(){
updateDisplay();
}
void Tetris::updateDisplay(){
matrix2.fillScreen(0);
matrix2.setCursor(1, 0);
matrix2.setTextColor(matrix2.Color333(7,5,0));
matrix2.print(str23);
matrix2.setCursor(0, 8);
matrix2.fillRect(0, 8, 32, 16, matrix2.Color333(0, 0, 0));
matrix2.setTextColor(matrix2.Color333(1,1,7));
matrix2.print(score);
//matrix2.swapBuffers(false);
// etc etc etc
Gehe ich vom Hauptmenü in die einzelnen Untermenüs funktioniert die LED-Matrix, gehe ich aber zurück aus dem Spiel ins Hauptmenü (main.ino) ist meine Matrix nicht mehr ansprechbar, es wird quasi von "matrix2" nicht auf "matrix" zurück geschaltet (mit einfachen Worten).
Es handelt sich bei diesem Problem nur um das Menü mit der LED-Matrix von adafruit, nicht um die WS2811 Matrix, diese Funktioniert perfekt.
Werde evtl für besseres Verständnis ein Video bei Youtube hochladen....
Lesezeichen