-1

I need to define an array, that later on will be the name of a class, and will be pointed to.

For example (in a non pre-processor way):

const char *button_names[4]={"b0","b1","b2","b3"};

and later on (when it will be define in pre-processor), in a function it will be called as :

static Button2 button_names[i]

How can I define it that array to be in pre-processor?

Edit2:

Button2 *Buttons[8] = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr}; // Here I use an optional up to 8 Buttons.

and later on, after I read parameter file: i get the numSW

/* Below example is only to check if I can use such technique - I know it is not a good coding. Example here is only for up to 4 Buttons*/

void init_buttons()
{
        for (uint8_t i = 0; i < numSW; i++)
        {
                if (i == 0)
                {
                        static Button2 bt1;
                        Buttons[i] = &bt1;
                }
                else if (i == 1)
                {
                        static Button2 bt2;
                        Buttons[i] = &bt2;
                }
                else if (i == 2)
                {
                        static Button2 bt3;
                        Buttons[i] = &bt3;
                }
                else if (i == 3)
                {
                        static Button2 bt4;
                        Buttons[i] = &bt4;
                }

                Buttons[i]->begin(buttonPins[i]);
                if (buttonTypes[i] == 0) /* On-Off Switch */
                {
                        Buttons[i]->setPressedHandler(OnOffSW_ON_handler);
                        Buttons[i]->setReleasedHandler(OnOffSW_OFF_handler);
                }
                else if (buttonTypes[i] == 1) /* Momentary press */
                {
                        Buttons[i]->setPressedHandler(toggle_handle);
                }
                Buttons[i]->setID(i);
        }
}
8
  • 2
    Could you give some background on what you are trying to achieve? Commented Aug 18, 2022 at 16:30
  • @EdgarBonet - Well it is complicated... I do not want to hard code the number of Button/ Relays each board have. it can be a dual relay, and possibly up to 16 relays on-board. The amount of Buttons/Relay are stored in a paramter file which obviously read after all classes have been defined and created. So .... see edit 2 in a sec.
    – guyd
    Commented Aug 18, 2022 at 16:36
  • 1
    I cannot see the usefulness of these preprocessor macros.Please, explain what you are trying to achieve rather than how (e.g. with the preprocessor) you think you may achieve it. So far this looks like an XY question. Commented Aug 18, 2022 at 16:36
  • @EdgarBonet I hope Edit 2 is clearing that up
    – guyd
    Commented Aug 18, 2022 at 16:41
  • 1
    use dynamic allocation with new
    – Juraj
    Commented Aug 18, 2022 at 16:54

1 Answer 1

0
void init_buttons()
{

        for (uint8_t i = 0; i < numSW; i++)
        {
                Buttons[i] = new Button2;   // <--- **Thnx to Juraj**!!!
                Buttons[i]->begin(buttonPins[i]);
                if (buttonTypes[i] == 0) /* On-Off Switch */
                {
                        Buttons[i]->setPressedHandler(OnOffSW_ON_handler);
                        Buttons[i]->setReleasedHandler(OnOffSW_OFF_handler);
                }
                else if (buttonTypes[i] == 1) /* Momentary press */
                {
                        Buttons[i]->setPressedHandler(toggle_handle);
                }
                Buttons[i]->setID(i);
        }
}
3
  • why not Button2* buttons = new Button2[numSW];? then you can do buttons[i].begin()
    – Juraj
    Commented Aug 18, 2022 at 18:03
  • I need Buttons[] to be defined at the beginning of code, meaning "global" (to have access to instances). I define it inside a function, since that was I can use nunSW that was read from parameter file in flash.
    – guyd
    Commented Aug 18, 2022 at 18:09
  • 1
    sorry, yes, in setup() buttons = new Button2[numSW]; while Button2* buttons; is global
    – Juraj
    Commented Aug 19, 2022 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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