1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-21 06:53:01 +08:00
lvgl/src/lv_widgets/lv_gauge.c

600 lines
19 KiB
C
Raw Normal View History

2020-03-24 10:13:52 +01:00
/**
* @file lv_gauge.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_gauge.h"
#if LV_USE_GAUGE != 0
2019-09-24 16:30:38 +02:00
#include "../lv_core/lv_debug.h"
#include "../lv_draw/lv_draw.h"
#include "../lv_themes/lv_theme.h"
2017-11-26 11:38:28 +01:00
#include "../lv_misc/lv_txt.h"
2017-11-23 20:42:14 +01:00
#include "../lv_misc/lv_math.h"
#include "../lv_misc/lv_utils.h"
2019-11-30 11:41:18 +01:00
#include "lv_img.h"
2017-04-24 16:16:36 +02:00
#include <stdio.h>
#include <string.h>
/*********************
* DEFINES
*********************/
#define LV_OBJX_NAME "lv_gauge"
2019-04-04 07:15:40 +02:00
#define LV_GAUGE_DEF_NEEDLE_COLOR LV_COLOR_RED
#define LV_GAUGE_DEF_LABEL_COUNT 6
#define LV_GAUGE_DEF_LINE_COUNT 21 /*Should be: ((label_cnt - 1) * internal_lines) + 1*/
2020-03-19 09:32:42 +01:00
#define LV_GAUGE_DEF_ANGLE 270
2017-04-21 09:15:39 +02:00
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2019-09-06 19:53:39 +02:00
static lv_design_res_t lv_gauge_design(lv_obj_t * gauge, const lv_area_t * clip_area, lv_design_mode_t mode);
2017-11-11 14:23:05 +01:00
static lv_res_t lv_gauge_signal(lv_obj_t * gauge, lv_signal_t sign, void * param);
static lv_style_list_t * lv_gauge_get_style(lv_obj_t * gauge, uint8_t part);
2020-01-01 21:44:16 +01:00
static void lv_gauge_draw_labels(lv_obj_t * gauge, const lv_area_t * mask);
2019-11-30 11:41:18 +01:00
static void lv_gauge_draw_needle(lv_obj_t * gauge, 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 gauge objects
* @param par pointer to an object, it will be the parent of the new gauge
* @param copy pointer to a gauge object, if not NULL then the new object will be copied from it
* @return pointer to the created gauge
*/
lv_obj_t * lv_gauge_create(lv_obj_t * par, const lv_obj_t * copy)
{
2018-10-05 17:22:49 +02:00
LV_LOG_TRACE("gauge create started");
2018-07-25 17:57:08 +02:00
/*Create the ancestor gauge*/
lv_obj_t * new_gauge = lv_linemeter_create(par, copy);
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(new_gauge);
if(new_gauge == NULL) return NULL;
/*Allocate the gauge type specific extended data*/
lv_gauge_ext_t * ext = lv_obj_allocate_ext_attr(new_gauge, sizeof(lv_gauge_ext_t));
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(ext);
if(ext == NULL) {
lv_obj_del(new_gauge);
return NULL;
}
/*Initialize the allocated 'ext' */
2019-04-04 07:15:40 +02:00
ext->needle_count = 0;
ext->values = NULL;
2017-04-21 17:11:47 +02:00
ext->needle_colors = NULL;
2019-04-04 07:15:40 +02:00
ext->label_count = LV_GAUGE_DEF_LABEL_COUNT;
2020-03-20 04:17:11 -07:00
ext->format_cb = NULL;
2019-11-30 11:41:18 +01:00
ext->needle_img = 0;
ext->needle_img_pivot.x = 0;
ext->needle_img_pivot.y = 0;
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_gauge);
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_gauge);
lv_style_list_init(&ext->style_strong);
lv_style_list_init(&ext->style_needle);
/*The signal and design functions are not copied so set them here*/
lv_obj_set_signal_cb(new_gauge, lv_gauge_signal);
lv_obj_set_design_cb(new_gauge, lv_gauge_design);
/*Init the new gauge gauge*/
if(copy == NULL) {
2019-06-06 06:05:40 +02:00
lv_gauge_set_scale(new_gauge, LV_GAUGE_DEF_ANGLE, LV_GAUGE_DEF_LINE_COUNT, LV_GAUGE_DEF_LABEL_COUNT);
lv_gauge_set_needle_count(new_gauge, 1, NULL);
2017-12-19 22:00:32 +01:00
lv_gauge_set_critical_value(new_gauge, 80);
lv_obj_set_size(new_gauge, 3 * LV_DPI / 2, 3 * LV_DPI / 2);
lv_theme_apply(new_gauge, LV_THEME_GAUGE);
2020-01-01 21:44:16 +01:00
}
/*Copy an existing gauge*/
else {
2018-06-19 09:49:58 +02:00
lv_gauge_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
lv_gauge_set_needle_count(new_gauge, copy_ext->needle_count, copy_ext->needle_colors);
uint8_t i;
for(i = 0; i < ext->needle_count; i++) {
ext->values[i] = copy_ext->values[i];
}
ext->label_count = copy_ext->label_count;
2020-03-20 04:17:11 -07:00
ext->format_cb = copy_ext->format_cb;
2020-03-06 13:14:25 +01:00
/*Refresh the style with new signal function*/
2020-03-10 10:47:14 +01:00
lv_obj_refresh_style(new_gauge, LV_STYLE_PROP_ALL);
}
2018-10-05 17:22:49 +02:00
LV_LOG_INFO("gauge created");
2018-07-25 17:57:08 +02:00
return new_gauge;
}
/*=====================
* Setter functions
*====================*/
/**
* Set the number of needles
* @param gauge pointer to gauge object
* @param needle_cnt new count of needles
* @param colors an array of colors for needles (with 'num' elements)
*/
void lv_gauge_set_needle_count(lv_obj_t * gauge, uint8_t needle_cnt, const lv_color_t colors[])
{
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
if(ext->needle_count != needle_cnt) {
2018-06-19 09:49:58 +02:00
if(ext->values != NULL) {
lv_mem_free(ext->values);
ext->values = NULL;
}
2017-04-11 10:50:57 +02:00
2018-06-19 09:49:58 +02:00
ext->values = lv_mem_realloc(ext->values, needle_cnt * sizeof(int16_t));
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(ext->values);
if(ext->values == NULL) return;
2018-06-19 09:49:58 +02:00
int16_t min = lv_gauge_get_min_value(gauge);
uint8_t n;
for(n = ext->needle_count; n < needle_cnt; n++) {
ext->values[n] = min;
}
2018-06-19 09:49:58 +02:00
ext->needle_count = needle_cnt;
2017-04-11 10:50:57 +02:00
}
2017-04-21 17:11:47 +02:00
ext->needle_colors = colors;
lv_obj_invalidate(gauge);
}
/**
* Set the value of a needle
* @param gauge pointer to a gauge
* @param needle_id the id of the needle
* @param value the new value
*/
2020-03-20 03:51:19 -07:00
void lv_gauge_set_value(lv_obj_t * gauge, uint8_t needle_id, int32_t value)
{
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
if(needle_id >= ext->needle_count) return;
if(ext->values[needle_id] == value) return;
int16_t min = lv_gauge_get_min_value(gauge);
int16_t max = lv_gauge_get_max_value(gauge);
2017-04-21 17:11:47 +02:00
2019-04-04 07:15:40 +02:00
if(value > max)
value = max;
else if(value < min)
value = min;
ext->values[needle_id] = value;
lv_obj_invalidate(gauge);
}
/**
* Set the scale settings of a gauge
* @param gauge pointer to a gauge object
* @param angle angle of the scale (0..360)
* @param line_cnt count of scale lines.
2019-11-30 11:41:18 +01:00
* To get a given "subdivision" lines between labels:
* `line_cnt = (sub_div + 1) * (label_cnt - 1) + 1 `
* @param label_cnt count of scale labels.
*/
void lv_gauge_set_scale(lv_obj_t * gauge, uint16_t angle, uint8_t line_cnt, uint8_t label_cnt)
{
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
lv_linemeter_set_scale(gauge, angle, line_cnt);
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
2019-04-04 07:15:40 +02:00
ext->label_count = label_cnt;
lv_obj_invalidate(gauge);
}
2019-11-30 11:41:18 +01:00
/**
* Set an image to display as needle(s).
* The needle image should be horizontal and pointing to the right (`--->`).
* @param gauge pointer to a gauge object
* @param img_src pointer to an `lv_img_dsc_t` variable or a path to an image
* (not an `lv_img` object)
* @param pivot_x the X coordinate of rotation center of the image
* @param pivot_y the Y coordinate of rotation center of the image
*/
void lv_gauge_set_needle_img(lv_obj_t * gauge, const void * img, lv_coord_t pivot_x, lv_coord_t pivot_y)
{
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
ext->needle_img = img;
ext->needle_img_pivot.x = pivot_x;
ext->needle_img_pivot.y = pivot_y;
lv_obj_invalidate(gauge);
}
2020-03-17 04:17:43 -07:00
/**
* Assign a function to format gauge values
* @param gauge pointer to a gauge object
2020-03-20 04:17:11 -07:00
* @param format_cb pointer to function of lv_gauge_format_cb_t
2020-03-17 04:17:43 -07:00
*/
2020-03-20 04:17:11 -07:00
void lv_gauge_set_formatter_cb(lv_obj_t * gauge, lv_gauge_format_cb_t format_cb)
2020-03-17 04:17:43 -07:00
{
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
2020-03-20 04:17:11 -07:00
ext->format_cb = format_cb;
2020-03-17 04:17:43 -07:00
}
/*=====================
* Getter functions
*====================*/
/**
* Get the value of a needle
* @param gauge pointer to gauge object
* @param needle the id of the needle
* @return the value of the needle [min,max]
*/
2020-03-20 03:51:19 -07:00
int32_t lv_gauge_get_value(const lv_obj_t * gauge, uint8_t needle)
{
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
2019-04-04 07:15:40 +02:00
int16_t min = lv_gauge_get_min_value(gauge);
if(needle >= ext->needle_count) return min;
return ext->values[needle];
}
/**
* Get the count of needles on a gauge
* @param gauge pointer to gauge
* @return count of needles
*/
uint8_t lv_gauge_get_needle_count(const lv_obj_t * gauge)
{
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
return ext->needle_count;
}
/**
* Set the number of labels (and the thicker lines too)
* @param gauge pointer to a gauge object
* @return count of labels
*/
uint8_t lv_gauge_get_label_count(const lv_obj_t * gauge)
{
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
return ext->label_count;
}
2019-11-30 11:41:18 +01:00
/**
* Get an image to display as needle(s).
* @param gauge pointer to a gauge object
* @return pointer to an `lv_img_dsc_t` variable or a path to an image
* (not an `lv_img` object). `NULL` if not used.
*/
const void * lv_gauge_get_needle_img(lv_obj_t * gauge, const void * img, lv_coord_t pivot_x, lv_coord_t pivot_y)
{
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
return ext->needle_img;
}
/**
* Get the X coordinate of the rotation center of the needle image
* @param gauge pointer to a gauge object
* @return the X coordinate of rotation center of the image
*/
lv_coord_t lv_gauge_get_needle_img_pivot_x(lv_obj_t * gauge)
{
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
return ext->needle_img_pivot.x;
}
/**
* Get the Y coordinate of the rotation center of the needle image
* @param gauge pointer to a gauge object
* @return the X coordinate of rotation center of the image
*/
lv_coord_t lv_gauge_get_needle_img_pivot_y(lv_obj_t * gauge)
{
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
return ext->needle_img_pivot.y;
}
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Handle the drawing related tasks of the gauges
* @param gauge pointer to an object
2019-09-06 19:53:39 +02:00
* @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
2019-09-06 19:53:39 +02:00
* @param return an element of `lv_design_res_t`
*/
2019-09-06 19:53:39 +02:00
static lv_design_res_t lv_gauge_design(lv_obj_t * gauge, 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) {
2019-09-06 19:53:39 +02:00
return LV_DESIGN_RES_NOT_COVER;
}
/*Draw the object*/
else if(mode == LV_DESIGN_DRAW_MAIN) {
2020-02-12 08:54:03 +01:00
ancestor_design(gauge, clip_area, mode);
2019-04-11 19:59:55 +08:00
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
2020-01-01 21:44:16 +01:00
lv_gauge_draw_labels(gauge, clip_area);
2020-02-12 08:54:03 +01:00
/*Add the strong lines*/
uint16_t line_cnt_tmp = ext->lmeter.line_cnt;
2020-01-01 21:44:16 +01:00
ext->lmeter.line_cnt = ext->label_count; /*Only to labels*/
2020-03-06 13:14:25 +01:00
lv_linemeter_draw_scale(gauge, clip_area, LV_GAUGE_PART_MAJOR);
2019-04-04 07:15:40 +02:00
ext->lmeter.line_cnt = line_cnt_tmp; /*Restore the parameters*/
2019-09-06 19:53:39 +02:00
lv_gauge_draw_needle(gauge, clip_area);
}
/*Post draw when the children are drawn*/
else if(mode == LV_DESIGN_DRAW_POST) {
2019-09-06 19:53:39 +02:00
ancestor_design(gauge, clip_area, mode);
}
2019-09-06 19:53:39 +02:00
return LV_DESIGN_RES_OK;
}
2017-11-11 14:23:05 +01:00
/**
* Signal function of the gauge
* @param gauge pointer to a gauge 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_gauge_signal(lv_obj_t * gauge, lv_signal_t sign, void * param)
{
lv_res_t res;
2020-01-01 21:44:16 +01:00
if(sign == LV_SIGNAL_GET_STYLE) {
2020-01-09 14:16:32 +01:00
lv_get_style_info_t * info = param;
info->result = lv_gauge_get_style(gauge, info->part);
if(info->result != NULL) return LV_RES_OK;
else return ancestor_signal(gauge, sign, param);
2020-01-01 21:44:16 +01:00
}
2017-11-11 14:23:05 +01:00
/* Include the ancient signal function */
res = ancestor_signal(gauge, 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);
2017-11-11 14:23:05 +01:00
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
if(sign == LV_SIGNAL_CLEANUP) {
2017-11-23 21:28:36 +01:00
lv_mem_free(ext->values);
2017-11-11 14:23:05 +01:00
ext->values = NULL;
lv_obj_clean_style_list(gauge, LV_GAUGE_PART_NEEDLE);
2020-03-06 13:14:25 +01:00
lv_obj_clean_style_list(gauge, LV_GAUGE_PART_MAJOR);
2018-02-28 15:37:41 +01:00
}
2017-11-11 14:23:05 +01:00
return res;
}
2020-01-01 21:44:16 +01:00
/**
* Get the style descriptor of a part of the object
* @param page pointer the object
* @param part the part from `lv_gauge_part_t`. (LV_GAUGE_PART_...)
* @return pointer to the style descriptor of the specified part
*/
static lv_style_list_t * lv_gauge_get_style(lv_obj_t * gauge, uint8_t part)
2020-01-01 21:44:16 +01:00
{
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
lv_style_list_t * style_dsc_p;
2020-01-01 21:44:16 +01:00
switch(part) {
2020-02-26 19:48:27 +01:00
case LV_GAUGE_PART_MAIN:
style_dsc_p = &gauge->style_list;
break;
2020-03-06 13:14:25 +01:00
case LV_GAUGE_PART_MAJOR:
2020-02-26 19:48:27 +01:00
style_dsc_p = &ext->style_strong;
break;
case LV_GAUGE_PART_NEEDLE:
style_dsc_p = &ext->style_needle;
break;
default:
style_dsc_p = NULL;
2020-01-01 21:44:16 +01:00
}
2017-11-11 14:23:05 +01:00
2020-01-01 21:44:16 +01:00
return style_dsc_p;
}
/**
* Draw the scale on a gauge
* @param gauge pointer to gauge object
* @param mask mask of drawing
*/
2020-01-01 21:44:16 +01:00
static void lv_gauge_draw_labels(lv_obj_t * gauge, const lv_area_t * mask)
{
char scale_txt[16];
2019-04-11 19:59:55 +08:00
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
2020-03-06 13:14:25 +01:00
lv_style_int_t scale_width = lv_obj_get_style_scale_width(gauge, LV_GAUGE_PART_MAJOR);
lv_style_int_t left = lv_obj_get_style_pad_left(gauge, LV_GAUGE_PART_MAIN);
lv_style_int_t right = lv_obj_get_style_pad_right(gauge, LV_GAUGE_PART_MAIN);
lv_style_int_t top = lv_obj_get_style_pad_top(gauge, LV_GAUGE_PART_MAIN);
lv_style_int_t txt_pad = lv_obj_get_style_pad_inner(gauge, LV_GAUGE_PART_MAIN);
lv_coord_t r = (lv_obj_get_width(gauge) - left - right) / 2 - scale_width - txt_pad;
lv_coord_t x_ofs = gauge->coords.x1 + r + left + scale_width + txt_pad;
lv_coord_t y_ofs = gauge->coords.y1 + r + top + scale_width + txt_pad;
int16_t scale_angle = lv_linemeter_get_scale_angle(gauge);
2019-06-06 06:05:40 +02:00
uint16_t label_num = ext->label_count;
int16_t angle_ofs = 90 + (360 - scale_angle) / 2;
2020-03-20 03:51:19 -07:00
int32_t min = lv_gauge_get_min_value(gauge);
int32_t max = lv_gauge_get_max_value(gauge);
2020-01-01 21:44:16 +01:00
lv_draw_label_dsc_t label_dsc;
lv_draw_label_dsc_init(&label_dsc);
2020-03-06 13:14:25 +01:00
lv_obj_init_draw_label_dsc(gauge, LV_GAUGE_PART_MAJOR, &label_dsc);
2020-01-01 21:44:16 +01:00
uint8_t i;
for(i = 0; i < label_num; i++) {
/*Calculate the position a scale label*/
int16_t angle = (i * scale_angle) / (label_num - 1) + angle_ofs;
lv_coord_t y = (int32_t)((int32_t)lv_trigo_sin(angle) * r) / LV_TRIGO_SIN_MAX;
y += y_ofs;
lv_coord_t x = (int32_t)((int32_t)lv_trigo_sin(angle + 90) * r) / LV_TRIGO_SIN_MAX;
x += x_ofs;
2020-03-20 03:51:19 -07:00
int32_t scale_act = (int32_t)((int32_t)(max - min) * i) / (label_num - 1);
2017-04-21 17:11:47 +02:00
scale_act += min;
2020-03-20 04:17:11 -07:00
if(ext->format_cb == NULL)
2020-03-17 04:17:43 -07:00
lv_utils_num_to_str(scale_act, scale_txt);
else
2020-03-20 04:17:11 -07:00
ext->format_cb(gauge, scale_txt, sizeof(scale_txt), scale_act);
2020-03-17 04:17:43 -07:00
2017-11-23 21:28:36 +01:00
lv_area_t label_cord;
lv_point_t label_size;
2020-01-01 21:44:16 +01:00
lv_txt_get_size(&label_size, scale_txt, label_dsc.font, label_dsc.letter_space, label_dsc.line_space,
2019-06-06 06:05:40 +02:00
LV_COORD_MAX, LV_TXT_FLAG_NONE);
/*Draw the label*/
label_cord.x1 = x - label_size.x / 2;
label_cord.y1 = y - label_size.y / 2;
label_cord.x2 = label_cord.x1 + label_size.x;
label_cord.y2 = label_cord.y1 + label_size.y;
2020-01-01 21:44:16 +01:00
lv_draw_label(&label_cord, mask, &label_dsc, scale_txt, NULL);
}
}
/**
* Draw the needles of a gauge
* @param gauge pointer to gauge object
* @param mask mask of drawing
*/
2019-11-30 11:41:18 +01:00
static void lv_gauge_draw_needle(lv_obj_t * gauge, const lv_area_t * clip_area)
{
2019-04-11 19:59:55 +08:00
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
lv_style_int_t pad = lv_obj_get_style_pad_inner(gauge, LV_GAUGE_PART_NEEDLE);
lv_style_int_t left = lv_obj_get_style_pad_left(gauge, LV_GAUGE_PART_MAIN);
lv_style_int_t right = lv_obj_get_style_pad_right(gauge, LV_GAUGE_PART_MAIN);
lv_style_int_t top = lv_obj_get_style_pad_top(gauge, LV_GAUGE_PART_MAIN);
lv_coord_t r = (lv_obj_get_width(gauge) - left - right) / 2 - pad;
lv_coord_t x_ofs = gauge->coords.x1 + r + left + pad;
lv_coord_t y_ofs = gauge->coords.y1 + r + top + pad;
uint16_t angle = lv_linemeter_get_scale_angle(gauge);
2017-04-21 17:11:47 +02:00
int16_t angle_ofs = 90 + (360 - angle) / 2;
2019-04-04 07:15:40 +02:00
int16_t min = lv_gauge_get_min_value(gauge);
int16_t max = lv_gauge_get_max_value(gauge);
2017-11-23 21:28:36 +01:00
lv_point_t p_mid;
lv_point_t p_end;
uint8_t i;
2020-01-01 21:44:16 +01:00
lv_draw_line_dsc_t line_dsc;
lv_draw_line_dsc_init(&line_dsc);
lv_obj_init_draw_line_dsc(gauge, LV_GAUGE_PART_NEEDLE, &line_dsc);
2020-01-01 21:44:16 +01:00
lv_draw_img_dsc_t img_dsc;
if(ext->needle_img == NULL) {
lv_draw_img_dsc_init(&img_dsc);
lv_obj_init_draw_img_dsc(gauge, LV_GAUGE_PART_MAIN, &img_dsc);
img_dsc.recolor_opa = LV_OPA_COVER;
img_dsc.pivot.x = ext->needle_img_pivot.x;
img_dsc.pivot.y = ext->needle_img_pivot.y;
2019-11-30 11:41:18 +01:00
}
p_mid.x = x_ofs;
p_mid.y = y_ofs;
for(i = 0; i < ext->needle_count; i++) {
/*Calculate the end point of a needle*/
2019-11-30 11:41:18 +01:00
int16_t needle_angle =
(ext->values[i] - min) * angle / (max - min) + angle_ofs;
2019-11-30 11:41:18 +01:00
/*Draw line*/
if(ext->needle_img == NULL) {
p_end.y = (lv_trigo_sin(needle_angle) * r) / LV_TRIGO_SIN_MAX + y_ofs;
p_end.x = (lv_trigo_sin(needle_angle + 90) * r) / LV_TRIGO_SIN_MAX + x_ofs;
2019-11-30 11:41:18 +01:00
/*Draw the needle with the corresponding color*/
2020-01-01 21:44:16 +01:00
if(ext->needle_colors != NULL) line_dsc.color = ext->needle_colors[i];
2020-01-01 21:44:16 +01:00
lv_draw_line(&p_mid, &p_end, clip_area, &line_dsc);
2019-11-30 11:41:18 +01:00
}
/*Draw image*/
else {
lv_img_header_t info;
lv_img_decoder_get_info(ext->needle_img, &info);
2019-11-30 11:41:18 +01:00
lv_area_t a;
a.x1 = gauge->coords.x1 + lv_area_get_width(&gauge->coords) / 2 - ext->needle_img_pivot.x;
a.y1 = gauge->coords.y1 + lv_area_get_height(&gauge->coords) / 2 - ext->needle_img_pivot.y;
a.x2 = a.x1 + info.w - 1;
a.y2 = a.y1 + info.h - 1;
2019-11-30 11:41:18 +01:00
if(ext->needle_colors != NULL)
2020-01-01 21:44:16 +01:00
img_dsc.recolor = ext->needle_colors[i];
2020-01-01 21:44:16 +01:00
img_dsc.angle = needle_angle;
lv_draw_img(&a, clip_area, ext->needle_img, &img_dsc);
2019-11-30 11:41:18 +01:00
}
}
2020-01-01 21:44:16 +01:00
lv_draw_rect_dsc_t mid_dsc;
lv_draw_rect_dsc_init(&mid_dsc);
lv_obj_init_draw_rect_dsc(gauge, LV_GAUGE_PART_NEEDLE, &mid_dsc);
lv_style_int_t size = lv_obj_get_style_size(gauge, LV_GAUGE_PART_NEEDLE) / 2;
2017-11-23 21:28:36 +01:00
lv_area_t nm_cord;
2020-01-01 21:44:16 +01:00
nm_cord.x1 = x_ofs - size;
nm_cord.y1 = y_ofs - size;
nm_cord.x2 = x_ofs + size;
nm_cord.y2 = y_ofs + size;
lv_draw_rect(&nm_cord, clip_area, &mid_dsc);
}
#endif