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

247 lines
6.9 KiB
C
Raw Normal View History

2016-09-28 15:53:27 +02:00
/**
* @file lv_led.c
2018-06-19 09:49:58 +02:00
*
2016-09-28 15:53:27 +02:00
*/
/*********************
* INCLUDES
*********************/
#include "lv_led.h"
#if LV_USE_LED != 0
2016-09-28 15:53:27 +02:00
2019-09-24 16:30:38 +02:00
#include "../lv_core/lv_debug.h"
#include "../lv_themes/lv_theme.h"
2016-09-28 15:53:27 +02:00
#include "../lv_draw/lv_draw.h"
/*********************
* DEFINES
*********************/
#define LV_OBJX_NAME "lv_led"
2019-04-04 07:15:40 +02:00
#define LV_LED_WIDTH_DEF (LV_DPI / 3)
#define LV_LED_HEIGHT_DEF (LV_DPI / 3)
2020-01-10 11:10:07 +01:00
#define LV_LED_BRIGHT_OFF 60
2019-04-04 07:15:40 +02:00
#define LV_LED_BRIGHT_ON 255
2016-09-28 15:53:27 +02:00
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2019-09-06 19:53:39 +02:00
static lv_design_res_t lv_led_design(lv_obj_t * led, const lv_area_t * clip_area, lv_design_mode_t mode);
static lv_res_t lv_led_signal(lv_obj_t * led, lv_signal_t sign, void * param);
2016-09-28 15:53:27 +02:00
/**********************
* STATIC VARIABLES
**********************/
2019-09-06 19:53:39 +02:00
static lv_design_cb_t ancestor_design;
static lv_signal_cb_t ancestor_signal;
2017-04-11 10:50:57 +02:00
2016-09-28 15:53:27 +02:00
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Create a led objects
2016-10-07 11:15:46 +02:00
* @param par pointer to an object, it will be the parent of the new led
* @param copy pointer to a led object, if not NULL then the new object will be copied from it
2016-09-28 15:53:27 +02:00
* @return pointer to the created led
*/
lv_obj_t * lv_led_create(lv_obj_t * par, const lv_obj_t * copy)
2016-09-28 15:53:27 +02:00
{
2018-10-05 17:22:49 +02:00
LV_LOG_TRACE("led create started");
2018-07-25 17:57:08 +02:00
2016-09-28 15:53:27 +02:00
/*Create the ancestor basic object*/
2018-06-19 09:49:58 +02:00
lv_obj_t * new_led = lv_obj_create(par, copy);
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(new_led);
if(new_led == NULL) return NULL;
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_led);
2019-09-06 19:53:39 +02:00
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_led);
2018-06-19 09:49:58 +02:00
2016-09-28 15:53:27 +02:00
/*Allocate the object type specific extended data*/
lv_led_ext_t * ext = lv_obj_allocate_ext_attr(new_led, sizeof(lv_led_ext_t));
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(ext);
if(ext == NULL) {
lv_obj_del(new_led);
return NULL;
}
2017-04-11 10:50:57 +02:00
ext->bright = LV_LED_BRIGHT_ON;
2017-01-11 16:18:49 +01:00
lv_obj_set_signal_cb(new_led, lv_led_signal);
lv_obj_set_design_cb(new_led, lv_led_design);
2016-09-28 15:53:27 +02:00
/*Init the new led object*/
2016-10-07 11:15:46 +02:00
if(copy == NULL) {
2018-06-19 09:49:58 +02:00
lv_obj_set_size(new_led, LV_LED_WIDTH_DEF, LV_LED_HEIGHT_DEF);
2020-01-16 21:25:11 +01:00
lv_style_list_init(&new_led->style_list);
lv_obj_add_style(new_led, LV_LED_PART_MAIN, lv_theme_get_style(LV_THEME_LED));
2016-09-28 15:53:27 +02:00
}
/*Copy an existing object*/
else {
2018-06-19 09:49:58 +02:00
lv_led_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
2019-04-04 07:15:40 +02:00
ext->bright = copy_ext->bright;
/*Refresh the style with new signal function*/
lv_obj_refresh_style(new_led);
2016-09-28 15:53:27 +02:00
}
2018-06-19 09:49:58 +02:00
2018-10-05 17:22:49 +02:00
LV_LOG_INFO("led created");
2018-07-25 17:57:08 +02:00
2016-10-07 11:15:46 +02:00
return new_led;
2016-09-28 15:53:27 +02:00
}
/*=====================
* Setter functions
*====================*/
/**
* Set the brightness of a LED object
2016-10-07 11:15:46 +02:00
* @param led pointer to a LED object
2016-09-28 15:53:27 +02:00
* @param bright 0 (max. dark) ... 255 (max. light)
*/
2016-10-07 11:15:46 +02:00
void lv_led_set_bright(lv_obj_t * led, uint8_t bright)
2016-09-28 15:53:27 +02:00
{
LV_ASSERT_OBJ(led, LV_OBJX_NAME);
2018-06-19 09:49:58 +02:00
/*Set the brightness*/
lv_led_ext_t * ext = lv_obj_get_ext_attr(led);
if(ext->bright == bright) return;
2018-06-19 09:49:58 +02:00
ext->bright = bright;
2016-09-28 15:53:27 +02:00
2018-06-19 09:49:58 +02:00
/*Invalidate the object there fore it will be redrawn*/
lv_obj_invalidate(led);
2016-09-28 15:53:27 +02:00
}
/**
* Light on a LED
2016-10-07 11:15:46 +02:00
* @param led pointer to a LED object
2016-09-28 15:53:27 +02:00
*/
2016-10-07 11:15:46 +02:00
void lv_led_on(lv_obj_t * led)
2016-09-28 15:53:27 +02:00
{
LV_ASSERT_OBJ(led, LV_OBJX_NAME);
2018-06-19 09:49:58 +02:00
lv_led_set_bright(led, LV_LED_BRIGHT_ON);
2016-09-28 15:53:27 +02:00
}
/**
* Light off a LED
2016-10-07 11:15:46 +02:00
* @param led pointer to a LED object
2016-09-28 15:53:27 +02:00
*/
2016-10-07 11:15:46 +02:00
void lv_led_off(lv_obj_t * led)
2016-09-28 15:53:27 +02:00
{
LV_ASSERT_OBJ(led, LV_OBJX_NAME);
2018-06-19 09:49:58 +02:00
lv_led_set_bright(led, LV_LED_BRIGHT_OFF);
2016-09-28 15:53:27 +02:00
}
/**
* Toggle the state of a LED
2016-10-07 11:15:46 +02:00
* @param led pointer to a LED object
2016-09-28 15:53:27 +02:00
*/
2017-12-19 22:00:32 +01:00
void lv_led_toggle(lv_obj_t * led)
2016-09-28 15:53:27 +02:00
{
LV_ASSERT_OBJ(led, LV_OBJX_NAME);
2018-06-19 09:49:58 +02:00
uint8_t bright = lv_led_get_bright(led);
2019-04-04 07:15:40 +02:00
if(bright > (LV_LED_BRIGHT_OFF + LV_LED_BRIGHT_ON) >> 1)
lv_led_off(led);
else
lv_led_on(led);
2016-09-28 15:53:27 +02:00
}
/*=====================
* Getter functions
*====================*/
2016-10-04 15:29:52 +02:00
/**
* Get the brightness of a LEd object
2016-10-07 11:15:46 +02:00
* @param led pointer to LED object
2016-10-04 15:29:52 +02:00
* @return bright 0 (max. dark) ... 255 (max. light)
*/
uint8_t lv_led_get_bright(const lv_obj_t * led)
2016-10-04 15:29:52 +02:00
{
LV_ASSERT_OBJ(led, LV_OBJX_NAME);
2018-06-19 09:49:58 +02:00
lv_led_ext_t * ext = lv_obj_get_ext_attr(led);
return ext->bright;
2016-10-04 15:29:52 +02:00
}
2016-09-28 15:53:27 +02:00
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Handle the drawing related tasks of the leds
2016-10-07 11:15:46 +02:00
* @param led pointer to an object
2019-09-06 19:53:39 +02:00
* @param clip_area the object will be drawn only in this area
2016-09-28 15:53:27 +02: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`
2016-09-28 15:53:27 +02:00
*/
2019-09-06 19:53:39 +02:00
static lv_design_res_t lv_led_design(lv_obj_t * led, const lv_area_t * clip_area, lv_design_mode_t mode)
2016-09-28 15:53:27 +02:00
{
if(mode == LV_DESIGN_COVER_CHK) {
2019-09-06 19:53:39 +02:00
/*Return false if the object is not covers the clip_area area*/
return ancestor_design(led, clip_area, mode);
} else if(mode == LV_DESIGN_DRAW_MAIN) {
2018-06-19 09:49:58 +02:00
/*Make darker colors in a temporary style according to the brightness*/
2019-06-06 06:05:40 +02:00
lv_led_ext_t * ext = lv_obj_get_ext_attr(led);
2020-01-10 11:10:07 +01:00
lv_draw_rect_dsc_t rect_dsc;
lv_draw_rect_dsc_init(&rect_dsc);
lv_obj_init_draw_rect_dsc(led, LV_LED_PART_MAIN, &rect_dsc);
2016-09-28 15:53:27 +02:00
2018-06-19 09:49:58 +02:00
/*Mix. the color with black proportionally with brightness*/
2020-01-10 11:10:07 +01:00
rect_dsc.bg_color = lv_color_mix(rect_dsc.bg_color, LV_COLOR_BLACK, ext->bright);
rect_dsc.bg_grad_color = lv_color_mix(rect_dsc.bg_grad_color, LV_COLOR_BLACK, ext->bright);
rect_dsc.border_color = lv_color_mix(rect_dsc.border_color, LV_COLOR_BLACK, ext->bright);
2019-04-04 07:15:40 +02:00
2020-01-10 11:10:07 +01:00
/*Set the current shadow width according to brightness proportionally between LV_LED_BRIGHT_OFF
2019-04-04 07:15:40 +02:00
* and LV_LED_BRIGHT_ON*/
2020-01-10 11:10:07 +01:00
rect_dsc.shadow_width = ((ext->bright - LV_LED_BRIGHT_OFF) * rect_dsc.shadow_width) / (LV_LED_BRIGHT_ON - LV_LED_BRIGHT_OFF);
2017-01-11 16:18:49 +01:00
2020-01-10 11:10:07 +01:00
lv_draw_rect(&led->coords, clip_area, &rect_dsc);
}
2019-09-06 19:53:39 +02:00
return LV_DESIGN_RES_OK;
2016-09-28 15:53:27 +02:00
}
/**
* Signal function of the led
* @param led pointer to a led 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_led_signal(lv_obj_t * led, lv_signal_t sign, void * param)
{
lv_res_t res;
/* Include the ancient signal function */
res = ancestor_signal(led, sign, param);
if(res != LV_RES_OK) return res;
2018-02-28 15:37:41 +01:00
if(sign == LV_SIGNAL_GET_TYPE) {
lv_obj_type_t * buf = param;
uint8_t i;
2019-04-04 07:15:40 +02:00
for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
2018-02-28 15:37:41 +01:00
if(buf->type[i] == NULL) break;
}
buf->type[i] = "lv_led";
}
return res;
}
2016-09-28 15:53:27 +02:00
#endif