1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00

merge rotary to arc

This commit is contained in:
Gabor Kiss-Vamosi 2020-08-11 14:17:05 +02:00
parent 461619411d
commit 14a4c190c9
7 changed files with 490 additions and 968 deletions

View File

@ -1,7 +1,7 @@
# Changelog
## v7.4.0 (planned on 01.09.2020)
*Available in the `dev` branch*
- arc: add set value by click feature
## v7.3.1 (planned on 18.08.2020)

1
lvgl.h
View File

@ -53,7 +53,6 @@ extern "C" {
#include "src/lv_widgets/lv_keyboard.h"
#include "src/lv_widgets/lv_dropdown.h"
#include "src/lv_widgets/lv_roller.h"
#include "src/lv_widgets/lv_rotary.h"
#include "src/lv_widgets/lv_textarea.h"
#include "src/lv_widgets/lv_canvas.h"
#include "src/lv_widgets/lv_win.h"

View File

@ -86,6 +86,7 @@ typedef struct {
#if LV_USE_ARC
lv_style_t arc_indic;
lv_style_t arc_bg;
lv_style_t arc_knob;
#endif
#if LV_USE_BAR
@ -148,10 +149,6 @@ 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
@ -509,6 +506,14 @@ static void arc_init(void)
lv_style_set_line_color(&styles->arc_bg, LV_STATE_DEFAULT, COLOR_BG_SEC);
lv_style_set_line_width(&styles->arc_bg, LV_STATE_DEFAULT, LV_DPX(25));
lv_style_set_line_rounded(&styles->arc_bg, LV_STATE_DEFAULT, true);
style_init_reset(&styles->arc_knob);
lv_style_set_radius(&styles->arc_knob, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
lv_style_set_pad_top(&styles->arc_knob, LV_STATE_DEFAULT, LV_DPX(10));
lv_style_set_pad_bottom(&styles->arc_knob, LV_STATE_DEFAULT, LV_DPX(10));
lv_style_set_pad_left(&styles->arc_knob, LV_STATE_DEFAULT, LV_DPX(10));
lv_style_set_pad_right(&styles->arc_knob, LV_STATE_DEFAULT, LV_DPX(10));
#endif
}
@ -795,41 +800,6 @@ 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, LV_DPX(6));
lv_style_set_pad_right(&styles->rotary_bg, LV_STATE_DEFAULT, LV_DPX(6));
lv_style_set_pad_top(&styles->rotary_bg, LV_STATE_DEFAULT, LV_DPX(6));
lv_style_set_pad_bottom(&styles->rotary_bg, LV_STATE_DEFAULT, LV_DPX(6));
lv_style_set_pad_inner(&styles->rotary_bg, LV_STATE_DEFAULT, LV_DPX(6));
lv_style_set_border_width(&styles->rotary_bg, LV_STATE_DEFAULT, LV_DPX(2));
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_color(&styles->rotary_knob, LV_STATE_CHECKED, COLOR_BTN);
lv_style_set_text_color(&styles->rotary_knob, LV_STATE_CHECKED, IS_LIGHT ? lv_color_hex(0x31404f) : lv_color_hex(0xffffff));
lv_style_set_value_color(&styles->rotary_knob, LV_STATE_CHECKED, IS_LIGHT ? lv_color_hex(0x31404f) : lv_color_hex(0xffffff));
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
@ -969,7 +939,6 @@ 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();
@ -1099,6 +1068,11 @@ static void theme_apply(lv_theme_t * th, lv_obj_t * obj, lv_theme_style_t name)
list = lv_obj_get_style_list(obj, LV_ARC_PART_INDIC);
_lv_style_list_add_style(list, &styles->arc_indic);
list = lv_obj_get_style_list(obj, LV_ARC_PART_KNOB);
_lv_style_list_add_style(list, &styles->bg);
_lv_style_list_add_style(list, &styles->bg_click);
_lv_style_list_add_style(list, &styles->arc_knob);
break;
#endif
@ -1225,27 +1199,6 @@ static void theme_apply(lv_theme_t * th, 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:
list = lv_obj_get_style_list(obj, LV_OBJMASK_PART_MAIN);

View File

@ -9,6 +9,8 @@
#include "lv_arc.h"
#if LV_USE_ARC != 0
#include "../lv_core/lv_group.h"
#include "../lv_core/lv_indev.h"
#include "../lv_misc/lv_debug.h"
#include "../lv_misc/lv_math.h"
#include "../lv_draw/lv_draw_arc.h"
@ -30,6 +32,7 @@ static lv_design_res_t lv_arc_design(lv_obj_t * arc, const lv_area_t * clip_area
static lv_res_t lv_arc_signal(lv_obj_t * arc, lv_signal_t sign, void * param);
static lv_style_list_t * lv_arc_get_style(lv_obj_t * arc, uint8_t part);
static void inv_arc_area(lv_obj_t * arc, uint16_t start_angle, uint16_t end_angle);
static void get_areas(lv_obj_t * arc, lv_point_t * center, lv_coord_t * arc_r, lv_area_t * knob_area);
/**********************
* STATIC VARIABLES
@ -78,7 +81,16 @@ lv_obj_t * lv_arc_create(lv_obj_t * par, const lv_obj_t * copy)
ext->bg_angle_end = 45;
ext->arc_angle_start = 135;
ext->arc_angle_end = 270;
ext->type = LV_ARC_TYPE_NORMAL;
ext->cur_value = -1;
ext->min_value = 0;
ext->max_value = 100;
ext->dragging = false;
ext->chg_rate = 540;
ext->last_tick = lv_tick_get();
ext->last_angle = ext->arc_angle_end;
lv_style_list_init(&ext->style_arc);
lv_style_list_init(&ext->style_knob);
lv_obj_set_size(arc, LV_DPI, LV_DPI);
@ -88,6 +100,10 @@ lv_obj_t * lv_arc_create(lv_obj_t * par, const lv_obj_t * copy)
/*Init the new arc arc*/
if(copy == NULL) {
lv_obj_set_click(arc, true);
lv_obj_add_protect(arc, LV_PROTECT_PRESS_LOST);
lv_obj_set_ext_click_area(arc, LV_DPI / 10, LV_DPI / 10, LV_DPI / 10, LV_DPI / 10);
lv_arc_set_value(arc, ext->min_value);
lv_theme_apply(arc, LV_THEME_ARC);
}
/*Copy an existing arc*/
@ -97,7 +113,15 @@ lv_obj_t * lv_arc_create(lv_obj_t * par, const lv_obj_t * copy)
ext->arc_angle_end = copy_ext->arc_angle_end;
ext->bg_angle_start = copy_ext->bg_angle_start;
ext->bg_angle_end = copy_ext->bg_angle_end;
ext->type = copy_ext->type;
ext->cur_value = copy_ext->cur_value;
ext->min_value = copy_ext->min_value;
ext->max_value = copy_ext->max_value;
ext->dragging = copy_ext->dragging;
ext->chg_rate = copy_ext->chg_rate;
ext->last_tick = copy_ext->last_tick;
ext->last_angle = copy_ext->last_angle;
lv_style_list_copy(&ext->style_knob, &copy_ext->style_knob);
lv_style_list_copy(&ext->style_arc, &copy_ext->style_arc);
/*Refresh the style with new signal function*/
@ -316,6 +340,131 @@ void lv_arc_set_rotation(lv_obj_t * arc, uint16_t rotation_angle)
lv_obj_invalidate(arc);
}
/**
* Set the type of arc.
* @param arc pointer to arc object
* @param type arc type
*/
void lv_arc_set_type(lv_obj_t * arc, lv_arc_type_t type)
{
LV_ASSERT_OBJ(arc, LV_OBJX_NAME);
lv_arc_ext_t *ext = (lv_arc_ext_t *)lv_obj_get_ext_attr(arc);
int16_t val = ext->cur_value;
ext->type = type;
ext->cur_value = -1; /** Force set_value handling*/
int16_t bg_midpoint, bg_end = ext->bg_angle_end;
if (ext->bg_angle_end < ext->bg_angle_start) bg_end = ext->bg_angle_end + 360;
switch(ext->type) {
case LV_ARC_TYPE_SYMMETRIC:
bg_midpoint = (ext->bg_angle_start + bg_end) / 2;
lv_arc_set_start_angle(arc, bg_midpoint);
lv_arc_set_end_angle(arc, bg_midpoint);
break;
case LV_ARC_TYPE_REVERSE:
lv_arc_set_end_angle(arc, ext->bg_angle_end);
break;
default: /** LV_ARC_TYPE_NORMAL*/
lv_arc_set_start_angle(arc, ext->bg_angle_start);
}
lv_arc_set_value(arc, val);
}
/**
* Set a new value on the arc
* @param arc pointer to a arc object
* @param value new value
*/
void lv_arc_set_value(lv_obj_t * arc, int16_t value)
{
LV_ASSERT_OBJ(arc, LV_OBJX_NAME);
lv_arc_ext_t * ext = (lv_arc_ext_t *)lv_obj_get_ext_attr(arc);
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;
int16_t bg_midpoint, range_midpoint, bg_end = ext->bg_angle_end;
if (ext->bg_angle_end < ext->bg_angle_start) bg_end = ext->bg_angle_end + 360;
int16_t angle;
switch(ext->type) {
case LV_ARC_TYPE_SYMMETRIC:
bg_midpoint = (ext->bg_angle_start + bg_end) / 2;
range_midpoint = (int32_t)(ext->min_value + ext->max_value) / 2;
if (ext->cur_value < range_midpoint) {
angle = _lv_map(ext->cur_value, ext->min_value, range_midpoint, ext->bg_angle_start, bg_midpoint);
lv_arc_set_start_angle(arc, angle);
lv_arc_set_end_angle(arc, bg_midpoint);
} else {
angle = _lv_map(ext->cur_value, range_midpoint, ext->max_value, bg_midpoint, bg_end);
lv_arc_set_start_angle(arc, bg_midpoint);
lv_arc_set_end_angle(arc, angle);
}
break;
case LV_ARC_TYPE_REVERSE:
angle = _lv_map(ext->cur_value, ext->min_value, ext->max_value, ext->bg_angle_start, bg_end);
lv_arc_set_start_angle(arc, angle);
break;
default: /** LV_ARC_TYPE_NORMAL*/
angle = _lv_map(ext->cur_value, ext->min_value, ext->max_value, ext->bg_angle_start, bg_end);
lv_arc_set_end_angle(arc, angle);
}
ext->last_angle = angle; /*Cache angle for slew rate limiting*/
}
/**
* Set minimum and the maximum values of a arc
* @param arc pointer to the arc object
* @param min minimum value
* @param max maximum value
*/
void lv_arc_set_range(lv_obj_t * arc, int16_t min, int16_t max)
{
LV_ASSERT_OBJ(arc, LV_OBJX_NAME);
lv_arc_ext_t * ext = (lv_arc_ext_t *)lv_obj_get_ext_attr(arc);
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_arc_set_value(arc, ext->cur_value);
}
/**
* Set the threshold of arc knob increments
* position.
* @param arc pointer to a arc object
* @param threshold increment threshold
*/
void lv_arc_set_chg_rate(lv_obj_t * arc, uint16_t rate)
{
LV_ASSERT_OBJ(arc, LV_OBJX_NAME);
lv_arc_ext_t *ext = (lv_arc_ext_t *)lv_obj_get_ext_attr(arc);
ext->chg_rate = rate;
}
/*=====================
* Getter functions
*====================*/
@ -376,6 +525,72 @@ uint16_t lv_arc_get_bg_angle_end(lv_obj_t * arc)
return ext->bg_angle_end;
}
/**
* Get the value of a arc
* @param arc pointer to a arc object
* @return the value of the arc
*/
int16_t lv_arc_get_value(const lv_obj_t * arc)
{
LV_ASSERT_OBJ(arc, LV_OBJX_NAME);
lv_arc_ext_t * ext = lv_obj_get_ext_attr(arc);
return ext->cur_value;
}
/**
* Get the minimum value of a arc
* @param arc pointer to a arc object
* @return the minimum value of the arc
*/
int16_t lv_arc_get_min_value(const lv_obj_t * arc)
{
LV_ASSERT_OBJ(arc, LV_OBJX_NAME);
lv_arc_ext_t *ext = (lv_arc_ext_t *)lv_obj_get_ext_attr(arc);
return ext->min_value;
}
/**
* Get the maximum value of a arc
* @param arc pointer to a arc object
* @return the maximum value of the arc
*/
int16_t lv_arc_get_max_value(const lv_obj_t * arc)
{
LV_ASSERT_OBJ(arc, LV_OBJX_NAME);
lv_arc_ext_t *ext = (lv_arc_ext_t *)lv_obj_get_ext_attr(arc);
return ext->max_value;
}
/**
* Give the arc is being dragged or not
* @param arc pointer to a arc object
* @return true: drag in progress false: not dragged
*/
bool lv_arc_is_dragged(const lv_obj_t * arc)
{
LV_ASSERT_OBJ(arc, LV_OBJX_NAME);
lv_arc_ext_t * ext = lv_obj_get_ext_attr(arc);
return ext->dragging;
}
/**
* Get whether the arc is type or not.
* @param arc pointer to a arc object
* @return arc type
*/
lv_arc_type_t lv_arc_get_type(const lv_obj_t * arc)
{
LV_ASSERT_OBJ(arc, LV_OBJX_NAME);
lv_arc_ext_t * ext = (lv_arc_ext_t *)lv_obj_get_ext_attr(arc);
return ext->type;
}
/*=====================
* Other functions
*====================*/
@ -414,21 +629,18 @@ static lv_design_res_t lv_arc_design(lv_obj_t * arc, const lv_area_t * clip_area
lv_draw_rect(&arc->coords, clip_area, &bg_dsc);
lv_coord_t left_bg = lv_obj_get_style_pad_left(arc, LV_ARC_PART_BG);
lv_coord_t right_bg = lv_obj_get_style_pad_right(arc, LV_ARC_PART_BG);
lv_coord_t top_bg = lv_obj_get_style_pad_top(arc, LV_ARC_PART_BG);
lv_coord_t bottom_bg = lv_obj_get_style_pad_bottom(arc, LV_ARC_PART_BG);
lv_coord_t r = (LV_MATH_MIN(lv_obj_get_width(arc) - left_bg - right_bg,
lv_obj_get_height(arc) - top_bg - bottom_bg)) / 2;
lv_draw_line_dsc_t arc_dsc;
lv_coord_t x = arc->coords.x1 + r + left_bg;
lv_coord_t y = arc->coords.y1 + r + top_bg;
lv_point_t center;
lv_coord_t arc_r;
lv_area_t knob_area;
get_areas(arc, &center, &arc_r, &knob_area);
if(r > 0) {
/*Draw the background arc*/
lv_draw_line_dsc_t arc_dsc;
if(arc_r > 0) {
lv_draw_line_dsc_init(&arc_dsc);
lv_obj_init_draw_line_dsc(arc, LV_ARC_PART_BG, &arc_dsc);
lv_draw_arc(x, y, r, ext->bg_angle_start + ext->rotation_angle, ext->bg_angle_end + ext->rotation_angle, clip_area,
lv_draw_arc(center.x, center.y, arc_r, ext->bg_angle_start + ext->rotation_angle, ext->bg_angle_end + ext->rotation_angle, clip_area,
&arc_dsc);
}
@ -438,15 +650,23 @@ static lv_design_res_t lv_arc_design(lv_obj_t * arc, const lv_area_t * clip_area
lv_coord_t right_indic = lv_obj_get_style_pad_right(arc, LV_ARC_PART_INDIC);
lv_coord_t top_indic = lv_obj_get_style_pad_top(arc, LV_ARC_PART_INDIC);
lv_coord_t bottom_indic = lv_obj_get_style_pad_bottom(arc, LV_ARC_PART_INDIC);
r -= LV_MATH_MAX4(left_indic, right_indic, top_indic, bottom_indic);
arc_r -= LV_MATH_MAX4(left_indic, right_indic, top_indic, bottom_indic);
if(r > 0) {
if(arc_r > 0) {
lv_draw_line_dsc_init(&arc_dsc);
lv_obj_init_draw_line_dsc(arc, LV_ARC_PART_INDIC, &arc_dsc);
lv_draw_arc(x, y, r, ext->arc_angle_start + ext->rotation_angle, ext->arc_angle_end + ext->rotation_angle, clip_area,
lv_draw_arc(center.x, center.y, arc_r, ext->arc_angle_start + ext->rotation_angle, ext->arc_angle_end + ext->rotation_angle, clip_area,
&arc_dsc);
}
lv_draw_rect_dsc_t knob_rect_dsc;
lv_draw_rect_dsc_init(&knob_rect_dsc);
lv_obj_init_draw_rect_dsc(arc, LV_ARC_PART_KNOB, &knob_rect_dsc);
lv_draw_rect(&knob_area, clip_area, &knob_rect_dsc);
}
/*Post draw when the children are drawn*/
else if(mode == LV_DESIGN_DRAW_POST) {
@ -478,7 +698,118 @@ static lv_res_t lv_arc_signal(lv_obj_t * arc, lv_signal_t sign, void * param)
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
if(sign == LV_SIGNAL_CLEANUP) {
lv_arc_ext_t * ext = lv_obj_get_ext_attr(arc);
if(sign == LV_SIGNAL_PRESSING) {
lv_indev_t * indev = lv_indev_get_act();
if(indev == NULL) return res;
/*Handle only pointers here*/
lv_indev_type_t indev_type = lv_indev_get_type(indev);
if(indev_type != LV_INDEV_TYPE_POINTER) return res;
lv_point_t p;
lv_indev_get_point(indev, &p);
/*Make point relative to the arc's center*/
lv_point_t center;
lv_coord_t r;
lv_area_t knob_area;
get_areas(arc, &center, &r, &knob_area);
p.x -= center.x;
p.y -= center.y;
/*Enter dragging mode if pressed out of the knob*/
if(ext->dragging == false) {
lv_coord_t indic_width = lv_obj_get_style_line_width(arc, LV_ARC_PART_INDIC);
r -= indic_width;
if(p.x * p.x + p.y * p.y > r * r) {
ext->dragging = true;
ext->last_tick = lv_tick_get(); /*Capture timestamp at dragging start*/
}
}
/*It must be in "dragging" mode to turn the arc*/
if(ext->dragging == false) return res;
/*Calculate the angle of the pressed point*/
int16_t angle;
int16_t bg_end = ext->bg_angle_end;
if (ext->bg_angle_end < ext->bg_angle_start) {
bg_end = ext->bg_angle_end + 360;
}
angle = 360 - _lv_atan2(p.x, p.y) + 90; /*Some transformation is required*/
if(angle < ext->bg_angle_start) angle = ext->bg_angle_start;
if(angle > bg_end) angle = bg_end;
/*Calculate the slew rate limited angle based on change rate (degrees/sec)*/
int16_t delta_angle = angle - ext->last_angle;
uint32_t delta_tick = lv_tick_elaps(ext->last_tick);
int16_t delta_angle_max = (ext->chg_rate * delta_tick) / 1000;
if (delta_angle > delta_angle_max) {
delta_angle = delta_angle_max;
} else if (delta_angle < -delta_angle_max) {
delta_angle = -delta_angle_max;
}
angle = ext->last_angle + delta_angle; /*Apply the limited angle change*/
/*Rounding for symmetry*/
int32_t round = ((bg_end - ext->bg_angle_start) * 8) / (ext->max_value - ext->min_value);
round = (round + 4) >> 4;
angle += round;
/*Set the new value*/
int16_t new_value = _lv_map(angle, ext->bg_angle_start, bg_end, ext->min_value, ext->max_value);
if(new_value != lv_arc_get_value(arc)) {
ext->last_tick = lv_tick_get(); /*Cache timestamp for the next iteration*/
lv_arc_set_value(arc, new_value); /*set_value caches the last_angle for the next iteration*/
if(new_value != ext->cur_value) {
res = lv_event_send(arc, LV_EVENT_VALUE_CHANGED, NULL);
if(res != LV_RES_OK) return res;
}
}
/*Don1't let the elapsed time to big while sitting on an end point*/
if(new_value == ext->min_value || new_value == ext->max_value) {
ext->last_tick = lv_tick_get(); /*Cache timestamp for the next iteration*/
}
}
else if(sign == LV_SIGNAL_RELEASED || sign == LV_SIGNAL_PRESS_LOST) {
ext->dragging = false;
#if LV_USE_GROUP
/*Leave edit mode if released. (No need to wait for LONG_PRESS) */
lv_group_t * g = lv_obj_get_group(arc);
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);
int16_t old_value = ext->cur_value;
if(c == LV_KEY_RIGHT || c == LV_KEY_UP) {
lv_arc_set_value(arc, lv_arc_get_value(arc) + 1);
}
else if(c == LV_KEY_LEFT || c == LV_KEY_DOWN) {
lv_arc_set_value(arc, lv_arc_get_value(arc) - 1);
}
if (old_value != ext->cur_value) {
res = lv_event_send(arc, LV_EVENT_VALUE_CHANGED, NULL);
if(res != LV_RES_OK) return res;
}
}
else if(sign == LV_SIGNAL_CLEANUP) {
lv_obj_clean_style_list(arc, LV_ARC_PART_KNOB);
lv_obj_clean_style_list(arc, LV_ARC_PART_INDIC);
}
@ -506,6 +837,9 @@ static lv_style_list_t * lv_arc_get_style(lv_obj_t * arc, uint8_t part)
case LV_ARC_PART_INDIC:
style_dsc_p = &ext->style_arc;
break;
case LV_ARC_PART_KNOB:
style_dsc_p = &ext->style_knob;
break;
default:
style_dsc_p = NULL;
}
@ -617,4 +951,36 @@ static void inv_arc_area(lv_obj_t * arc, uint16_t start_angle, uint16_t end_angl
lv_obj_invalidate(arc);
}
}
static void get_areas(lv_obj_t * arc, lv_point_t * center, lv_coord_t * arc_r, lv_area_t * knob_area)
{
lv_coord_t left_bg = lv_obj_get_style_pad_left(arc, LV_ARC_PART_BG);
lv_coord_t right_bg = lv_obj_get_style_pad_right(arc, LV_ARC_PART_BG);
lv_coord_t top_bg = lv_obj_get_style_pad_top(arc, LV_ARC_PART_BG);
lv_coord_t bottom_bg = lv_obj_get_style_pad_bottom(arc, LV_ARC_PART_BG);
lv_coord_t r = (LV_MATH_MIN(lv_obj_get_width(arc) - left_bg - right_bg,
lv_obj_get_height(arc) - top_bg - bottom_bg)) / 2;
*arc_r = r;
center->x = arc->coords.x1 + r + left_bg;
center->y = arc->coords.y1 + r + top_bg;
lv_coord_t indic_width = lv_obj_get_style_line_width(arc, LV_ARC_PART_INDIC);
r -= indic_width;
lv_coord_t left_knob = lv_obj_get_style_pad_left(arc, LV_ARC_PART_KNOB);
lv_coord_t right_knob = lv_obj_get_style_pad_right(arc, LV_ARC_PART_KNOB);
lv_coord_t top_knob = lv_obj_get_style_pad_top(arc, LV_ARC_PART_KNOB);
lv_coord_t bottom_knob = lv_obj_get_style_pad_bottom(arc, LV_ARC_PART_KNOB);
knob_area->x1 = center->x - r + left_knob;
knob_area->x2 = center->x + r - right_knob;
knob_area->y1 = center->y - r + top_knob;
knob_area->y2 = center->y + r - bottom_knob;
}
#endif

View File

@ -26,6 +26,14 @@ extern "C" {
/**********************
* TYPEDEFS
**********************/
enum {
LV_ARC_TYPE_NORMAL,
LV_ARC_TYPE_SYMMETRIC,
LV_ARC_TYPE_REVERSE
};
typedef uint8_t lv_arc_type_t;
/*Data of arc*/
typedef struct {
/*New data for this type */
@ -35,14 +43,24 @@ typedef struct {
uint16_t bg_angle_start;
uint16_t bg_angle_end;
lv_style_list_t style_arc;
lv_style_list_t style_knob; /* Style of the knob */
int16_t cur_value; /*Current value of the arc*/
int16_t min_value; /*Minimum value of the arc*/
int16_t max_value; /*Maximum value of the arc*/
uint16_t dragging :1;
uint16_t type :2;
uint16_t chg_rate; /*Drag angle rate of change of the arc (degrees/sec)*/
uint32_t last_tick; /*Last dragging event timestamp of the arc*/
int16_t last_angle; /*Last dragging angle of the arc*/
} lv_arc_ext_t;
/*Parts of the arc*/
enum {
LV_ARC_PART_BG = LV_OBJ_PART_MAIN,
LV_ARC_PART_INDIC,
LV_ARC_PART_KNOB,
_LV_ARC_PART_VIRTUAL_LAST,
_LV_ARC_PART_REAL_LAST = _LV_OBJ_PART_REAL_LAST,
};
typedef uint8_t lv_arc_part_t;
@ -118,6 +136,45 @@ void lv_arc_set_bg_angles(lv_obj_t * arc, uint16_t start, uint16_t end);
*/
void lv_arc_set_rotation(lv_obj_t * arc, uint16_t rotation_angle);
/**
* Set the type of arc.
* @param arc pointer to arc object
* @param type arc type
*/
void lv_arc_set_type(lv_obj_t * arc, lv_arc_type_t type);
/**
* Set a new value on the arc
* @param arc pointer to a arc object
* @param value new value
*/
void lv_arc_set_value(lv_obj_t * arc, int16_t value);
/**
* Set minimum and the maximum values of a arc
* @param arc pointer to the arc object
* @param min minimum value
* @param max maximum value
*/
void lv_arc_set_range(lv_obj_t * arc, int16_t min, int16_t max);
/**
* Reverse arc behavior. The indicator will grow from arc end instead of arc start.
* position.
* @param arc pointer to a arc object
* @param reverse true: enable disable reverse behavior; false: disable
*/
void lv_arc_set_reverse(lv_obj_t * arc, bool reverse);
/**
* Set the threshold of arc knob increments
* position.
* @param arc pointer to a arc object
* @param threshold increment threshold
*/
void lv_arc_set_chg_rate(lv_obj_t * arc, uint16_t threshold);
/*=====================
* Getter functions
*====================*/
@ -150,6 +207,41 @@ uint16_t lv_arc_get_bg_angle_start(lv_obj_t * arc);
*/
uint16_t lv_arc_get_bg_angle_end(lv_obj_t * arc);
/**
* Get whether the arc is type or not.
* @param arc pointer to a arc object
* @return arc type
*/
lv_arc_type_t lv_arc_get_type(const lv_obj_t * arc);
/**
* Get the value of the of a arc
* @param arc pointer to a arc object
* @return the value of the of the arc
*/
int16_t lv_arc_get_value(const lv_obj_t * arc);
/**
* Get the minimum value of a arc
* @param arc pointer to a arc object
* @return the minimum value of the arc
*/
int16_t lv_arc_get_min_value(const lv_obj_t * arc);
/**
* Get the maximum value of a arc
* @param arc pointer to a arc object
* @return the maximum value of the arc
*/
int16_t lv_arc_get_max_value(const lv_obj_t * arc);
/**
* Give the arc is being dragged or not
* @param arc pointer to a arc object
* @return true: drag in progress false: not dragged
*/
bool lv_arc_is_dragged(const lv_obj_t * arc);
/*=====================
* Other functions
*====================*/

View File

@ -1,566 +0,0 @@
/**
* @file lv_rotary.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_rotary.h"
#if LV_USE_ROTARY != 0
#include "../lv_misc/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_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->type = LV_ROTARY_TYPE_NORMAL;
ext->cur_value = -1;
ext->min_value = 0;
ext->max_value = 100;
ext->sensitivity = 1;
ext->dragging = false;
ext->chg_rate = 540;
ext->last_tick = lv_tick_get();
ext->last_angle = ext->arc.arc_angle_end;
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, LV_DPI / 10, LV_DPI / 10, LV_DPI / 10, LV_DPI / 10);
lv_rotary_set_value(rotary, 50, LV_ANIM_OFF);
lv_theme_apply(rotary, LV_THEME_ROTARY);
}
/*Copy an existing rotary*/
else {
lv_rotary_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
ext->type = copy_ext->type;
ext->cur_value = copy_ext->cur_value;
ext->min_value = copy_ext->min_value;
ext->max_value = copy_ext->max_value;
ext->sensitivity = copy_ext->sensitivity;
ext->dragging = copy_ext->dragging;
ext->chg_rate = copy_ext->chg_rate;
ext->last_tick = copy_ext->last_tick;
ext->last_angle = copy_ext->last_angle;
lv_style_list_copy(&ext->style_knob, &copy_ext->style_knob);
lv_obj_refresh_style(rotary, LV_OBJ_PART_ALL);
}
LV_LOG_INFO("rotary created");
return rotary;
}
/*=====================
* Setter functions
*====================*/
/**
* Set the type of rotary.
* @param rotary pointer to rotary object
* @param type rotary type
*/
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);
int16_t val = ext->cur_value;
ext->type = type;
ext->cur_value = -1; /** Force set_value handling*/
int16_t bg_midpoint, bg_end = ext->arc.bg_angle_end;
if (ext->arc.bg_angle_end < ext->arc.bg_angle_start) bg_end = ext->arc.bg_angle_end + 360;
switch(ext->type) {
case LV_ROTARY_TYPE_SYMMETRIC:
bg_midpoint = (ext->arc.bg_angle_start + bg_end) / 2;
lv_rotary_set_start_angle(rotary, bg_midpoint);
lv_rotary_set_end_angle(rotary, bg_midpoint);
break;
case LV_ROTARY_TYPE_REVERSE:
lv_rotary_set_end_angle(rotary, ext->arc.bg_angle_end);
break;
default: /** LV_ROTARY_TYPE_NORMAL*/
lv_rotary_set_start_angle(rotary, ext->arc.bg_angle_start);
}
lv_rotary_set_value(rotary, val, false);
}
/**
* 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
*/
bool 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 false;
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 false;
ext->cur_value = new_value;
int16_t bg_midpoint, range_midpoint, bg_end = ext->arc.bg_angle_end;
if (ext->arc.bg_angle_end < ext->arc.bg_angle_start) bg_end = ext->arc.bg_angle_end + 360;
int16_t angle;
switch(ext->type) {
case LV_ROTARY_TYPE_SYMMETRIC:
bg_midpoint = (ext->arc.bg_angle_start + bg_end) / 2;
range_midpoint = (int32_t)(ext->min_value + ext->max_value) / 2;
if (ext->cur_value < range_midpoint) {
angle = _lv_map(ext->cur_value, ext->min_value, range_midpoint, ext->arc.bg_angle_start, bg_midpoint);
lv_arc_set_start_angle(rotary, angle);
lv_arc_set_end_angle(rotary, bg_midpoint);
} else {
angle = _lv_map(ext->cur_value, range_midpoint, ext->max_value, bg_midpoint, bg_end);
lv_arc_set_start_angle(rotary, bg_midpoint);
lv_arc_set_end_angle(rotary, angle);
}
break;
case LV_ROTARY_TYPE_REVERSE:
angle = _lv_map(ext->cur_value, ext->min_value, ext->max_value, ext->arc.bg_angle_start, bg_end);
lv_arc_set_start_angle(rotary, angle);
break;
default: /** LV_ROTARY_TYPE_NORMAL*/
angle = _lv_map(ext->cur_value, ext->min_value, ext->max_value, ext->arc.bg_angle_start, bg_end);
lv_arc_set_end_angle(rotary, angle);
}
ext->last_angle = angle; /*Cache angle for slew rate limiting*/
return true;
}
/**
* 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);
}
/**
* Set the sesitivity of rotary knob increments
* position.
* @param rotary pointer to a rotary object
* @param sensitivity increment multiplier
*/
void lv_rotary_set_sensitivity(lv_obj_t * rotary, uint16_t sensitivity)
{
LV_ASSERT_OBJ(rotary, LV_OBJX_NAME);
lv_rotary_ext_t *ext = (lv_rotary_ext_t *)lv_obj_get_ext_attr(rotary);
ext->sensitivity = sensitivity;
}
/**
* Set the threshold of rotary knob increments
* position.
* @param rotary pointer to a rotary object
* @param threshold increment threshold
*/
void lv_rotary_set_chg_rate(lv_obj_t * rotary, uint16_t rate)
{
LV_ASSERT_OBJ(rotary, LV_OBJX_NAME);
lv_rotary_ext_t *ext = (lv_rotary_ext_t *)lv_obj_get_ext_attr(rotary);
ext->chg_rate = rate;
}
/*=====================
* 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 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;
}
/**
* Get whether the rotary is type or not.
* @param rotary pointer to a rotary object
* @return rotary type
*/
lv_rotary_type_t lv_rotary_get_type(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->type;
}
/**********************
* 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);
if(sign == LV_SIGNAL_PRESSING) {
lv_indev_t * indev = lv_indev_get_act();
if(indev == NULL) return res;
/*Handle only pointers here*/
lv_indev_type_t indev_type = lv_indev_get_type(indev);
if(indev_type != LV_INDEV_TYPE_POINTER) return res;
lv_point_t p;
lv_indev_get_point(indev, &p);
/*Make point relative to the rotary's center*/
lv_coord_t w_half = lv_obj_get_width(rotary) / 2;
p.x -= rotary->coords.x1 + w_half;
p.y -= rotary->coords.y1 + w_half;
/*Enter dragging mode if pressed out of the knob*/
if(ext->dragging == false) {
lv_coord_t r_in = lv_area_get_width(&ext->knob_area) / 2;
if(p.x * p.x + p.y * p.y > r_in * r_in) {
ext->dragging = true;
ext->last_tick = lv_tick_get(); /*Capture timestamp at dragging start*/
}
}
/*It must be in "dragging" mode to turn the arc*/
if(ext->dragging == false) return res;
/*Calculate the angle of the pressed point*/
int16_t angle;
int16_t bg_end = ext->arc.bg_angle_end;
if (ext->arc.bg_angle_end < ext->arc.bg_angle_start) {
bg_end = ext->arc.bg_angle_end + 360;
}
angle = 360 - _lv_atan2(p.x, p.y) + 90; /*Some transformation is required*/
if(angle < ext->arc.bg_angle_start) angle = ext->arc.bg_angle_start;
if(angle > bg_end) angle = bg_end;
/*Calculate the slew rate limited angle based on change rate (degrees/sec)*/
int16_t delta_angle = angle - ext->last_angle;
uint32_t delta_tick = lv_tick_elaps(ext->last_tick);
int16_t delta_angle_max = (ext->chg_rate * delta_tick) / 1000;
if (delta_angle > delta_angle_max) {
delta_angle = delta_angle_max;
} else if (delta_angle < -delta_angle_max) {
delta_angle = -delta_angle_max;
}
angle = ext->last_angle + delta_angle; /*Apply the limited angle change*/
/*Rounding for symmetry*/
int32_t round = ((bg_end - ext->arc.bg_angle_start) * 8) / (ext->max_value - ext->min_value);
round = (round + 4) >> 4;
angle += round;
/*Set the new value*/
int16_t new_value = _lv_map(angle, ext->arc.bg_angle_start, bg_end, ext->min_value, ext->max_value);
if(new_value != lv_rotary_get_value(rotary)) {
ext->last_tick = lv_tick_get(); /*Cache timestamp for the next iteration*/
if (lv_rotary_set_value(rotary, new_value, LV_ANIM_OFF)) { /*set_value caches the last_angle for the next interation*/
res = lv_event_send(rotary, LV_EVENT_VALUE_CHANGED, NULL);
if(res != LV_RES_OK) return res;
}
}
/*Don1't let the elapsed time to big while sitting on an end point*/
if(new_value == ext->min_value || new_value == ext->max_value) {
ext->last_tick = lv_tick_get(); /*Cache timestamp for the next iteration*/
}
}
else if(sign == LV_SIGNAL_RELEASED || sign == LV_SIGNAL_PRESS_LOST) {
ext->dragging = false;
#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) {
if (lv_rotary_set_value(rotary, lv_rotary_get_value(rotary) + ext->sensitivity, 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) {
if (lv_rotary_set_value(rotary, lv_rotary_get_value(rotary) - ext->sensitivity, 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_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_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;
lv_coord_t knob_inner = lv_obj_get_style_pad_inner(rotary, LV_ROTARY_PART_KNOB);
lv_rotary_ext_t * ext = lv_obj_get_ext_attr(rotary);
if(r > 0) {
knob_area.x1 = center_x - r + indic_width + knob_inner;
knob_area.x2 = center_x + r - indic_width - knob_inner;
knob_area.y1 = center_y - r + indic_width + knob_inner;
knob_area.y2 = center_y + r - indic_width - knob_inner;
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);
}
lv_area_copy(&ext->knob_area, &knob_area);
}
#endif

View File

@ -1,322 +0,0 @@
/**
* @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
#include "../lv_core/lv_obj.h"
#include "lv_arc.h"
#include "lv_btn.h"
/*********************
* DEFINES
*********************/
#define LV_ROTARY_KNOB_SIZE_MIN 4 /* cannot make the knob smaller then this [px] */
/**********************
* TYPEDEFS
**********************/
enum {
LV_ROTARY_TYPE_NORMAL,
LV_ROTARY_TYPE_SYMMETRIC,
LV_ROTARY_TYPE_REVERSE
};
typedef uint8_t lv_rotary_type_t;
/** Possible states of a rotary.*/
enum {
LV_ROTARY_STATE_RELEASED = LV_BTN_STATE_RELEASED,
LV_ROTARY_STATE_PRESSED = LV_BTN_STATE_PRESSED,
LV_ROTARY_STATE_DISABLED = LV_BTN_STATE_DISABLED,
LV_ROTARY_STATE_CHECKED_RELEASED = LV_BTN_STATE_CHECKED_RELEASED,
LV_ROTARY_STATE_CHECKED_PRESSED = LV_BTN_STATE_CHECKED_PRESSED,
LV_ROTARY_STATE_CHECKED_DISABLED = LV_BTN_STATE_CHECKED_DISABLED,
_LV_ROTARY_STATE_LAST = _LV_BTN_STATE_LAST, /* Number of states*/
};
typedef uint8_t lv_rotary_state_t;
/*Data of rotary*/
typedef struct {
/*Ext. of ancestor*/
lv_arc_ext_t arc;
/*New data for this type*/
lv_style_list_t style_knob; /* Style of the knob */
lv_area_t knob_area; /* Area of the knob */
uint16_t type :2;
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 sensitivity; /*Control signal increment multiplier of the rotary*/
uint16_t dragging :1;
uint16_t chg_rate; /*Drag angle rate of change of the rotary (degrees/sec)*/
int32_t last_tick; /*Last dragging event timestamp of the rotary*/
int16_t last_angle; /*Last dragging angle of the rotary*/
} lv_rotary_ext_t;
/** Built-in styles of rotary*/
enum {
LV_ROTARY_PART_BG = LV_ARC_PART_BG, /** Rotary background style. */
LV_ROTARY_PART_INDIC = LV_ARC_PART_INDIC, /** Rotary indicator (filled area) style. */
LV_ROTARY_PART_KNOB = _LV_ARC_PART_VIRTUAL_LAST, /** Rotary knob style. */
_LV_ROTARY_PART_VIRTUAL_LAST,
_LV_ROTARY_PART_REAL_LAST = _LV_ARC_PART_REAL_LAST
};
/**********************
* 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 the type of rotary.
* @param rotary pointer to rotary object
* @param type rotary type
*/
void lv_rotary_set_type(lv_obj_t * rotary, lv_rotary_type_t type);
/**
* 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
* @return true if value changed, otw false
*/
bool lv_rotary_set_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);
/**
* Reverse rotary behavior. The indicator will grow from arc end instead of arc start.
* position.
* @param rotary pointer to a rotary object
* @param reverse true: enable disable reverse behavior; false: disable
*/
void lv_rotary_set_reverse(lv_obj_t * rotary, bool reverse);
/**
* Set the sesitivity of rotary knob increments
* position.
* @param rotary pointer to a rotary object
* @param sensitivity increment multiplier
*/
void lv_rotary_set_sensitivity(lv_obj_t * rotary, uint16_t sensitivity);
/**
* Set the threshold of rotary knob increments
* position.
* @param rotary pointer to a rotary object
* @param threshold increment threshold
*/
void lv_rotary_set_chg_rate(lv_obj_t * rotary, uint16_t threshold);
/**
* 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);
}
/**
* Set the state of the rotary
* @param rotary pointer to a rotary object
* @param state the new state of the rotary (from lv_rotary_state_t enum)
*/
static inline void lv_rotary_set_state(lv_obj_t * rotary, lv_rotary_state_t state) {
lv_btn_set_state(rotary, state);
}
/*=====================
* 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 whether the rotary is type or not.
* @param rotary pointer to a rotary object
* @return rotary type
*/
lv_rotary_type_t lv_rotary_get_type(const lv_obj_t * 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 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 the current state of the rotary
* @param rotary pointer to a rotary object
* @return the state of the rotary (from lv_rotary_state_t enum).
* If the rotary is in disabled state `LV_ROTARY_STATE_DISABLED` will be ORed to the other rotary states.
*/
static inline lv_rotary_state_t lv_rotary_get_state(const lv_obj_t * rotary) {
return lv_btn_get_state(rotary);
}
/**********************
* MACROS
**********************/
#endif /*LV_USE_rotary*/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /*LV_ROTARY_H*/