Unable to change text color on monochrome display

Hardware

  • nRF52840 DK
  • Adafruit LS027B7DH01 monochrome display breakout

Problem

I’m using LVGL as a Zephyr module. I was able to display a “Hello World” but it shows up as white text on a black background. I added the code from this post, which didn’t seem to do anything (still white text on black background). The code below is where I’m currently at, and I’m able to change the font but the text color still can’t be changed for some reason. Changing the background color using lv_style_set_bg_color does work (EDIT: turns out this actually crashes the program, added more info in my 2nd comment), which then produces a blank all-white screen. Am I doing something wrong here?

Code

void main(void)
{
	const struct device *display_dev;

	display_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));

	static lv_style_t text_style;
    lv_style_init(&text_style);
	lv_style_set_text_font(&text_style, &lv_font_montserrat_20);
	lv_style_set_text_color(&text_style, lv_color_black());
	lv_obj_t *hello_world_label = lv_label_create(lv_scr_act());
	lv_obj_add_style(hello_world_label, &text_style, 0);
	lv_obj_refresh_style(hello_world_label, LV_PART_ANY, LV_STYLE_PROP_ANY);
	lv_label_set_text(hello_world_label, "Hello world!");
	lv_obj_align(hello_world_label, LV_ALIGN_CENTER, 0, 0);

	lv_task_handler();
	display_blanking_off(display_dev);

	while (1) {
		lv_task_handler();
		k_sleep(K_MSEC(10));
	}
}

This is the code I had initially used based on what was provided by kisvegabor and the LVGL documentation:

	lv_obj_set_style_text_color(lv_scr_act(), lv_color_white(), LV_PART_MAIN);
	lv_obj_set_style_bg_color(lv_scr_act(), lv_color_white(), LV_PART_MAIN);
	lv_obj_t* label = lv_label_create(lv_scr_act());
	lv_label_set_text(label, "Hello World!");
	lv_obj_set_style_text_color(lv_scr_act(), lv_color_black(), LV_PART_MAIN);
	lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);

I’m not sure why this isn’t working either. When I include these two lines:

lv_style_set_bg_color(lv_scr_act(), lv_color_white());
lv_style_set_text_color(lv_scr_act(), lv_color_black());

I get a bus fault, so I assume the screen was only white because no data was sent to it.

The issue was related to lv_color_white and lv_color_black being interpreted differently for my display. Swapping them and avoiding the lv_style_set functions in favor of lv_obj_set_style fixed the issue. I’m still new to LVGL so I’m not sure why the former causes bus faults, but I’m just happy my display finally works at this point.

It caused bus fault because you passed lv_scr_act() instead of a style.

So it should have been either:

lv_style_set_text_color(&text_style, lv_color_black());

or

lv_obj_set_style_text_color(lv_scr_act(), lv_color_black(), LV_PART_MAIN);
1 Like

That makes sense, thank you!