Referencing a panel element by a variable

Support forum for MegunoLink
Post Reply
J_aktv8
Posts: 4
Joined: Wed Mar 17, 2021 6:33 pm

Wed Mar 17, 2021 7:57 pm

Hello,
I'm making a tester that tests 12 devices at once, and I want my Interface Panel to have 12 buttons that I turn green or red depending on if a particular device passes or fails a standard test. Each of the 12 buttons corresponds to a device. Example Interface Panel included below.

Is there a way to accomplish this?:
for(i = 0; i++; i < 12){
if(a(i) and b(i) and c(i) and (d(i) > e(i))){
mystring = "IPButton"+i; // I try to make a string out of my index i
MyPanel.SetText(F(mystring), F("Device has passed the test!")); // the button related to device i is updated, if only (mystring) would evaluate to IPButton1
}
}
I want to chose to update a particular button based on the logic. There's a nontrivial amount of logic going into the decision to update each one and it's all exactly the same so I'd hate to have to copy it 12 times and end up maintaining 12 instances of the same logic just to point at the UI elements.

Thank you!
Attachments
meguonolinkquestion.JPG
meguonolinkquestion.JPG (69.66 KiB) Viewed 11377 times
J_aktv8
Posts: 4
Joined: Wed Mar 17, 2021 6:33 pm

Thu Mar 18, 2021 4:51 pm

Hey everybody,
I figured out a way to pull this off. This is a not very clean little bit of code but it works to get the idea across. We can use itoa() to turn our int index into a char and then put that char into our array of chars that is our container for the element (in this case button) name. We can send that char into SetText without using F().

#include <MegunoLink.h>
#include <string.h>
InterfacePanel MyPanel;

char buttons[11];
int tester = 3;

void setup()
{
Serial.begin(9600);
}

void loop()
{
buttons[0] = 'I';
buttons[1] = 'P';
buttons[2] = 'B';
buttons[3] = 'u';
buttons[4] = 't';
buttons[5] = 't';
buttons[6] = 'o';
buttons[7] = 'n';
buttons[8] = '1';
char mychar[2];
itoa(tester, mychar, 10);
buttons[9] = mychar[0];
buttons[10] = 0;

MyPanel.SetText(buttons,F("it worked!"));
delay(1000);
MyPanel.SetText(buttons,F("backagain!"));
delay(1000);
tester++;
if(tester > 9){
tester = 1;
}
}

Which produces output like this:

{UI|SET|IPButton17.Text=it worked!}
{UI|SET|IPButton17.Text=backagain!}
{UI|SET|IPButton18.Text=it worked!}
{UI|SET|IPButton18.Text=backagain!}
{UI|SET|IPButton19.Text=it worked!}
{UI|SET|IPButton19.Text=backagain!}
{UI|SET|IPButton11.Text=it worked!}
{UI|SET|IPButton11.Text=backagain!}
Post Reply