The simplest would be to make your label global, so you can access (change) the label from anywhere.
You also have a potential problematic code in ble_written function:
When the string from characteristic is for ever reason longer than your buffer (the remoteCharArray), it might result in a crash.
The code would look like the following:
String message = "Hello World";
lv_obj_t* gtLabel = NULL; // make the label object global
void display_screen ()
{
lv_obj_set_style_bg_color (lv_scr_act (), lv_color_black (), LV_PART_MAIN);
// Create a white label, set its text and align it to the center
gtLabel = lv_label_create (lv_scr_act ());
lv_label_set_text (gtLabel, message.c_str ());
//lv_obj_set_style_text_color(lv_scr_act(), lv_color_hex(0xffffff), LV_PART_MAIN);
lv_obj_set_style_text_color (lv_scr_act (), lv_color_white (), LV_PART_MAIN);
lv_obj_set_style_text_font (gtLabel, &lv_font_montserrat_24, 0);
lv_obj_align (gtLabel, LV_ALIGN_CENTER, 0, 0);
}
#define MAX_REMOTE_CHAR 22
void ble_written (BLECentral& central, BLECharacteristic& characteristic)
{
char remoteCharArray[MAX_REMOTE_CHAR];
remoteCharArray[0] = '\0';
strncat (remoteCharArray, characteristic.value (), MAX_REMOTE_CHAR - 1);
if (gtLabel) {
lv_label_set_text (gtLabel, remoteCharArray);
}
Serial.println (remoteCharArray);
}
Just a quick fix: within the file where your ble_written is (e.g. after all the includes, before your functions) write:
(The better way would be a proper include file, with all the extern and prototypes for the specific implementation file)
extern lv_obj_t* gtLabel;
So now the compiler nows the type (how to handle the variable gtLabel).
This is great, @robekras! Thank you. I am using Arduino, so I need to figure out the extern part, but will try to follow what you recommend by creating a globals.h or something similar. It’s giving me a ‘undefined reference’ error now but that’s probably an Arduino loading thing.
I could also create a function which is in my display.ccp and call it from the ble_written function in the ble.cpp, but this well off the intent of the original post, which you solved. Thank you again.