Function calling with params ?

Hi all, I would like to know if it is possible to execute functions with parameters. My case is the following:

I have a display designed to control the on-off and brightness of a couple of GPIO, ok, so far so good.
Then I have a button, which is used to access this screen.

My doubt comes at the moment of mixing both functionalities, I want to call the same screen from different buttons, so that this “recycled” screen has different parameters, as it could be to point to other GPIO pairs, because the functionality would not change at all.

Is that possible ? thank you very much for your help and time.

Hi, I can help you complete your forum post with the additional information about adding an event to a button in Squareline Studio. Here’s your completed post with the additional information:

One approach that works well without modifying the generated Squareline code is to use a global parameter structure:

// Global parameter structure for screen configuration
typedef struct {
    int gpio1;
    int gpio2;
    // Add more parameters as needed
} screen_params_t;

// Global instance to store current parameters
screen_params_t current_screen_params;

Then, in your button event handlers, set these parameters before loading the screen:

void button1_handler(lv_event_t * e) {
    if(lv_event_get_code(e) == LV_EVENT_CLICKED) {
        // Set parameters for first GPIO pair
        current_screen_params.gpio1 = 5;
        current_screen_params.gpio2 = 6;
        
        // Load the screen
        lv_disp_load_scr(ui_Screen1);
    }
}

void button2_handler(lv_event_t * e) {
    if(lv_event_get_code(e) == LV_EVENT_CLICKED) {
        // Set parameters for second GPIO pair
        current_screen_params.gpio1 = 12;
        current_screen_params.gpio2 = 13;
        
        // Load the screen
        lv_disp_load_scr(ui_Screen1);
    }
}

In your screen’s event callbacks, use the stored parameters:

void ui_event_OnOffSwitch(lv_event_t * e) {
    if(lv_event_get_code(e) == LV_EVENT_VALUE_CHANGED) {
        bool state = lv_obj_has_state(ui_OnOffSwitch, LV_STATE_CHECKED);
        
        // Use stored parameters instead of hardcoded values
        gpio_set_level(current_screen_params.gpio1, state);
        gpio_set_level(current_screen_params.gpio2, state);
    }
}

You can also update screen labels when the screen loads to reflect which GPIOs are being controlled:

void ui_event_Screen1(lv_event_t * e) {
    if(lv_event_get_code(e) == LV_EVENT_SCREEN_LOADED) {
        char label_text[32];
        sprintf(label_text, "GPIO %d & %d Control", 
                current_screen_params.gpio1, 
                current_screen_params.gpio2);
        lv_label_set_text(ui_TitleLabel, label_text);
    }
}

This approach keeps your Squareline-generated code intact and gives you the flexibility you need.

To set this up in Squareline Studio, you need to add an event to your button by:

  1. Select the button in Squareline Studio
  2. Go to the Events tab in the properties panel
  3. Add a new event with the trigger type “Clicked”
  4. Choose “Call function” as the action type
  5. Enter your function name (e.g., “button1_handler”) in the function name field

Hope this helps!