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

303 lines
8.0 KiB
C
Raw Normal View History

2016-09-28 15:53:27 +02:00
/**
* @file lv_led.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_conf.h"
#if USE_LV_LED != 0
#include "lv_rect.h"
#include "lv_led.h"
#include "../lv_draw/lv_draw.h"
/*********************
* DEFINES
*********************/
2017-01-11 16:18:49 +01:00
#define LV_LED_WIDTH_DEF (30 * LV_DOWNSCALE)
#define LV_LED_HEIGHT_DEF (30 * LV_DOWNSCALE)
#define LV_LED_BRIGHT_DEF 128
#define LV_LED_BRIGHT_OFF 60
#define LV_LED_BRIGHT_ON 255
2016-09-28 15:53:27 +02:00
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2016-10-07 11:15:46 +02:00
static bool lv_led_design(lv_obj_t * led, const area_t * mask, lv_design_mode_t mode);
static void lv_leds_init(void);
2016-09-28 15:53:27 +02:00
/**********************
* STATIC VARIABLES
**********************/
2017-01-11 16:18:49 +01:00
static lv_leds_t lv_leds_def;
static lv_leds_t lv_leds_red;
static lv_leds_t lv_leds_green;
2017-01-11 16:18:49 +01:00
static lv_design_f_t ancestor_design_f;
2016-09-28 15:53:27 +02:00
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/*-----------------
* Create function
*-----------------*/
/**
* 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
*/
2016-10-07 11:15:46 +02:00
lv_obj_t * lv_led_create(lv_obj_t * par, lv_obj_t * copy)
2016-09-28 15:53:27 +02:00
{
/*Create the ancestor basic object*/
2016-10-07 11:15:46 +02:00
lv_obj_t * new_led = lv_rect_create(par, copy);
dm_assert(new_led);
2016-09-28 15:53:27 +02:00
/*Allocate the object type specific extended data*/
2016-10-07 11:15:46 +02:00
lv_led_ext_t * ext = lv_obj_alloc_ext(new_led, sizeof(lv_led_ext_t));
dm_assert(ext);
2017-01-11 16:18:49 +01:00
ext->bright = LV_LED_BRIGHT_DEF;
if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_f(new_led);
2016-09-28 15:53:27 +02:00
2016-10-07 11:15:46 +02:00
lv_obj_set_signal_f(new_led, lv_led_signal);
lv_obj_set_design_f(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) {
lv_obj_set_style(new_led, lv_leds_get(LV_LEDS_DEF, NULL));
2017-01-11 16:18:49 +01:00
lv_obj_set_size(new_led, LV_LED_WIDTH_DEF, LV_LED_HEIGHT_DEF);
2016-09-28 15:53:27 +02:00
}
/*Copy an existing object*/
else {
2016-10-07 11:15:46 +02:00
lv_led_ext_t * copy_ext = lv_obj_get_ext(copy);
ext->bright = copy_ext->bright;
/*Refresh the style with new signal function*/
lv_obj_refr_style(new_led);
2016-09-28 15:53:27 +02:00
}
2016-10-07 11:15:46 +02:00
return new_led;
2016-09-28 15:53:27 +02:00
}
/**
* Signal function of the led
2016-10-07 11:15:46 +02:00
* @param led pointer to a led object
2016-09-28 15:53:27 +02:00
* @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
*/
2016-10-07 11:15:46 +02:00
bool lv_led_signal(lv_obj_t * led, lv_signal_t sign, void * param)
2016-09-28 15:53:27 +02:00
{
bool valid;
/* Include the ancient signal function */
2016-10-07 11:15:46 +02:00
valid = lv_rect_signal(led, sign, param);
2016-09-28 15:53:27 +02:00
/* The object can be deleted so check its validity and then
* make the object specific signal handling */
if(valid != false) {
switch(sign) {
case LV_SIGNAL_CLEANUP:
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
break;
default:
break;
}
}
return valid;
}
/*=====================
* 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
{
/*Set the brightness*/
2016-10-07 11:15:46 +02:00
lv_led_ext_t * ext = lv_obj_get_ext(led);
ext->bright = bright;
2016-09-28 15:53:27 +02:00
/*Invalidate the object there fore it will be redrawn*/
2016-10-07 11:15:46 +02:00
lv_obj_inv(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
{
2017-01-11 16:18:49 +01: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
{
2017-01-11 16:18:49 +01: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
*/
2016-10-07 11:15:46 +02:00
void lv_led_tgl(lv_obj_t * led)
2016-09-28 15:53:27 +02:00
{
2016-10-07 11:15:46 +02:00
uint8_t bright = lv_led_get_bright(led);
2017-01-11 16:18:49 +01:00
if(bright > (LV_LED_BRIGHT_OFF + LV_LED_BRIGHT_ON) >> 1) lv_led_off(led);
2016-10-07 11:15:46 +02:00
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)
*/
2016-10-07 11:15:46 +02:00
uint8_t lv_led_get_bright(lv_obj_t * led)
2016-10-04 15:29:52 +02:00
{
2016-10-07 11:15:46 +02:00
lv_led_ext_t * ext = lv_obj_get_ext(led);
return ext->bright;
2016-10-04 15:29:52 +02:00
}
2016-09-28 15:53:27 +02:00
/**
* Return with a pointer to a built-in style and/or copy it to a variable
* @param style a style name from lv_leds_builtin_t enum
2016-10-07 11:15:46 +02:00
* @param copy copy the style to this variable. (NULL if unused)
2016-09-28 15:53:27 +02:00
* @return pointer to an lv_leds_t style
*/
2016-10-07 11:15:46 +02:00
lv_leds_t * lv_leds_get(lv_leds_builtin_t style, lv_leds_t * copy)
2016-09-28 15:53:27 +02:00
{
static bool style_inited = false;
/*Make the style initialization if it is not done yet*/
if(style_inited == false) {
lv_leds_init();
style_inited = true;
}
2016-09-28 15:53:27 +02:00
lv_leds_t *style_p;
switch(style) {
case LV_LEDS_DEF:
style_p = &lv_leds_def;
break;
2017-01-11 16:18:49 +01:00
case LV_LEDS_RED:
style_p = &lv_leds_red;
break;
2016-09-28 15:53:27 +02:00
case LV_LEDS_GREEN:
style_p = &lv_leds_green;
break;
default:
style_p = &lv_leds_def;
}
2016-10-07 11:15:46 +02:00
if(copy != NULL) {
if(style_p != NULL) memcpy(copy, style_p, sizeof(lv_leds_t));
else memcpy(copy, &lv_leds_def, sizeof(lv_leds_t));
2016-09-28 15:53:27 +02:00
}
return style_p;
}
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Handle the drawing related tasks of the leds
2016-10-07 11:15:46 +02:00
* @param led pointer to an object
2016-09-28 15:53:27 +02:00
* @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
2016-09-28 15:53:27 +02:00
* @param return true/false, depends on 'mode'
*/
2016-10-07 11:15:46 +02:00
static bool lv_led_design(lv_obj_t * led, const area_t * mask, lv_design_mode_t mode)
2016-09-28 15:53:27 +02:00
{
if(mode == LV_DESIGN_COVER_CHK) {
2017-01-11 16:18:49 +01:00
/*Return false if the object is not covers the mask area*/
return ancestor_design_f(led, mask, mode);
} else if(mode == LV_DESIGN_DRAW_MAIN) {
/*Make darker colors in a temporary style according to the brightness*/
2016-10-07 11:15:46 +02:00
lv_led_ext_t * ext = lv_obj_get_ext(led);
lv_leds_t * style = lv_obj_get_style(led);
2016-09-28 15:53:27 +02:00
2017-01-11 16:18:49 +01:00
/*Create a temporal style*/
lv_leds_t leds_tmp;
2016-10-07 11:15:46 +02:00
memcpy(&leds_tmp, style, sizeof(leds_tmp));
2016-09-28 15:53:27 +02:00
2017-01-11 16:18:49 +01:00
/*Mix. the color with black proportionally with brightness*/
2016-10-07 11:15:46 +02:00
leds_tmp.bg_rect.objs.color = color_mix(leds_tmp.bg_rect.objs.color, COLOR_BLACK, ext->bright);
leds_tmp.bg_rect.gcolor = color_mix(leds_tmp.bg_rect.gcolor, COLOR_BLACK, ext->bright);
2016-09-28 15:53:27 +02:00
2017-01-11 16:18:49 +01:00
/*Set smaller light size with lower brightness*/
/*light = 0 comes to LV_LED_BRIGHTNESS_OFF and the original light comes to LV_LED_BRIGHTNESS_ON*/
leds_tmp.bg_rect.light = (uint16_t)((uint16_t)(ext->bright - LV_LED_BRIGHT_OFF) * style->bg_rect.light) /
(LV_LED_BRIGHT_ON - LV_LED_BRIGHT_OFF);
2016-09-28 15:53:27 +02:00
2017-01-11 16:18:49 +01:00
led->style_p = &leds_tmp;
ancestor_design_f(led, mask, mode);
led->style_p = style;
}
2016-09-28 15:53:27 +02:00
return true;
}
/**
* Initialize the led styles
*/
static void lv_leds_init(void)
{
2017-01-11 16:18:49 +01:00
/*Default style (red)*/
2016-10-07 11:15:46 +02:00
lv_rects_get(LV_RECTS_DEF, &lv_leds_def.bg_rect);
lv_leds_def.bg_rect.objs.color = COLOR_RED;
lv_leds_def.bg_rect.gcolor = COLOR_MARRON,
2017-01-11 16:18:49 +01:00
lv_leds_def.bg_rect.bcolor = COLOR_MAKE(0x40, 0x00, 0x00);
lv_leds_def.bg_rect.lcolor = COLOR_RED;
lv_leds_def.bg_rect.bwidth = 4 * LV_DOWNSCALE;
2016-10-07 11:15:46 +02:00
lv_leds_def.bg_rect.bopa = 50;
2017-01-11 16:18:49 +01:00
lv_leds_def.bg_rect.light = 15 * LV_DOWNSCALE;
lv_leds_def.bg_rect.radius = LV_RECT_CIRCLE;
2016-10-07 11:15:46 +02:00
lv_leds_def.bg_rect.hpad = 0;
lv_leds_def.bg_rect.vpad = 0;
lv_leds_def.bg_rect.opad = 0;
2017-01-11 16:18:49 +01:00
/*Red style*/
memcpy(&lv_leds_red, &lv_leds_def, sizeof(lv_leds_t));
/* Green style */
memcpy(&lv_leds_green, &lv_leds_def, sizeof(lv_leds_t));
2016-10-07 11:15:46 +02:00
lv_leds_green.bg_rect.objs.color = COLOR_LIME;
lv_leds_green.bg_rect.gcolor = COLOR_GREEN;
2017-01-11 16:18:49 +01:00
lv_leds_green.bg_rect.bcolor = COLOR_MAKE(0x00, 0x40, 0x00);
lv_leds_green.bg_rect.lcolor = COLOR_LIME;
}
2016-09-28 15:53:27 +02:00
#endif