mirror of
https://github.com/lvgl/lvgl.git
synced 2025-01-14 06:42:58 +08:00
Initial lv_rotary work extending lv_arc with stubbed signal handler.
This commit is contained in:
parent
7af64df008
commit
1a41f13fd6
@ -654,6 +654,9 @@ typedef void * lv_obj_user_data_t;
|
||||
# define LV_ROLLER_INF_PAGES 7
|
||||
#endif
|
||||
|
||||
/*Rotary (dependencies: lv_arc, lv_btn)*/
|
||||
#define LV_USE_ROTARY 1
|
||||
|
||||
/*Slider (dependencies: lv_bar)*/
|
||||
#define LV_USE_SLIDER 1
|
||||
|
||||
|
@ -991,6 +991,9 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*Rotary (dependencies: lv_arc, lv_btn)*/
|
||||
#define LV_USE_ROTARY 1
|
||||
|
||||
/*Slider (dependencies: lv_bar)*/
|
||||
#ifndef LV_USE_SLIDER
|
||||
#define LV_USE_SLIDER 1
|
||||
|
@ -31,6 +31,8 @@ extern "C" {
|
||||
|
||||
#define LV_MATH_UDIV255(x) ((uint32_t)((uint32_t) (x) * 0x8081) >> 0x17)
|
||||
|
||||
#define LV_MATH_MAP(a, min_in, max_in, min, max) (min - (((max - min) / (max_in - min_in)) * min_in) + (((max - min) / (max_in - min_in)) * a))
|
||||
|
||||
#define LV_IS_SIGNED(t) (((t)(-1)) < ((t) 0))
|
||||
#define LV_UMAX_OF(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | (0xFULL << ((sizeof(t) * 8ULL) - 4ULL)))
|
||||
#define LV_SMAX_OF(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | (0x7ULL << ((sizeof(t) * 8ULL) - 4ULL)))
|
||||
|
@ -147,6 +147,10 @@ typedef struct {
|
||||
lv_style_t roller_bg, roller_sel;
|
||||
#endif
|
||||
|
||||
#if LV_USE_ROTARY
|
||||
lv_style_t rotary_bg, rotary_indic, rotary_knob;
|
||||
#endif
|
||||
|
||||
#if LV_USE_SLIDER
|
||||
lv_style_t slider_knob, slider_bg;
|
||||
#endif
|
||||
@ -789,6 +793,38 @@ static void roller_init(void)
|
||||
#endif
|
||||
}
|
||||
|
||||
static void rotary_init(void)
|
||||
{
|
||||
#if LV_USE_ROTARY != 0
|
||||
style_init_reset(&styles->rotary_bg);
|
||||
lv_style_set_clip_corner(&styles->rotary_bg, LV_STATE_DEFAULT, true);
|
||||
lv_style_set_pad_left(&styles->rotary_bg, LV_STATE_DEFAULT, 0);
|
||||
lv_style_set_pad_right(&styles->rotary_bg, LV_STATE_DEFAULT, 0);
|
||||
lv_style_set_pad_top(&styles->rotary_bg, LV_STATE_DEFAULT, 0);
|
||||
lv_style_set_pad_bottom(&styles->rotary_bg, LV_STATE_DEFAULT, 0);
|
||||
lv_style_set_pad_inner(&styles->rotary_bg, LV_STATE_DEFAULT, 0);
|
||||
lv_style_set_border_width(&styles->rotary_bg, LV_STATE_DEFAULT, 0);
|
||||
lv_style_set_line_color(&styles->rotary_bg, LV_STATE_DEFAULT, COLOR_BG_SEC);
|
||||
lv_style_set_line_width(&styles->rotary_bg, LV_STATE_DEFAULT, LV_DPX(8));
|
||||
lv_style_set_line_rounded(&styles->rotary_bg, LV_STATE_DEFAULT, true);
|
||||
|
||||
style_init_reset(&styles->rotary_indic);
|
||||
lv_style_set_line_color(&styles->rotary_indic, LV_STATE_DEFAULT, theme.color_primary);
|
||||
lv_style_set_line_width(&styles->rotary_indic, LV_STATE_DEFAULT, LV_DPX(8));
|
||||
lv_style_set_line_rounded(&styles->rotary_indic, LV_STATE_DEFAULT, true);
|
||||
|
||||
style_init_reset(&styles->rotary_knob);
|
||||
lv_style_set_bg_opa(&styles->rotary_knob, LV_STATE_DEFAULT, LV_OPA_COVER);
|
||||
lv_style_set_bg_color(&styles->rotary_knob, LV_STATE_DEFAULT, LV_COLOR_WHITE);
|
||||
lv_style_set_radius(&styles->rotary_knob, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
|
||||
lv_style_set_pad_top(&styles->rotary_knob, LV_STATE_DEFAULT, - LV_DPX(4));
|
||||
lv_style_set_pad_bottom(&styles->rotary_knob, LV_STATE_DEFAULT, - LV_DPX(4));
|
||||
lv_style_set_pad_left(&styles->rotary_knob, LV_STATE_DEFAULT, - LV_DPX(4));
|
||||
lv_style_set_pad_right(&styles->rotary_knob, LV_STATE_DEFAULT, - LV_DPX(4));
|
||||
lv_style_set_pad_inner(&styles->rotary_knob, LV_STATE_DEFAULT, LV_DPX(6));
|
||||
#endif
|
||||
}
|
||||
|
||||
static void tabview_init(void)
|
||||
{
|
||||
#if LV_USE_TABVIEW != 0
|
||||
@ -923,6 +959,7 @@ lv_theme_t * lv_theme_material_init(lv_color_t color_primary, lv_color_t color_s
|
||||
list_init();
|
||||
ddlist_init();
|
||||
roller_init();
|
||||
rotary_init();
|
||||
tabview_init();
|
||||
tileview_init();
|
||||
table_init();
|
||||
@ -1223,6 +1260,26 @@ static void theme_apply(lv_obj_t * obj, lv_theme_style_t name)
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if LV_USE_ROTARY
|
||||
case LV_THEME_ROTARY:
|
||||
lv_obj_clean_style_list(obj, LV_ROTARY_PART_BG);
|
||||
list = lv_obj_get_style_list(obj, LV_ROTARY_PART_BG);
|
||||
_lv_style_list_add_style(list, &styles->bg);
|
||||
_lv_style_list_add_style(list, &styles->arc_bg);
|
||||
_lv_style_list_add_style(list, &styles->rotary_bg);
|
||||
|
||||
lv_obj_clean_style_list(obj, LV_ROTARY_PART_INDIC);
|
||||
list = lv_obj_get_style_list(obj, LV_ROTARY_PART_INDIC);
|
||||
_lv_style_list_add_style(list, &styles->arc_indic);
|
||||
_lv_style_list_add_style(list, &styles->rotary_indic);
|
||||
|
||||
lv_obj_clean_style_list(obj, LV_ROTARY_PART_KNOB);
|
||||
list = lv_obj_get_style_list(obj, LV_ROTARY_PART_KNOB);
|
||||
_lv_style_list_add_style(list, &styles->btn);
|
||||
_lv_style_list_add_style(list, &styles->rotary_knob);
|
||||
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if LV_USE_OBJMASK
|
||||
case LV_THEME_OBJMASK:
|
||||
|
458
src/lv_widgets/lv_rotary.c
Normal file
458
src/lv_widgets/lv_rotary.c
Normal file
@ -0,0 +1,458 @@
|
||||
|
||||
/**
|
||||
* @file lv_rotary.c
|
||||
*
|
||||
*/
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
#include "lv_rotary.h"
|
||||
#if LV_USE_ROTARY != 0
|
||||
|
||||
#include "../lv_core/lv_debug.h"
|
||||
#include "../lv_core/lv_group.h"
|
||||
#include "../lv_core/lv_indev.h"
|
||||
#include "../lv_draw/lv_draw.h"
|
||||
#include "../lv_themes/lv_theme.h"
|
||||
#include "../lv_misc/lv_math.h"
|
||||
#include "lv_img.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
#define LV_OBJX_NAME "lv_rotary"
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* STATIC PROTOTYPES
|
||||
**********************/
|
||||
static lv_design_res_t lv_rotary_design(lv_obj_t * rotary, const lv_area_t * clip_area, lv_design_mode_t mode);
|
||||
static lv_res_t lv_rotary_signal(lv_obj_t * rotary, lv_signal_t sign, void * param);
|
||||
static lv_style_list_t * lv_rotary_get_style(lv_obj_t * rotary, uint8_t part);
|
||||
// static void draw_bg(lv_obj_t * rotary, const lv_area_t * clip_area);
|
||||
// static void draw_indic(lv_obj_t * rotary, const lv_area_t * clip_area);
|
||||
static void draw_knob(lv_obj_t * rotary, const lv_area_t * clip_area);
|
||||
|
||||
/**********************
|
||||
* STATIC VARIABLES
|
||||
**********************/
|
||||
static lv_design_cb_t ancestor_design;
|
||||
static lv_signal_cb_t ancestor_signal;
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* GLOBAL FUNCTIONS
|
||||
**********************/
|
||||
|
||||
/**
|
||||
* Create a rotary objects
|
||||
* @param par pointer to an object, it will be the parent of the new rotary
|
||||
* @param copy pointer to a rotary object, if not NULL then the new object will be copied from it
|
||||
* @return pointer to the created rotary
|
||||
*/
|
||||
lv_obj_t * lv_rotary_create(lv_obj_t * par, const lv_obj_t * copy)
|
||||
{
|
||||
LV_LOG_TRACE("rotary create started");
|
||||
|
||||
/*Create the ancestor rotary*/
|
||||
lv_obj_t * rotary = lv_arc_create(par, copy);
|
||||
LV_ASSERT_MEM(rotary);
|
||||
if(rotary == NULL) return NULL;
|
||||
|
||||
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(rotary);
|
||||
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(rotary);
|
||||
|
||||
/*Allocate the rotary type specific extended data*/
|
||||
lv_rotary_ext_t * ext = lv_obj_allocate_ext_attr(rotary, sizeof(lv_rotary_ext_t));
|
||||
LV_ASSERT_MEM(ext);
|
||||
if(ext == NULL) {
|
||||
lv_obj_del(rotary);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*Initialize the allocated 'ext' */
|
||||
ext->arc.rotation_angle = 0;
|
||||
ext->arc.bg_angle_start = 135;
|
||||
ext->arc.bg_angle_end = 45;
|
||||
ext->arc.arc_angle_start = 135;
|
||||
ext->arc.arc_angle_end = 270;
|
||||
ext->cur_value = 0;
|
||||
ext->min_value = 0;
|
||||
ext->max_value = 0;
|
||||
ext->start_value = 0;
|
||||
ext->dragging = false;
|
||||
lv_style_list_init(&ext->style_bg);
|
||||
lv_style_list_init(&ext->style_indic);
|
||||
lv_style_list_init(&ext->style_knob);
|
||||
|
||||
/*The signal and design functions are not copied so set them here*/
|
||||
lv_obj_set_signal_cb(rotary, lv_rotary_signal);
|
||||
lv_obj_set_design_cb(rotary, lv_rotary_design);
|
||||
|
||||
/*Init the new rotary rotary*/
|
||||
if(copy == NULL) {
|
||||
lv_obj_set_click(rotary, true);
|
||||
lv_obj_add_protect(rotary, LV_PROTECT_PRESS_LOST);
|
||||
lv_obj_set_ext_click_area(rotary, 0, 0, LV_DPI / 10, LV_DPI / 10);
|
||||
|
||||
lv_theme_apply(rotary, LV_THEME_ROTARY);
|
||||
lv_obj_set_height(rotary, LV_DPI / 15);
|
||||
}
|
||||
/*Copy an existing rotary*/
|
||||
else {
|
||||
lv_rotary_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
|
||||
ext->cur_value = copy_ext->cur_value;
|
||||
ext->min_value = copy_ext->min_value;
|
||||
ext->max_value = copy_ext->max_value;
|
||||
ext->start_value = copy_ext->start_value;
|
||||
ext->dragging = copy_ext->dragging;
|
||||
lv_style_list_copy(&ext->style_bg, ©_ext->style_bg);
|
||||
lv_style_list_copy(&ext->style_indic, ©_ext->style_indic);
|
||||
lv_style_list_copy(&ext->style_knob, ©_ext->style_knob);
|
||||
|
||||
lv_obj_refresh_style(rotary, LV_OBJ_PART_ALL);
|
||||
}
|
||||
|
||||
LV_LOG_INFO("rotary created");
|
||||
|
||||
return rotary;
|
||||
}
|
||||
|
||||
/*=====================
|
||||
* Setter functions
|
||||
*====================*/
|
||||
|
||||
/**
|
||||
* Set a new value on the rotary
|
||||
* @param rotary pointer to a rotary object
|
||||
* @param value new value
|
||||
* @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
|
||||
*/
|
||||
void lv_rotary_set_value(lv_obj_t * rotary, int16_t value, lv_anim_enable_t anim)
|
||||
{
|
||||
LV_ASSERT_OBJ(rotary, LV_OBJX_NAME);
|
||||
|
||||
lv_rotary_ext_t * ext = (lv_rotary_ext_t *)lv_obj_get_ext_attr(rotary);
|
||||
if(ext->cur_value == value) return;
|
||||
|
||||
int16_t new_value;
|
||||
new_value = value > ext->max_value ? ext->max_value : value;
|
||||
new_value = new_value < ext->min_value ? ext->min_value : new_value;
|
||||
|
||||
if(ext->cur_value == new_value) return;
|
||||
|
||||
ext->cur_value = new_value;
|
||||
|
||||
lv_arc_set_end_angle(
|
||||
rotary,
|
||||
LV_MATH_MAP(
|
||||
ext->cur_value,
|
||||
ext->min_value,
|
||||
ext->max_value,
|
||||
ext->arc.arc_angle_start,
|
||||
ext->arc.arc_angle_start + 360
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a new value for the left knob of a rotary
|
||||
* @param rotary pointer to a rotary object
|
||||
* @param left_value new value
|
||||
* @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
|
||||
*/
|
||||
void lv_rotary_set_start_value(lv_obj_t * rotary, int16_t value, lv_anim_enable_t anim)
|
||||
{
|
||||
LV_ASSERT_OBJ(rotary, LV_OBJX_NAME);
|
||||
|
||||
lv_rotary_ext_t * ext = (lv_rotary_ext_t *)lv_obj_get_ext_attr(rotary);
|
||||
ext->start_value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set minimum and the maximum values of a rotary
|
||||
* @param rotary pointer to the rotary object
|
||||
* @param min minimum value
|
||||
* @param max maximum value
|
||||
*/
|
||||
void lv_rotary_set_range(lv_obj_t * rotary, int16_t min, int16_t max)
|
||||
{
|
||||
LV_ASSERT_OBJ(rotary, LV_OBJX_NAME);
|
||||
|
||||
lv_rotary_ext_t * ext = (lv_rotary_ext_t *)lv_obj_get_ext_attr(rotary);
|
||||
if(ext->min_value == min && ext->max_value == max) return;
|
||||
|
||||
ext->min_value = min;
|
||||
ext->max_value = max;
|
||||
|
||||
if(ext->cur_value < min) {
|
||||
ext->cur_value = min;
|
||||
}
|
||||
if(ext->cur_value > max) {
|
||||
ext->cur_value = max;
|
||||
}
|
||||
|
||||
lv_rotary_set_value(rotary, ext->cur_value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the rotary symmetric to zero. The indicator will grow from zero instead of the minimum
|
||||
* position.
|
||||
* @param rotary pointer to a rotary object
|
||||
* @param en true: enable disable symmetric behavior; false: disable
|
||||
*/
|
||||
void lv_rotary_set_type(lv_obj_t * rotary, lv_rotary_type_t type)
|
||||
{
|
||||
LV_ASSERT_OBJ(rotary, LV_OBJX_NAME);
|
||||
|
||||
lv_rotary_ext_t *ext = (lv_rotary_ext_t *)lv_obj_get_ext_attr(rotary);
|
||||
ext->type = type;
|
||||
|
||||
lv_obj_invalidate(rotary);
|
||||
}
|
||||
|
||||
/*=====================
|
||||
* Getter functions
|
||||
*====================*/
|
||||
|
||||
/**
|
||||
* Get the value of a rotary
|
||||
* @param rotary pointer to a rotary object
|
||||
* @return the value of the rotary
|
||||
*/
|
||||
int16_t lv_rotary_get_value(const lv_obj_t * rotary)
|
||||
{
|
||||
LV_ASSERT_OBJ(rotary, LV_OBJX_NAME);
|
||||
|
||||
lv_rotary_ext_t * ext = lv_obj_get_ext_attr(rotary);
|
||||
return ext->cur_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the left knob of a rotary
|
||||
* @param rotary pointer to a rotary object
|
||||
* @return the start value of the rotary
|
||||
*/
|
||||
int16_t lv_rotary_get_start_value(const lv_obj_t * rotary)
|
||||
{
|
||||
LV_ASSERT_OBJ(rotary, LV_OBJX_NAME);
|
||||
|
||||
lv_rotary_ext_t *ext = (lv_rotary_ext_t *)lv_obj_get_ext_attr(rotary);
|
||||
return ext->start_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the minimum value of a rotary
|
||||
* @param rotary pointer to a rotary object
|
||||
* @return the minimum value of the rotary
|
||||
*/
|
||||
int16_t lv_rotary_get_min_value(const lv_obj_t * rotary)
|
||||
{
|
||||
LV_ASSERT_OBJ(rotary, LV_OBJX_NAME);
|
||||
|
||||
lv_rotary_ext_t *ext = (lv_rotary_ext_t *)lv_obj_get_ext_attr(rotary);
|
||||
return ext->min_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the maximum value of a rotary
|
||||
* @param rotary pointer to a rotary object
|
||||
* @return the maximum value of the rotary
|
||||
*/
|
||||
int16_t lv_rotary_get_max_value(const lv_obj_t * rotary)
|
||||
{
|
||||
LV_ASSERT_OBJ(rotary, LV_OBJX_NAME);
|
||||
|
||||
lv_rotary_ext_t *ext = (lv_rotary_ext_t *)lv_obj_get_ext_attr(rotary);
|
||||
return ext->max_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Give the rotary is being dragged or not
|
||||
* @param rotary pointer to a rotary object
|
||||
* @return true: drag in progress false: not dragged
|
||||
*/
|
||||
bool lv_rotary_is_dragged(const lv_obj_t * rotary)
|
||||
{
|
||||
LV_ASSERT_OBJ(rotary, LV_OBJX_NAME);
|
||||
|
||||
lv_rotary_ext_t * ext = lv_obj_get_ext_attr(rotary);
|
||||
return ext->dragging;
|
||||
}
|
||||
|
||||
/**********************
|
||||
* STATIC FUNCTIONS
|
||||
**********************/
|
||||
|
||||
/**
|
||||
* Handle the drawing related tasks of the rotarys
|
||||
* @param rotary pointer to an object
|
||||
* @param clip_area 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_rotary_design(lv_obj_t * rotary, 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) {
|
||||
/*The ancestor design function will draw the background and the indicator.*/
|
||||
ancestor_design(rotary, clip_area, mode);
|
||||
draw_knob(rotary, clip_area);
|
||||
}
|
||||
/*Post draw when the children are drawn*/
|
||||
else if(mode == LV_DESIGN_DRAW_POST) {
|
||||
return ancestor_design(rotary, clip_area, mode);
|
||||
}
|
||||
|
||||
return LV_DESIGN_RES_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Signal function of the rotary
|
||||
* @param rotary pointer to a rotary 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_rotary_signal(lv_obj_t * rotary, lv_signal_t sign, void * param)
|
||||
{
|
||||
lv_res_t res;
|
||||
|
||||
if(sign == LV_SIGNAL_GET_STYLE) {
|
||||
lv_get_style_info_t * info = param;
|
||||
info->result = lv_rotary_get_style(rotary, info->part);
|
||||
if(info->result != NULL) return LV_RES_OK;
|
||||
else return ancestor_signal(rotary, sign, param);
|
||||
}
|
||||
|
||||
/* Include the ancient signal function */
|
||||
res = ancestor_signal(rotary, 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);
|
||||
|
||||
lv_rotary_ext_t * ext = lv_obj_get_ext_attr(rotary);
|
||||
lv_point_t p;
|
||||
|
||||
if(sign == LV_SIGNAL_PRESSED) {
|
||||
ext->dragging = true;
|
||||
if(lv_rotary_get_type(rotary) == LV_ROTARY_TYPE_NORMAL) {
|
||||
ext->value_to_set = &ext->cur_value;
|
||||
}
|
||||
}
|
||||
else if(sign == LV_SIGNAL_PRESSING && ext->value_to_set != NULL) {
|
||||
lv_indev_get_point(param, &p);
|
||||
|
||||
// TODO: get new_value and set
|
||||
}
|
||||
else if(sign == LV_SIGNAL_RELEASED || sign == LV_SIGNAL_PRESS_LOST) {
|
||||
ext->dragging = false;
|
||||
ext->value_to_set = NULL;
|
||||
|
||||
#if LV_USE_GROUP
|
||||
/*Leave edit mode if released. (No need to wait for LONG_PRESS) */
|
||||
lv_group_t * g = lv_obj_get_group(rotary);
|
||||
bool editing = lv_group_get_editing(g);
|
||||
lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
|
||||
if(indev_type == LV_INDEV_TYPE_ENCODER) {
|
||||
if(editing) lv_group_set_editing(g, false);
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
else if(sign == LV_SIGNAL_CONTROL) {
|
||||
char c = *((char *)param);
|
||||
|
||||
if(c == LV_KEY_RIGHT || c == LV_KEY_UP) {
|
||||
lv_rotary_set_value(rotary, lv_rotary_get_value(rotary) + 1, LV_ANIM_ON);
|
||||
res = lv_event_send(rotary, LV_EVENT_VALUE_CHANGED, NULL);
|
||||
if(res != LV_RES_OK) return res;
|
||||
}
|
||||
else if(c == LV_KEY_LEFT || c == LV_KEY_DOWN) {
|
||||
lv_rotary_set_value(rotary, lv_rotary_get_value(rotary) - 1, LV_ANIM_ON);
|
||||
res = lv_event_send(rotary, LV_EVENT_VALUE_CHANGED, NULL);
|
||||
if(res != LV_RES_OK) return res;
|
||||
}
|
||||
}
|
||||
else if(sign == LV_SIGNAL_CLEANUP) {
|
||||
lv_obj_clean_style_list(rotary, LV_ROTARY_PART_KNOB);
|
||||
}
|
||||
else if(sign == LV_SIGNAL_GET_EDITABLE) {
|
||||
bool * editable = (bool *)param;
|
||||
*editable = true;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static lv_style_list_t * lv_rotary_get_style(lv_obj_t * rotary, uint8_t part)
|
||||
{
|
||||
LV_ASSERT_OBJ(rotary, LV_OBJX_NAME);
|
||||
|
||||
lv_rotary_ext_t * ext = lv_obj_get_ext_attr(rotary);
|
||||
lv_style_list_t * style_dsc_p;
|
||||
|
||||
switch(part) {
|
||||
case LV_ROTARY_PART_BG:
|
||||
style_dsc_p = &ext->style_bg;
|
||||
break;
|
||||
case LV_ROTARY_PART_INDIC:
|
||||
style_dsc_p = &ext->style_indic;
|
||||
break;
|
||||
case LV_ROTARY_PART_KNOB:
|
||||
style_dsc_p = &ext->style_knob;
|
||||
break;
|
||||
default:
|
||||
style_dsc_p = NULL;
|
||||
}
|
||||
|
||||
return style_dsc_p;
|
||||
}
|
||||
|
||||
static void draw_knob(lv_obj_t * rotary, const lv_area_t * clip_area)
|
||||
{
|
||||
lv_bidi_dir_t base_dir = lv_obj_get_base_dir(rotary);
|
||||
|
||||
lv_rotary_ext_t *ext = (lv_rotary_ext_t *)lv_obj_get_ext_attr(rotary);
|
||||
|
||||
lv_coord_t left_bg = lv_obj_get_style_pad_left(rotary, LV_ROTARY_PART_BG);
|
||||
lv_coord_t right_bg = lv_obj_get_style_pad_right(rotary, LV_ROTARY_PART_BG);
|
||||
lv_coord_t top_bg = lv_obj_get_style_pad_top(rotary, LV_ROTARY_PART_BG);
|
||||
lv_coord_t bottom_bg = lv_obj_get_style_pad_bottom(rotary, LV_ROTARY_PART_BG);
|
||||
lv_coord_t r = (LV_MATH_MIN(lv_obj_get_width(rotary) - left_bg - right_bg,
|
||||
lv_obj_get_height(rotary) - top_bg - bottom_bg)) / 2;
|
||||
lv_coord_t indic_width = lv_obj_get_style_line_width(rotary, LV_ROTARY_PART_INDIC);
|
||||
lv_area_t knob_area;
|
||||
lv_draw_rect_dsc_t knob_rect_dsc;
|
||||
lv_coord_t center_x = rotary->coords.x1 + r + left_bg;
|
||||
lv_coord_t center_y = rotary->coords.y1 + r + top_bg;
|
||||
|
||||
|
||||
if(r > 0) {
|
||||
knob_area.x1 = center_x - r + indic_width + lv_obj_get_style_pad_inner(rotary, LV_ROTARY_PART_KNOB);
|
||||
knob_area.x2 = center_x + r - indic_width - lv_obj_get_style_pad_inner(rotary, LV_ROTARY_PART_KNOB);
|
||||
knob_area.y1 = center_y - r + indic_width + lv_obj_get_style_pad_inner(rotary, LV_ROTARY_PART_KNOB);
|
||||
knob_area.y2 = center_y + r - indic_width - lv_obj_get_style_pad_inner(rotary, LV_ROTARY_PART_KNOB);
|
||||
|
||||
lv_draw_rect_dsc_init(&knob_rect_dsc);
|
||||
lv_obj_init_draw_rect_dsc(rotary, LV_ROTARY_PART_KNOB, &knob_rect_dsc);
|
||||
|
||||
lv_draw_rect(&knob_area, clip_area, &knob_rect_dsc);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
281
src/lv_widgets/lv_rotary.h
Normal file
281
src/lv_widgets/lv_rotary.h
Normal file
@ -0,0 +1,281 @@
|
||||
/**
|
||||
* @file lv_rotary.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LV_ROTARY_H
|
||||
#define LV_ROTARY_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
#include "../lv_conf_internal.h"
|
||||
|
||||
#if LV_USE_ROTARY != 0
|
||||
|
||||
/*Testing of dependencies*/
|
||||
#if LV_USE_ARC == 0
|
||||
#error "lv_rotary: lv_arc is required. Enable it in lv_conf.h (LV_USE_ARC 1) "
|
||||
#endif
|
||||
|
||||
#if LV_USE_BTN == 0
|
||||
#error "lv_rotary: lv_btn is required. Enable it in lv_conf.h (LV_USE_BTN 1) "
|
||||
#endif
|
||||
|
||||
#define LV_ROTARY_KNOB_SIZE_MIN 4 /* cannot make the knob smaller then this [px] */
|
||||
|
||||
#include "../lv_core/lv_obj.h"
|
||||
#include "lv_arc.h"
|
||||
#include "lv_btn.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
enum {
|
||||
LV_ROTARY_TYPE_NORMAL
|
||||
};
|
||||
typedef uint8_t lv_rotary_type_t;
|
||||
|
||||
/*Data of rotary*/
|
||||
typedef struct {
|
||||
/*Ext. of ancestor*/
|
||||
lv_arc_ext_t arc;
|
||||
/*New data for this type*/
|
||||
lv_btn_ext_t btn;
|
||||
lv_rotary_type_t type;
|
||||
lv_style_list_t style_bg; /* Style of the background */
|
||||
lv_style_list_t style_indic; /* Style of the indicator */
|
||||
lv_style_list_t style_knob; /* Style of the knob */
|
||||
int16_t cur_value; /*Current value of the rotary*/
|
||||
int16_t min_value; /*Minimum value of the rotary*/
|
||||
int16_t max_value; /*Maximum value of the rotary*/
|
||||
int16_t start_value; /*Start value of the rotary*/
|
||||
int16_t value_to_set; /*Start value of the rotary*/
|
||||
bool dragging;
|
||||
} lv_rotary_ext_t;
|
||||
|
||||
/** Built-in styles of rotary*/
|
||||
enum {
|
||||
LV_ROTARY_PART_BG, /** Rotary background style. */
|
||||
LV_ROTARY_PART_INDIC, /** Rotary indicator (filled area) style. */
|
||||
LV_ROTARY_PART_KNOB, /** Rotary knob style. */
|
||||
};
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
|
||||
/**
|
||||
* Create a rotary objects
|
||||
* @param par pointer to an object, it will be the parent of the new rotary
|
||||
* @param copy pointer to a rotary object, if not NULL then the new object will be copied from it
|
||||
* @return pointer to the created rotary
|
||||
*/
|
||||
lv_obj_t * lv_rotary_create(lv_obj_t * par, const lv_obj_t * copy);
|
||||
|
||||
/*=====================
|
||||
* Setter functions
|
||||
*====================*/
|
||||
|
||||
/**
|
||||
* Set a new value on the rotary
|
||||
* @param rotary pointer to a rotary object
|
||||
* @param value new value
|
||||
* @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
|
||||
*/
|
||||
void lv_rotary_set_value(lv_obj_t * rotary, int16_t value, lv_anim_enable_t anim);
|
||||
|
||||
/**
|
||||
* Set a new value for the left knob of a rotary
|
||||
* @param rotary pointer to a rotary object
|
||||
* @param left_value new value
|
||||
* @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
|
||||
*/
|
||||
void lv_rotary_set_start_value(lv_obj_t * rotary, int16_t value, lv_anim_enable_t anim);
|
||||
|
||||
/**
|
||||
* Set minimum and the maximum values of a rotary
|
||||
* @param rotary pointer to the rotary object
|
||||
* @param min minimum value
|
||||
* @param max maximum value
|
||||
*/
|
||||
void lv_rotary_set_range(lv_obj_t * rotary, int16_t min, int16_t max);
|
||||
|
||||
/**
|
||||
* Make the rotary symmetric to zero. The indicator will grow from zero instead of the minimum
|
||||
* position.
|
||||
* @param rotary pointer to a rotary object
|
||||
* @param en true: enable disable symmetric behavior; false: disable
|
||||
*/
|
||||
void lv_rotary_set_type(lv_obj_t * rotary, lv_rotary_type_t type);
|
||||
|
||||
/**
|
||||
* Set the start angle of rotary indicator. 0 deg: right, 90 bottom, etc.
|
||||
* @param arc pointer to an arc object
|
||||
* @param start the start angle
|
||||
*/
|
||||
static inline void lv_rotary_set_start_angle(lv_obj_t * rotary, uint16_t start) {
|
||||
lv_arc_set_start_angle(rotary, start);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the start angle of rotary indicator. 0 deg: right, 90 bottom, etc.
|
||||
* @param rotary pointer to an rotary object
|
||||
* @param end the end angle
|
||||
*/
|
||||
static inline void lv_rotary_set_end_angle(lv_obj_t * rotary, uint16_t end) {
|
||||
lv_arc_set_end_angle(rotary, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the start and end angles
|
||||
* @param rotary pointer to an rotary object
|
||||
* @param start the start angle
|
||||
* @param end the end angle
|
||||
*/
|
||||
static inline void lv_rotary_set_angles(lv_obj_t * rotary, uint16_t start, uint16_t end) {
|
||||
lv_arc_set_angles(rotary, start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the start angle of ar otary indicator background. 0 deg: right, 90 bottom, etc.
|
||||
* @param rotary pointer to an rotary object
|
||||
* @param start the start angle
|
||||
*/
|
||||
static inline void lv_rotary_set_bg_start_angle(lv_obj_t * rotary, uint16_t start) {
|
||||
lv_arc_set_bg_start_angle(rotary, start);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the start angle of rotary indicator background. 0 deg: right, 90 bottom etc.
|
||||
* @param rotary pointer to an rotary object
|
||||
* @param end the end angle
|
||||
*/
|
||||
static inline void lv_rotary_set_bg_end_angle(lv_obj_t * rotary, uint16_t end) {
|
||||
lv_arc_set_bg_end_angle(rotary, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the start and end angles of the rotary indicator background
|
||||
* @param rotary pointer to an rotary object
|
||||
* @param start the start angle
|
||||
* @param end the end angle
|
||||
*/
|
||||
static inline void lv_rotary_set_bg_angles(lv_obj_t * rotary, uint16_t start, uint16_t end) {
|
||||
lv_arc_set_bg_angles(rotary, start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the rotation for the whole rotary indicator
|
||||
* @param rotary pointer to an rotary object
|
||||
* @param rotation_angle rotation angle
|
||||
*/
|
||||
static inline void lv_rotary_set_rotation(lv_obj_t * rotary, uint16_t rotation_angle) {
|
||||
lv_arc_set_rotation(rotary, rotation_angle);
|
||||
}
|
||||
|
||||
/*=====================
|
||||
* Getter functions
|
||||
*====================*/
|
||||
|
||||
/**
|
||||
* Get the start angle of a rotary indicator.
|
||||
* @param rotary pointer to an rotary object
|
||||
* @return the start angle [0..360]
|
||||
*/
|
||||
static inline uint16_t lv_rotary_get_angle_start(lv_obj_t * rotary) {
|
||||
return lv_rotary_get_angle_start(rotary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the end angle of a rotary indicator.
|
||||
* @param rotary pointer to an rotary object
|
||||
* @return the end angle [0..360]
|
||||
*/
|
||||
static inline uint16_t lv_rotary_get_angle_end(lv_obj_t * rotary) {
|
||||
return lv_arc_get_angle_end(rotary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the start angle of a rotary indicator background.
|
||||
* @param rotary pointer to an rotary object
|
||||
* @return the start angle [0..360]
|
||||
*/
|
||||
static inline uint16_t lv_rotary_get_bg_angle_start(lv_obj_t * rotary) {
|
||||
return lv_arc_get_bg_angle_start(rotary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the end angle of a rotary indicator background.
|
||||
* @param rotary pointer to an rotary object
|
||||
* @return the end angle [0..360]
|
||||
*/
|
||||
static inline uint16_t lv_rotary_get_bg_angle_end(lv_obj_t * rotary) {
|
||||
return lv_arc_get_bg_angle_end(rotary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the of a rotary
|
||||
* @param rotary pointer to a rotary object
|
||||
* @return the value of the of the rotary
|
||||
*/
|
||||
int16_t lv_rotary_get_value(const lv_obj_t * rotary);
|
||||
|
||||
/**
|
||||
* Get the value of the left knob of a rotary
|
||||
* @param rotary pointer to a rotary object
|
||||
* @return the start value of the rotary
|
||||
*/
|
||||
int16_t lv_rotary_get_start_value(const lv_obj_t * rotary);
|
||||
|
||||
/**
|
||||
* Get the minimum value of a rotary
|
||||
* @param rotary pointer to a rotary object
|
||||
* @return the minimum value of the rotary
|
||||
*/
|
||||
int16_t lv_rotary_get_min_value(const lv_obj_t * rotary);
|
||||
|
||||
/**
|
||||
* Get the maximum value of a rotary
|
||||
* @param rotary pointer to a rotary object
|
||||
* @return the maximum value of the rotary
|
||||
*/
|
||||
int16_t lv_rotary_get_max_value(const lv_obj_t * rotary);
|
||||
|
||||
/**
|
||||
* Give the rotary is being dragged or not
|
||||
* @param rotary pointer to a rotary object
|
||||
* @return true: drag in progress false: not dragged
|
||||
*/
|
||||
bool lv_rotary_is_dragged(const lv_obj_t * rotary);
|
||||
|
||||
/**
|
||||
* Get whether the rotary is symmetric or not.
|
||||
* @param rotary pointer to a rotary object
|
||||
* @return true: symmetric is enabled; false: disable
|
||||
*/
|
||||
static inline lv_rotary_type_t lv_rotary_get_type(lv_obj_t * rotary)
|
||||
{
|
||||
return LV_ROTARY_TYPE_NORMAL;
|
||||
}
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
#endif /*LV_USE_rotary*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /*LV_ROTARY_H*/
|
Loading…
x
Reference in New Issue
Block a user