1

I'm trying to make a project using the 16x32 RGB Led matrix, from the Adafruit store, but for some reason Visual Studio keeps telling me I've defined the matrix class multiple times.

This all started happening when I started making my own classes for my program (I'm still learning OOP). I have put define protection in the header file where I instantiate the matrix class, so that shouldn't be the problem

I've tried moving the class definition around. When I put it into the cpp file where I wanted to use it I stopped getting the multiple definition error, but then the matrix doesn't draw anything.

I've tried parsing a pointer to the matrix class to the class where I wanted to use the matrix but then I get this weird error where the matrix redraws the first thing it has to draw over and over again and it stops responding to serial input/doesn't give any serial output.

code:

//===========Matrix.h===========
#ifndef MATRIX_H
#define MATRIX_H
#include "RGBmatrixPanel.h"
#include <SPI.h>
#include <Adafruit_I2CDevice.h>

#define CLK  8 
#define OE   9
#define LAT 10
#define A   A0
#define B   A1
#define C   A2

RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);

#endif
//===========main.cpp===========
#include <Arduino.h>
#include "GuiDrawer.h"

GuiDrawer guiDrawer;

void setup() {
  Serial.begin(9600);
  guiDrawer.drawBaseGui();
}

void loop() {}
//===========GuiDrawer.h===========
#ifndef GUIDRAWER_H
#define GUIDRAWER_H
#include <Arduino.h>
#include "matrix.h"

class GuiDrawer {
  private:

  enum COLOURS {
    BLACK = 0,      //0
    WHITE,          //1
    RED,            //2
    YELLOW,         //3
    DARKER_YELLOW,  //4
    DARKEST_YELLOW, //5
    GRAY            //6
  };

  enum SHAPES {
    HEART = 0,      //0
    AMMO,           //1
    COIN            //2
  };

  const int HEARTSIZE_X = 8;
  const int HEARTSIZE_Y = 8;
  const int HEARTSHAPE [8][8] = {
    {0, 2, 2, 0, 0, 2, 2, 0},
    {2, 2, 1, 2, 2, 2, 2, 2},
    {2, 1, 2, 2, 2, 2, 2, 2},
    {2, 1, 2, 2, 2, 2, 2, 2},  
    {2, 2, 2, 2, 2, 2, 2, 2},  
    {0, 2, 2, 2, 2, 2, 2, 0},  
    {0, 0, 2, 2, 2, 2, 0, 0},  
    {0, 0, 0, 2, 2, 0, 0, 0},  
  };
  const int COINSIZE_X = 8;
  const int COINSIZE_Y = 7;
  const int COINSHAPE [8][7] = {
    {0, 0, 4, 4, 4, 0, 0},
    {0, 4, 3, 3, 3, 4, 0},
    {4, 3, 3, 4, 3, 3, 4},
    {4, 3, 3, 4, 3, 3, 4},
    {4, 3, 3, 4, 3, 3, 4},
    {4, 3, 3, 4, 3, 3, 4},
    {0, 4, 3, 3, 3, 4, 0},
    {0, 0, 4, 4, 4, 0, 0}
  };
    
  const int AMMOSIZE_X = 8;
  const int AMMOSIZE_Y = 6;
  const int AMMOSHAPE [8][6] = {
    {0, 6, 0, 0, 6, 0},
    {6, 6, 6, 6, 6, 6},
    {3, 3, 5, 3, 3, 5},
    {3, 3, 5, 3, 3, 5},
    {3, 3, 5, 3, 3, 5},
    {3, 3, 5, 3, 3, 5},
    {3, 3, 5, 3, 3, 5},
    {3, 3, 5, 3, 3, 5}
  };
 
  void drawShape(SHAPES shape, int startX, int startY);
  void pixelDrawer(COLOURS colourToDraw, int drawX, int drawY);
  
  public:
  GuiDrawer();
  void drawBaseGui();
};
  
#endif
//===========GuiDrawer.cpp===========
#include <Arduino.h>
#include "GuiDrawer.h"
 
GuiDrawer::GuiDrawer() {
  matrix.begin();
  Serial.begin(9600);
}
 
