s00500 / ESPUI

A simple web user interface library for ESP32 and ESP8266

Home Page:https://valencia.lbsfilm.at/midterm-presentation/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to add a static value into the callback to use it in other functions?

AWSW-de opened this issue · comments

Hello,
i have a question that i could not figure out so far by reading the documentation and many of the older reports i found here...

I am using uo to 12 color selectors ESPUI that send their current value to the function "colCallew" listed below. Link to the project: https://github.com/AWSW-de/WordClock-16x16-LED-matrix

To decide to which of the 12 RGB values in the ESP32 preferences storage values the current RGB color code is stored to, i am currently using the non static "sender->id", which is not ideal of course... The 1st color selector currently has id 25 and the last one has currently 36... I tried what happens when i add a new control in front of the 12 color selectors and of course all IDs changed +1...

So my question is how to add a static value to such functions used here to be able to add own values to be used in the functions like
changing:

void colCallew(Control* sender, int type) {

to e.g.:

void colCallew(Control* sender, int type, int ewnum) {

But when i am adding this value the function call is no longer done and seems to want to have the sender and type value named too...

Thanks for any hint in advance :)

Here is my current solution with the not stable sender->id:


// ------------------------------------------------------------------------------------------------------

...

// Get the selected colors for the extra words 1 to 12:

  // Color Extra Word ew1: (Current ID = 25)
  char hex_ew1[7] = { 0 };
  sprintf(hex_ew1, "#%02X%02X%02X", redVal_ew1, greenVal_ew1, blueVal_ew1);
  uint16_t text_colour_ew1;
  text_colour_ew1 = ESPUI.text(ewtext1.c_str(), colCallew, ControlColor::Dark, hex_ew1);
  ESPUI.setInputType(text_colour_ew1, "color");

  // Color Extra Word ew2: (Current ID = 26)
  char hex_ew2[7] = { 0 };
  sprintf(hex_ew2, "#%02X%02X%02X", redVal_ew2, greenVal_ew2, blueVal_ew2);
  uint16_t text_colour_ew2;
  text_colour_ew2 = ESPUI.text(ewtext2.c_str(), colCallew, ControlColor::Dark, hex_ew2);
  ESPUI.setInputType(text_colour_ew2, "color");

  ...

  // Color Extra Word ew12: (Current ID = 36)
  char hex_ew12[7] = { 0 };
  sprintf(hex_ew12, "#%02X%02X%02X", redVal_ew12, greenVal_ew12, blueVal_ew12);
  uint16_t text_colour_ew12;
  text_colour_ew12 = ESPUI.text(ewtext12.c_str(), colCallew, ControlColor::Dark, hex_ew12);
  ESPUI.setInputType(text_colour_ew12, "color");

...

// ------------------------------------------------------------------------------------------------------


// ###########################################################################################################################################
// # GUI: Color change for Extra Word ew1 to ew12:
// ###########################################################################################################################################
void colCallew(Control* sender, int type) {
  sender->value.toUpperCase();
  char c[7];
  sender->value.toCharArray(c, 8);
  int red = hexcolorToInt(c[1], c[2]);
  int green = hexcolorToInt(c[3], c[4]);
  int blue = hexcolorToInt(c[5], c[6]);

  Serial.print("Sender: ");
  Serial.println(sender->id);
  int ewnum = sender->id;

  switch (ewnum) {
    case 25:
      {
        redVal_ew1 = red;
        greenVal_ew1 = green;
        blueVal_ew1 = blue;
        break;
      }
    case 26:
      {
        redVal_ew2 = red;
        greenVal_ew2 = green;
        blueVal_ew2 = blue;
        break;
      }
    
...

    case 36:
      {
        redVal_ew12 = red;
        greenVal_ew12 = green;
        blueVal_ew12 = blue;
        break;
      }
  }
}

There was a patch added a while back that allowed callbacks to carry additional data. The README shows you how to use an "extended callback function" although the example is a little overcomplicated.

Basically the final parameter to ESPUI.addControl can be anything, and it will appear as the param parameter in the callback. So therefore instead of relying on the ID remaining the same (which will, by the way, but I agree it is fragile) then you can addControl the controls with a static number and then check it in the callback.

Is that clear?

Hello,
Thanks for the reply. I will give it a try and will create an example to check if I get your reply correctly.

Thank you 😊

Hello,
worked fine like you explained it. Thanks again :)

Kind regards!