2017-01-09 18:08:57 +01:00
|
|
|
/**
|
|
|
|
* @file lv_gauge.c
|
2018-03-01 11:37:54 +08:00
|
|
|
*
|
2017-01-09 18:08:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
2018-07-07 11:53:22 +02:00
|
|
|
#include "lv_gauge.h"
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_GAUGE != 0
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2019-09-24 16:30:38 +02:00
|
|
|
#include "../lv_core/lv_debug.h"
|
2017-01-09 18:08:57 +01:00
|
|
|
#include "../lv_draw/lv_draw.h"
|
2017-11-16 15:32:33 +01:00
|
|
|
#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"
|
2019-03-18 07:25:20 +01:00
|
|
|
#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>
|
2017-01-09 18:08:57 +01:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2019-09-26 10:51:54 +02:00
|
|
|
#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*/
|
|
|
|
#define LV_GAUGE_DEF_ANGLE 220
|
2017-04-21 09:15:39 +02:00
|
|
|
|
2017-01-09 18:08:57 +01: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);
|
2020-01-01 21:44:16 +01:00
|
|
|
static lv_style_dsc_t * lv_gauge_get_style(lv_obj_t * gauge, uint8_t part);
|
|
|
|
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);
|
2017-01-09 18:08:57 +01:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
2019-02-26 09:25:46 +01:00
|
|
|
static lv_design_cb_t ancestor_design;
|
|
|
|
static lv_signal_cb_t ancestor_signal;
|
2017-04-13 15:57:02 +02:00
|
|
|
|
2017-01-09 18:08:57 +01:00
|
|
|
/**********************
|
|
|
|
* 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
|
|
|
|
*/
|
2018-07-30 06:52:29 +02:00
|
|
|
lv_obj_t * lv_gauge_create(lv_obj_t * par, const lv_obj_t * copy)
|
2017-01-09 18:08:57 +01:00
|
|
|
{
|
2018-10-05 17:22:49 +02:00
|
|
|
LV_LOG_TRACE("gauge create started");
|
2018-07-25 17:57:08 +02:00
|
|
|
|
2017-01-09 18:08:57 +01:00
|
|
|
/*Create the ancestor gauge*/
|
2017-04-21 17:11:47 +02:00
|
|
|
lv_obj_t * new_gauge = lv_lmeter_create(par, copy);
|
2019-09-24 23:14:17 +02:00
|
|
|
LV_ASSERT_MEM(new_gauge);
|
2018-07-25 13:33:53 +02:00
|
|
|
if(new_gauge == NULL) return NULL;
|
2018-03-01 11:37:54 +08:00
|
|
|
|
2017-01-09 18:08:57 +01:00
|
|
|
/*Allocate the gauge type specific extended data*/
|
2017-10-20 10:17:02 +02:00
|
|
|
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);
|
2019-12-03 18:16:14 +01:00
|
|
|
if(ext == NULL) {
|
|
|
|
lv_obj_del(new_gauge);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-01-09 18:08:57 +01:00
|
|
|
|
|
|
|
/*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;
|
2019-11-30 11:41:18 +01:00
|
|
|
|
|
|
|
ext->needle_img = 0;
|
|
|
|
ext->needle_img_pivot.x = 0;
|
|
|
|
ext->needle_img_pivot.y = 0;
|
2019-04-10 06:40:49 +02:00
|
|
|
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);
|
2017-01-09 18:08:57 +01:00
|
|
|
|
|
|
|
/*The signal and design functions are not copied so set them here*/
|
2019-02-26 09:25:46 +01:00
|
|
|
lv_obj_set_signal_cb(new_gauge, lv_gauge_signal);
|
|
|
|
lv_obj_set_design_cb(new_gauge, lv_gauge_design);
|
2017-01-09 18:08:57 +01:00
|
|
|
|
|
|
|
/*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);
|
2017-11-05 00:48:57 +01:00
|
|
|
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);
|
2017-04-21 17:11:47 +02:00
|
|
|
lv_obj_set_size(new_gauge, 2 * LV_DPI, 2 * LV_DPI);
|
2017-11-16 15:32:33 +01:00
|
|
|
|
2020-01-01 21:44:16 +01:00
|
|
|
lv_style_dsc_reset(&new_gauge->style_dsc);
|
|
|
|
_ot(new_gauge, LV_GAUGE_PART_MAIN, GAUGE);
|
|
|
|
_ot(new_gauge, LV_GAUGE_PART_STRONG, GAUGE_STRONG);
|
|
|
|
|
2017-01-09 18:08:57 +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);
|
2017-11-05 00:48:57 +01:00
|
|
|
lv_gauge_set_needle_count(new_gauge, copy_ext->needle_count, copy_ext->needle_colors);
|
2017-01-10 20:38:14 +01:00
|
|
|
|
2017-01-10 08:36:12 +01:00
|
|
|
uint8_t i;
|
2017-10-26 21:20:10 +02:00
|
|
|
for(i = 0; i < ext->needle_count; i++) {
|
2017-01-10 08:36:12 +01:00
|
|
|
ext->values[i] = copy_ext->values[i];
|
|
|
|
}
|
2018-03-01 11:37:54 +08:00
|
|
|
ext->label_count = copy_ext->label_count;
|
2017-01-09 18:08:57 +01:00
|
|
|
/*Refresh the style with new signal function*/
|
2020-01-01 21:44:16 +01:00
|
|
|
// lv_obj_refresh_style(new_gauge);
|
2017-01-09 18:08:57 +01:00
|
|
|
}
|
2018-03-01 11:37:54 +08:00
|
|
|
|
2018-10-05 17:22:49 +02:00
|
|
|
LV_LOG_INFO("gauge created");
|
2018-07-25 17:57:08 +02:00
|
|
|
|
2017-01-09 18:08:57 +01:00
|
|
|
return new_gauge;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Setter functions
|
|
|
|
*====================*/
|
|
|
|
|
2017-01-10 08:36:12 +01:00
|
|
|
/**
|
2017-04-13 15:57:02 +02:00
|
|
|
* Set the number of needles
|
2017-01-10 08:36:12 +01:00
|
|
|
* @param gauge pointer to gauge object
|
2017-10-26 21:20:10 +02:00
|
|
|
* @param needle_cnt new count of needles
|
2017-04-13 15:57:02 +02:00
|
|
|
* @param colors an array of colors for needles (with 'num' elements)
|
2017-01-10 08:36:12 +01:00
|
|
|
*/
|
2019-04-10 08:40:52 +02:00
|
|
|
void lv_gauge_set_needle_count(lv_obj_t * gauge, uint8_t needle_cnt, const lv_color_t colors[])
|
2017-01-10 08:36:12 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
|
|
|
|
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
|
2017-01-10 08:36:12 +01:00
|
|
|
|
2018-05-16 23:09:30 +02:00
|
|
|
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);
|
2018-07-25 14:43:46 +02:00
|
|
|
if(ext->values == NULL) return;
|
2018-05-16 23:09:30 +02:00
|
|
|
|
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-05-16 23:09:30 +02:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
ext->needle_count = needle_cnt;
|
2017-04-11 10:50:57 +02:00
|
|
|
}
|
2017-01-10 08:36:12 +01:00
|
|
|
|
2017-04-21 17:11:47 +02:00
|
|
|
ext->needle_colors = colors;
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_obj_invalidate(gauge);
|
2017-01-10 08:36:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value of a needle
|
2017-10-26 21:20:10 +02:00
|
|
|
* @param gauge pointer to a gauge
|
|
|
|
* @param needle_id the id of the needle
|
2017-01-10 20:40:57 +01:00
|
|
|
* @param value the new value
|
2017-01-10 08:36:12 +01:00
|
|
|
*/
|
2017-10-26 21:20:10 +02:00
|
|
|
void lv_gauge_set_value(lv_obj_t * gauge, uint8_t needle_id, int16_t value)
|
2017-01-09 18:08:57 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
|
|
|
|
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
|
2017-01-10 08:36:12 +01:00
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
if(needle_id >= ext->needle_count) return;
|
2018-05-16 23:09:30 +02:00
|
|
|
if(ext->values[needle_id] == value) return;
|
|
|
|
|
2017-11-05 00:48:57 +01:00
|
|
|
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;
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
ext->values[needle_id] = value;
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_obj_invalidate(gauge);
|
2017-01-09 18:08:57 +01:00
|
|
|
}
|
|
|
|
|
2017-11-05 00:48:57 +01:00
|
|
|
/**
|
|
|
|
* Set the scale settings of a gauge
|
|
|
|
* @param gauge pointer to a gauge object
|
|
|
|
* @param angle angle of the scale (0..360)
|
2018-09-04 07:03:47 +02:00
|
|
|
* @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 `
|
2018-09-04 07:03:47 +02:00
|
|
|
* @param label_cnt count of scale labels.
|
2017-11-05 00:48:57 +01:00
|
|
|
*/
|
|
|
|
void lv_gauge_set_scale(lv_obj_t * gauge, uint16_t angle, uint8_t line_cnt, uint8_t label_cnt)
|
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
|
|
|
|
|
2017-11-05 00:48:57 +01:00
|
|
|
lv_lmeter_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;
|
2018-05-16 23:09:30 +02:00
|
|
|
lv_obj_invalidate(gauge);
|
2017-11-05 00:48:57 +01:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
/*=====================
|
|
|
|
* Getter functions
|
|
|
|
*====================*/
|
|
|
|
|
2017-11-05 00:48:57 +01:00
|
|
|
/**
|
|
|
|
* 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]
|
|
|
|
*/
|
2019-04-04 07:15:40 +02:00
|
|
|
int16_t lv_gauge_get_value(const lv_obj_t * gauge, uint8_t needle)
|
2017-11-05 00:48:57 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
|
|
|
|
|
2017-11-05 00:48:57 +01:00
|
|
|
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);
|
2017-11-05 00:48:57 +01:00
|
|
|
|
|
|
|
if(needle >= ext->needle_count) return min;
|
|
|
|
|
|
|
|
return ext->values[needle];
|
|
|
|
}
|
|
|
|
|
2017-01-10 21:03:59 +01:00
|
|
|
/**
|
2017-10-26 21:20:10 +02:00
|
|
|
* Get the count of needles on a gauge
|
|
|
|
* @param gauge pointer to gauge
|
|
|
|
* @return count of needles
|
2017-01-10 21:03:59 +01:00
|
|
|
*/
|
2018-07-30 06:52:29 +02:00
|
|
|
uint8_t lv_gauge_get_needle_count(const lv_obj_t * gauge)
|
2017-01-10 21:03:59 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
|
|
|
|
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
|
2017-10-26 21:20:10 +02:00
|
|
|
return ext->needle_count;
|
2017-01-10 21:03:59 +01:00
|
|
|
}
|
|
|
|
|
2017-04-13 15:57:02 +02:00
|
|
|
/**
|
2017-10-26 21:20:10 +02:00
|
|
|
* Set the number of labels (and the thicker lines too)
|
2017-04-13 15:57:02 +02:00
|
|
|
* @param gauge pointer to a gauge object
|
2017-10-26 21:20:10 +02:00
|
|
|
* @return count of labels
|
2017-04-13 15:57:02 +02:00
|
|
|
*/
|
2018-07-30 06:52:29 +02:00
|
|
|
uint8_t lv_gauge_get_label_count(const lv_obj_t * gauge)
|
2017-04-13 15:57:02 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
|
|
|
|
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
|
2017-10-26 21:20:10 +02:00
|
|
|
return ext->label_count;
|
2017-04-13 15:57:02 +02:00
|
|
|
}
|
2017-01-10 21:03:59 +01:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-09 18:08:57 +01:00
|
|
|
/**********************
|
|
|
|
* 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
|
2017-01-09 18:08:57 +01:00
|
|
|
* @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`
|
2017-01-09 18:08:57 +01:00
|
|
|
*/
|
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-01-09 18:08:57 +01:00
|
|
|
{
|
|
|
|
/*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;
|
2017-01-09 18:08:57 +01:00
|
|
|
}
|
|
|
|
/*Draw the object*/
|
|
|
|
else if(mode == LV_DESIGN_DRAW_MAIN) {
|
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);
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
/*Draw the ancestor line meter with max value to show the rainbow like line colors*/
|
2017-11-05 00:48:57 +01:00
|
|
|
uint16_t line_cnt_tmp = ext->lmeter.line_cnt;
|
2019-09-06 19:53:39 +02:00
|
|
|
ancestor_design(gauge, clip_area, mode); /*To draw lines*/
|
2017-01-10 20:38:14 +01:00
|
|
|
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2020-01-01 21:44:16 +01:00
|
|
|
lv_lmeter_draw_scale(gauge, clip_area, LV_GAUGE_PART_MAIN);
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2020-01-01 21:44:16 +01:00
|
|
|
ext->lmeter.line_cnt = ext->label_count; /*Only to labels*/
|
|
|
|
lv_lmeter_draw_scale(gauge, clip_area, LV_GAUGE_PART_STRONG);
|
2019-04-04 07:15:40 +02:00
|
|
|
ext->lmeter.line_cnt = line_cnt_tmp; /*Restore the parameters*/
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2019-09-06 19:53:39 +02:00
|
|
|
lv_gauge_draw_needle(gauge, clip_area);
|
2017-01-10 08:36:12 +01:00
|
|
|
}
|
|
|
|
/*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);
|
2017-01-10 08:36:12 +01:00
|
|
|
}
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2019-09-06 19:53:39 +02:00
|
|
|
return LV_DESIGN_RES_OK;
|
2017-01-10 08:36:12 +01:00
|
|
|
}
|
2017-01-09 18:08:57 +01:00
|
|
|
|
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) {
|
|
|
|
uint8_t ** type_p = param;
|
|
|
|
lv_style_dsc_t ** style_dsc_p = param;
|
|
|
|
*style_dsc_p = lv_gauge_get_style(gauge, **type_p);
|
|
|
|
return LV_RES_OK;
|
|
|
|
}
|
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;
|
2019-09-26 12:54:40 +02:00
|
|
|
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;
|
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_dsc_t * lv_gauge_get_style(lv_obj_t * gauge, uint8_t part)
|
|
|
|
{
|
|
|
|
LV_ASSERT_OBJ(gauge, LV_OBJX_NAME);
|
|
|
|
|
|
|
|
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
|
|
|
|
lv_style_dsc_t * style_dsc_p;
|
|
|
|
|
|
|
|
switch(part) {
|
|
|
|
case LV_GAUGE_PART_MAIN:
|
|
|
|
style_dsc_p = &gauge->style_dsc;
|
|
|
|
break;
|
|
|
|
case LV_GAUGE_PART_STRONG:
|
|
|
|
style_dsc_p = &ext->style_strong;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
style_dsc_p = NULL;
|
|
|
|
}
|
2017-11-11 14:23:05 +01:00
|
|
|
|
2020-01-01 21:44:16 +01:00
|
|
|
return style_dsc_p;
|
|
|
|
}
|
2017-01-10 08:36:12 +01:00
|
|
|
/**
|
2017-01-10 20:38:14 +01:00
|
|
|
* Draw the scale on a gauge
|
2017-01-10 08:36:12 +01:00
|
|
|
* @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)
|
2017-01-10 08:36:12 +01:00
|
|
|
{
|
|
|
|
char scale_txt[16];
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2019-04-11 19:59:55 +08:00
|
|
|
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
|
2020-01-06 22:14:04 +01:00
|
|
|
lv_style_int_t scale_width = lv_obj_get_style_int(gauge, LV_GAUGE_PART_STRONG, LV_STYLE_SCALE_WIDTH);
|
|
|
|
lv_style_int_t txt_pad = lv_obj_get_style_int(gauge, LV_GAUGE_PART_STRONG, LV_STYLE_PAD_INNER);
|
2020-01-01 21:44:16 +01:00
|
|
|
lv_coord_t r = lv_obj_get_width(gauge) / 2 - scale_width - txt_pad;
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_coord_t x_ofs = lv_obj_get_width(gauge) / 2 + gauge->coords.x1;
|
|
|
|
lv_coord_t y_ofs = lv_obj_get_height(gauge) / 2 + gauge->coords.y1;
|
|
|
|
int16_t scale_angle = lv_lmeter_get_scale_angle(gauge);
|
|
|
|
uint16_t label_num = ext->label_count;
|
|
|
|
int16_t angle_ofs = 90 + (360 - scale_angle) / 2;
|
|
|
|
int16_t min = lv_gauge_get_min_value(gauge);
|
|
|
|
int16_t max = lv_gauge_get_max_value(gauge);
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2020-01-01 21:44:16 +01:00
|
|
|
lv_draw_label_dsc_t label_dsc;
|
|
|
|
lv_draw_label_dsc_init(&label_dsc);
|
|
|
|
lv_obj_init_draw_label_dsc(gauge, LV_GAUGE_PART_STRONG, &label_dsc);
|
|
|
|
|
2017-01-10 08:36:12 +01:00
|
|
|
uint8_t i;
|
2017-10-26 21:20:10 +02:00
|
|
|
for(i = 0; i < label_num; i++) {
|
2017-01-10 08:36:12 +01:00
|
|
|
/*Calculate the position a scale label*/
|
2017-10-26 21:20:10 +02:00
|
|
|
int16_t angle = (i * scale_angle) / (label_num - 1) + angle_ofs;
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2018-02-15 10:12:28 +01:00
|
|
|
lv_coord_t y = (int32_t)((int32_t)lv_trigo_sin(angle) * r) / LV_TRIGO_SIN_MAX;
|
2017-01-10 08:36:12 +01:00
|
|
|
y += y_ofs;
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2018-02-15 10:12:28 +01:00
|
|
|
lv_coord_t x = (int32_t)((int32_t)lv_trigo_sin(angle + 90) * r) / LV_TRIGO_SIN_MAX;
|
2017-01-10 08:36:12 +01:00
|
|
|
x += x_ofs;
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
int16_t scale_act = (int32_t)((int32_t)(max - min) * i) / (label_num - 1);
|
2017-04-21 17:11:47 +02:00
|
|
|
scale_act += min;
|
2019-03-18 07:25:20 +01:00
|
|
|
lv_utils_num_to_str(scale_act, scale_txt);
|
2017-01-09 18:08:57 +01: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);
|
2017-01-10 08:36:12 +01:00
|
|
|
|
|
|
|
/*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;
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2020-01-01 21:44:16 +01:00
|
|
|
lv_draw_label(&label_cord, mask, &label_dsc, scale_txt, NULL);
|
2017-01-14 23:54:16 +01:00
|
|
|
}
|
2017-01-10 08:36:12 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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)
|
2017-01-10 08:36:12 +01:00
|
|
|
{
|
2019-04-11 19:59:55 +08:00
|
|
|
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
|
2017-10-26 21:20:10 +02:00
|
|
|
|
2020-01-06 22:14:04 +01:00
|
|
|
lv_style_int_t scale_width = lv_obj_get_style_int(gauge, LV_GAUGE_PART_STRONG, LV_STYLE_SCALE_WIDTH);
|
2020-01-01 21:44:16 +01:00
|
|
|
lv_coord_t r = lv_obj_get_width(gauge) / 2 - scale_width;
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_coord_t x_ofs = lv_obj_get_width(gauge) / 2 + gauge->coords.x1;
|
|
|
|
lv_coord_t y_ofs = lv_obj_get_height(gauge) / 2 + gauge->coords.y1;
|
|
|
|
uint16_t angle = lv_lmeter_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;
|
2017-01-10 08:36:12 +01:00
|
|
|
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_MAIN, &line_dsc);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-04-13 15:57:02 +02:00
|
|
|
|
2017-01-10 08:36:12 +01:00
|
|
|
p_mid.x = x_ofs;
|
|
|
|
p_mid.y = y_ofs;
|
2017-10-26 21:20:10 +02:00
|
|
|
for(i = 0; i < ext->needle_count; i++) {
|
2017-01-10 08:36:12 +01:00
|
|
|
/*Calculate the end point of a needle*/
|
2018-12-30 22:00:48 +01:00
|
|
|
|
2019-11-30 11:41:18 +01:00
|
|
|
int16_t needle_angle =
|
|
|
|
(ext->values[i] - min) * angle / (max - min) + angle_ofs;
|
2018-12-30 22:00:48 +01:00
|
|
|
|
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;
|
2018-12-30 22:00:48 +01:00
|
|
|
|
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];
|
2018-12-30 22:00:48 +01:00
|
|
|
|
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);
|
2018-12-30 22:00:48 +01:00
|
|
|
|
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;
|
2017-01-10 08:36:12 +01:00
|
|
|
|
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];
|
2017-01-10 20:38:14 +01:00
|
|
|
|
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
|
|
|
}
|
2017-01-09 18:08:57 +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_MAIN, &mid_dsc);
|
2020-01-06 22:14:04 +01:00
|
|
|
lv_style_int_t size = lv_obj_get_style_int(gauge, LV_GAUGE_PART_MAIN, LV_STYLE_SIZE) / 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);
|
2017-01-09 18:08:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|