2017-01-09 18:08:57 +01:00
|
|
|
/**
|
|
|
|
* @file lv_gauge.c
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
|
|
|
#include "lv_conf.h"
|
|
|
|
#if USE_LV_GAUGE != 0
|
|
|
|
|
|
|
|
#include "lv_gauge.h"
|
|
|
|
#include "../lv_draw/lv_draw.h"
|
2017-04-21 09:15:39 +02:00
|
|
|
#include "misc/gfx/text.h"
|
2017-01-09 18:08:57 +01:00
|
|
|
#include "misc/math/trigo.h"
|
2017-01-10 08:36:12 +01:00
|
|
|
#include "misc/math/math_base.h"
|
2017-04-24 16:16:36 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2017-01-09 18:08:57 +01:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2017-07-09 15:32:49 +02:00
|
|
|
#ifndef LV_GAUGE_MAX_NEEDLE
|
|
|
|
#define LV_GAUGE_MAX_NEEDLE 4 /*Max number of needles. Used in the style.*/
|
|
|
|
#endif
|
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
#define LV_GAUGE_DEF_NEEDLE_COLOR COLOR_RED
|
|
|
|
#define LV_GAUGE_DEF_LABEL_COUNT 6
|
|
|
|
#define LV_GAUGE_DEF_SCALE_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
|
|
|
|
**********************/
|
|
|
|
static bool lv_gauge_design(lv_obj_t * gauge, const area_t * mask, lv_design_mode_t mode);
|
2017-10-26 21:20:10 +02:00
|
|
|
static void lv_gauge_draw_scale(lv_obj_t * gauge, const area_t * mask);
|
|
|
|
static void lv_gauge_draw_needle(lv_obj_t * gauge, const area_t * mask);
|
2017-01-09 18:08:57 +01:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
2017-10-20 10:17:02 +02:00
|
|
|
static lv_design_func_t ancestor_design_f = NULL;
|
2017-04-13 15:57:02 +02:00
|
|
|
|
2017-01-09 18:08:57 +01:00
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/*-----------------
|
|
|
|
* Create function
|
|
|
|
*-----------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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, lv_obj_t * copy)
|
|
|
|
{
|
|
|
|
/*Create the ancestor gauge*/
|
2017-04-21 17:11:47 +02:00
|
|
|
lv_obj_t * new_gauge = lv_lmeter_create(par, copy);
|
2017-01-09 18:08:57 +01:00
|
|
|
dm_assert(new_gauge);
|
|
|
|
|
|
|
|
/*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));
|
2017-01-09 18:08:57 +01:00
|
|
|
dm_assert(ext);
|
|
|
|
|
|
|
|
/*Initialize the allocated 'ext' */
|
2017-10-26 21:20:10 +02:00
|
|
|
ext->needle_count = 0;
|
2017-01-10 08:36:12 +01:00
|
|
|
ext->values = NULL;
|
2017-04-21 17:11:47 +02:00
|
|
|
ext->needle_colors = NULL;
|
2017-10-26 21:20:10 +02:00
|
|
|
ext->label_count = LV_GAUGE_DEF_LABEL_COUNT;
|
2017-10-20 10:17:02 +02:00
|
|
|
if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_func(new_gauge);
|
2017-01-09 18:08:57 +01:00
|
|
|
|
|
|
|
/*The signal and design functions are not copied so set them here*/
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_obj_set_signal_func(new_gauge, lv_gauge_signal);
|
|
|
|
lv_obj_set_design_func(new_gauge, lv_gauge_design);
|
2017-01-09 18:08:57 +01:00
|
|
|
|
|
|
|
/*Init the new gauge gauge*/
|
|
|
|
if(copy == NULL) {
|
2017-10-26 21:20:10 +02:00
|
|
|
lv_lmeter_set_scale(new_gauge, LV_GAUGE_DEF_ANGLE, LV_GAUGE_DEF_SCALE_LINE_COUNT);
|
2017-04-13 15:57:02 +02:00
|
|
|
lv_gauge_set_needle_num(new_gauge, 1, NULL);
|
2017-04-21 17:11:47 +02:00
|
|
|
lv_obj_set_size(new_gauge, 2 * LV_DPI, 2 * LV_DPI);
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_obj_set_style(new_gauge, lv_style_get(LV_STYLE_PRETTY));
|
2017-01-09 18:08:57 +01:00
|
|
|
}
|
|
|
|
/*Copy an existing gauge*/
|
|
|
|
else {
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_gauge_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
|
2017-10-26 21:20:10 +02:00
|
|
|
lv_gauge_set_needle_num(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];
|
|
|
|
}
|
2017-01-09 18:08:57 +01:00
|
|
|
|
|
|
|
/*Refresh the style with new signal function*/
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_obj_refresh_style(new_gauge);
|
2017-01-09 18:08:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return new_gauge;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 true: the object is still valid (not deleted), false: the object become invalid
|
|
|
|
*/
|
|
|
|
bool lv_gauge_signal(lv_obj_t * gauge, lv_signal_t sign, void * param)
|
|
|
|
{
|
|
|
|
bool valid;
|
|
|
|
|
|
|
|
/* Include the ancient signal function */
|
2017-04-21 17:11:47 +02:00
|
|
|
valid = lv_lmeter_signal(gauge, sign, param);
|
2017-01-09 18:08:57 +01:00
|
|
|
|
|
|
|
/* The object can be deleted so check its validity and then
|
|
|
|
* make the object specific signal handling */
|
|
|
|
if(valid != false) {
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
|
2017-04-13 15:57:02 +02:00
|
|
|
if(sign == LV_SIGNAL_CLEANUP) {
|
|
|
|
dm_free(ext->values);
|
|
|
|
ext->values = NULL;
|
|
|
|
}
|
2017-01-09 18:08:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* 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
|
|
|
*/
|
2017-10-26 21:20:10 +02:00
|
|
|
void lv_gauge_set_needle_num(lv_obj_t * gauge, uint8_t needle_cnt, color_t * colors)
|
2017-01-10 08:36:12 +01:00
|
|
|
{
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
|
2017-04-21 17:11:47 +02:00
|
|
|
if(ext->values != NULL) {
|
|
|
|
dm_free(ext->values);
|
|
|
|
ext->values = NULL;
|
|
|
|
}
|
2017-01-10 08:36:12 +01:00
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
ext->values = dm_realloc(ext->values, needle_cnt * sizeof(int16_t));
|
2017-04-11 10:50:57 +02:00
|
|
|
|
2017-04-21 17:11:47 +02:00
|
|
|
int16_t min = lv_bar_get_min_value(gauge);
|
2017-04-11 10:50:57 +02:00
|
|
|
uint8_t n;
|
2017-10-26 21:20:10 +02:00
|
|
|
for(n = ext->needle_count; n < needle_cnt; n++) {
|
2017-04-21 17:11:47 +02:00
|
|
|
ext->values[n] = min;
|
2017-04-11 10:50:57 +02:00
|
|
|
}
|
2017-01-10 08:36:12 +01:00
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
ext->needle_count = needle_cnt;
|
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
|
|
|
}
|
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
/**
|
|
|
|
* Set the number of labels (and the thicker lines too)
|
|
|
|
* @param gauge pointer to a gauge object
|
|
|
|
* @param label_cnt new count of labels
|
|
|
|
*/
|
|
|
|
void lv_gauge_set_label_count(lv_obj_t * gauge, uint8_t label_cnt)
|
|
|
|
{
|
|
|
|
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
|
|
|
|
ext->label_count = label_cnt;
|
|
|
|
}
|
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
|
|
|
{
|
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;
|
2017-01-10 08:36:12 +01:00
|
|
|
|
2017-04-21 17:11:47 +02:00
|
|
|
int16_t min = lv_bar_get_min_value(gauge);
|
|
|
|
int16_t max = lv_bar_get_max_value(gauge);
|
|
|
|
|
|
|
|
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-04-28 16:12:35 +02:00
|
|
|
/*To be consistent with bar set the first needle's value for the bar*/
|
2017-10-26 21:20:10 +02:00
|
|
|
if(needle_id == 0) lv_bar_set_value(gauge, value);
|
2017-04-28 16:12:35 +02:00
|
|
|
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_obj_invalidate(gauge);
|
2017-01-09 18:08:57 +01:00
|
|
|
}
|
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
/*=====================
|
|
|
|
* Getter functions
|
|
|
|
*====================*/
|
|
|
|
|
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
|
|
|
*/
|
2017-10-26 21:20:10 +02:00
|
|
|
uint8_t lv_gauge_get_needle_count(lv_obj_t * gauge)
|
2017-01-10 21:03:59 +01:00
|
|
|
{
|
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
|
|
|
*/
|
2017-10-26 21:20:10 +02:00
|
|
|
uint8_t lv_gauge_get_label_count(lv_obj_t * gauge)
|
2017-04-13 15:57:02 +02:00
|
|
|
{
|
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
|
|
|
|
2017-01-10 08:36:12 +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]
|
|
|
|
*/
|
|
|
|
int16_t lv_gauge_get_value(lv_obj_t * gauge, uint8_t needle)
|
|
|
|
{
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
|
2017-04-21 17:11:47 +02:00
|
|
|
int16_t min = lv_bar_get_min_value(gauge);
|
2017-01-10 08:36:12 +01:00
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
if(needle >= ext->needle_count) return min;
|
2017-01-10 08:36:12 +01:00
|
|
|
|
|
|
|
return ext->values[needle];
|
|
|
|
}
|
|
|
|
|
2017-01-09 18:08:57 +01:00
|
|
|
/**********************
|
|
|
|
* STATIC FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the drawing related tasks of the gauges
|
|
|
|
* @param gauge pointer to an object
|
|
|
|
* @param mask the object will be drawn only in this area
|
|
|
|
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
|
|
|
|
* (return 'true' if yes)
|
|
|
|
* LV_DESIGN_DRAW: draw the object (always return 'true')
|
|
|
|
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
|
|
|
|
* @param return true/false, depends on 'mode'
|
|
|
|
*/
|
|
|
|
static bool lv_gauge_design(lv_obj_t * gauge, const area_t * mask, lv_design_mode_t mode)
|
|
|
|
{
|
2017-04-21 17:11:47 +02:00
|
|
|
|
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) {
|
2017-04-21 17:11:47 +02:00
|
|
|
return false;
|
2017-01-09 18:08:57 +01:00
|
|
|
}
|
|
|
|
/*Draw the object*/
|
|
|
|
else if(mode == LV_DESIGN_DRAW_MAIN) {
|
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
lv_style_t * style = lv_obj_get_style(gauge);
|
|
|
|
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
|
2017-04-28 16:12:35 +02:00
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
lv_gauge_draw_scale(gauge, mask);
|
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*/
|
|
|
|
uint16_t scale_num_tmp = ext->lmeter.scale_num;
|
|
|
|
int16_t value_tmp = ext->lmeter.bar.act_value;
|
|
|
|
ext->lmeter.bar.act_value = ext->lmeter.bar.max_value;
|
|
|
|
ancestor_design_f(gauge, mask, mode); /*To draw lines*/
|
2017-01-10 20:38:14 +01:00
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
/*Temporally modify the line meter to draw thicker and longer lines where labels are*/
|
|
|
|
lv_style_t style_tmp;
|
|
|
|
lv_style_copy(&style_tmp, style);
|
|
|
|
ext->lmeter.scale_num = ext->label_count; /*Only to labels*/
|
|
|
|
style_tmp.line.width = style_tmp.line.width * 2; /*Ticker lines*/
|
|
|
|
style_tmp.body.padding.hor = style_tmp.body.padding.hor * 2; /*Longer lines*/
|
|
|
|
gauge->style_p = &style_tmp;
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
ancestor_design_f(gauge, mask, mode); /*To draw lines*/
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
ext->lmeter.scale_num = scale_num_tmp; /*Restore the parameters*/
|
|
|
|
ext->lmeter.bar.act_value = value_tmp;
|
|
|
|
gauge->style_p = style;
|
2017-04-13 15:57:02 +02:00
|
|
|
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
lv_gauge_draw_needle(gauge, mask);
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2017-01-10 08:36:12 +01:00
|
|
|
}
|
|
|
|
/*Post draw when the children are drawn*/
|
|
|
|
else if(mode == LV_DESIGN_DRAW_POST) {
|
|
|
|
ancestor_design_f(gauge, mask, mode);
|
|
|
|
}
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2017-01-10 08:36:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-01-09 18:08:57 +01:00
|
|
|
|
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
|
|
|
|
*/
|
2017-10-26 21:20:10 +02:00
|
|
|
static void lv_gauge_draw_scale(lv_obj_t * gauge, const area_t * mask)
|
2017-01-10 08:36:12 +01:00
|
|
|
{
|
|
|
|
char scale_txt[16];
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
|
|
|
|
lv_style_t * style = lv_obj_get_style(gauge);
|
|
|
|
cord_t r = lv_obj_get_width(gauge) / 2 - (3 * style->body.padding.hor) - style->body.padding.inner;
|
2017-10-20 10:17:02 +02:00
|
|
|
cord_t x_ofs = lv_obj_get_width(gauge) / 2 + gauge->coords.x1;
|
|
|
|
cord_t y_ofs = lv_obj_get_height(gauge) / 2 + gauge->coords.y1;
|
2017-04-21 17:11:47 +02:00
|
|
|
int16_t scale_angle = lv_lmeter_get_scale_angle(gauge);
|
2017-10-26 21:20:10 +02:00
|
|
|
uint16_t label_num = ext->label_count;
|
2017-04-21 17:11:47 +02:00
|
|
|
int16_t angle_ofs = 90 + (360 - scale_angle) / 2;
|
|
|
|
int16_t min = lv_bar_get_min_value(gauge);
|
|
|
|
int16_t max = lv_bar_get_max_value(gauge);
|
2017-01-09 18:08:57 +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 < 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
|
|
|
|
2017-01-10 08:36:12 +01:00
|
|
|
cord_t y = (int32_t)((int32_t)trigo_sin(angle) * r) / TRIGO_SIN_MAX;
|
|
|
|
y += y_ofs;
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2017-01-10 08:36:12 +01:00
|
|
|
cord_t x = (int32_t)((int32_t)trigo_sin(angle + 90) * r) / TRIGO_SIN_MAX;
|
|
|
|
x += x_ofs;
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2017-10-26 21:20:10 +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;
|
2017-01-10 08:36:12 +01:00
|
|
|
sprintf(scale_txt, "%d", scale_act);
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2017-01-10 08:36:12 +01:00
|
|
|
area_t label_cord;
|
|
|
|
point_t label_size;
|
2017-10-19 12:46:49 +02:00
|
|
|
txt_get_size(&label_size, scale_txt, style->text.font,
|
|
|
|
style->text.space_letter, style->text.space_line,
|
2017-04-21 09:15:39 +02:00
|
|
|
CORD_MAX, 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
|
|
|
|
2017-06-18 18:25:25 +02:00
|
|
|
lv_draw_label(&label_cord, mask, style, scale_txt, TXT_FLAG_NONE, 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
|
|
|
|
*/
|
2017-10-26 21:20:10 +02:00
|
|
|
static void lv_gauge_draw_needle(lv_obj_t * gauge, const area_t * mask)
|
2017-01-10 08:36:12 +01:00
|
|
|
{
|
2017-04-13 15:57:02 +02:00
|
|
|
lv_style_t style_needle;
|
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
|
|
|
lv_style_t * style = lv_obj_get_style(gauge);
|
|
|
|
|
|
|
|
/*To be consistent with bar use the bar value as the first needle*/
|
|
|
|
if(ext->needle_count != 0) {
|
|
|
|
ext->values[0] = lv_bar_get_value(gauge);
|
|
|
|
}
|
2017-01-10 08:36:12 +01:00
|
|
|
|
2017-10-26 21:20:10 +02:00
|
|
|
cord_t r = lv_obj_get_width(gauge) / 2 - style->body.padding.hor;
|
2017-10-20 10:17:02 +02:00
|
|
|
cord_t x_ofs = lv_obj_get_width(gauge) / 2 + gauge->coords.x1;
|
|
|
|
cord_t y_ofs = lv_obj_get_height(gauge) / 2 + gauge->coords.y1;
|
2017-04-21 17:11:47 +02:00
|
|
|
uint16_t angle = lv_lmeter_get_scale_angle(gauge);
|
|
|
|
int16_t angle_ofs = 90 + (360 - angle) / 2;
|
|
|
|
int16_t min = lv_bar_get_min_value(gauge);
|
|
|
|
int16_t max = lv_bar_get_max_value(gauge);
|
2017-01-10 08:36:12 +01:00
|
|
|
point_t p_mid;
|
|
|
|
point_t p_end;
|
|
|
|
uint8_t i;
|
|
|
|
|
2017-04-13 15:57:02 +02:00
|
|
|
memcpy(&style_needle, style, sizeof(lv_style_t));
|
|
|
|
|
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*/
|
2017-04-21 17:11:47 +02:00
|
|
|
int16_t needle_angle = (ext->values[i] - min) * angle / (max - min) + angle_ofs;
|
2017-01-10 08:36:12 +01:00
|
|
|
p_end.y = (trigo_sin(needle_angle) * r) / TRIGO_SIN_MAX + y_ofs;
|
|
|
|
p_end.x = (trigo_sin(needle_angle + 90) * r) / TRIGO_SIN_MAX + x_ofs;
|
|
|
|
|
|
|
|
/*Draw the needle with the corresponding color*/
|
2017-10-18 16:07:19 +02:00
|
|
|
if(ext->needle_colors == NULL) style_needle.line.color = LV_GAUGE_DEF_NEEDLE_COLOR;
|
|
|
|
else style_needle.line.color = ext->needle_colors[i];
|
2017-01-10 20:38:14 +01:00
|
|
|
|
2017-04-13 15:57:02 +02:00
|
|
|
lv_draw_line(&p_mid, &p_end, mask, &style_needle);
|
2017-01-09 18:08:57 +01:00
|
|
|
}
|
|
|
|
|
2017-01-10 08:36:12 +01:00
|
|
|
/*Draw the needle middle area*/
|
2017-04-13 15:57:02 +02:00
|
|
|
lv_style_t style_neddle_mid;
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_style_copy(&style_neddle_mid, lv_style_get(LV_STYLE_PLAIN));
|
|
|
|
style_neddle_mid.body.color_main = style->body.border.color;
|
|
|
|
style_neddle_mid.body.color_gradient = style->body.border.color;
|
2017-10-18 16:07:19 +02:00
|
|
|
style_neddle_mid.body.radius = LV_RADIUS_CIRCLE;
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2017-04-13 15:57:02 +02:00
|
|
|
area_t nm_cord;
|
2017-10-26 21:20:10 +02:00
|
|
|
nm_cord.x1 = x_ofs - style->body.padding.ver;
|
|
|
|
nm_cord.y1 = y_ofs - style->body.padding.ver;
|
|
|
|
nm_cord.x2 = x_ofs + style->body.padding.ver;
|
|
|
|
nm_cord.y2 = y_ofs + style->body.padding.ver;
|
2017-01-09 18:08:57 +01:00
|
|
|
|
2017-04-13 15:57:02 +02:00
|
|
|
lv_draw_rect(&nm_cord, mask, &style_neddle_mid);
|
2017-01-09 18:08:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|