How to create indexable set of class objects in Arduino (using platformIO)

55 Views Asked by At

Noob (and a hobbyist) hobbyist question.

I have a class library that I can feed instances of with data from arrays and structures by indexing through those arrays, but I can't for the life of me find a simple way to also index each instance of the class.

What I'm having to do is instantiate discrete instances #include <myclass.h>

Instantiate each class for each button myclass class0; .. myclass classN;

Then I have to manually populate an array with the results of all the tests bool myTests[] {class0.result(),class1.result(),..,classN.result()};

before I can use the test results in digitalWrite() calls to outputs.

I'd really like to be able to instantiate my methods sort of like an array and just iterate over the methods at the same time as I'm iterating over the outputs the results are for.

AFAIKT, the Arduino platform doesn't do vectors, although I'm not entirely sure that's the "droid" I'm looking for, either.

Anybody got a basic idea of how to do this at my nuts-and-bolts/beginnerish level?

Using platformIO with Arduino platform.

I've tried instantiating as an array, for(int i = 0; i < N; i++){ myclass myinstance[i]; } Of course, that was an epic fail. It's OK to laugh, I figured it wasn't going to work.

Tried to access vectors, nup, not on the Arduino platform, IDE or PIO.

Tried some things that, in hindsight are too stupid to mention, from what I've read while googling my brains out.

And, yep, I've googled this for maybe four hours, and I'm convinced I don't even know enough about this sort of thing to get my search terms right. Also poured over this venerable site, as is often my first resort.

Surely the gods of C++ have an elegant solution for this? Maybe kindly steer me to a tutorial and kick my bum as I head off there...

Cheers Crunchy

2

There are 2 best solutions below

1
Frodyne On

Your question is not very clear, but does something like this give you a hint or a way to move forward?

void myFakePrintFunc(int) {}

// Dummy class with a result() function
// The constructor takes 2 arguments just because
class myClass {
   public:
    myClass(int i, bool notUsed) : m_i{i} {}
    int result() const { return m_i; }

   private:
    int m_i;
};

int main() {
    constexpr int SIZE = 3;

    // Make an array of 3 myClass - you don't _need_ the SIZE here, but if it is there the compiler yells at you if there is a mismatch between the array size and the number of objects in the initializer list
    // The small lists are the constructor arguments (2 to show how it works)
    myClass myClasses[SIZE] = {{1, true}, {2, false}, {3, true}};

    // Use SIZE to make a bool array of the same size - here you need SIZE
    // Then populate it with the results
    bool myResults[SIZE];
    for (int i = 0; i < SIZE; ++i) {
        myResults[i] = myClasses[i].result();
    }

    // Or use the range-for loop, if you don't need to care about indexes
    for (const auto& mc : myClasses) {
        myFakePrintFunc(mc.result());
    }
}

Also from a quick Google search it looks like there is a Vector library that you can install for Arduino.

But this is all rather basic C++, so I think you may also get a lot out of just reading a good C++ book.

0
hcheung On

Without your actual code, we can only guess what you exactly want. I think this is what you are looking for. You don't really need Vector, you can accomplish with an array that holding the pointers to each class instance.

Button.h


class Button
{
    public:
        Button() {};
        ~Button() {};
        void test(uint8_t btn);
};


void Button::test(uint8_t btn) {
    Serial.print("test button ");
    Serial.println(btn);}

Arduino Code

#incude <Button.h>

Button *buttons[10]{nullptr}; // array of pointer to each Class instances


void setup() {
    for (int i=0; i<10; i++)
        buttons[i] = static_cast<Button*> (new Button()); // create each instances

    for (int btn=0; btn<10; btn++) // calling each instance's method
        buttons[btn]->test(btn);
}

void loop() {

}

If this is not what you want, please edit your post and includes your code and elaborates the problem you are facing.

BTW, Arduino does supports Vector, it just that it does not come with the default C++ standard library.