Problems accessing user_data

Hello,

First post in the forum. Hello everyone!

Enjoying the programming of a ESP32 system that embeds a 480x320 screen. I’m having problems creating an array of controls and accessing the individual user_data of each one of the buttons.

The intention is to create a maximum of 10 groups of controls. Each group is composed by a list, label and a spinbox.

I’m able to create the user interface without problems, but the events linked with the buttons responds to all of them, no matter which one I press. I have read some threads about the same issue, but could not find a solution.

// this is the class I created
static const uint8_t numberOfSteps = 10;
class SemiAutomaticStep
{
public:
	lv_obj_t *container;
	lv_obj_t *stepOptions;
	lv_obj_t *labelTime;
	lv_obj_t *timeSpinbox;
	lv_obj_t *timeIncButton;
	lv_obj_t *timeDecButton;
	WorkingMode workingMode;
	uint16_t time;
	uint16_t position;
	SemiAutomaticStep() {}
};
static SemiAutomaticStep semiAutomaticSteps[numberOfSteps];

//this is the function to create the groups (all the controls inside an object)
static void IRAM_ATTR addStep(SemiAutomaticStep step)
{
	static lv_style_t style_container;
	lv_style_init(&style_container);
	lv_style_set_bg_color(&style_container, lv_palette_main(LV_PALETTE_GREY));

	step.container = lv_obj_create(semiAutoStepsContainer);
	lv_obj_set_size(step.container, 314, 70);
	lv_obj_set_pos(step.container, -12, -12 + (step.position * 72));
	lv_obj_add_style(step.container, &style_container, 0);

	char buffer[1024];
	snprintf(buffer, sizeof(buffer), "Air%i\nWater%i\nNone%i", step.position, step.position, step.position);
	step.stepOptions = lv_dropdown_create(step.container);
	lv_dropdown_set_options(step.stepOptions, buffer);
	lv_obj_set_size(step.stepOptions, 95, 40);
	lv_obj_set_pos(step.stepOptions, -10, 0);

	step.labelTime = lv_label_create(step.container);
	lv_label_set_recolor(step.labelTime, true);
	lv_label_set_text_fmt(step.labelTime, "#FCFCFC Secs");
	lv_obj_set_pos(step.labelTime, 95, 10);

	//  timer spin box
	step.timeSpinbox = lv_spinbox_create(step.container);
	lv_obj_set_size(step.timeSpinbox, 50, 40);
	lv_obj_set_pos(step.timeSpinbox, 190, 0);
	lv_spinbox_set_range(step.timeSpinbox, 0, 120);
	lv_spinbox_set_digit_format(step.timeSpinbox, 3, 0);
	lv_spinbox_set_step(step.timeSpinbox, 1);
	lv_spinbox_set_value(step.timeSpinbox, getManualTime());
	// timer inc button
	step.timeIncButton = lv_btn_create(step.container);
	lv_obj_set_size(step.timeIncButton, 40, 40);
	lv_obj_align_to(step.timeIncButton, step.timeSpinbox, LV_ALIGN_OUT_RIGHT_MID, 3, 0);
	lv_obj_set_style_bg_img_src(step.timeIncButton, LV_SYMBOL_PLUS, 0);
	lv_obj_add_event_cb(step.timeIncButton, semiAutoModeTime_spinbox_increment_event, LV_EVENT_ALL, (void*)&step);

	Serial.printf("Add event with user_data=%i\r\n", stepCounter);
	// timer dec button
	step.timeDecButton = lv_btn_create(step.container);
	lv_obj_set_size(step.timeDecButton, 40, 40);
	lv_obj_align_to(step.timeDecButton, step.timeSpinbox, LV_ALIGN_OUT_LEFT_MID, -3, 0);
	lv_obj_set_style_bg_img_src(step.timeDecButton, LV_SYMBOL_MINUS, 0);
	lv_obj_add_event_cb(step.timeDecButton, semiAutoModeTime_spinbox_decrement_event, LV_EVENT_ALL, (void*)&step);
	Serial.printf("Add event with user_data=%i\r\n", stepCounter);
}

// and these are the event handlers
void semiAutoModeTime_spinbox_decrement_event(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	lv_obj_t *target = lv_event_get_target(e);
	if (code == LV_EVENT_SHORT_CLICKED || code == LV_EVENT_LONG_PRESSED_REPEAT)
	{
		static SemiAutomaticStep *step= (SemiAutomaticStep *)lv_event_get_user_data(e);
		Serial.printf("UserData is: %d\r\n", step->position);
	}
}

void semiAutoModeTime_spinbox_increment_event(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	lv_obj_t *target = lv_event_get_target(e);
	if (code == LV_EVENT_SHORT_CLICKED || code == LV_EVENT_LONG_PRESSED_REPEAT)
	{
		static SemiAutomaticStep *step= (SemiAutomaticStep *)lv_event_get_user_data(e);
		Serial.printf("UserData is: %d\r\n", step->position);
	}
}


This is the user interface. I want to know which of the ‘+’ or ‘-’ buttons are pressed without having to write an individual function for each control.

Could anyone give me a clue about how to know which button raised the event? Am I using a good strategy or there’s a better method?

Thanks in advance,
Paco

Hello,

Problem solved by myself. Casting has the power… :wink:

Regards,
PacoLM

1 Like

Great! :slight_smile: