diff --git a/lv_objx/lv_led.c b/lv_objx/lv_led.c new file mode 100644 index 0000000000..3c5d688d8d --- /dev/null +++ b/lv_objx/lv_led.c @@ -0,0 +1,269 @@ +/** + * @file lv_led.c + * + */ + +/*Search an replace: led -> object normal name with lower case (e.g. button, label etc.) + * led -> object short name with lower case(e.g. btn, label etc) + * LED -> object short name with upper case (e.g. BTN, LABEL etc.) + * + */ + +/********************* + * 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 + *********************/ +#define LV_LED_BRIGHTNESS_DEF 128 +#define LV_LED_BRIGHTNESS_ON 50 +#define LV_LED_BRIGHTNESS_OFF 255 + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * STATIC PROTOTYPES + **********************/ +static bool lv_led_design(lv_obj_t* obj_dp, const area_t * mask_p, lv_design_mode_t mode); + +/********************** + * STATIC VARIABLES + **********************/ + +/* A RED style */ +static lv_leds_t lv_leds_def = +{ .rects.objs.color = COLOR_RED, .rects.gcolor = COLOR_MARRON, + .rects.bcolor = COLOR_WHITE, .rects.bwidth = 4 * LV_STYLE_MULT, .rects.bopa = 50, + .rects.round = LV_RECT_CIRCLE, .rects.empty = 0, + .rects.hpad = 0, .rects.vpad = 0, .rects.opad = 0 /*Fit and layout is not used*/ }; + + +/* A GREEN style */ +static lv_leds_t lv_leds_green = +{ .rects.objs.color = COLOR_LIME, .rects.gcolor = COLOR_GREEN, + .rects.bcolor = COLOR_WHITE, .rects.bwidth = 4 * LV_STYLE_MULT, .rects.bopa = 50, + .rects.round = LV_RECT_CIRCLE, .rects.empty = 0, + .rects.hpad = 0, .rects.vpad = 0, .rects.opad = 0 /*Fit and layout is not used*/ }; + +/********************** + * MACROS + **********************/ + +/********************** + * GLOBAL FUNCTIONS + **********************/ + +/*----------------- + * Create function + *-----------------*/ + +/** + * Create a led objects + * @param par_dp pointer to an object, it will be the parent of the new led + * @param copy_dp pointer to a led object, if not NULL then the new object will be copied from it + * @return pointer to the created led + */ +lv_obj_t* lv_led_create(lv_obj_t* par_dp, lv_obj_t * copy_dp) +{ + /*Create the ancestor basic object*/ + lv_obj_t* new_obj_dp = lv_rect_create(par_dp, copy_dp); + dm_assert(new_obj_dp); + + /*Allocate the object type specific extended data*/ + lv_led_ext_t * ext_dp = lv_obj_alloc_ext(new_obj_dp, sizeof(lv_led_ext_t)); + dm_assert(ext_dp); + + lv_obj_set_signal_f(new_obj_dp, lv_led_signal); + lv_obj_set_design_f(new_obj_dp, lv_led_design); + + /*Init the new led object*/ + if(copy_dp == NULL) { + ext_dp->bright = LV_LED_BRIGHTNESS_DEF; + lv_obj_set_style(new_obj_dp, &lv_leds_def); + lv_obj_set_size_us(new_obj_dp, 40, 40); + } + /*Copy an existing object*/ + else { + lv_led_ext_t * copy_ext_p = lv_obj_get_ext(copy_dp); + ext_dp->bright = copy_ext_p->bright; + } + + return new_obj_dp; +} + +/** + * Signal function of the led + * @param obj_dp pointer to a led 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_led_signal(lv_obj_t* obj_dp, lv_signal_t sign, void * param) +{ + bool valid; + + /* Include the ancient signal function */ + valid = lv_rect_signal(obj_dp, sign, param); + + /* 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 + * @param obj_dp pointer to a LED object + * @param bright 0 (max. dark) ... 255 (max. light) + */ +void lv_led_set_bright(lv_obj_t * obj_dp, uint8_t bright) +{ + /*Set the brightness*/ + lv_led_ext_t * ext_dp = lv_obj_get_ext(obj_dp); + ext_dp->bright = bright; + + /*Invalidate the object there fore it will be redrawn*/ + lv_obj_inv(obj_dp); +} + +/** + * Light on a LED + * @param obj_dp pointer to a LED object + */ +void lv_led_on(lv_obj_t * obj_dp) +{ + lv_led_set_bright(obj_dp, LV_LED_BRIGHTNESS_ON); +} + +/** + * Light off a LED + * @param obj_dp pointer to a LED object + */ +void lv_led_off(lv_obj_t * obj_dp) +{ + lv_led_set_bright(obj_dp, LV_LED_BRIGHTNESS_OFF); +} + + +/** + * Toggle the state of a LED + * @param obj_dp pointer to a LED object + */ +void lv_led_tgl(lv_obj_t * obj_dp) +{ + uint8_t bright = lv_led_get_bright(obj_dp); + if(bright > 60) lv_led_off(obj_dp); + else lv_led_on(obj_dp); +} + +/*===================== + * Getter functions + *====================*/ + +/** + * 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 + * @param copy_p copy the style to this variable. (NULL if unused) + * @return pointer to an lv_leds_t style + */ +lv_leds_t * lv_leds_get(lv_leds_builtin_t style, lv_leds_t * copy_p) +{ + lv_leds_t *style_p; + + switch(style) { + case LV_LEDS_DEF: + case LV_LEDS_RED: + style_p = &lv_leds_def; + break; + case LV_LEDS_GREEN: + style_p = &lv_leds_green; + break; + default: + style_p = &lv_leds_def; + } + + if(copy_p != NULL) { + if(style_p != NULL) memcpy(copy_p, style_p, sizeof(lv_leds_t)); + else memcpy(copy_p, &lv_leds_def, sizeof(lv_leds_t)); + } + + return style_p; +} + +/** + * Get the brightness of a LEd object + * @param obj_dp pointer to LED object + * @return bright 0 (max. dark) ... 255 (max. light) + */ +uint8_t lv_led_get_bright(lv_obj_t * obj_dp) +{ + lv_led_ext_t * ext_dp = lv_obj_get_ext(obj_dp); + return ext_dp->bright; +} + +/********************** + * STATIC FUNCTIONS + **********************/ + + +/** + * Handle the drawing related tasks of the leds + * @param obj_dp 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') + * @param return true/false, depends on 'mode' + */ +static bool lv_led_design(lv_obj_t* obj_dp, const area_t * mask_p, lv_design_mode_t mode) +{ + if(mode == LV_DESIGN_COVER_CHK) { + /*Return false if the object is not covers the mask_p area*/ + return false; + } + + /*Draw the object*/ + + /*Make darker colors in a temporary style according to the brightness*/ + lv_led_ext_t * led_ext_p = lv_obj_get_ext(obj_dp); + lv_leds_t * leds_p = lv_obj_get_style(obj_dp); + lv_leds_t leds_tmp; + + memcpy(&leds_tmp, leds_p, sizeof(leds_tmp)); + + leds_tmp.rects.objs.color = color_mix(leds_tmp.rects.objs.color, COLOR_BLACK, led_ext_p->bright); + leds_tmp.rects.gcolor = color_mix(leds_tmp.rects.gcolor, COLOR_BLACK, led_ext_p->bright); + + opa_t opa = lv_obj_get_opa(obj_dp); + area_t area; + lv_obj_get_cords(obj_dp, &area); + + lv_draw_rect(&area, mask_p, &leds_tmp.rects, opa); + + return true; +} + + +#endif diff --git a/lv_objx/lv_led.h b/lv_objx/lv_led.h new file mode 100644 index 0000000000..d2622033d9 --- /dev/null +++ b/lv_objx/lv_led.h @@ -0,0 +1,76 @@ +/** + * @file lv_led.h + * + */ + + +/*Search an replace: led -> object normal name with lower case (e.g. button, label etc.) + * led -> object short name with lower case(e.g. btn, label etc) + * LED -> object short name with upper case (e.g. BTN, LABEL etc.) + * + */ + +#ifndef LV_LED_H +#define LV_LED_H + +/********************* + * INCLUDES + *********************/ +#include "lv_conf.h" +#if USE_LV_LED != 0 + +#include "../lv_obj/lv_obj.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/*Style of led*/ +typedef struct +{ + lv_rects_t rects;/*Style of ancestor*/ + /*New style element for this type */ +}lv_leds_t; + +/*Built-in styles of led*/ +typedef enum +{ + LV_LEDS_DEF, + LV_LEDS_RED, + LV_LEDS_GREEN, +}lv_leds_builtin_t; + +/*Data of led*/ +typedef struct +{ + lv_rect_ext_t rect_ext; /*Ext. of ancestor*/ + /*New data for this type */ + uint8_t bright; /*Current brightness of the LED*/ +}lv_led_ext_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ +lv_obj_t* lv_led_create(lv_obj_t* par_dp, lv_obj_t * copy_dp); +bool lv_led_signal(lv_obj_t* obj_dp, lv_signal_t sign, void * param); +lv_leds_t * lv_leds_get(lv_leds_builtin_t style, lv_leds_t * copy_p); + +void lv_led_set_bright(lv_obj_t * obj_dp, uint8_t bright); +uint8_t lv_led_get_bright(lv_obj_t * obj_dp); + +void lv_led_on(lv_obj_t * obj_dp); +void lv_led_off(lv_obj_t * obj_dp); +void lv_led_tgl(lv_obj_t * obj_dp); + + +/********************** + * MACROS + **********************/ + +#endif + +#endif diff --git a/lv_objx/lv_objx_templ.c b/lv_objx/lv_objx_templ.c index 8a947012c5..b70e2102c9 100644 --- a/lv_objx/lv_objx_templ.c +++ b/lv_objx/lv_objx_templ.c @@ -56,7 +56,7 @@ static lv_templs_t lv_templs_def = */ lv_obj_t* lv_templ_create(lv_obj_t* par_dp, lv_obj_t * copy_dp) { - /*Create the ancestor basic object*/ + /*Create the ancestor object*/ lv_obj_t* new_obj_dp = lv_obj_create(par_dp, copy_dp); dm_assert(new_obj_dp); @@ -64,10 +64,15 @@ lv_obj_t* lv_templ_create(lv_obj_t* par_dp, lv_obj_t * copy_dp) lv_templ_ext_t * ext_dp = lv_obj_alloc_ext(new_obj_dp, sizeof(lv_templ_ext_t)); dm_assert(ext_dp); + lv_obj_set_signal_f(new_obj_dp, lv_templ_signal); + lv_obj_set_design_f(new_obj_dp, lv_templ_design); + /*Init the new template object*/ if(copy_dp == NULL) { - lv_obj_set_signal_f(new_obj_dp, lv_templ_signal); - } else { + + } + /*Copy an existing object*/ + else { } @@ -79,13 +84,14 @@ lv_obj_t* lv_templ_create(lv_obj_t* par_dp, lv_obj_t * copy_dp) * @param obj_dp pointer to a template 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_templ_signal(lv_obj_t* obj_dp, lv_signal_t sign, void * param) { bool valid; /* Include the ancient signal function */ - /* TODO update it to the ancient signal function*/ + /* TODO update it to the ancestor's signal function*/ valid = lv_obj_signal(obj_dp, sign, param); /* The object can be deleted so check its validity and then diff --git a/lvgl.h b/lvgl.h index 9cb9c1ece3..bfd4be88ba 100644 --- a/lvgl.h +++ b/lvgl.h @@ -20,6 +20,7 @@ #include "lv_objx/lv_list.h" #include "lv_objx/lv_cb.h" #include "lv_objx/lv_pb.h" +#include "lv_objx/lv_led.h" /********************* * DEFINES