how to use button->addaction to call a function

micdj
Posts: 31
Joined: Wed Jan 06, 2021 2:23 am

Post

Hi! I need to call a function when I use a button instead make a relay change, now I use this workaround:

Code: Select all

  void handleButton1Press() {
  Serial.println("Premuto tasto 1");
  if (mode == 1)  // "diretto"
    relay1->toggle();
  else
    nextStepState();
}
  .....
  auto button1 = new Supla::Control::Button(BUTTON1_PIN, true, true);
  //button1->setMulticlickTime(2000);  // tempo per il riconoscimento sequenze
  button1->addAction(Supla::TURN_ON, vrelay1, Supla::ON_PRESS);
  ...
  if (vrelay1->isOn()) {
    vrelay1->turnOff();
    handleButton1Press();
  }
Is there any other solution?
User avatar
lukfud
Posts: 2480
Joined: Thu Nov 23, 2017 11:33 pm
Location: Warszawa
Has thanked: 13 times
Been thanked: 11 times

Post

micdj wrote: Thu Aug 21, 2025 7:16 am

Code: Select all

enum list {
  ACTION1,
  ACTION2,
};

class CustomActions : public Supla::ActionHandler {
 public:
  CustomActions() {}

  void handleAction(int event, int action) {
    if (action == ACTION1) {
      //  do something
    }
    if (action == ACTION2) {
      //  do something else
    }
  }
};

CustomActions *cActions = new CustomActions;

(...)

button1->addAction(ACTION1, cActions, Supla::ON_CLICK_1);
button1->addAction(ACTION2, cActions, Supla::ON_CLICK_2);
https://www.facebook.com/groups/supladiy
https://www.facebook.com/groups/suplazamel
https://www.facebook.com/groups/suplaauraton
https://smartnerzeczy.pl
micdj
Posts: 31
Joined: Wed Jan 06, 2021 2:23 am

Post

Thank you for fast answer, I Will try as soon as possible.

Return to “Help”