/**1* @file lv_led.c2*3*/45/*********************6* INCLUDES7*********************/8#include "lv_led.h"9#if USE_LV_LED != 01011#include "../lv_themes/lv_theme.h"12#include "../lv_draw/lv_draw.h"1314/*********************15* DEFINES16*********************/17#define LV_LED_WIDTH_DEF (LV_DPI / 3)18#define LV_LED_HEIGHT_DEF (LV_DPI / 3)19#define LV_LED_BRIGHT_OFF 10020#define LV_LED_BRIGHT_ON 2552122/**********************23* TYPEDEFS24**********************/2526/**********************27* STATIC PROTOTYPES28**********************/29static bool lv_led_design(lv_obj_t * led, const lv_area_t * mask, lv_design_mode_t mode);30static lv_res_t lv_led_signal(lv_obj_t * led, lv_signal_t sign, void * param);3132/**********************33* STATIC VARIABLES34**********************/35static lv_design_func_t ancestor_design_f;36static lv_signal_func_t ancestor_signal;3738/**********************39* MACROS40**********************/4142/**********************43* GLOBAL FUNCTIONS44**********************/4546/**47* Create a led objects48* @param par pointer to an object, it will be the parent of the new led49* @param copy pointer to a led object, if not NULL then the new object will be copied from it50* @return pointer to the created led51*/52lv_obj_t * lv_led_create(lv_obj_t * par, const lv_obj_t * copy)53{54LV_LOG_TRACE("led create started");5556/*Create the ancestor basic object*/57lv_obj_t * new_led = lv_obj_create(par, copy);58lv_mem_assert(new_led);59if(new_led == NULL) return NULL;6061if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_led);62if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_func(new_led);6364/*Allocate the object type specific extended data*/65lv_led_ext_t * ext = lv_obj_allocate_ext_attr(new_led, sizeof(lv_led_ext_t));66lv_mem_assert(ext);67if(ext == NULL) return NULL;6869ext->bright = LV_LED_BRIGHT_ON;7071lv_obj_set_signal_func(new_led, lv_led_signal);72lv_obj_set_design_func(new_led, lv_led_design);7374/*Init the new led object*/75if(copy == NULL) {76lv_obj_set_size(new_led, LV_LED_WIDTH_DEF, LV_LED_HEIGHT_DEF);7778/*Set the default styles*/79lv_theme_t * th = lv_theme_get_current();80if(th) {81lv_led_set_style(new_led, th->led);82} else {83lv_led_set_style(new_led, &lv_style_pretty_color);84}85}86/*Copy an existing object*/87else {88lv_led_ext_t * copy_ext = lv_obj_get_ext_attr(copy);89ext->bright = copy_ext->bright;9091/*Refresh the style with new signal function*/92lv_obj_refresh_style(new_led);93}949596LV_LOG_INFO("led created");9798return new_led;99}100101/*=====================102* Setter functions103*====================*/104105/**106* Set the brightness of a LED object107* @param led pointer to a LED object108* @param bright 0 (max. dark) ... 255 (max. light)109*/110void lv_led_set_bright(lv_obj_t * led, uint8_t bright)111{112/*Set the brightness*/113lv_led_ext_t * ext = lv_obj_get_ext_attr(led);114if(ext->bright == bright) return;115116ext->bright = bright;117118/*Invalidate the object there fore it will be redrawn*/119lv_obj_invalidate(led);120}121122/**123* Light on a LED124* @param led pointer to a LED object125*/126void lv_led_on(lv_obj_t * led)127{128lv_led_set_bright(led, LV_LED_BRIGHT_ON);129}130131/**132* Light off a LED133* @param led pointer to a LED object134*/135void lv_led_off(lv_obj_t * led)136{137lv_led_set_bright(led, LV_LED_BRIGHT_OFF);138}139140141/**142* Toggle the state of a LED143* @param led pointer to a LED object144*/145void lv_led_toggle(lv_obj_t * led)146{147uint8_t bright = lv_led_get_bright(led);148if(bright > (LV_LED_BRIGHT_OFF + LV_LED_BRIGHT_ON) >> 1) lv_led_off(led);149else lv_led_on(led);150}151152/*=====================153* Getter functions154*====================*/155156/**157* Get the brightness of a LEd object158* @param led pointer to LED object159* @return bright 0 (max. dark) ... 255 (max. light)160*/161uint8_t lv_led_get_bright(const lv_obj_t * led)162{163lv_led_ext_t * ext = lv_obj_get_ext_attr(led);164return ext->bright;165}166167/**********************168* STATIC FUNCTIONS169**********************/170171/**172* Handle the drawing related tasks of the leds173* @param led pointer to an object174* @param mask the object will be drawn only in this area175* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area176* (return 'true' if yes)177* LV_DESIGN_DRAW: draw the object (always return 'true')178* LV_DESIGN_DRAW_POST: drawing after every children are drawn179* @param return true/false, depends on 'mode'180*/181static bool lv_led_design(lv_obj_t * led, const lv_area_t * mask, lv_design_mode_t mode)182{183if(mode == LV_DESIGN_COVER_CHK) {184/*Return false if the object is not covers the mask area*/185return ancestor_design_f(led, mask, mode);186} else if(mode == LV_DESIGN_DRAW_MAIN) {187/*Make darker colors in a temporary style according to the brightness*/188lv_led_ext_t * ext = lv_obj_get_ext_attr(led);189lv_style_t * style = lv_obj_get_style(led);190191/* Store the real pointer because of 'lv_group'192* If the object is in focus 'lv_obj_get_style()' will give a pointer to tmp style193* and to the real object style. It is important because of style change tricks below*/194lv_style_t * style_ori_p = led->style_p;195196/*Create a temporal style*/197lv_style_t leds_tmp;198memcpy(&leds_tmp, style, sizeof(leds_tmp));199200/*Mix. the color with black proportionally with brightness*/201leds_tmp.body.main_color = lv_color_mix(leds_tmp.body.main_color, LV_COLOR_BLACK, ext->bright);202leds_tmp.body.grad_color = lv_color_mix(leds_tmp.body.grad_color, LV_COLOR_BLACK, ext->bright);203leds_tmp.body.border.color = lv_color_mix(leds_tmp.body.border.color, LV_COLOR_BLACK, ext->bright);204205/*Set the current swidth according to brightness proportionally between LV_LED_BRIGHT_OFF and LV_LED_BRIGHT_ON*/206uint16_t bright_tmp = ext->bright;207leds_tmp.body.shadow.width = ((bright_tmp - LV_LED_BRIGHT_OFF) * style->body.shadow.width) / (LV_LED_BRIGHT_ON - LV_LED_BRIGHT_OFF);208209led->style_p = &leds_tmp;210ancestor_design_f(led, mask, mode);211led->style_p = style_ori_p; /*Restore the ORIGINAL style pointer*/212}213return true;214}215216/**217* Signal function of the led218* @param led pointer to a led object219* @param sign a signal type from lv_signal_t enum220* @param param pointer to a signal specific variable221* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted222*/223static lv_res_t lv_led_signal(lv_obj_t * led, lv_signal_t sign, void * param)224{225lv_res_t res;226227/* Include the ancient signal function */228res = ancestor_signal(led, sign, param);229if(res != LV_RES_OK) return res;230231232if(sign == LV_SIGNAL_GET_TYPE) {233lv_obj_type_t * buf = param;234uint8_t i;235for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/236if(buf->type[i] == NULL) break;237}238buf->type[i] = "lv_led";239}240241return res;242}243#endif244245246