How to pass value from ui_events to ui.ino

Hey everyone!

I’m new to LVGL, and I’m trying to do something with ESP32 and SquareLine Studio.
at now i create a little ui with some buttons. I would like to start some functions depending on the key I press. to lunch or not the functions i’m using boolen variables like in the example below.
how i can change the value of the variable from an button event (in ui_event.c) to change the running functions

thanks for your help

in ui.ino

bool VAR=false;

void setup() {
...bla bla..
}
void loop() {
 if (VAR)  function 1
else  function 2
}

in ui_event.c

void StartFunction(lv_event_t * e){

lv_label_set_text(ui_Label13, "PRESSED");

if (!VAL) VAL=true;  // the pressure of the button changes the VAL value like a toggle button
else VAL=false; 
}

Well I’m no expert and while I could tell you several ways to do this - they are likely wrong, but you probably don’t really need a variable - you can just do something like this in your ui.ino

if(lv_label_get_text(ui_Label13) == "PRESSED")
  // do something
else
  // do something else

or how about this:
ui.ino

void setup()
{
	//...bla bla..
}
void loop() 
{
	if(getVAL())
		//function 1
	else
		//function 2
}

ui_event.c

static bool VAL = false;

bool getVAL(void);

bool getVAL(void)
{
	return VAL;
}

void StartFunction(lv_event_t * e)
{
	lv_label_set_text(ui_Label13, "PRESSED");
	if (!VAL) VAL=true;  // the pressure of the button changes the VAL value like a toggle button
	else VAL=false; 
}

YEAAAHHHHHH thanks a lot… WoRKS

works this way too, but I prefer the first solution. probably more academic

thank you very much for your help.

I am new to this as well and I can’t get it to work either. Can you please clarify what needs to go in ui.ino and ui.c?

In ui.ino loop I added

if (getVAL()){
  Serial.println("Someone pressed the button!!!");     
}

in ui.c I added

static bool VAL = false;
bool getVAL(void);
bool getVAL(void)
{
return VAL;
}

void ui_event_goBtn( lv_event_t * e) {
lv_event_code_t event_code = lv_event_get_code(e);lv_obj_t * target = lv_event_get_target(e);
if ( event_code == LV_EVENT_CLICKED) {
VAL = 1;
}
}

when I try to compile it I get ‘getVAL’ not declared.

What am I not understanding on this learning curve? Thank you.

Hi @Mark_C ,

You will need the function prototype bool getVAL(void); visible to ui.c…

If you have a “ui.h” file which is included from “ui.ino” you could add the prototype to the “ui.h” file or you can add the prototype directly under the includes in your “ui.ino” file.

Hope that makes sense.

Kind Regards,

Pete