1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00
lvgl/src/lv_objx/lv_sw.c

260 lines
6.9 KiB
C
Raw Normal View History

2017-08-18 17:43:14 +02:00
/**
2017-08-18 21:14:11 +02:00
* @file lv_sw.c
2018-06-19 09:49:58 +02:00
*
2017-08-18 17:43:14 +02:00
*/
/*********************
* INCLUDES
*********************/
#include "lv_sw.h"
#if LV_USE_SW != 0
2017-08-18 17:43:14 +02:00
/*Testing of dependencies*/
#if LV_USE_SLIDER == 0
#error "lv_sw: lv_slider is required. Enable it in lv_conf.h (LV_USE_SLIDER 1) "
#endif
2019-09-24 16:30:38 +02:00
#include "../lv_core/lv_debug.h"
#include "../lv_themes/lv_theme.h"
2018-11-20 07:33:32 +01:00
#include "../lv_misc/lv_math.h"
#include "../lv_core/lv_indev.h"
#include "lv_img.h"
2017-08-18 17:43:14 +02:00
/*********************
* DEFINES
*********************/
#define LV_OBJX_NAME "lv_sw"
2017-08-18 17:43:14 +02:00
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2017-11-09 17:13:04 +01:00
static lv_res_t lv_sw_signal(lv_obj_t * sw, lv_signal_t sign, void * param);
static lv_style_list_t * lv_sw_get_style(lv_obj_t * sw, uint8_t part);
static lv_style_list_t * lv_sw_get_style(lv_obj_t * sw, uint8_t part);
2017-11-09 17:13:04 +01:00
2017-08-18 17:43:14 +02:00
/**********************
* STATIC VARIABLES
**********************/
static lv_signal_cb_t ancestor_signal;
2017-08-18 17:43:14 +02:00
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
2017-08-18 21:14:11 +02:00
* Create a switch objects
* @param par pointer to an object, it will be the parent of the new switch
* @param copy pointer to a switch object, if not NULL then the new object will be copied from it
* @return pointer to the created switch
2017-08-18 17:43:14 +02:00
*/
lv_obj_t * lv_sw_create(lv_obj_t * par, const lv_obj_t * copy)
2017-08-18 17:43:14 +02:00
{
2018-10-05 17:22:49 +02:00
LV_LOG_TRACE("switch create started");
2018-07-25 17:57:08 +02:00
2017-08-18 21:14:11 +02:00
/*Create the ancestor of switch*/
2019-12-31 22:13:32 +01:00
lv_obj_t * new_sw = lv_slider_create(par, copy);
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(new_sw);
2019-09-26 15:24:47 +02:00
if(new_sw == NULL) return NULL;
2017-11-08 11:07:29 +01:00
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_sw);
2018-06-19 09:49:58 +02:00
2017-08-18 21:14:11 +02:00
/*Allocate the switch type specific extended data*/
lv_sw_ext_t * ext = lv_obj_allocate_ext_attr(new_sw, sizeof(lv_sw_ext_t));
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(ext);
if(ext == NULL) {
lv_obj_del(new_sw);
return NULL;
}
2017-08-18 17:43:14 +02:00
/*The signal and design functions are not copied so set them here*/
lv_obj_set_signal_cb(new_sw, lv_sw_signal);
2017-08-18 17:43:14 +02:00
2017-08-18 21:14:11 +02:00
/*Init the new switch switch*/
2017-08-18 17:43:14 +02:00
if(copy == NULL) {
2019-09-16 10:58:28 +02:00
lv_obj_set_click(new_sw, true);
lv_obj_set_protect(new_sw, LV_PROTECT_PRESS_LOST);
2019-04-04 07:15:40 +02:00
lv_obj_set_size(new_sw, 2 * LV_DPI / 3, LV_DPI / 3);
2019-12-31 22:13:32 +01:00
lv_slider_set_range(new_sw, 0, 1);
_ot(new_sw, LV_SW_PART_KNOB, SW_KNOB);
2017-08-18 17:43:14 +02:00
}
2017-08-18 21:14:11 +02:00
/*Copy an existing switch*/
2017-08-18 17:43:14 +02:00
else {
2019-12-31 22:13:32 +01:00
// lv_sw_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
// ext->style_knob_off = copy_ext->style_knob_off;
// ext->style_knob_on = copy_ext->style_knob_on;
2017-08-18 17:43:14 +02:00
}
2018-06-19 09:49:58 +02:00
/*Refresh the style with new signal function*/
2018-10-05 17:22:49 +02:00
LV_LOG_INFO("switch created");
2018-07-25 17:57:08 +02:00
2017-08-18 21:14:11 +02:00
return new_sw;
2017-08-18 17:43:14 +02:00
}
2017-11-09 17:13:04 +01:00
/*=====================
* Setter functions
*====================*/
2017-08-18 17:43:14 +02:00
/**
2017-11-09 17:13:04 +01:00
* Turn ON the switch
2019-03-01 23:50:30 +01:00
* @param sw pointer to a switch objec
2019-06-11 13:51:14 +02:00
* @param anim LV_ANOM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
2017-08-18 17:43:14 +02:00
*/
2019-06-11 13:51:14 +02:00
void lv_sw_on(lv_obj_t * sw, lv_anim_enable_t anim)
2018-11-20 07:33:32 +01:00
{
LV_ASSERT_OBJ(sw, LV_OBJX_NAME);
#if LV_USE_ANIMATION == 0
2019-06-11 13:51:14 +02:00
anim = LV_ANIM_OFF;
2019-03-01 23:50:30 +01:00
#endif
2018-11-20 07:33:32 +01:00
lv_sw_ext_t * ext = lv_obj_get_ext_attr(sw);
2019-09-16 10:58:28 +02:00
ext->state = 1;
2019-12-31 22:13:32 +01:00
lv_slider_set_value(sw, 1, anim);
lv_obj_set_state(sw, LV_OBJ_STATE_CHECKED);
2018-11-20 07:33:32 +01:00
}
/**
* Turn OFF the switch
* @param sw pointer to a switch object
2019-06-11 13:51:14 +02:00
* @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
2018-11-20 07:33:32 +01:00
*/
2019-06-11 13:51:14 +02:00
void lv_sw_off(lv_obj_t * sw, lv_anim_enable_t anim)
2018-11-20 07:33:32 +01:00
{
LV_ASSERT_OBJ(sw, LV_OBJX_NAME);
#if LV_USE_ANIMATION == 0
2019-06-11 13:51:14 +02:00
anim = LV_ANIM_OFF;
2019-03-01 23:50:30 +01:00
#endif
2018-11-20 07:33:32 +01:00
lv_sw_ext_t * ext = lv_obj_get_ext_attr(sw);
2019-09-16 10:58:28 +02:00
ext->state = 0;
lv_bar_set_value(sw, 0, anim);
2019-12-31 22:13:32 +01:00
lv_obj_clear_state(sw, LV_OBJ_STATE_CHECKED);
2018-11-20 07:33:32 +01:00
}
2019-01-25 17:09:59 -08:00
/**
2019-01-25 17:42:08 -08:00
* Toggle the position of the switch
* @param sw pointer to a switch object
2019-06-11 13:51:14 +02:00
* @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
2019-01-25 17:42:08 -08:00
* @return resulting state of the switch.
2019-01-25 17:09:59 -08:00
*/
2019-06-11 13:51:14 +02:00
bool lv_sw_toggle(lv_obj_t * sw, lv_anim_enable_t anim)
2017-08-18 17:43:14 +02:00
{
LV_ASSERT_OBJ(sw, LV_OBJX_NAME);
#if LV_USE_ANIMATION == 0
2019-06-11 13:51:14 +02:00
anim = LV_ANIM_OFF;
2019-03-01 23:50:30 +01:00
#endif
2017-08-18 21:14:11 +02:00
bool state = lv_sw_get_state(sw);
2019-04-04 07:15:40 +02:00
if(state)
lv_sw_off(sw, anim);
else
lv_sw_on(sw, anim);
2019-03-01 23:50:30 +01:00
return !state;
}
/*=====================
* Getter functions
*====================*/
2017-08-18 17:43:14 +02:00
/**********************
* STATIC FUNCTIONS
**********************/
/**
2017-11-09 17:13:04 +01:00
* Signal function of the switch
* @param sw pointer to a switch 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
2017-08-18 17:43:14 +02:00
*/
2017-11-09 17:13:04 +01:00
static lv_res_t lv_sw_signal(lv_obj_t * sw, lv_signal_t sign, void * param)
2017-08-18 17:43:14 +02:00
{
lv_res_t res;
2019-12-31 22:13:32 +01:00
if(sign == LV_SIGNAL_GET_STYLE) {
2020-01-08 21:31:05 +01:00
lv_get_style_info_t * info = param;
info->result = lv_sw_get_style(sw, info->part);
if(info->result != NULL) return LV_RES_OK;
else return ancestor_signal(sw, sign, param);
2019-12-31 22:13:32 +01:00
}
if(sign == LV_SIGNAL_GET_TYPE) {
res = ancestor_signal(sw, sign, param);
if(res != LV_RES_OK) return res;
return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
}
2017-11-09 17:13:04 +01:00
lv_sw_ext_t * ext = lv_obj_get_ext_attr(sw);
/* Include the ancient signal function */
2020-01-10 07:58:39 +01:00
if(sign != LV_SIGNAL_PRESSED &&
sign != LV_SIGNAL_PRESSING &&
sign != LV_SIGNAL_RELEASED) {
res = ancestor_signal(sw, sign, param);
if(res != LV_RES_OK) return res;
}
2017-08-18 17:43:14 +02:00
2017-11-09 17:13:04 +01:00
if(sign == LV_SIGNAL_CLEANUP) {
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
2019-04-04 07:15:40 +02:00
} else if(sign == LV_SIGNAL_RELEASED) {
2020-01-10 07:58:39 +01:00
if(lv_sw_get_state(sw)) lv_sw_off(sw, LV_ANIM_ON);
else lv_sw_on(sw, LV_ANIM_ON);
2019-12-31 22:13:32 +01:00
res = lv_event_send(sw, LV_EVENT_VALUE_CHANGED, NULL);
if(res != LV_RES_OK) return res;
2019-09-16 10:58:28 +02:00
2019-12-31 22:13:32 +01:00
} else if(sign == LV_SIGNAL_CONTROL) {
char c = *((char *)param);
if(c == LV_KEY_RIGHT || c == LV_KEY_UP) lv_sw_on(sw, LV_ANIM_ON);
else if(c == LV_KEY_LEFT || c == LV_KEY_DOWN) lv_sw_off(sw, LV_ANIM_ON);
2019-09-16 10:58:28 +02:00
2019-12-31 22:13:32 +01:00
res = lv_event_send(sw, LV_EVENT_VALUE_CHANGED, NULL);
if(res != LV_RES_OK) return res;
2019-09-16 10:58:28 +02:00
}
else if(sign == LV_SIGNAL_GET_EDITABLE) {
2018-10-05 17:22:49 +02:00
bool * editable = (bool *)param;
2019-04-04 07:15:40 +02:00
*editable = false; /*The ancestor slider is editable the switch is not*/
2018-02-28 15:37:41 +01:00
}
2017-08-18 17:43:14 +02:00
2017-11-09 17:13:04 +01:00
return res;
2017-08-18 17:43:14 +02:00
}
static lv_style_list_t * lv_sw_get_style(lv_obj_t * sw, uint8_t part)
2019-12-31 22:13:32 +01:00
{
LV_ASSERT_OBJ(sw, LV_OBJX_NAME);
lv_sw_ext_t * ext = lv_obj_get_ext_attr(sw);
lv_style_list_t * style_dsc_p;
2019-12-31 22:13:32 +01:00
switch(part) {
case LV_SW_PART_BG:
2020-01-16 21:25:11 +01:00
style_dsc_p = &sw->style_list;
2019-12-31 22:13:32 +01:00
break;
case LV_SW_PART_INDIC:
style_dsc_p = &ext->slider.bar.style_indic;
break;
case LV_SW_PART_KNOB:
style_dsc_p = &ext->slider.style_knob;
break;
default:
style_dsc_p = NULL;
}
return style_dsc_p;
}
2017-08-18 17:43:14 +02:00
#endif