以下代码在运行中出现了如是错误_lv_inv_area: Asserted at expression: !disp->rendering_in_progress Invalidate area is not allowed during rendering. lv_refr.c:257:#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include "chinese.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
int sockfd=-1;
static lv_obj_t* keyboard;//键盘
static lv_obj_t* input_area;//输入区
static lv_obj_t* send_btn;//发送按钮
lv_obj_t* scroll_container;//滚动容器
lv_obj_t* msg_container; // 消息容器
pthread_t recv_thread;
void Add_event_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
//获取被点击的文本区域对象
lv_obj_t * mbox = lv_event_get_target(e);
if(code == LV_EVENT_FOCUSED) {
// 文本区域被点击并获得了焦点
printf("Textarea focused\n");
//按照文本区域对象来调整键盘的绑定对象
if (mbox != NULL) {
// 显示键盘
lv_obj_remove_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
}
}
else if(code == LV_EVENT_DEFOCUSED) {
// 文本区域失去焦点
printf("Textarea defocused\n");
// 在这里可以添加其他逻辑,例如隐藏键盘等
// 隐藏键盘
lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
}
}
void log_message(const char* msg) {
FILE* logfile = fopen("chat.log", "a");
if(!logfile) return;
time_t now = time(NULL);
struct tm* tm_info = localtime(&now);
char timestamp[20];
strftime(timestamp, 20, "%Y-%m-%d %H:%M:%S", tm_info);
fprintf(logfile, "[%s] %s\n", timestamp, msg);
fclose(logfile);
}
void add_message(const char* msg) {
lv_obj_t* item = lv_label_create(msg_container);
lv_label_set_text(item, msg);
lv_obj_set_width(item, LV_PCT(95)); // 留出滚动条空间
lv_obj_set_height(item, LV_SIZE_CONTENT); // 高度自适应
lv_label_set_long_mode(item, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_font(item, &chinese, 0);
// 确保消息项正确添加到容器
lv_obj_update_layout(msg_container);
lv_obj_update_layout(scroll_container);
// 滚动到底部
lv_obj_scroll_to_view(item, LV_ANIM_ON);
}
int connect_to_server(const char* ip, int port) {
sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serv_addr = {
.sin_family = AF_INET,
.sin_port = htons(port),
.sin_addr.s_addr = inet_addr(ip)
};
if(connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) {
perror("连接失败");
return -1;
}
return sockfd;
}
void* recv_handler(void* arg) {
int sockfd = *(int*)arg;
char buffer[1024];
while(1) {
int len = recv(sockfd, buffer, sizeof(buffer)-1, 0);
if(len <= 0) break;
buffer[len] = '\0';
add_message(buffer); // 添加到UI显示
log_message(buffer); // 记录到日志
}
return NULL;
}
void send_message(const char* msg) {
send(sockfd, msg, strlen(msg), 0);
}
// 回调函数,当发送按钮被点击时调用
void send_btn_cb(lv_event_t* e) {
// 获取输入框中的文本
const char* txt = lv_textarea_get_text(input_area);
// 如果文本长度大于0
if(strlen(txt) > 0) {
// 发送消息
send_message(txt);
// 创建一个缓冲区
char buf[256];
// 将文本和发送者信息格式化到缓冲区
snprintf(buf, sizeof(buf), "我: %s", txt);
char buf1[256];
strncpy(buf1, buf, sizeof(buf1));
// 将格式化后的文本添加到消息列表中
add_message(buf);
send_message(buf1);
// 清空输入框
lv_textarea_set_text(input_area, "");
}
}
void create_ui() {
// 分组头
int group_member_count = 2; // 示例成员数量
lv_obj_t* header = lv_obj_create(lv_scr_act());
lv_obj_set_size(header, LV_PCT(100), 50); // 固定高度
lv_obj_set_style_bg_color(header, lv_color_hex(0x3498db), 0); // 蓝色背景
lv_obj_remove_style(header, NULL, LV_PART_SCROLLBAR); // 移除滚动条样式
lv_obj_align(header, LV_ALIGN_TOP_MID, 0, 0);
lv_obj_t* header_label = lv_label_create(header);
lv_label_set_text_fmt(header_label, "相亲相爱一家人(%d)", group_member_count);
lv_obj_set_style_text_font(header_label, &chinese, 0);
lv_obj_set_style_text_color(header_label, lv_color_white(), 0); // 白色文字
lv_obj_center(header_label);
// 添加一个装饰图标
lv_obj_t* icon = lv_label_create(header);
lv_label_set_text(icon, LV_SYMBOL_HOME);
lv_obj_set_style_text_font(icon, &lv_font_montserrat_24, 0);
lv_obj_set_style_text_color(icon, lv_color_white(), 0);
lv_obj_align(icon, LV_ALIGN_LEFT_MID, 15, 0);
// 创建滚动容器 - 修正高度计算
scroll_container = lv_obj_create(lv_scr_act());
lv_obj_set_size(scroll_container, LV_PCT(100), lv_pct(70)); // 使用 lv_pct 函数
lv_obj_align(scroll_container, LV_ALIGN_TOP_MID, 0, 50);
lv_obj_set_style_bg_color(scroll_container, lv_color_hex(0xf9f9f9), 0);
lv_obj_set_style_border_width(scroll_container, 1, 0);
lv_obj_set_style_border_color(scroll_container, lv_color_hex(0xdcdde1), 0);
lv_obj_set_flex_flow(scroll_container, LV_FLEX_FLOW_COLUMN);
lv_obj_set_scroll_dir(scroll_container, LV_DIR_VER);
lv_obj_set_scrollbar_mode(scroll_container, LV_SCROLLBAR_MODE_ACTIVE);
lv_obj_set_style_pad_all(scroll_container, 0, 0); // 移除内边距
// 创建消息容器 - 关键修正
msg_container = lv_obj_create(scroll_container);
lv_obj_set_width(msg_container, LV_PCT(100));
lv_obj_set_height(msg_container, LV_SIZE_CONTENT); // 高度自适应内容
lv_obj_set_layout(msg_container, LV_LAYOUT_FLEX);
lv_obj_set_flex_flow(msg_container, LV_FLEX_FLOW_COLUMN);
lv_obj_clear_flag(msg_container, LV_OBJ_FLAG_SCROLLABLE); // 禁用自身滚动
// 设置消息样式 - 移除高度限制
static lv_style_t msg_style;
lv_style_init(&msg_style);
lv_style_set_pad_all(&msg_style, 5);
lv_style_set_pad_row(&msg_style, 3);
lv_style_set_min_height(&msg_style, 30);
// 移除最大高度限制: lv_style_set_max_height(&msg_style, 200);
lv_obj_add_style(msg_container, &msg_style, 0);
//键盘
keyboard= lv_keyboard_create(lv_scr_act());
lv_obj_set_size(keyboard, LV_PCT(100), LV_PCT(50));
lv_obj_align(keyboard, LV_ALIGN_BOTTOM_MID, 0, 0);
lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);//默认隐藏
lv_obj_move_foreground(keyboard);
// 输入区域容器
lv_obj_t* input_container = lv_obj_create(lv_scr_act());
lv_obj_set_size(input_container, LV_PCT(100), LV_PCT(15));
lv_obj_set_style_bg_color(input_container, lv_color_hex(0xecf0f1), 0);
lv_obj_set_style_border_width(input_container, 1, 0);
lv_obj_set_style_border_color(input_container, lv_color_hex(0xdcdde1), 0);
lv_obj_align(input_container, LV_ALIGN_BOTTOM_MID, 0, 0);
// 输入区域
input_area = lv_textarea_create(input_container);
//将键盘与输入区域关联
lv_keyboard_set_textarea(keyboard, input_area);
lv_obj_set_style_text_font(input_area, &chinese, 0);
lv_obj_set_width(input_area, LV_PCT(70));
lv_obj_set_height(input_area, LV_PCT(70)); // 高度调整为容器高度的70%
lv_obj_align(input_area, LV_ALIGN_LEFT_MID, 10, 0);
lv_textarea_set_placeholder_text(input_area, "输入消息...");
lv_textarea_set_one_line(input_area, true); // 单行输入
lv_obj_add_event_cb(input_area,Add_event_cb,LV_EVENT_FOCUSED, NULL);
lv_obj_add_event_cb(input_area,Add_event_cb,LV_EVENT_DEFOCUSED, NULL);
// 发送按钮
send_btn = lv_btn_create(input_container);
lv_obj_set_size(send_btn, LV_PCT(25), 60); // 与输入框高度一致
lv_obj_align(send_btn, LV_ALIGN_RIGHT_MID, -10, 0);
lv_obj_set_style_bg_color(send_btn, lv_color_hex(0x2ecc71), 0);
lv_obj_set_style_bg_color(send_btn, lv_color_hex(0x27ae60), LV_STATE_PRESSED);
lv_obj_t* btn_label = lv_label_create(send_btn);
lv_label_set_text(btn_label, "发送");
lv_obj_set_style_text_font(btn_label, &chinese, 0);
lv_obj_set_style_text_color(btn_label, lv_color_white(), 0);
lv_obj_center(btn_label);
lv_obj_add_event_cb(send_btn, send_btn_cb, LV_EVENT_CLICKED, NULL);
}
int main(void)
{
lv_init();
/*Linux frame buffer device init*/
lv_display_t * disp = lv_linux_fbdev_create();
lv_linux_fbdev_set_file(disp, "/dev/fb0");
lv_indev_t * indev = lv_evdev_create(LV_INDEV_TYPE_POINTER,"/dev/input/event0");
/*Create a Demo*/
//lv_demo_widgets();
//lv_demo_widgets_start_slideshow();
if((sockfd = connect_to_server("172.40.1.210", 15486)) < 0) {
printf("Failed to connect to server\n");
return -1;
}
create_ui();
// 创建接收线程
pthread_create(&recv_thread, NULL, recv_handler, (void*)&sockfd);
/*Handle LVGL tasks*/
while(1) {
lv_timer_handler();
usleep(5000);
}
close(sockfd);
return 0;
}