Event callback function error occurs

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

STM32F103RCT6 (RAM 48 KB, custom board)
display: 128*160, RGB565(16 bit), ST7735/ST7789 lcd driver, SPI comm.
IDE: STM32CubeIDE

What LVGL version are you using?

the latest one (v8.0)

What do you want to achieve?

When I add lvgl examples that have event callback function, it makes hard fault error, I don’t know the reason. All of the examples with events make errors.
Do I need another custom function in main.c?

And I couldn’t find the information of lv_obj_add_event_cb() function.
When I’ve searched it, it was set_event_cb in previous version.

What have you tried so far?

Code to reproduce

main function

void App_Start(void)
{
// lcd init
			lv_init();
			LCD_Init();
			HAL_Delay(5);

// function for setting lcd and touch configuration
			lv_disp_config();
			lv_touch_config();

// put button example function 
            lv_example_btn_1();
}

indev setting

void lv_touch_config(void)
{
	static lv_indev_drv_t indev_drv;
	lv_indev_drv_init(&indev_drv);		// basic initialization
	indev_drv.type = LV_INDEV_TYPE_POINTER;

	static lv_indev_data_t* data;
	my_input_read(&indev_drv, &data);
	indev_drv.read_cb = my_input_read;
	lv_indev_t * my_indev = lv_indev_drv_register(&indev_drv);
}

my_input_read()

bool my_input_read(lv_indev_drv_t * drv, lv_indev_data_t*data)
{
	static int8_t lastX = 0;
	static int8_t lastY = 0;

	if(XPT2046_TouchPressed())
	{
		XPT2046_TouchGetCoordinates(&lastX, &lastY);
		data->state = LV_INDEV_STATE_PRESSED;
	}
	else
	{
		data->point.x = lastX;
		data->point.y = lastY;
		data->state = LV_INDEV_STATE_RELEASED;
	}
	return false;
}

example that I imported

static void event_handler(lv_obj_t * obj, lv_event_t event)
{
    LV_UNUSED(obj);
    if(event == LV_EVENT_CLICKED) {
        LV_LOG_USER("Clicked");
    }
    else if(event == LV_EVENT_VALUE_CHANGED) {
        LV_LOG_USER("Toggled");
    }
}
void lv_example_btn_1(void)
{
    lv_obj_t * label;

    lv_obj_t * btn1 = lv_btn_create(lv_scr_act(), NULL);
    lv_obj_set_pos(btn1, 10, 10);
    lv_obj_set_size(btn1, 80, 50);
    lv_obj_add_event_cb(btn1, event_handler, NULL);
    lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, -40);

    label = lv_label_create(btn1, NULL);
    lv_label_set_text(label, "Button");

    lv_obj_t * btn2 = lv_btn_create(lv_scr_act(), NULL);
    lv_obj_add_event_cb(btn2, event_handler, NULL);
    lv_obj_align(btn2, NULL, LV_ALIGN_CENTER, 0, 40);
    lv_obj_add_flag(btn2, LV_OBJ_FLAG_CHECKABLE);
    lv_obj_set_height(btn2, LV_SIZE_CONTENT);

    label = lv_label_create(btn2, NULL);
    lv_label_set_text(label, "Toggle");
}
#endif

Screenshot and/or video

spi setting
1234
12345

Your event handler function signature is outdated; please take a look at the newer example.

your code:

static lv_indev_data_t* data;
my_input_read(&indev_drv, &data);

my_input_read(lv_indev_drv_t * drv, lv_indev_data_t*data)
    data->point.x = lastX;
    data->point.y = lastY;

the source code make me confused, on compiling the source code, is that any warnings here? just like pointer incompatible. you just define the static pointer data, but you dont alloc the memory to it. may be, you should drop out the star, just liked that:

static lv_indev_data_t data;

I’m not sure why that manual call to my_input_read is there. There is no need to call the read function directly as LVGL will call it when necessary.

normally, we add some code to project, run it then some hardware fault occured. we dont know wich situation touch errors. maybe, he think the input function has any bugs, then he call this function manually (for debug purpose)

in some times, i do same things too(the program not work, call the function manually).

1 Like

@haorongMango says right. I called it because I wanted to test it if it was properly called or not.