老师,请问一下我代码模拟时没有问题,按钮功能都能实现,烧录到板子上,屏幕按钮点击都没有反应,好像程序就卡死了,这是内存泄漏的问题还是什么问题啊

#include "app.h"

static btn_attr_t attr;
static btn_attr_t btn_attributes[NUM_ROWS][NUM_COLS];
static btn_pos_t * pos;

/*事件函数*/
void serial_btn_event_cb(lv_event_t * e){
    /*获取事件类型*/
    lv_event_code_t code = lv_event_get_code(e);
    /*获取触发事件的目标对象*/
    lv_obj_t * btn = lv_event_get_target(e);
    /*获取btn的后代label*/
    lv_obj_t * img = lv_obj_get_child(btn, 0);



    if (code == LV_EVENT_SHORT_CLICKED){
        lv_color_t current_color = lv_obj_get_style_bg_color(btn, LV_PART_MAIN);// 获取按钮当前的背景颜色
        const char *label_text = lv_label_get_text(img);  /*获取按钮中的内容*/

        // 获取按钮在网格中的位置
        btn_pos_t *pos = (btn_pos_t *)lv_obj_get_user_data(btn);
        int row = pos->row;
        int col = pos->col;

        char msg[20]; // 用于存储发送的信息

        if(current_color.full == lv_color_hex(0x4684FD).full) //判断按钮的开关情况,默认蓝色为关
            {
            lv_obj_set_style_bg_color(btn, lv_color_hex(0x00ff00), LV_PART_MAIN); //绿色开
            if (strcmp(label_text, "关") == 0){
                lv_label_set_text(img, "开");//按钮获取按键的文本
            }
            sprintf(msg, "(%d,%d):1", row+1, col+1); // 生成开启时的消息
            //LV_LOG_USER(msg);
            serial_send("1"); // 发送开启消息
            }
        else{
            lv_obj_set_style_bg_color(btn, lv_color_hex(0x4684FD), LV_PART_MAIN);
            if (strcmp(label_text, "开") == 0){
                lv_label_set_text(img, "关");//按钮获取按键的文本
            }
            sprintf(msg, "(%d,%d):0", row + 1, col + 1); // 生成关闭时的消息
            //LV_LOG_USER(msg);
            serial_send("2"); // 发送关闭消息
            }
    }
}


lv_obj_t *create_serial_btn(lv_obj_t * parent, int row, int col, btn_attr_t attr) {
    lv_obj_t * btn = lv_btn_create(parent);

    /*设置按钮位置*/
    lv_coord_t x = 150 + col * X_OFFSET;
    lv_coord_t y = 140 + row * Y_OFFSET;
    lv_obj_set_pos(btn, x, y);
    lv_obj_set_size(btn, BTN_WIDTH, BTN_HEIGHT);
    lv_obj_set_style_bg_color(btn, attr.color, 0);


    /*设置图标样式*/
    lv_obj_t * img = lv_label_create(btn);
    lv_obj_set_style_text_font(img, &myfont, 0);
    lv_label_set_text(img, attr.img);
    lv_obj_align(img, LV_ALIGN_CENTER, 0 ,10);

    /*设置文本样式*/
    lv_obj_t * label = lv_label_create(parent);
    lv_label_set_text(label, attr.label);
    lv_obj_set_style_text_font(label, &myfont, 0);
    lv_obj_set_style_text_opa(label, LV_OPA_100, 0);
    lv_obj_align_to(label, btn, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_style_text_color(label, lv_color_hex(0x000000), 0);

    lv_obj_add_event_cb(btn, serial_btn_event_cb, LV_EVENT_ALL, NULL);

    /*动态分配位置数据并设置为用户数据*/
    btn_pos_t *pos = (btn_pos_t *)lv_mem_alloc(sizeof(btn_pos_t));
    pos->row = row;
    pos->col = col;
    lv_obj_set_user_data(btn, pos);

    return btn;
}

void create_buttons_matrix(lv_obj_t * parent) {
    btn_attr_t btn_attributes[NUM_ROWS][NUM_COLS] = {
        {{"关", "卷膜(左)", lv_color_hex(0x4684FD)}, {"关", "卷膜(右)", lv_color_hex(0x4684FD)}, {"关", "棉被(左)", lv_color_hex(0x4684FD)}, {"关", "棉被(右)", lv_color_hex(0x4684FD)}},
        {{"关", "风机", lv_color_hex(0x4684FD)}, {"关", "地加热", lv_color_hex(0x4684FD)}, {"关", "喷雾", lv_color_hex(0x4684FD)}, {"关", "滴灌", lv_color_hex(0x4684FD)}},
        {{"关", "预备", lv_color_hex(0x4684FD)}, {"关", "预备", lv_color_hex(0x4684FD)}, {"关", "预备", lv_color_hex(0x4684FD)}, {"关", "预备", lv_color_hex(0x4684FD)}}
    };

    for (int row = 0; row < NUM_ROWS; row++) {
        for (int col = 0; col < NUM_COLS; col++) {
            create_serial_btn(parent, row, col, btn_attributes[row][col]);
        }
    }
}

如果是内存泄漏问题我尝试添加了释放内存代码,结果还是那样的,是不是因为我创建按钮矩阵的原因啊?请教一下怎么解决这个问题啊,没有头绪。

void destroy_serial_btn(lv_obj_t * btn) {
    btn_pos_t *pos = (btn_pos_t *)lv_obj_get_user_data(btn);
    lv_mem_free(pos);
    lv_obj_del(btn);
}

应该是板子的资源不够用


周老师,我把这段代码屏蔽了就可以在屏幕上点击了,是因为这段代码占用的资源太大了吗,我想根据按钮位置来发送对应的数据有什么改进的方法吗

我解决了,谢谢老师解惑

1 个赞