Metadata being fed continuously line by line , how( code functions ) to capture on scrolling screen 240 x320 lilygo THMI screen like serial monitor ( Arduino ). deleting previous data continuously and keeping readable scrolling speed ? can any one help on this topic ,
I implemented it using chatgpt, I’m not sure if this is the effect you’re looking for
#define BUF_SIZE 1024 // Total buffer size
#define MAX_DISPLAY_LEN 256 // Maximum number of characters displayed by the label
#define TIMER_PERIOD 1000 // Timer period (in milliseconds)
static char buf[BUF_SIZE]; // Buffer to store received data
static size_t write_index = 0; // Current write position in the buffer
static size_t data_counter = 0; // Counter to generate simulated data with unique IDs
static char display_buf[MAX_DISPLAY_LEN + 1]; // Temporary buffer for display
static lv_obj_t* label; // Label widget
/**
-
Function to simulate receiving new data
-
@param data Simulated received data
*/
void simulate_receive_data() {
char data[64];
// Generate simulated data with a unique ID
snprintf(data, sizeof(data), “New data %zu…\n”, data_counter++);
size_t data_len = strlen(data);// Check if the buffer will overflow
if (write_index + data_len >= BUF_SIZE) {
// If it overflows, start overwriting from the beginning
write_index = 0;
}// Write the data into the buffer
strncpy(buf + write_index, data, data_len);
write_index += data_len;// Add a null terminator to ensure the data is valid
buf[write_index] = ‘\0’;
}
/**
- Timer callback function: Simulate data reception periodically
/
static void timer_cb(lv_timer_t timer) {
// Simulate receiving new data from the serial port
simulate_receive_data();
}
/**
-
Timer callback function: Update the label content
/
static void update_label_cb(lv_timer_t timer) {
// Read the last MAX_DISPLAY_LEN characters from the buffer
size_t len = strlen(buf);
if (len > MAX_DISPLAY_LEN) {
strncpy(display_buf, buf + len - MAX_DISPLAY_LEN, MAX_DISPLAY_LEN);
}
else {
strncpy(display_buf, buf, len);
}
display_buf[MAX_DISPLAY_LEN] = ‘\0’; // Ensure the string is null-terminated// Update the label widget with the new content
lv_label_set_text(label, display_buf);
}
/**
-
Create a label widget and initialize it
/
void create_serial_display(lv_obj_t parent) {
// Create the label widget
label = lv_label_create(parent);
lv_obj_set_width(label, lv_pct(100)); // Set label width
lv_obj_set_height(label, lv_pct(100)); // Set label height
//lv_label_set_long_mode(label, LV_LABEL_LONG_WRAP); // Enable text wrapping
lv_obj_align(label, LV_ALIGN_TOP_LEFT, 0, 0); // Align label to the top-left corner// Set the default content for the label
lv_label_set_text(label, “Waiting for data…\n”);// Clear the buffer
memset(buf, 0, sizeof(buf));
memset(display_buf, 0, sizeof(display_buf));
}
void lv_example_label_5(void)
{
lv_obj_t* cont = lv_obj_create(lv_scr_act());
lv_obj_set_size(cont, 200, 200);
label = lv_label_create(cont);
lv_obj_align(label, LV_ALIGN_TOP_LEFT, 10, 10);
lv_timer_create(timer_cb, TIMER_PERIOD, NULL);
lv_timer_create(update_label_cb, TIMER_PERIOD, NULL);
}