2

So, I'm trying to make a custom library to drive a 8x8 Bi-Color LED Matrix from 2 MAX7219's that incorporates two "Matrix" objects from the "Matrix" library and I cannot, for the life of me, figure out how to initialize them in my class. Can someone help me? By the way, I have been programming for 8 years, and consequently have a decent knowledge of the topic! Here is my current code:

header file

#include "Arduino.h"
#include "Matrix.h"
#include "Sprite.h"


class ZackBiColorMatrix{

    public:


        ZackBiColorMatrix(byte dataR, byte loadR, byte clockR, byte dataG, byte loadG, byte clockG);

        Matrix r;
        Matrix g;

        void setLED(byte col, byte row, byte val, char color);
        void writeSprite(byte x, byte y, Sprite sp, char color);



    private:



        byte redData;
        byte redLoad;
        byte redClock;

        byte greenData;
        byte greenLoad;
        byte greenClock;


};

and the .cpp file:

#include "Arduino.h"
#include "Sprite.h"
#include "Matrix.h"
#include "ZackBiColorMatrix.h"



ZackBiColorMatrix::ZackBiColorMatrix (byte dataR, byte loadR, byte clockR, byte dataG, byte loadG, byte clockG) {



    redData = dataR;
    redLoad = loadR;
    redClock = clockR;

    greenData = dataG;
    greenLoad = loadG;
    greenClock = clockG;

    r = Matrix (redData, redClock, redLoad);
    g = Matrix (greenData, greenClock, greenLoad);

}


void ZackBiColorMatrix::setLED (byte col, byte row, byte val, char color) {

    if (color == 'g') {

        g.write (col, row, val);

    }
    else if (color == 'r') {

        r.write (col, row, val);

    }

}
void ZackBiColorMatrix::writeSprite (byte x, byte y, Sprite sp, char color) {

    if (color == 'g') {

        g.write (x, y, sp);

    }
    else if (color == 'r') {

        r.write (x, y, sp);

    }

}

Also, I use the Eclipse Arduino IDE if that matters.

  • What you have in your constructor is perfectly valid C++, and it will initialise the member variables. – Kingsley Jan 11 '16 at 19:30
1

This is a C++ question actually.

In C++, you initialize class members in the class constructor definition by using a "member intialization list" as follows for your example:

ZackBiColorMatrix::ZackBiColorMatrix (byte dataR, byte loadR, byte clockR, byte dataG, byte loadG, byte clockG)
:   r(dataR, clockR, loadR),
    g(dataG, clockG, loadG) {
    ... Additional initialization here
}

This is a special C++ construct (note the colon : after the constructor declaration but before its implementation).

Also note that, in your example, I think you don't need to declare redData, redLoad, redClock, greenData, greenLoad, greenClock, sicne they are not being used once your 2 Matrix fields have been initialized.

Hence you could simplify your class further, I think.

| improve this answer | |
  • 1
    Hey @ZackElec, if the answer helped you, please consider accepting it. You can do it by clicking the checkmark on the left-hand side of the question, just below the up/down vote arrows and the question vote count. Once you get 15 reputation points, you can also upvote the question. Thanks!! – Ricardo May 22 '14 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.