New to Arduino and C, I am trying to write some classes to draw on a led matrix. ( I am web developer and trying to learn C by myself )
To describe simply what I try to achieve :
XSprite : _x,_y,_width,_height,_data[], setData()
XMatrix : _sprites[], numSprites, addSprite()
XSprite frame : this is the object I'm using to draw the led matrix.
XMatrix has n XSprite[], I want to animate these sprites and update the frame object based on all these sprites.
So far, I can create XSprite frame, update its data and the led display updates as expected.
I am now trying to implement XMatrix.
When I instantiate new sprites (other than the frame), no problem, but once I add them to my Matrix class, the display doesn't work any more ![]()
I have been looking around for some kind explanation, but due to my lack of C knowledge it is pretty hard.
I am copying my code in hope for some generous soul to have a look, I have tried to keep things very simple and added comments.
Thank you in advance.
#include <TimerOne.h>
// GRAPHICS
byte A[] = {255,136,136,255,136,136,136}; //data representing letter A
byte B[] = {240,136,136,240,136,136,240}; // data representing letter B
// SHIFT REGISTER 595
int latchPin = 8;
int clockPin = 12;
int dataPin = 11;
int currentRow = 0;
byte rowsByte[] = {128,64,32,16,8,4,2,1};
class XSprite
{
public:
int _width; //num columns
int _height;//num rows
int _x;//position x in the XMatrix
int _y;//position x in the XMatrix
byte _data[7];//bytes for display
XSprite(int w, int h, int x, int y);//constructor
void setData(byte[7]);//set bytes for display
void move(int x, int y);//to update x y properties
};
//CONSTRUCTOR
XSprite::XSprite(int w, int h, int x, int y){
_width = w;
_height = h;
_x = x;
_y = y;
}
//COPY BYTE ARRAY IN _DATA
void XSprite::setData(byte data[7]){
for(int i = 0; i < _height; i++){
_data = data*;*
* }*
}
class XMatrix
{
* public:*
* int _numSprites;//number of sprites*
* XSprite _sprites[];//array to store sprites*
* //XSprite _frame; //REFERENCE TO XSPRITE FRAME CODE, COMPILE FAILS WHEN I UNCOMMENT*
* XMatrix();//constructor*
* void addSprite(XSprite sprite);//add sprite in array and increment counter*
};
//CONSTRUCTOR
XMatrix::XMatrix()
{
* numSprites = 0;
_}*
void XMatrix::addSprite(XSprite sprite)
{
* _sprites[_numSprites] = sprite;
numSprites++;
_}*
//FRAME - XSPRITE INSTANCE
XSprite frame(5,7,0,0);
//TWO XSPRITES INSTANCES, IDEALY I WOULD LIKE TO BE ABLE TO ADD XSPRITES DYNAMICALY
XSprite xA(5,7,0,0);
XSprite xB(5,7,6,0);
*//MATRIX - ONLY ONE INSTANCE *
XMatrix matrix;
void setup() {
* //SHIFT REGISTER*
* pinMode(latchPin, OUTPUT);*
* pinMode(clockPin, OUTPUT);*
* pinMode(dataPin, OUTPUT);*
* //update frame in background process : microseconds*
* Timer1.initialize(1000);*
* Timer1. attachInterrupt(render);*
* //THIS IS THE PART THAT FAILS, CODE COMPILES BUT THE LED DISPLAY SHOW A FEW RANDOM DOTS AND STAYS SO*
* //WHEN UNCOMMENTING THESE 2 LINES NO PROBLEM*
* //xA.setData(A);//copies data declared in the beginning*
* //xB.setData(B);//copies data declared in the beginning*
* //WHEN UNCOMMENTING ONE OF THESE LINES CODE COMPILES BUT DISPLAY DOESNT WORK*
* //matrix.addSprite(xA);//add XSprite to matrix*
* //matrix.addSprite(xB);*
}
void loop() {
* frame.setData(A);//copies data declared in the beginning*
* delay(1000);*
* frame.setData(B);//copies data declared in the beginning*
* delay(1000);*
}
//UPDATE DISPLAY
void render(){
* digitalWrite(latchPin, LOW);*
* shiftOut(dataPin, clockPin, LSBFIRST, frame._data[currentRow]);//NOT SURE THIS IS THE WAY I SHOULD ACCESS THE FRAME DATA BUT IT WORKS*
* shiftOut(dataPin, clockPin, LSBFIRST, rowsByte[currentRow]);*
* digitalWrite(latchPin, HIGH);*
* currentRow++;*
* currentRow%=7;*
}