说明
lvgl版本7.11。
1.模板文件
先从lvgl\src\lv_widgets\lv_objx_templ.c
复制出lv_objx_templ.c
和lv_objx_templ.h
,重命名为你需要创建的组件名称例如lv_objxscrol.c
和lv_objxscrol.h
。
2.修改文件
根据提示我们将对应的temp字段替换成我们自己的组件名称。
2.1全局替换成下面这样
3.包含头文件
3.1如下
4.去掉defined() &&
5.在文件lvgl\lvgl.h
中添加头文件
6.在文件lv_conf.h
中添加#define LV_USE_OBJSCROL 1
开启组件功能
7.添加源文件
目录
gui\lvgl\CMakeLists.txt
编译问题记录
问题1
先注释掉,后面用到再改。
问题2
注释掉,没有用。
问题3
参数不对,注释掉
问题4
找到对应函数
改成
因为我是打算在创建的页上做扩展。
问题5
找到对应函数
改成
因为在这个版本里是对应的是这个宏。
例 自定义组件lv_scale
需要在配置文件中启用LV_USE_SCALE 宏定义
/**
* @file lv_scale.c
*
*/
/*********************
* INCLUDES
*********************/
#include "../lv_misc/lv_debug.h"
#include "lv_scale.h"
#if LV_USE_SCALE != 0
/*********************
* DEFINES
*********************/
#define LV_OBJX_NAME "lv_scale"
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static lv_design_res_t lv_scale_design(lv_obj_t * scale, const lv_area_t * clip_area, lv_design_mode_t mode);
static lv_res_t lv_scale_signal(lv_obj_t * scale, lv_signal_t sign, void * param);
/**********************
* STATIC VARIABLES
**********************/
static lv_signal_cb_t ancestor_signal;
static lv_design_cb_t ancestor_design;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Create a scalemenu object
* @param par pointer to an object, it will be the parent of the new scalemenu
* @param copy pointer to a scalemenu object, if not NULL then the new object will be copied from it
* @return pointer to the created scalemenu
*/
lv_obj_t * lv_scale_create(lv_obj_t * par, const lv_obj_t * copy)
{
LV_LOG_TRACE("scalemenu create started");
/*Create the ancestor of scalemenu*/
/*TODO modify it to the ancestor create function */
lv_obj_t * new_scale = lv_cont_create(par, copy);
LV_ASSERT_MEM(new_scale);
if(new_scale == NULL) return NULL;
/*Allocate the scalemenu type specific extended data*/
lv_scale_ext_t * ext = lv_obj_allocate_ext_attr(new_scale, sizeof(lv_scale_ext_t));
lv_mem_assert(ext);
if(ext == NULL) {
lv_obj_del(new_scale);
return NULL;
}
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_scale);
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_scale);
/*Initialize the allocated 'ext' */
/*The signal and design functions are not copied so set them here*/
lv_obj_set_signal_cb(new_scale, lv_scale_signal);
lv_obj_set_design_cb(new_scale, lv_scale_design);
/*Init the new scalemenu scalemenu*/
if(copy == NULL) {
}
/*Copy an existing scalemenu*/
else {
lv_scale_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
/*Refresh the style with new signal function*/
}
LV_LOG_INFO("scalemenu created");
return new_scale;
}
/*======================
* Add/remove functions
*=====================*/
/*
* New object specific "add" or "remove" functions come here
*/
/*=====================
* Setter functions
*====================*/
/*
* New object specific "set" functions come here
*/
/**
* Set a style of a scalemenu.
* @param scale pointer to scalemenu object
* @param type which style should be set
* @param style pointer to a style
*/
void lv_scale_set_style(lv_obj_t * scale, lv_scale_style_t type, const lv_style_t * style)
{
LV_ASSERT_OBJ(scale, LV_OBJX_NAME);
lv_scale_ext_t * ext = lv_obj_get_ext_attr(scale);
switch(type) {
case LV_SCALE_STYLE_X:
break;
case LV_SCALE_STYLE_Y:
break;
}
}
/*=====================
* Getter functions
*====================*/
/*
* New object specific "get" functions come here
*/
/**
* Get style of a scalemenu.
* @param scale pointer to scalemenu object
* @param type which style should be get
* @return style pointer to the style
*/
lv_style_t * lv_scale_get_style(const lv_obj_t * scale, lv_scale_style_t type)
{
LV_ASSERT_OBJ(scale, LV_OBJX_NAME);
lv_scale_ext_t * ext = lv_obj_get_ext_attr(scale);
lv_style_t * style = NULL;
switch(type) {
case LV_SCALE_STYLE_X:
style = NULL; /*Replace NULL with a pointer to the style*/
case LV_SCALE_STYLE_Y:
style = NULL; /*Replace NULL with a pointer to the style*/
default:
style = NULL;
}
return style;
}
/*=====================
* Other functions
*====================*/
/*
* New object specific "other" functions come here
*/
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Handle the drawing related tasks of the scalemenus
* @param scale pointer to an object
* @param mask the object will be drawn only in this area
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
* (return 'true' if yes)
* LV_DESIGN_DRAW: draw the object (always return 'true')
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
* @param return an element of `lv_design_res_t`
*/
static lv_design_res_t lv_scale_design(lv_obj_t * scale, const lv_area_t * clip_area, lv_design_mode_t mode)
{
/*Return false if the object is not covers the mask_p area*/
if(mode == LV_DESIGN_COVER_CHK) {
return LV_DESIGN_RES_NOT_COVER;
}
/*Draw the object*/
else if(mode == LV_DESIGN_DRAW_MAIN) {
}
/*Post draw when the children are drawn*/
else if(mode == LV_DESIGN_DRAW_POST) {
}
return LV_DESIGN_RES_OK;
}
/**
* Signal function of the scalemenu
* @param scale pointer to a scalemenu object
* @param sign a signal type from lv_signal_t enum
* @param param pointer to a signal specific variable
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
*/
static lv_res_t lv_scale_signal(lv_obj_t * scale, lv_signal_t sign, void * param)
{
lv_res_t res;
/* Include the ancient signal function */
res = ancestor_signal(scale, sign, param);
if(res != LV_RES_OK) return res;
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
if(sign == LV_SIGNAL_CLEANUP) {
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
}
return res;
}
#else /* Enable this file at the top */
/* This dummy typedef exists purely to silence -Wpedantic. */
typedef int keep_pedantic_happy;
#endif
/**
* @file lv_scale.h
*
*/
#ifndef LV_SCALE_H
#define LV_SCALE_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
#include "../lv_conf_internal.h"
#if LV_USE_SCALE != 0
#include "../lv_core/lv_obj.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/*Data of scalemenu*/
typedef struct {
// lv_ANCESTOR_ext_t ANCESTOR; /*Ext. of ancestor*/
/*New data for this type */
} lv_scale_ext_t;
/*Styles*/
enum {
LV_SCALE_STYLE_X,
LV_SCALE_STYLE_Y,
};
typedef uint8_t lv_scale_style_t;
/**********************
* GLOBAL PROTOTYPES
**********************/
/**
* Create a scalemenu objects
* @param par pointer to an object, it will be the parent of the new scalemenu
* @param copy pointer to a scalemenu object, if not NULL then the new object will be copied from it
* @return pointer to the created scalemenu
*/
lv_obj_t * lv_scale_create(lv_obj_t * par, const lv_obj_t * copy);
/*======================
* Add/remove functions
*=====================*/
/*=====================
* Setter functions
*====================*/
/**
* Set a style of a scalemenu.
* @param scale pointer to scalemenu object
* @param type which style should be set
* @param style pointer to a style
*/
void lv_scale_set_style(lv_obj_t * scale, lv_scale_style_t type, const lv_style_t * style);
/*=====================
* Getter functions
*====================*/
/**
* Get style of a scalemenu.
* @param scale pointer to scalemenu object
* @param type which style should be get
* @return style pointer to the style
*/
lv_style_t * lv_scale_get_style(const lv_obj_t * scale, lv_scale_style_t type);
/*=====================
* Other functions
*====================*/
/**********************
* MACROS
**********************/
#endif /*LV_USE_SCALE*/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /*LV_SCALE_H*/