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

Extract the position number of a color from a string

serlancelot opened this issue · comments

Hello! question.... how can I get the number of each color corresponding to the String?
I've tried indexOf but get nothing, I've never dealt with strings.

ESPUI.addControl(Separator, "Colour", "", None, conftab); static String coloursel[] {"Red", "Green", "Blue"}; auto mainselector = ESPUI.addControl(Select, "Colour", "Colour", Wetasphalt, conftab, guardaridioma); for(auto const& v : coloursel) { ESPUI.addControl(Option, v.c_str(), v, None, mainselector); }

The idea is that when selecting the color the lighting is changed, it would load the value of the String in a variable and then with "if" or "Switch case" the corresponding ones would be turned on.

Thank you!

Sorry I don't know what you mean by "the chain".

When the selector is changed you will get "Red" "Green" or "Blue" in the callback, is that what you're asking?

Ugh sorry... I meant String.
String and Chain in my language is the same and I lost my head...

Sorry I didn't see the other part of the question...
No nope I mean that's what I'll show you an example
String color[3] {"Red", "Green", "Blue"}
If I want to select red, I indicate that position 0 loads the value, green 1 and blue 2, correct?
but with the drop-down options menu the colors are visible and for example with Serial.println(sender->value); It sends the selected color, but I wanted it to indicate the position, that is, 0,1,2
Is that possible?

Ah right "chain" === "string", I can see that! :)

As for everything else though, your question is too vague for me to really get at your answer. So I'm going to write some stuff, you can see if it helps.

So you seem to be referring to the need to use both the String value (i.e. "red") and the index (i.e. 0).

Your code uses an auto iterator to add the options:

for(auto const& v : coloursel) {
    ESPUI.addControl(Option, v.c_str(), v, None, mainselector); 
}

But you could have used an index iterator if you also needed the number:

#include <vector>
std::vector<String> coloursel {"Red", "Green", "Blue"};

for(int i = 0; i < coloursel.size(); i++) {
    ESPUI.addControl(Option, coloursel[i].c_str(), i, None, mainselector); 
    //coloursel[i].c_str() is the String value, i is the numerical index
}

Here, we switched from a static array of Strings to use a vector because in C arrays don't know how big they are but vectors do, and so we can more easily use things like coloursel.size() to iterate an index.

So you can use loops like that to check both the index and the value. Maybe that helps?

Hello, thanks for the answer, yes, I want to use the value (to be able to see it on the screen) and the index or address to save it in a variable. If I have not misunderstood... I would have to replace the for loop (original) with the one you indicate, add the #include and the vector with the string, right?
Original

ESPUI.addControl(Separator, "Colour", "", None, conftab); 
static String coloursel[] {"Red", "Green", "Blue"}; 
auto mainselector = ESPUI.addControl(Select, "Colour", "Colour", Wetasphalt, conftab, guardaridioma); //ir a la funcion Guardar Iluminacion del Interior Ordenador Material Acrilico (reducir el nombre...)
for(auto const& v : coloursel) { 
ESPUI.addControl(Option, v.c_str(), v, None, mainselector); }

modification

#include <vector>
std::vector<String> coloursel {"Red", "Green", "Blue"}; //genera un contenedor del tipo string y conoce los tamaños¿?
....................
..................
..............
ESPUI.addControl(Separator, "Colour", "", None, conftab); 
//static String coloursel[] {"Red", "Green", "Blue"}; 
auto mainselector = ESPUI.addControl(Select, "Colour", "Colour", Wetasphalt, conftab, guardaridioma); //ir a la funcion Guardar Iluminacion del Interior Ordenador Material Acrilico(reducir el nombre...)
for(int i = 0; i < coloursel.size(); i++) { //utiliza el tamaño de la cadena para definir el maximo de la variable "i"
    ESPUI.addControl(Option, coloursel[i].c_str(), i, None, mainselector); 
    //coloursel[i].c_str() is the String value, i is the numerical index
}
/*for(auto const& v : coloursel) { 
ESPUI.addControl(Option, v.c_str(), v, None, mainselector); }*/

If so, I'll try it today and see what happens... Thanks!

Hello! Just a quick comment... I've been away and I couldn't do much. I modified it as you indicated but it gives me an error when compiling.

Tomorrow I will try to post the error so you can see it. Thanks for everything and sorry for the delay in answering.
All the best!

This is the error...

error: converting to 'String' from initializer list would use explicit constructor 'String::String(int, unsigned char)'
     ESPUI.addControl(Option, coloursel[i].c_str(), i, None, mainselector4); 
                                                                          ^

 initializing argument 3 of 'uint16_t ESPUIClass::addControl(ControlType, const char*, const String&, ControlColor, uint16_t)'
     uint16_t addControl(ControlType type, const char* label, const String& value, ControlColor color, uint16_t parentControl);
              ^

exit status 1

Compilation error: converting to 'String' from initializer list would use explicit constructor 'String::String(int, unsigned char)'

Hello Ian, nothing... I don't know what to do. I have tried it with an example of the library and copying and pasting only the example and removing the rest of the tabs and buttons... but the error continues. How can I solve that? I would like to do it with the web interface (I could put some buttons and do something but it already loses everything interesting on the web...)
You'll tell me, thanks for all your patience and help!

Finally I managed to solve it! but otherwise it is anything but elegant... tomorrow I will indicate that now I am going to sleep, it is 3 AM in the morning... Regards!

Well, in the end I solved it by comparing the strings, create one for each color and with
if (String.equals(sender->value)) To compare red, green and blue.

String colorro, colorve, coloraz; // cadena comparacion color rojo verde azul
//part of code
--------
------
all the same as in the example espui
-------.
------

//seccion para comparar y guardar iluminacion de interior ordenador material acrilico
void guardaridioma(Control *sender, int type){ 
EEPROM.begin(100); //inicia la eeprom con 100 espacios hasta el 96 lo usa la configuracion wifi
if (colorro.equals(sender->value)) { //comprueba si el valor seleccionado es el rojo
  color = 0; //carga valor en la variable
  EEPROM.write(99,color); //guarda en la direccion el valor
  EEPROM.end(); // finaliza la memoria
}

probably not the best way... but it works!

Thanks Ian for the help! I close this thread. And now for the next step in the application and a new problem to solve...