I’m trying to make a project using the 16×32 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 doesnt 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/doesnt 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},
};
//===========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;
}
}
led matrix: https://www.adafruit.com/product/420
Source: Windows Questions C++