void GuiDrawer::drawBaseGui() {
  drawShape(HEART, 0, 0);
  drawShape(COIN, 0, 8);
  drawShape(COIN, 25, 8);
  Serial.println("drawn base gui");
}
 
void GuiDrawer::drawShape(SHAPES shape, int startX, int startY) {
  switch (shape) {
    case HEART:
      for (int i = 0; i < HEARTSIZE_X; i++) {
        for (int j = 0; j < HEARTSIZE_Y; j++) {
          COLOURS pixelToDraw = (COLOURS)HEARTSHAPE[i][j];
          pixelDrawer(pixelToDraw, (j + startX), (i + startY));
        }  
        Serial.println();  
      }
      break;
    
    case AMMO:
      for (int i = 0; i < AMMOSIZE_X; i++) {
        for (int j = 0; j < AMMOSIZE_Y; j++) {
          COLOURS pixelToDraw = (COLOURS)AMMOSHAPE[i][j];
          pixelDrawer(pixelToDraw, (j + startX), (i + startY));
        }    
      }
      break;
        
    case COIN:
      for (int i = 0; i < COINSIZE_X; i++) {
        for (int j = 0; j < COINSIZE_Y; j++) {
          COLOURS pixelToDraw = (COLOURS)COINSHAPE[i][j];
          pixelDrawer(pixelToDraw, (j + startX), (i + startY));
        }    
      }
      break;
 
    default:
      break;
  }
}
 
void GuiDrawer::pixelDrawer(COLOURS colourToDraw, int drawX, int drawY) {
  switch (colourToDraw) {
    case BLACK:
      matrix.drawPixel(drawX, drawY, matrix.Color333(0, 0, 0));
      Serial.print("black ");
      break;
 
    case WHITE:
      matrix.drawPixel(drawX, drawY, matrix.Color333(255, 255, 255));
        Serial.print("white ");
        break;
    
    case RED:
      matrix.drawPixel(drawX, drawY, matrix.Color333(255, 0, 0));
      Serial.print("red   ");
      break;
 
    case YELLOW:
      matrix.drawPixel(drawX, drawY, matrix.Color333(255, 255, 0));
      break;
 
    case DARKER_YELLOW:
      matrix.drawPixel(drawX, drawY, matrix.Color888(16, 16, 0));
      break;
 
    default:
      break;
  }
}

error I am getting:

.pio\build\uno\src\main.cpp.o (symbol from plugin): In function `matrix':
(.text+0x0): multiple definition of `matrix'
.pio\build\uno\src\GuiDrawer.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\uno\firmware.elf] Error 1

led matrix: https://www.adafruit.com/product/420

reddog34
  • 11
  • 3
  • GuiDrawer class defined in GuiDrawer.h and you define it Again – mehdi Jan 13 '21 at 11:08
  • @mehdi i dont really understand what you mean? do you mean I've put ```#include "GuiDrawer.h"```` in my file twice, or something else? – reddog34 Jan 13 '21 at 12:07
  • What is this question about? "multiple definition of matrix" as in the title or "the matrix redraws ... over and over again" as in the body? – Yunnosch Jan 13 '21 at 12:21
  • @Yunnosch the multiple definition of matrix, i should've probably included the exact error im getting, ive now added it to the post – reddog34 Jan 13 '21 at 12:29
  • I see the declaration of `matrix` in `Matrix.h`; where's the definition? You've not listed some of the include files—and, presumably, implementation files. – Ruslan Jan 13 '21 at 13:15
  • don't define variables in .h. it is then in all translation units which included the .h and the linker detects it as duplicate symbol – Juraj Jan 13 '21 at 13:27
  • I do not see a function `matrix` in your main.cpp and hence doubt that the shown code can get a compiler error mentioning `main.cpp.o (symbol from plugin): In function `matrix':`. I see such a function in matrix.h, but only as a prototype, which should not cause the mentioned error either. Please double check that what you show is a [mre]. – Yunnosch Jan 13 '21 at 13:58
  • Still, are you asking about redefinition or redrawing endlessly. You have to focus on one problem. – Yunnosch Jan 13 '21 at 14:00

0 Answers0