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
|
|
|
|
*********************/
|
2018-07-07 11:53:22 +02:00
|
|
|
#include "lv_sw.h"
|
2018-11-14 20:09:09 -05:00
|
|
|
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_SW != 0
|
2017-08-18 17:43:14 +02:00
|
|
|
|
2018-01-19 15:40:22 +01:00
|
|
|
/*Testing of dependencies*/
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_SLIDER == 0
|
|
|
|
#error "lv_sw: lv_slider is required. Enable it in lv_conf.h (LV_USE_SLIDER 1) "
|
2018-01-19 15:40:22 +01:00
|
|
|
#endif
|
|
|
|
|
2017-11-16 15:32:33 +01:00
|
|
|
#include "../lv_themes/lv_theme.h"
|
2018-11-20 07:33:32 +01:00
|
|
|
#include "../lv_misc/lv_math.h"
|
2017-08-18 17:43:14 +02:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2018-11-14 20:09:09 -05:00
|
|
|
|
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);
|
|
|
|
|
2017-08-18 17:43:14 +02:00
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
2019-02-26 09:25:46 +01:00
|
|
|
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
|
|
|
*/
|
2018-07-30 06:52:29 +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*/
|
|
|
|
lv_obj_t * new_sw = lv_slider_create(par, copy);
|
2017-11-26 11:38:28 +01:00
|
|
|
lv_mem_assert(new_sw);
|
2018-07-25 13:33:53 +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_func(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*/
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_sw_ext_t * ext = lv_obj_allocate_ext_attr(new_sw, sizeof(lv_sw_ext_t));
|
2017-11-26 11:38:28 +01:00
|
|
|
lv_mem_assert(ext);
|
2018-07-25 13:33:53 +02:00
|
|
|
if(ext == NULL) return NULL;
|
2017-08-18 17:43:14 +02:00
|
|
|
|
|
|
|
/*Initialize the allocated 'ext' */
|
2018-11-16 18:36:12 +01:00
|
|
|
ext->changed = 0;
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_ANIMATION
|
2018-11-14 20:09:09 -05:00
|
|
|
ext->anim_time = 0;
|
2018-12-10 15:35:57 -05:00
|
|
|
#endif
|
2017-11-15 15:50:33 +01:00
|
|
|
ext->style_knob_off = ext->slider.style_knob;
|
|
|
|
ext->style_knob_on = ext->slider.style_knob;
|
2017-08-18 17:43:14 +02:00
|
|
|
|
|
|
|
/*The signal and design functions are not copied so set them here*/
|
2019-02-26 09:25:46 +01:00
|
|
|
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-03-01 23:50:30 +01:00
|
|
|
lv_obj_set_size(new_sw, 2 * LV_DPI / 3, LV_DPI / 3);
|
2017-08-18 21:14:11 +02:00
|
|
|
lv_slider_set_knob_in(new_sw, true);
|
2019-03-01 23:50:30 +01:00
|
|
|
lv_slider_set_range(new_sw, 0, LV_SW_MAX_VALUE);
|
2017-11-16 15:32:33 +01:00
|
|
|
|
|
|
|
/*Set the default styles*/
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_theme_t * th = lv_theme_get_current();
|
2017-11-16 15:32:33 +01:00
|
|
|
if(th) {
|
2019-02-11 09:35:06 +01:00
|
|
|
lv_sw_set_style(new_sw, LV_SW_STYLE_BG, th->style.sw.bg);
|
|
|
|
lv_sw_set_style(new_sw, LV_SW_STYLE_INDIC, th->style.sw.indic);
|
|
|
|
lv_sw_set_style(new_sw, LV_SW_STYLE_KNOB_OFF, th->style.sw.knob_off);
|
|
|
|
lv_sw_set_style(new_sw, LV_SW_STYLE_KNOB_ON, th->style.sw.knob_on);
|
2017-11-16 15:32:33 +01:00
|
|
|
} else {
|
|
|
|
/*Let the slider' style*/
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_sw_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
|
2017-11-15 15:50:33 +01:00
|
|
|
ext->style_knob_off = copy_ext->style_knob_off;
|
|
|
|
ext->style_knob_on = copy_ext->style_knob_on;
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_ANIMATION
|
2018-11-20 07:33:32 +01:00
|
|
|
ext->anim_time = copy_ext->anim_time;
|
2018-12-10 15:35:57 -05:00
|
|
|
#endif
|
2017-08-18 17:43:14 +02:00
|
|
|
|
2017-11-15 15:50:33 +01:00
|
|
|
if(lv_sw_get_state(new_sw)) lv_slider_set_style(new_sw, LV_SLIDER_STYLE_KNOB, ext->style_knob_on);
|
|
|
|
else lv_slider_set_style(new_sw, LV_SLIDER_STYLE_KNOB, ext->style_knob_off);
|
2018-11-20 07:33:32 +01:00
|
|
|
|
|
|
|
|
2017-08-18 17:43:14 +02:00
|
|
|
/*Refresh the style with new signal function*/
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_obj_refresh_style(new_sw);
|
2017-08-18 17:43:14 +02:00
|
|
|
}
|
2018-06-19 09:49:58 +02:00
|
|
|
|
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
|
|
|
|
* @param anim true: set the value with an animation; false: change the value immediatelly
|
2017-08-18 17:43:14 +02:00
|
|
|
*/
|
2019-03-01 23:50:30 +01:00
|
|
|
void lv_sw_on(lv_obj_t * sw, bool anim)
|
2018-11-20 07:33:32 +01:00
|
|
|
{
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_ANIMATION == 0
|
2019-03-01 23:50:30 +01:00
|
|
|
anim = false;
|
|
|
|
#endif
|
2018-11-20 07:33:32 +01:00
|
|
|
lv_sw_ext_t * ext = lv_obj_get_ext_attr(sw);
|
2019-03-01 23:50:30 +01:00
|
|
|
lv_slider_set_value(sw, LV_SW_MAX_VALUE, anim);
|
2018-11-20 07:33:32 +01:00
|
|
|
lv_slider_set_style(sw, LV_SLIDER_STYLE_KNOB, ext->style_knob_on);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn OFF the switch
|
|
|
|
* @param sw pointer to a switch object
|
2019-03-01 23:50:30 +01:00
|
|
|
* @param anim true: set the value with an animation; false: change the value immediatelly
|
2018-11-20 07:33:32 +01:00
|
|
|
*/
|
2019-03-01 23:50:30 +01:00
|
|
|
void lv_sw_off(lv_obj_t * sw, bool anim)
|
2018-11-20 07:33:32 +01:00
|
|
|
{
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_ANIMATION == 0
|
2019-03-01 23:50:30 +01:00
|
|
|
anim = false;
|
|
|
|
#endif
|
2018-11-20 07:33:32 +01:00
|
|
|
lv_sw_ext_t * ext = lv_obj_get_ext_attr(sw);
|
2019-03-01 23:50:30 +01:00
|
|
|
lv_slider_set_value(sw, 0, anim);
|
2018-11-20 07:33:32 +01:00
|
|
|
lv_slider_set_style(sw, LV_SLIDER_STYLE_KNOB, ext->style_knob_off);
|
|
|
|
}
|
|
|
|
|
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-03-01 23:50:30 +01:00
|
|
|
* @param anim true: set the value with an animation; false: change the value immediatelly
|
2019-01-25 17:42:08 -08:00
|
|
|
* @return resulting state of the switch.
|
2019-01-25 17:09:59 -08:00
|
|
|
*/
|
2019-03-01 23:50:30 +01:00
|
|
|
bool lv_sw_toggle(lv_obj_t *sw, bool anim)
|
2017-08-18 17:43:14 +02:00
|
|
|
{
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_ANIMATION == 0
|
2019-03-01 23:50:30 +01:00
|
|
|
anim = false;
|
|
|
|
#endif
|
2017-08-18 21:14:11 +02:00
|
|
|
|
2019-01-25 20:17:43 -08:00
|
|
|
bool state = lv_sw_get_state(sw);
|
2019-03-01 23:50:30 +01:00
|
|
|
if(state) lv_sw_off(sw, anim);
|
|
|
|
else lv_sw_on(sw, anim);
|
|
|
|
|
2019-01-25 20:17:43 -08:00
|
|
|
return !state;
|
|
|
|
}
|
|
|
|
|
2017-11-09 17:13:04 +01:00
|
|
|
/**
|
2017-11-15 15:50:33 +01:00
|
|
|
* Set a style of a switch
|
2017-11-09 17:13:04 +01:00
|
|
|
* @param sw pointer to a switch object
|
2017-11-15 15:50:33 +01:00
|
|
|
* @param type which style should be set
|
|
|
|
* @param style pointer to a style
|
2017-11-09 17:13:04 +01:00
|
|
|
*/
|
2018-06-19 09:49:58 +02:00
|
|
|
void lv_sw_set_style(lv_obj_t * sw, lv_sw_style_t type, lv_style_t * style)
|
2017-11-09 17:13:04 +01:00
|
|
|
{
|
2017-11-15 15:50:33 +01:00
|
|
|
lv_sw_ext_t * ext = lv_obj_get_ext_attr(sw);
|
2017-08-18 21:14:11 +02:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
switch(type) {
|
2017-11-15 15:50:33 +01:00
|
|
|
case LV_SLIDER_STYLE_BG:
|
|
|
|
lv_slider_set_style(sw, LV_SLIDER_STYLE_BG, style);
|
|
|
|
break;
|
|
|
|
case LV_SLIDER_STYLE_INDIC:
|
|
|
|
lv_bar_set_style(sw, LV_SLIDER_STYLE_INDIC, style);
|
|
|
|
break;
|
|
|
|
case LV_SW_STYLE_KNOB_OFF:
|
|
|
|
ext->style_knob_off = style;
|
|
|
|
if(lv_sw_get_state(sw) == 0) lv_slider_set_style(sw, LV_SLIDER_STYLE_KNOB, style);
|
|
|
|
break;
|
|
|
|
case LV_SW_STYLE_KNOB_ON:
|
|
|
|
ext->style_knob_on = style;
|
|
|
|
if(lv_sw_get_state(sw) != 0) lv_slider_set_style(sw, LV_SLIDER_STYLE_KNOB, style);
|
|
|
|
break;
|
|
|
|
}
|
2017-08-18 17:43:14 +02:00
|
|
|
}
|
2018-12-12 20:15:09 -05:00
|
|
|
|
2018-11-14 20:09:09 -05:00
|
|
|
void lv_sw_set_anim_time(lv_obj_t *sw, uint16_t anim_time)
|
|
|
|
{
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_ANIMATION
|
2018-12-10 15:35:57 -05:00
|
|
|
lv_sw_ext_t * ext = lv_obj_get_ext_attr(sw);
|
2018-11-16 17:42:29 +01:00
|
|
|
ext->anim_time = anim_time;
|
2018-12-10 15:35:57 -05:00
|
|
|
#endif
|
2018-12-12 20:15:09 -05:00
|
|
|
}
|
|
|
|
|
2018-11-14 20:09:09 -05:00
|
|
|
|
2017-08-18 17:43:14 +02:00
|
|
|
/*=====================
|
2017-11-09 17:13:04 +01:00
|
|
|
* Getter functions
|
2017-08-18 17:43:14 +02:00
|
|
|
*====================*/
|
|
|
|
|
2017-11-09 17:13:04 +01:00
|
|
|
/**
|
2017-11-15 15:50:33 +01:00
|
|
|
* Get a style of a switch
|
|
|
|
* @param sw pointer to a switch object
|
|
|
|
* @param type which style should be get
|
|
|
|
* @return style pointer to a style
|
2017-08-18 17:43:14 +02:00
|
|
|
*/
|
2018-07-30 06:52:29 +02:00
|
|
|
lv_style_t * lv_sw_get_style(const lv_obj_t * sw, lv_sw_style_t type)
|
2017-11-09 17:13:04 +01:00
|
|
|
{
|
2018-11-05 22:21:20 -06:00
|
|
|
lv_style_t * style = NULL;
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_sw_ext_t * ext = lv_obj_get_ext_attr(sw);
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case LV_SW_STYLE_BG:
|
2018-11-05 22:21:20 -06:00
|
|
|
style = lv_slider_get_style(sw, LV_SLIDER_STYLE_BG);
|
|
|
|
break;
|
2018-06-19 09:49:58 +02:00
|
|
|
case LV_SW_STYLE_INDIC:
|
2018-11-05 22:21:20 -06:00
|
|
|
style = lv_slider_get_style(sw, LV_SLIDER_STYLE_INDIC);
|
|
|
|
break;
|
2018-06-19 09:49:58 +02:00
|
|
|
case LV_SW_STYLE_KNOB_OFF:
|
2018-11-05 22:21:20 -06:00
|
|
|
style = ext->style_knob_off;
|
|
|
|
break;
|
2018-06-19 09:49:58 +02:00
|
|
|
case LV_SW_STYLE_KNOB_ON:
|
2018-11-05 22:21:20 -06:00
|
|
|
style = ext->style_knob_on;
|
|
|
|
break;
|
2018-06-19 09:49:58 +02:00
|
|
|
default:
|
2018-11-05 22:21:20 -06:00
|
|
|
style = NULL;
|
|
|
|
break;
|
2017-11-15 15:50:33 +01:00
|
|
|
}
|
2017-08-18 17:43:14 +02:00
|
|
|
|
2018-11-05 22:21:20 -06:00
|
|
|
return style;
|
2017-11-09 17:13:04 +01:00
|
|
|
}
|
2018-11-14 20:09:09 -05:00
|
|
|
|
|
|
|
uint16_t lv_sw_get_anim_time(const lv_obj_t *sw)
|
|
|
|
{
|
2018-11-16 17:42:29 +01:00
|
|
|
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_ANIMATION
|
2018-11-14 20:09:09 -05:00
|
|
|
lv_sw_ext_t * ext = lv_obj_get_ext_attr(sw);
|
|
|
|
return ext->anim_time;
|
2018-11-16 17:42:29 +01:00
|
|
|
#else
|
|
|
|
return 0;
|
2018-11-14 20:24:13 -05:00
|
|
|
#endif
|
2018-11-16 17:42:29 +01:00
|
|
|
}
|
2018-11-14 20:09:09 -05:00
|
|
|
|
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
|
|
|
{
|
2017-11-09 17:13:04 +01:00
|
|
|
lv_sw_ext_t * ext = lv_obj_get_ext_attr(sw);
|
|
|
|
|
2019-03-01 23:50:30 +01:00
|
|
|
/*Save the current (old) value before slider signal modifies it. It will be required in the later colcualtions*/
|
2017-11-09 17:13:04 +01:00
|
|
|
int16_t old_val;
|
|
|
|
if(sign == LV_SIGNAL_PRESSING) old_val = ext->slider.drag_value;
|
|
|
|
else old_val = lv_slider_get_value(sw);
|
|
|
|
|
2019-03-01 23:50:30 +01:00
|
|
|
/*Don't let the slider to call the action. Switch handles it differently*/
|
2019-03-02 01:23:33 +01:00
|
|
|
lv_event_cb_t event_cb = sw->event_cb;
|
|
|
|
sw->event_cb = NULL;
|
2017-11-09 17:13:04 +01:00
|
|
|
|
|
|
|
lv_res_t res;
|
|
|
|
/* Include the ancient signal function */
|
2019-03-01 23:50:30 +01:00
|
|
|
|
2017-11-09 17:13:04 +01:00
|
|
|
res = ancestor_signal(sw, sign, param);
|
|
|
|
if(res != LV_RES_OK) return res;
|
2017-08-18 17:43:14 +02:00
|
|
|
|
2019-03-02 01:23:33 +01:00
|
|
|
sw->event_cb = event_cb;
|
|
|
|
|
2017-11-09 17:13:04 +01:00
|
|
|
if(sign == LV_SIGNAL_CLEANUP) {
|
|
|
|
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
|
2018-11-16 17:42:29 +01:00
|
|
|
}
|
2018-11-20 07:33:32 +01:00
|
|
|
else if(sign == LV_SIGNAL_PRESSED) {
|
|
|
|
|
|
|
|
/*Save the x coordinate of the pressed point to see if the switch was slid*/
|
|
|
|
lv_indev_t * indev = lv_indev_get_act();
|
|
|
|
if(indev) {
|
|
|
|
lv_point_t p;
|
|
|
|
lv_indev_get_point(indev, &p);
|
|
|
|
ext->start_x = p.x;
|
|
|
|
}
|
|
|
|
ext->slided = 0;
|
|
|
|
ext->changed = 0;
|
|
|
|
}
|
2018-11-16 18:36:12 +01:00
|
|
|
else if(sign == LV_SIGNAL_PRESSING) {
|
2019-03-01 23:50:30 +01:00
|
|
|
/*See if the switch was slid (moved at least a little)*/
|
2018-11-20 07:33:32 +01:00
|
|
|
lv_indev_t * indev = lv_indev_get_act();
|
|
|
|
if(indev) {
|
|
|
|
lv_point_t p = {0,0};
|
|
|
|
lv_indev_get_point(indev, &p);
|
|
|
|
if(LV_MATH_ABS(p.x - ext->start_x) > LV_INDEV_DRAG_LIMIT) ext->slided = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*If didn't slide then revert the min/max value. So click without slide won't move the switch as a slider*/
|
|
|
|
if(ext->slided == 0) {
|
2019-03-01 23:50:30 +01:00
|
|
|
if(lv_sw_get_state(sw)) ext->slider.drag_value = LV_SW_MAX_VALUE;
|
2018-11-20 07:33:32 +01:00
|
|
|
else ext->slider.drag_value = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*If explicitly changed (by slide) don't need to be toggled on release*/
|
2019-03-01 23:50:30 +01:00
|
|
|
int16_t threshold = LV_SW_MAX_VALUE / 2;
|
2018-11-16 18:36:12 +01:00
|
|
|
if((old_val < threshold && ext->slider.drag_value > threshold) ||
|
|
|
|
(old_val > threshold && ext->slider.drag_value < threshold))
|
|
|
|
{
|
2018-11-20 07:33:32 +01:00
|
|
|
ext->changed = 1;
|
2018-11-16 18:36:12 +01:00
|
|
|
}
|
2018-11-16 17:42:29 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_PRESS_LOST) {
|
2018-11-16 18:36:12 +01:00
|
|
|
if(lv_sw_get_state(sw)) {
|
|
|
|
lv_slider_set_style(sw, LV_SLIDER_STYLE_KNOB, ext->style_knob_on);
|
2019-03-01 23:50:30 +01:00
|
|
|
lv_slider_set_value(sw, LV_SW_MAX_VALUE, true);
|
2019-03-07 00:42:08 +01:00
|
|
|
res = lv_obj_send_event(sw, LV_EVENT_VALUE_CHANGED);
|
|
|
|
if(res != LV_RES_OK) return res;
|
2018-11-16 18:36:12 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
lv_slider_set_style(sw, LV_SLIDER_STYLE_KNOB, ext->style_knob_off);
|
2019-03-01 23:50:30 +01:00
|
|
|
lv_slider_set_value(sw, 0, true);
|
2019-03-07 00:42:08 +01:00
|
|
|
res = lv_obj_send_event(sw, LV_EVENT_VALUE_CHANGED);
|
|
|
|
if(res != LV_RES_OK) return res;
|
2018-11-16 18:36:12 +01:00
|
|
|
}
|
2018-11-16 17:42:29 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_RELEASED) {
|
2018-11-16 18:36:12 +01:00
|
|
|
/*If not dragged then toggle the switch*/
|
|
|
|
if(ext->changed == 0) {
|
2019-03-01 23:50:30 +01:00
|
|
|
if(lv_sw_get_state(sw)) lv_sw_off(sw, true);
|
|
|
|
else lv_sw_on(sw, true);
|
2018-12-26 19:29:08 +01:00
|
|
|
|
2017-11-09 17:13:04 +01:00
|
|
|
}
|
2018-11-16 18:36:12 +01:00
|
|
|
/*If the switch was dragged then calculate the new state based on the current position*/
|
2018-11-16 17:42:29 +01:00
|
|
|
else {
|
2018-11-16 18:36:12 +01:00
|
|
|
int16_t v = lv_slider_get_value(sw);
|
2019-03-01 23:50:30 +01:00
|
|
|
if(v > LV_SW_MAX_VALUE / 2) lv_sw_on(sw, true);
|
|
|
|
else lv_sw_off(sw, true);
|
2018-11-16 18:36:12 +01:00
|
|
|
|
2019-03-07 00:42:08 +01:00
|
|
|
res = lv_obj_send_event(sw, LV_EVENT_VALUE_CHANGED);
|
|
|
|
if(res != LV_RES_OK) return res;
|
2018-11-14 20:09:09 -05:00
|
|
|
}
|
2018-06-19 09:49:58 +02:00
|
|
|
} else if(sign == LV_SIGNAL_CONTROLL) {
|
|
|
|
char c = *((char *)param);
|
2018-06-19 11:13:03 +02:00
|
|
|
if(c == LV_GROUP_KEY_ENTER) {
|
2019-03-02 01:23:33 +01:00
|
|
|
lv_sw_toggle(sw, true);
|
2019-03-07 00:42:08 +01:00
|
|
|
res = lv_obj_send_event(sw, LV_EVENT_VALUE_CHANGED);
|
|
|
|
if(res != LV_RES_OK) return res;
|
2019-03-01 23:50:30 +01:00
|
|
|
} else if(c == LV_GROUP_KEY_RIGHT || c == LV_GROUP_KEY_UP) {
|
|
|
|
lv_slider_set_value(sw, LV_SW_MAX_VALUE, true);
|
2019-03-07 00:42:08 +01:00
|
|
|
res = lv_obj_send_event(sw, LV_EVENT_VALUE_CHANGED);
|
|
|
|
if(res != LV_RES_OK) return res;
|
2019-03-01 23:50:30 +01:00
|
|
|
} else if(c == LV_GROUP_KEY_LEFT || c == LV_GROUP_KEY_DOWN) {
|
|
|
|
lv_slider_set_value(sw, 0, true);
|
2019-03-07 00:42:08 +01:00
|
|
|
res = lv_obj_send_event(sw, LV_EVENT_VALUE_CHANGED);
|
|
|
|
if(res != LV_RES_OK) return res;
|
2018-03-01 12:21:49 +01:00
|
|
|
}
|
2018-07-25 21:52:50 +02:00
|
|
|
} else if(sign == LV_SIGNAL_GET_EDITABLE) {
|
2018-10-05 17:22:49 +02:00
|
|
|
bool * editable = (bool *)param;
|
|
|
|
*editable = false; /*The ancestor slider is editable the switch is not*/
|
2018-06-19 09:49:58 +02:00
|
|
|
} else if(sign == LV_SIGNAL_GET_TYPE) {
|
2018-02-28 15:37:41 +01:00
|
|
|
lv_obj_type_t * buf = param;
|
|
|
|
uint8_t i;
|
|
|
|
for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
|
|
|
|
if(buf->type[i] == NULL) break;
|
|
|
|
}
|
|
|
|
buf->type[i] = "lv_sw";
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|