How to use a variable in both ui.c and main.c

I want to use a variable that will be changed by ui.c in my main.c
If i declare the variable inside ui.c, then it is not recognized in the main.c (amd the opposite).

main.c
void loop()
{
serial.print(var1);
}

ui.c
int var1;
void ui_event_onoffpano(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_GESTURE && lv_indev_get_gesture_dir(lv_indev_get_act()) == LV_DIR_LEFT) {

var1=0;}
if(event_code == LV_EVENT_GESTURE && lv_indev_get_gesture_dir(lv_indev_get_act()) == LV_DIR_RIGHT) {
var1=1; }
}

(The next step is to have a boolean value and switch a relay on and off-from a widget-).
Should i use it as a global? If yes, how?

What MCU/Processor/Board and compiler are you using?

ESP32

What LVGL version are you using?

8…3

Arduino edition

Arduino 1.8.19

Thanks in advance!

Hi @Evangelos ,

See below…

/*main.c*/

extern int var1;  // If your variable is declared globally in ui.c you need to declare it external here.
void loop()
{
serial.print(var1);
}

/*ui.c*/
int var1;
void ui_event_onoffpano(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_GESTURE && lv_indev_get_gesture_dir(lv_indev_get_act()) == LV_DIR_LEFT) {


var1=0;}
if(event_code == LV_EVENT_GESTURE && lv_indev_get_gesture_dir(lv_indev_get_act()) == LV_DIR_RIGHT) {
var1=1; }
}

You can also declare and use your boolean relay flag in the same way.

Hope that helps.

Kind Regards,

Pete

Pete, you are the best!
Great explanation, how i must do it!
All worked!
I can’t thank you enough!

(if i was a prist i would say: God bless you!!! xexe)
Thanks a lot!

1 Like