2016-09-30 13:35:54 +02:00
|
|
|
/**
|
|
|
|
* @file lv_btnm.c
|
2018-06-19 09:49:58 +02:00
|
|
|
*
|
2016-09-30 13:35:54 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
2020-02-14 12:44:15 +01:00
|
|
|
#include "lv_btnmatrix.h"
|
2020-02-14 12:36:44 +01:00
|
|
|
#if LV_USE_BTNMATRIX != 0
|
2016-09-30 13:35:54 +02:00
|
|
|
|
2019-09-24 16:30:38 +02:00
|
|
|
#include "../lv_core/lv_debug.h"
|
2017-11-30 11:35:33 +01:00
|
|
|
#include "../lv_core/lv_group.h"
|
2017-04-24 16:16:36 +02:00
|
|
|
#include "../lv_draw/lv_draw.h"
|
2017-11-30 11:35:33 +01:00
|
|
|
#include "../lv_core/lv_refr.h"
|
2017-11-16 15:32:33 +01:00
|
|
|
#include "../lv_themes/lv_theme.h"
|
2017-11-26 23:57:39 +01:00
|
|
|
#include "../lv_misc/lv_txt.h"
|
2016-09-30 13:35:54 +02:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2020-02-14 12:36:44 +01:00
|
|
|
#define LV_OBJX_NAME "lv_btnmatrix"
|
2016-09-30 13:35:54 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* TYPEDEFS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC PROTOTYPES
|
|
|
|
**********************/
|
2020-02-14 12:36:44 +01:00
|
|
|
static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * param);
|
|
|
|
static lv_design_res_t lv_btnmatrix_design(lv_obj_t * btnm, const lv_area_t * clip_area, lv_design_mode_t mode);
|
|
|
|
static lv_style_list_t * lv_btnmatrix_get_style(lv_obj_t * btnm, uint8_t part);
|
|
|
|
|
|
|
|
static uint8_t get_button_width(lv_btnmatrix_ctrl_t ctrl_bits);
|
|
|
|
static bool button_is_hidden(lv_btnmatrix_ctrl_t ctrl_bits);
|
|
|
|
static bool button_is_repeat_disabled(lv_btnmatrix_ctrl_t ctrl_bits);
|
|
|
|
static bool button_is_inactive(lv_btnmatrix_ctrl_t ctrl_bits);
|
|
|
|
static bool button_is_click_trig(lv_btnmatrix_ctrl_t ctrl_bits);
|
|
|
|
static bool button_is_tgl_enabled(lv_btnmatrix_ctrl_t ctrl_bits);
|
|
|
|
static bool button_get_tgl_state(lv_btnmatrix_ctrl_t ctrl_bits);
|
2017-11-23 21:28:36 +01:00
|
|
|
static uint16_t get_button_from_point(lv_obj_t * btnm, lv_point_t * p);
|
2019-02-19 15:06:23 +08:00
|
|
|
static void allocate_btn_areas_and_controls(const lv_obj_t * btnm, const char ** map);
|
2019-02-12 16:55:30 +08:00
|
|
|
static void invalidate_button_area(const lv_obj_t * btnm, uint16_t btn_idx);
|
|
|
|
static bool maps_are_identical(const char ** map1, const char ** map2);
|
2019-04-04 07:15:40 +02:00
|
|
|
static void make_one_button_toggled(lv_obj_t * btnm, uint16_t btn_idx);
|
2016-09-30 13:35:54 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
2020-02-14 12:36:44 +01:00
|
|
|
static const char * lv_btnmatrix_def_map[] = {"Btn1", "Btn2", "Btn3", "\n", "Btn4", "Btn5", ""};
|
2016-09-30 13:35:54 +02:00
|
|
|
|
2019-02-26 09:25:46 +01:00
|
|
|
static lv_design_cb_t ancestor_design_f;
|
|
|
|
static lv_signal_cb_t ancestor_signal;
|
2016-12-19 11:57:42 +01:00
|
|
|
|
2016-09-30 13:35:54 +02:00
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a button matrix objects
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param par pointer to an object, it will be the parent of the new button matrix
|
2019-04-04 07:15:40 +02:00
|
|
|
* @param copy pointer to a button matrix object, if not NULL then the new object will be copied
|
|
|
|
* from it
|
2016-09-30 13:35:54 +02:00
|
|
|
* @return pointer to the created button matrix
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_obj_t * lv_btnmatrix_create(lv_obj_t * par, const lv_obj_t * copy)
|
2016-09-30 13:35:54 +02:00
|
|
|
{
|
2018-09-12 18:55:28 +03:00
|
|
|
LV_LOG_TRACE("button matrix create started");
|
2018-07-25 17:57:08 +02:00
|
|
|
|
2016-09-30 13:35:54 +02:00
|
|
|
/*Create the ancestor object*/
|
2020-01-20 16:11:38 +01:00
|
|
|
lv_obj_t * btnm = lv_obj_create(par, copy);
|
|
|
|
LV_ASSERT_MEM(btnm);
|
|
|
|
if(btnm == NULL) return NULL;
|
2018-07-25 13:33:53 +02:00
|
|
|
|
2020-01-20 16:11:38 +01:00
|
|
|
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(btnm);
|
2018-06-19 09:49:58 +02:00
|
|
|
|
2016-09-30 13:35:54 +02:00
|
|
|
/*Allocate the object type specific extended data*/
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_allocate_ext_attr(btnm, sizeof(lv_btnmatrix_ext_t));
|
2019-09-24 23:14:17 +02:00
|
|
|
LV_ASSERT_MEM(ext);
|
2019-12-03 18:16:14 +01:00
|
|
|
if(ext == NULL) {
|
2020-01-20 16:11:38 +01:00
|
|
|
lv_obj_del(btnm);
|
2019-12-03 18:16:14 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-07-25 13:33:53 +02:00
|
|
|
|
2019-12-31 11:13:09 +01:00
|
|
|
ext->btn_cnt = 0;
|
2020-02-14 12:36:44 +01:00
|
|
|
ext->btn_id_pr = LV_BTNMATRIX_BTN_NONE;
|
|
|
|
ext->btn_id_focused = LV_BTNMATRIX_BTN_NONE;
|
|
|
|
ext->btn_id_act = LV_BTNMATRIX_BTN_NONE;
|
2019-12-31 11:13:09 +01:00
|
|
|
ext->button_areas = NULL;
|
|
|
|
ext->ctrl_bits = NULL;
|
|
|
|
ext->map_p = NULL;
|
|
|
|
ext->recolor = 0;
|
2020-03-04 16:29:21 +01:00
|
|
|
ext->one_check = 0;
|
2020-01-16 14:26:36 +01:00
|
|
|
lv_style_list_init(&ext->style_btn);
|
2020-02-21 16:33:42 +01:00
|
|
|
ext->style_btn.ignore_trans = 1;
|
2016-09-30 13:35:54 +02:00
|
|
|
|
2020-01-20 16:11:38 +01:00
|
|
|
if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_cb(btnm);
|
2016-12-19 11:57:42 +01:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_obj_set_signal_cb(btnm, lv_btnmatrix_signal);
|
|
|
|
lv_obj_set_design_cb(btnm, lv_btnmatrix_design);
|
2016-09-30 13:35:54 +02:00
|
|
|
|
|
|
|
/*Init the new button matrix object*/
|
2016-10-07 11:15:46 +02:00
|
|
|
if(copy == NULL) {
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_set_map(btnm, lv_btnmatrix_def_map);
|
2020-03-26 15:50:57 +01:00
|
|
|
lv_obj_set_size(btnm, LV_DPI * 2, LV_DPI * 1);
|
2020-02-14 17:03:25 +01:00
|
|
|
lv_theme_apply(btnm, LV_THEME_BTNMATRIX);
|
2016-09-30 13:35:54 +02:00
|
|
|
}
|
|
|
|
/*Copy an existing object*/
|
|
|
|
else {
|
2020-03-26 15:50:57 +01:00
|
|
|
lv_btnmatrix_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
|
|
|
|
lv_btnmatrix_set_map(btnm, copy_ext->map_p);
|
|
|
|
lv_btnmatrix_set_ctrl_map(btnm, copy_ext->ctrl_bits);
|
|
|
|
lv_style_list_copy(&ext->style_btn, ©_ext->style_btn);
|
2016-09-30 13:35:54 +02:00
|
|
|
}
|
2018-07-25 20:39:24 +02:00
|
|
|
|
2018-09-12 18:55:28 +03:00
|
|
|
LV_LOG_INFO("button matrix created");
|
2018-06-19 09:49:58 +02:00
|
|
|
|
2020-01-20 16:11:38 +01:00
|
|
|
return btnm;
|
2016-09-30 13:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Setter functions
|
|
|
|
*====================*/
|
|
|
|
|
|
|
|
/**
|
2019-02-12 16:55:30 +08:00
|
|
|
* Set a new map. Buttons will be created/deleted according to the map. The
|
|
|
|
* button matrix keeps a reference to the map and so the string array must not
|
|
|
|
* be deallocated during the life of the matrix.
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param btnm pointer to a button matrix object
|
2019-03-11 06:18:44 +01:00
|
|
|
* @param map pointer a string array. The last string has to be: "". Use "\n" to make a line break.
|
2016-09-30 13:35:54 +02:00
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
void lv_btnmatrix_set_map(lv_obj_t * btnm, const char * map[])
|
2016-09-30 13:35:54 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
LV_ASSERT_NULL(map);
|
2018-06-19 09:49:58 +02:00
|
|
|
|
2019-02-12 16:55:30 +08:00
|
|
|
/*
|
2020-02-14 12:36:44 +01:00
|
|
|
* lv_btnmatrix_set_map is called on receipt of signals such as
|
2019-02-12 16:55:30 +08:00
|
|
|
* LV_SIGNAL_CORD_CHG regardless of whether the map has changed (e.g.
|
|
|
|
* calling lv_obj_align on the map will trigger this).
|
|
|
|
*
|
|
|
|
* We check if the map has changed here to avoid overwriting changes made
|
|
|
|
* to hidden/longpress/disabled states after the map was originally set.
|
|
|
|
*
|
|
|
|
* TODO: separate all map set/allocation from layout code below and skip
|
|
|
|
* set/allocation when map hasn't changed.
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2019-04-04 07:15:40 +02:00
|
|
|
if(!maps_are_identical(ext->map_p, map)) {
|
2019-02-12 16:55:30 +08:00
|
|
|
|
|
|
|
/*Analyze the map and create the required number of buttons*/
|
|
|
|
allocate_btn_areas_and_controls(btnm, map);
|
|
|
|
}
|
|
|
|
ext->map_p = map;
|
2018-06-19 09:49:58 +02:00
|
|
|
|
|
|
|
/*Set size and positions of the buttons*/
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_style_int_t left = lv_obj_get_style_pad_left(btnm, LV_BTNMATRIX_PART_BG);
|
|
|
|
lv_style_int_t right = lv_obj_get_style_pad_right(btnm, LV_BTNMATRIX_PART_BG);
|
|
|
|
lv_style_int_t top = lv_obj_get_style_pad_top(btnm, LV_BTNMATRIX_PART_BG);
|
|
|
|
lv_style_int_t bottom = lv_obj_get_style_pad_bottom(btnm, LV_BTNMATRIX_PART_BG);
|
|
|
|
lv_style_int_t inner = lv_obj_get_style_pad_inner(btnm, LV_BTNMATRIX_PART_BG);
|
2019-12-22 22:40:02 +01:00
|
|
|
|
|
|
|
lv_coord_t max_w = lv_obj_get_width(btnm) - left - right;
|
|
|
|
lv_coord_t max_h = lv_obj_get_height(btnm) - top - bottom;
|
|
|
|
lv_coord_t act_y = top;
|
2018-06-19 09:49:58 +02:00
|
|
|
|
|
|
|
/*Count the lines to calculate button height*/
|
|
|
|
uint8_t line_cnt = 1;
|
|
|
|
uint8_t li;
|
|
|
|
for(li = 0; strlen(map[li]) != 0; li++) {
|
2019-04-04 07:15:40 +02:00
|
|
|
if(strcmp(map[li], "\n") == 0) line_cnt++;
|
2018-06-19 09:49:58 +02:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:40:02 +01:00
|
|
|
lv_coord_t btn_h = max_h - ((line_cnt - 1) * inner);
|
2019-04-04 07:15:40 +02:00
|
|
|
btn_h = btn_h / line_cnt;
|
|
|
|
btn_h--; /*-1 because e.g. height = 100 means 101 pixels (0..100)*/
|
2018-06-19 09:49:58 +02:00
|
|
|
|
|
|
|
/* Count the units and the buttons in a line
|
|
|
|
* (A button can be 1,2,3... unit wide)*/
|
2019-04-04 07:15:40 +02:00
|
|
|
uint16_t unit_act_cnt; /*Number of units currently put in a row*/
|
|
|
|
uint16_t i_tot = 0; /*Act. index in the str map*/
|
|
|
|
uint16_t btn_i = 0; /*Act. index of button areas*/
|
|
|
|
const char ** map_p_tmp = map;
|
2018-06-19 09:49:58 +02:00
|
|
|
|
|
|
|
/*Count the units and the buttons in a line*/
|
|
|
|
while(1) {
|
2020-02-25 15:32:35 +01:00
|
|
|
uint16_t unit_cnt = 0; /*Number of units in a row*/
|
|
|
|
uint16_t btn_cnt = 0; /*Number of buttons in a row*/
|
2018-06-19 09:49:58 +02:00
|
|
|
/*Count the buttons in a line*/
|
2019-06-06 06:05:40 +02:00
|
|
|
while(strcmp(map_p_tmp[btn_cnt], "\n") != 0 && strlen(map_p_tmp[btn_cnt]) != 0) { /*Check a line*/
|
2019-02-20 03:29:53 +08:00
|
|
|
unit_cnt += get_button_width(ext->ctrl_bits[btn_i + btn_cnt]);
|
2019-04-04 07:15:40 +02:00
|
|
|
btn_cnt++;
|
2018-06-19 09:49:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-06 15:20:06 +01:00
|
|
|
/*Make sure the last row is at the bottom of 'btnm'*/
|
2019-04-04 07:15:40 +02:00
|
|
|
if(map_p_tmp[btn_cnt][0] == '\0') { /*Last row?*/
|
2020-02-26 19:48:27 +01:00
|
|
|
btn_h = lv_obj_get_height(btnm) - act_y - bottom - 1;
|
2019-01-06 15:20:06 +01:00
|
|
|
}
|
2018-12-30 15:58:14 +01:00
|
|
|
|
2019-10-15 11:04:49 +02:00
|
|
|
lv_bidi_dir_t base_dir = lv_obj_get_base_dir(btnm);
|
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
/*Only deal with the non empty lines*/
|
|
|
|
if(btn_cnt != 0) {
|
|
|
|
/*Calculate the width of all units*/
|
2019-12-22 22:40:02 +01:00
|
|
|
lv_coord_t all_unit_w = max_w - ((btn_cnt - 1) * inner);
|
2018-06-19 09:49:58 +02:00
|
|
|
|
|
|
|
/*Set the button size and positions and set the texts*/
|
|
|
|
uint16_t i;
|
2019-10-15 11:04:49 +02:00
|
|
|
lv_coord_t act_x;
|
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
unit_act_cnt = 0;
|
|
|
|
for(i = 0; i < btn_cnt; i++) {
|
|
|
|
/* one_unit_w = all_unit_w / unit_cnt
|
|
|
|
* act_unit_w = one_unit_w * button_width
|
|
|
|
* do this two operations but the multiply first to divide a greater number */
|
2020-02-25 15:32:35 +01:00
|
|
|
lv_coord_t act_unit_w = (all_unit_w * get_button_width(ext->ctrl_bits[btn_i])) / unit_cnt;
|
2019-04-04 07:15:40 +02:00
|
|
|
act_unit_w--; /*-1 because e.g. width = 100 means 101 pixels (0..100)*/
|
2018-06-19 09:49:58 +02:00
|
|
|
|
|
|
|
/*Always recalculate act_x because of rounding errors */
|
2019-10-15 11:04:49 +02:00
|
|
|
if(base_dir == LV_BIDI_DIR_RTL) {
|
2019-12-22 22:40:02 +01:00
|
|
|
act_x = (unit_act_cnt * all_unit_w) / unit_cnt + i * inner;
|
|
|
|
act_x = lv_obj_get_width(btnm) - right - act_x - act_unit_w - 1;
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-12-22 22:40:02 +01:00
|
|
|
act_x = (unit_act_cnt * all_unit_w) / unit_cnt + i * inner +
|
|
|
|
left;
|
2019-10-15 11:04:49 +02:00
|
|
|
}
|
2018-06-19 09:49:58 +02:00
|
|
|
/* Set the button's area.
|
2019-04-04 07:15:40 +02:00
|
|
|
* If inner padding is zero then use the prev. button x2 as x1 to avoid rounding
|
|
|
|
* errors*/
|
2020-04-03 13:31:11 +02:00
|
|
|
if(btn_i != 0 && inner == 0 && ((act_x != left && base_dir != LV_BIDI_DIR_RTL) ||
|
2020-03-24 10:13:52 +01:00
|
|
|
(act_x + act_unit_w == max_w - right && base_dir == LV_BIDI_DIR_RTL))) {
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_area_set(&ext->button_areas[btn_i], ext->button_areas[btn_i - 1].x2, act_y, act_x + act_unit_w,
|
2019-04-04 07:15:40 +02:00
|
|
|
act_y + btn_h);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_area_set(&ext->button_areas[btn_i], act_x, act_y, act_x + act_unit_w, act_y + btn_h);
|
2018-06-19 09:49:58 +02:00
|
|
|
}
|
2016-12-19 11:57:42 +01:00
|
|
|
|
2019-02-20 03:29:53 +08:00
|
|
|
unit_act_cnt += get_button_width(ext->ctrl_bits[btn_i]);
|
2016-12-19 11:57:42 +01:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
i_tot++;
|
|
|
|
btn_i++;
|
2018-06-19 09:49:58 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 22:40:02 +01:00
|
|
|
act_y += btn_h + inner + 1;
|
2017-12-20 21:27:39 +01:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
if(strlen(map_p_tmp[btn_cnt]) == 0) break; /*Break on end of map*/
|
2019-04-04 07:15:40 +02:00
|
|
|
map_p_tmp = &map_p_tmp[btn_cnt + 1]; /*Set the map to the next line*/
|
|
|
|
i_tot++; /*Skip the '\n'*/
|
2018-06-19 09:49:58 +02:00
|
|
|
}
|
2016-09-30 13:35:54 +02:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_obj_invalidate(btnm);
|
2016-09-30 13:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-02-19 14:56:45 +08:00
|
|
|
/**
|
|
|
|
* Set the button control map (hidden, disabled etc.) for a button matrix. The
|
|
|
|
* control map array will be copied and so may be deallocated after this
|
|
|
|
* function returns.
|
|
|
|
* @param btnm pointer to a button matrix object
|
|
|
|
* @param ctrl_map pointer to an array of `lv_btn_ctrl_t` control bytes. The
|
|
|
|
* length of the array and position of the elements must match
|
2019-03-01 07:25:16 +01:00
|
|
|
* the number and order of the individual buttons (i.e. excludes
|
|
|
|
* newline entries).
|
2019-06-17 16:05:30 +02:00
|
|
|
* An element of the map should look like e.g.:
|
2020-02-14 12:36:44 +01:00
|
|
|
* `ctrl_map[0] = width | LV_BTNMATRIX_CTRL_NO_REPEAT | LV_BTNMATRIX_CTRL_TGL_ENABLE`
|
2019-02-19 14:56:45 +08:00
|
|
|
*/
|
2020-02-15 00:33:26 +01:00
|
|
|
void lv_btnmatrix_set_ctrl_map(lv_obj_t * btnm, const lv_btnmatrix_ctrl_t ctrl_map[])
|
2019-02-19 14:56:45 +08:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2020-05-13 14:11:16 +02:00
|
|
|
_lv_memcpy(ext->ctrl_bits, ctrl_map, sizeof(lv_btnmatrix_ctrl_t) * ext->btn_cnt);
|
2019-02-19 14:56:45 +08:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_set_map(btnm, ext->map_p);
|
2019-02-19 14:56:45 +08:00
|
|
|
}
|
|
|
|
|
2016-09-30 13:35:54 +02:00
|
|
|
/**
|
2020-03-10 22:11:17 +01:00
|
|
|
* Set the focused button i.e. visually highlight it.
|
2019-02-28 09:27:25 +01:00
|
|
|
* @param btnm pointer to button matrix object
|
2020-03-10 22:11:17 +01:00
|
|
|
* @param id index of the button to focus(`LV_BTNMATRIX_BTN_NONE` to remove focus)
|
2019-02-28 09:27:25 +01:00
|
|
|
*/
|
2020-03-10 22:11:17 +01:00
|
|
|
void lv_btnmatrix_set_focused_btn(lv_obj_t * btnm, uint16_t id)
|
2019-02-28 09:27:25 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2019-02-28 09:27:25 +01:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
if(id >= ext->btn_cnt && id != LV_BTNMATRIX_BTN_NONE) return;
|
2019-02-28 09:27:25 +01:00
|
|
|
|
2020-03-10 22:11:17 +01:00
|
|
|
if(id == ext->btn_id_focused) return;
|
2019-02-28 09:27:25 +01:00
|
|
|
|
2020-03-10 22:11:17 +01:00
|
|
|
ext->btn_id_focused = id;
|
2019-02-28 09:27:25 +01:00
|
|
|
lv_obj_invalidate(btnm);
|
|
|
|
}
|
|
|
|
|
2016-09-30 13:35:54 +02:00
|
|
|
|
2019-03-11 06:18:44 +01:00
|
|
|
/**
|
|
|
|
* Enable recoloring of button's texts
|
|
|
|
* @param btnm pointer to button matrix object
|
|
|
|
* @param en true: enable recoloring; false: disable
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
void lv_btnmatrix_set_recolor(const lv_obj_t * btnm, bool en)
|
2018-12-15 21:44:44 -05:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2018-12-15 21:44:44 -05:00
|
|
|
|
|
|
|
ext->recolor = en;
|
|
|
|
lv_obj_invalidate(btnm);
|
|
|
|
}
|
|
|
|
|
2019-02-12 16:55:30 +08:00
|
|
|
/**
|
2019-03-19 06:30:05 +01:00
|
|
|
* Set the attributes of a button of the button matrix
|
2019-02-12 16:55:30 +08:00
|
|
|
* @param btnm pointer to button matrix object
|
2019-03-19 06:30:05 +01:00
|
|
|
* @param btn_id 0 based index of the button to modify. (Not counting new lines)
|
2019-02-12 16:55:30 +08:00
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
void lv_btnmatrix_set_btn_ctrl(const lv_obj_t * btnm, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl)
|
2019-06-17 16:05:30 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2019-06-17 16:05:30 +02:00
|
|
|
|
|
|
|
if(btn_id >= ext->btn_cnt) return;
|
|
|
|
|
|
|
|
ext->ctrl_bits[btn_id] |= ctrl;
|
|
|
|
invalidate_button_area(btnm, btn_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the attributes of a button of the button matrix
|
|
|
|
* @param btnm pointer to button matrix object
|
|
|
|
* @param btn_id 0 based index of the button to modify. (Not counting new lines)
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
void lv_btnmatrix_clear_btn_ctrl(const lv_obj_t * btnm, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl)
|
2019-02-12 16:55:30 +08:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2019-02-12 16:55:30 +08:00
|
|
|
|
2019-03-19 06:30:05 +01:00
|
|
|
if(btn_id >= ext->btn_cnt) return;
|
2019-03-11 06:18:44 +01:00
|
|
|
|
2019-06-17 16:05:30 +02:00
|
|
|
ext->ctrl_bits[btn_id] &= (~ctrl);
|
2019-03-11 06:18:44 +01:00
|
|
|
invalidate_button_area(btnm, btn_id);
|
2019-03-12 06:20:45 +01:00
|
|
|
}
|
2019-02-12 16:55:30 +08:00
|
|
|
|
2019-03-12 06:20:45 +01:00
|
|
|
/**
|
2019-03-19 06:30:05 +01:00
|
|
|
* Set the attributes of all buttons of a button matrix
|
|
|
|
* @param btnm pointer to a button matrix object
|
2020-02-14 12:36:44 +01:00
|
|
|
* @param ctrl attribute(s) to set from `lv_btnmatrix_ctrl_t`. Values can be ORed.
|
2019-06-17 16:05:30 +02:00
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
void lv_btnmatrix_set_btn_ctrl_all(lv_obj_t * btnm, lv_btnmatrix_ctrl_t ctrl)
|
2019-06-17 16:05:30 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2019-06-17 16:05:30 +02:00
|
|
|
uint16_t i;
|
|
|
|
for(i = 0; i < ext->btn_cnt; i++) {
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_set_btn_ctrl(btnm, i, ctrl);
|
2019-06-17 16:05:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the attributes of all buttons of a button matrix
|
|
|
|
* @param btnm pointer to a button matrix object
|
2020-02-14 12:36:44 +01:00
|
|
|
* @param ctrl attribute(s) to set from `lv_btnmatrix_ctrl_t`. Values can be ORed.
|
2019-03-19 06:30:05 +01:00
|
|
|
* @param en true: set the attributes; false: clear the attributes
|
2019-02-12 16:55:30 +08:00
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
void lv_btnmatrix_clear_btn_ctrl_all(lv_obj_t * btnm, lv_btnmatrix_ctrl_t ctrl)
|
2019-02-12 16:55:30 +08:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2019-03-19 06:30:05 +01:00
|
|
|
uint16_t i;
|
2019-04-04 07:15:40 +02:00
|
|
|
for(i = 0; i < ext->btn_cnt; i++) {
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_clear_btn_ctrl(btnm, i, ctrl);
|
2019-03-19 06:30:05 +01:00
|
|
|
}
|
2019-02-12 16:55:30 +08:00
|
|
|
}
|
2019-06-17 16:05:30 +02:00
|
|
|
|
2019-02-12 16:55:30 +08:00
|
|
|
/**
|
|
|
|
* Set a single buttons relative width.
|
|
|
|
* This method will cause the matrix be regenerated and is a relatively
|
|
|
|
* expensive operation. It is recommended that initial width be specified using
|
2020-02-14 12:36:44 +01:00
|
|
|
* `lv_btnmatrix_set_ctrl_map` and this method only be used for dynamic changes.
|
2019-02-12 16:55:30 +08:00
|
|
|
* @param btnm pointer to button matrix object
|
2019-03-11 06:18:44 +01:00
|
|
|
* @param btn_id 0 based index of the button to modify.
|
2019-02-12 16:55:30 +08:00
|
|
|
* @param width Relative width compared to the buttons in the same row. [1..7]
|
|
|
|
*/
|
2020-02-14 22:04:00 +01:00
|
|
|
void lv_btnmatrix_set_btn_width(lv_obj_t * btnm, uint16_t btn_id, uint8_t width)
|
2019-03-11 06:18:44 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2019-02-12 16:55:30 +08:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2019-04-04 07:15:40 +02:00
|
|
|
if(btn_id >= ext->btn_cnt) return;
|
2020-02-14 12:36:44 +01:00
|
|
|
ext->ctrl_bits[btn_id] &= (~LV_BTNMATRIX_WIDTH_MASK);
|
|
|
|
ext->ctrl_bits[btn_id] |= (LV_BTNMATRIX_WIDTH_MASK & width);
|
2019-02-12 16:55:30 +08:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_set_map(btnm, ext->map_p);
|
2019-02-12 16:55:30 +08:00
|
|
|
}
|
|
|
|
|
2019-03-20 18:33:33 -04:00
|
|
|
/**
|
|
|
|
* Make the button matrix like a selector widget (only one button may be toggled at a time).
|
2020-03-04 16:29:21 +01:00
|
|
|
* `Checkable` must be enabled on the buttons you want to be selected with `lv_btnmatrix_set_ctrl` or
|
2020-02-14 12:36:44 +01:00
|
|
|
* `lv_btnmatrix_set_btn_ctrl_all`.
|
2019-03-20 18:33:33 -04:00
|
|
|
* @param btnm Button matrix object
|
2020-03-04 16:29:21 +01:00
|
|
|
* @param one_chk Whether "one check" mode is enabled
|
2019-03-20 18:33:33 -04:00
|
|
|
*/
|
2020-03-04 16:29:21 +01:00
|
|
|
void lv_btnmatrix_set_one_check(lv_obj_t * btnm, bool one_chk)
|
2019-04-04 07:15:40 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2020-03-04 16:29:21 +01:00
|
|
|
ext->one_check = one_chk;
|
2019-03-20 18:33:33 -04:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
/*If more than one button is toggled only the first one should be*/
|
|
|
|
make_one_button_toggled(btnm, 0);
|
2019-03-20 18:33:33 -04:00
|
|
|
}
|
|
|
|
|
2016-09-30 13:35:54 +02:00
|
|
|
/*=====================
|
|
|
|
* Getter functions
|
|
|
|
*====================*/
|
|
|
|
|
2016-10-04 15:29:52 +02:00
|
|
|
/**
|
|
|
|
* Get the current map of a button matrix
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param btnm pointer to a button matrix object
|
2016-10-04 15:29:52 +02:00
|
|
|
* @return the current map
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
const char ** lv_btnmatrix_get_map_array(const lv_obj_t * btnm)
|
2016-10-04 15:29:52 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2018-06-19 09:49:58 +02:00
|
|
|
return ext->map_p;
|
2016-10-04 15:29:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-11 06:18:44 +01:00
|
|
|
* Check whether the button's text can use recolor or not
|
|
|
|
* @param btnm pointer to button matrix object
|
|
|
|
* @return true: text recolor enable; false: disabled
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
bool lv_btnmatrix_get_recolor(const lv_obj_t * btnm)
|
2019-03-11 06:18:44 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2019-03-11 06:18:44 +01:00
|
|
|
|
|
|
|
return ext->recolor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the index of the lastly "activated" button by the user (pressed, released etc)
|
|
|
|
* Useful in the the `event_cb` to get the text of the button, check if hidden etc.
|
|
|
|
* @param btnm pointer to button matrix object
|
2020-02-14 12:36:44 +01:00
|
|
|
* @return index of the last released button (LV_BTNMATRIX_BTN_NONE: if unset)
|
2016-10-04 15:29:52 +02:00
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
uint16_t lv_btnmatrix_get_active_btn(const lv_obj_t * btnm)
|
2016-10-04 15:29:52 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2019-03-12 06:20:45 +01:00
|
|
|
return ext->btn_id_act;
|
2016-10-04 15:29:52 +02:00
|
|
|
}
|
|
|
|
|
2019-03-12 06:20:45 +01:00
|
|
|
/**
|
|
|
|
* Get the text of the lastly "activated" button by the user (pressed, released etc)
|
|
|
|
* Useful in the the `event_cb`
|
|
|
|
* @param btnm pointer to button matrix object
|
|
|
|
* @return text of the last released button (NULL: if unset)
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
const char * lv_btnmatrix_get_active_btn_text(const lv_obj_t * btnm)
|
2019-03-12 06:20:45 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
|
|
|
if(ext->btn_id_act != LV_BTNMATRIX_BTN_NONE) {
|
|
|
|
return lv_btnmatrix_get_btn_text(btnm, ext->btn_id_act);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-03-12 06:20:45 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 07:00:17 +01:00
|
|
|
/**
|
2019-03-11 06:18:44 +01:00
|
|
|
* Get the pressed button's index.
|
2020-02-14 12:36:44 +01:00
|
|
|
* The button be really pressed by the user or manually set to pressed with `lv_btnmatrix_set_pressed`
|
2018-11-19 07:00:17 +01:00
|
|
|
* @param btnm pointer to button matrix object
|
2020-02-14 12:36:44 +01:00
|
|
|
* @return index of the pressed button (LV_BTNMATRIX_BTN_NONE: if unset)
|
2018-11-19 07:00:17 +01:00
|
|
|
*/
|
2020-03-10 22:11:17 +01:00
|
|
|
uint16_t lv_btnmatrix_get_focused_btn(const lv_obj_t * btnm)
|
2018-11-19 07:00:17 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2020-04-23 15:27:51 +02:00
|
|
|
return ext->btn_id_focused;
|
2018-11-19 07:00:17 +01:00
|
|
|
}
|
|
|
|
|
2016-09-30 13:35:54 +02:00
|
|
|
/**
|
2019-03-11 06:18:44 +01:00
|
|
|
* Get the button's text
|
|
|
|
* @param btnm pointer to button matrix object
|
2019-04-04 07:15:40 +02:00
|
|
|
* @param btn_id the index a button not counting new line characters. (The return value of
|
2020-02-14 12:36:44 +01:00
|
|
|
* lv_btnmatrix_get_pressed/released)
|
2019-03-11 06:18:44 +01:00
|
|
|
* @return text of btn_index` button
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
const char * lv_btnmatrix_get_btn_text(const lv_obj_t * btnm, uint16_t btn_id)
|
2019-03-11 06:18:44 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2019-04-04 07:15:40 +02:00
|
|
|
if(btn_id > ext->btn_cnt) return NULL;
|
2019-03-11 06:18:44 +01:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
uint16_t txt_i = 0;
|
|
|
|
uint16_t btn_i = 0;
|
2019-03-11 06:18:44 +01:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
/* Search the text of ext->btn_pr the buttons text in the map
|
|
|
|
* Skip "\n"-s*/
|
|
|
|
while(btn_i != btn_id) {
|
|
|
|
btn_i++;
|
|
|
|
txt_i++;
|
|
|
|
if(strcmp(ext->map_p[txt_i], "\n") == 0) txt_i++;
|
|
|
|
}
|
2019-03-11 06:18:44 +01:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
if(btn_i == ext->btn_cnt) return NULL;
|
2019-03-11 06:18:44 +01:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
return ext->map_p[txt_i];
|
2019-03-11 06:18:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-19 06:30:05 +01:00
|
|
|
* Get the whether a control value is enabled or disabled for button of a button matrix
|
2019-03-11 06:18:44 +01:00
|
|
|
* @param btnm pointer to a button matrix object
|
2019-04-04 07:15:40 +02:00
|
|
|
* @param btn_id the index a button not counting new line characters. (E.g. the return value of
|
2020-02-14 12:36:44 +01:00
|
|
|
* lv_btnmatrix_get_pressed/released)
|
2019-03-19 06:30:05 +01:00
|
|
|
* @param ctrl control values to check (ORed value can be used)
|
2019-03-11 06:18:44 +01:00
|
|
|
* @return true: long press repeat is disabled; false: long press repeat enabled
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
bool lv_btnmatrix_get_btn_ctrl(lv_obj_t * btnm, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl)
|
2019-03-11 06:18:44 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2019-03-19 06:30:05 +01:00
|
|
|
if(btn_id >= ext->btn_cnt) return false;
|
2019-03-11 06:18:44 +01:00
|
|
|
|
2020-02-25 15:32:35 +01:00
|
|
|
return (ext->ctrl_bits[btn_id] & ctrl) ? true : false;
|
2018-06-19 09:49:58 +02:00
|
|
|
}
|
2017-11-15 15:50:33 +01:00
|
|
|
|
2019-12-22 22:40:02 +01:00
|
|
|
|
2019-03-20 18:33:33 -04:00
|
|
|
/**
|
2020-03-04 16:29:21 +01:00
|
|
|
* Find whether "one check" mode is enabled.
|
2019-03-20 18:33:33 -04:00
|
|
|
* @param btnm Button matrix object
|
2020-03-04 16:29:21 +01:00
|
|
|
* @return whether "one check" mode is enabled
|
2019-03-20 18:33:33 -04:00
|
|
|
*/
|
2020-03-04 16:29:21 +01:00
|
|
|
bool lv_btnmatrix_get_one_check(const lv_obj_t * btnm)
|
2019-04-04 07:15:40 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2019-03-20 18:33:33 -04:00
|
|
|
|
2020-03-04 16:29:21 +01:00
|
|
|
return ext->one_check;
|
2019-03-20 18:33:33 -04:00
|
|
|
}
|
|
|
|
|
2016-09-30 13:35:54 +02:00
|
|
|
/**********************
|
|
|
|
* STATIC FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the drawing related tasks of the button matrixs
|
2016-12-19 11:57:42 +01:00
|
|
|
* @param btnm pointer to a button matrix object
|
2019-09-06 19:53:39 +02:00
|
|
|
* @param clip_area the object will be drawn only in this area
|
2016-09-30 13:35:54 +02:00
|
|
|
* @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')
|
2016-10-04 09:45:39 +02:00
|
|
|
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
|
2019-09-06 19:53:39 +02:00
|
|
|
* @param return an element of `lv_design_res_t`
|
2016-09-30 13:35:54 +02:00
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
static lv_design_res_t lv_btnmatrix_design(lv_obj_t * btnm, const lv_area_t * clip_area, lv_design_mode_t mode)
|
2016-09-30 13:35:54 +02:00
|
|
|
{
|
|
|
|
if(mode == LV_DESIGN_COVER_CHK) {
|
2019-09-06 19:53:39 +02:00
|
|
|
return ancestor_design_f(btnm, clip_area, mode);
|
2016-09-30 13:35:54 +02:00
|
|
|
}
|
|
|
|
/*Draw the object*/
|
2018-06-19 09:49:58 +02:00
|
|
|
else if(mode == LV_DESIGN_DRAW_MAIN) {
|
2019-09-06 19:53:39 +02:00
|
|
|
ancestor_design_f(btnm, clip_area, mode);
|
2016-12-19 11:57:42 +01:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2020-02-09 10:48:34 +01:00
|
|
|
if(ext->btn_cnt == 0) return LV_DESIGN_RES_OK;
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_area_t area_btnm;
|
2017-11-21 10:35:57 +01:00
|
|
|
lv_obj_get_coords(btnm, &area_btnm);
|
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_area_t area_tmp;
|
|
|
|
lv_coord_t btn_w;
|
|
|
|
lv_coord_t btn_h;
|
2016-12-19 11:57:42 +01:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
uint16_t btn_i = 0;
|
|
|
|
uint16_t txt_i = 0;
|
2018-12-15 21:44:44 -05:00
|
|
|
lv_txt_flag_t txt_flag = LV_TXT_FLAG_NONE;
|
2020-02-09 10:48:34 +01:00
|
|
|
if(ext->recolor) txt_flag = LV_TXT_FLAG_RECOLOR;
|
2018-12-15 21:44:44 -05:00
|
|
|
|
2019-12-31 11:13:09 +01:00
|
|
|
lv_draw_rect_dsc_t draw_rect_rel_dsc;
|
|
|
|
lv_draw_label_dsc_t draw_label_rel_dsc;
|
|
|
|
|
|
|
|
lv_draw_rect_dsc_t draw_rect_chk_dsc;
|
|
|
|
lv_draw_label_dsc_t draw_label_chk_dsc;
|
|
|
|
|
|
|
|
lv_draw_rect_dsc_t draw_rect_ina_dsc;
|
|
|
|
lv_draw_label_dsc_t draw_label_ina_dsc;
|
|
|
|
|
|
|
|
lv_draw_rect_dsc_t draw_rect_tmp_dsc;
|
|
|
|
lv_draw_label_dsc_t draw_label_tmp_dsc;
|
|
|
|
|
2020-01-06 22:14:04 +01:00
|
|
|
/*The state changes without re-caching the styles, disable the use of cache*/
|
2020-02-21 16:56:05 +01:00
|
|
|
lv_state_t state_ori = btnm->state;
|
2020-02-22 01:09:29 +01:00
|
|
|
btnm->state = LV_STATE_DEFAULT;
|
2019-12-31 11:13:09 +01:00
|
|
|
lv_draw_rect_dsc_init(&draw_rect_rel_dsc);
|
|
|
|
lv_draw_label_dsc_init(&draw_label_rel_dsc);
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_obj_init_draw_rect_dsc(btnm, LV_BTNMATRIX_PART_BTN, &draw_rect_rel_dsc);
|
|
|
|
lv_obj_init_draw_label_dsc(btnm, LV_BTNMATRIX_PART_BTN, &draw_label_rel_dsc);
|
2020-02-09 10:48:34 +01:00
|
|
|
draw_label_rel_dsc.flag = txt_flag;
|
2020-02-21 16:56:05 +01:00
|
|
|
btnm->state = state_ori;
|
2019-12-22 22:40:02 +01:00
|
|
|
|
2020-02-09 10:48:34 +01:00
|
|
|
bool chk_inited = false;
|
|
|
|
bool disabled_inited = false;
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_style_int_t padding_top = lv_obj_get_style_pad_top(btnm, LV_BTNMATRIX_PART_BG);
|
|
|
|
lv_style_int_t padding_bottom = lv_obj_get_style_pad_bottom(btnm, LV_BTNMATRIX_PART_BG);
|
2019-12-22 22:40:02 +01:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
for(btn_i = 0; btn_i < ext->btn_cnt; btn_i++, txt_i++) {
|
2017-08-16 12:36:22 +02:00
|
|
|
/*Search the next valid text in the map*/
|
2018-12-26 08:24:18 +01:00
|
|
|
while(strcmp(ext->map_p[txt_i], "\n") == 0) {
|
2019-04-04 07:15:40 +02:00
|
|
|
txt_i++;
|
2018-12-26 08:24:18 +01:00
|
|
|
}
|
2017-08-16 12:36:22 +02:00
|
|
|
|
2017-10-12 16:59:51 +02:00
|
|
|
/*Skip hidden buttons*/
|
2019-02-20 03:29:53 +08:00
|
|
|
if(button_is_hidden(ext->ctrl_bits[btn_i])) continue;
|
2017-08-16 12:36:22 +02:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_area_copy(&area_tmp, &ext->button_areas[btn_i]);
|
|
|
|
area_tmp.x1 += area_btnm.x1;
|
|
|
|
area_tmp.y1 += area_btnm.y1;
|
|
|
|
area_tmp.x2 += area_btnm.x1;
|
|
|
|
area_tmp.y2 += area_btnm.y1;
|
2016-12-19 11:57:42 +01:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
btn_w = lv_area_get_width(&area_tmp);
|
|
|
|
btn_h = lv_area_get_height(&area_tmp);
|
2016-12-19 11:57:42 +01:00
|
|
|
|
2019-12-31 11:13:09 +01:00
|
|
|
/*Choose the style*/
|
|
|
|
lv_draw_rect_dsc_t * draw_rect_dsc_act;
|
|
|
|
lv_draw_label_dsc_t * draw_label_dsc_act;
|
2019-03-19 06:30:05 +01:00
|
|
|
bool tgl_state = button_get_tgl_state(ext->ctrl_bits[btn_i]);
|
2019-12-31 11:13:09 +01:00
|
|
|
|
2020-02-09 10:48:34 +01:00
|
|
|
if(tgl_state) {
|
2020-02-26 19:48:27 +01:00
|
|
|
if(!chk_inited) {
|
|
|
|
btnm->state = LV_STATE_CHECKED;
|
|
|
|
lv_draw_rect_dsc_init(&draw_rect_chk_dsc);
|
|
|
|
lv_draw_label_dsc_init(&draw_label_chk_dsc);
|
|
|
|
lv_obj_init_draw_rect_dsc(btnm, LV_BTNMATRIX_PART_BTN, &draw_rect_chk_dsc);
|
|
|
|
lv_obj_init_draw_label_dsc(btnm, LV_BTNMATRIX_PART_BTN, &draw_label_chk_dsc);
|
|
|
|
draw_label_chk_dsc.flag = txt_flag;
|
|
|
|
btnm->state = state_ori;
|
|
|
|
chk_inited = true;
|
|
|
|
}
|
2020-02-09 10:48:34 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 11:13:09 +01:00
|
|
|
if(button_is_inactive(ext->ctrl_bits[btn_i])) {
|
2020-02-26 19:48:27 +01:00
|
|
|
if(!disabled_inited) {
|
|
|
|
btnm->state = LV_STATE_DISABLED;
|
|
|
|
lv_draw_rect_dsc_init(&draw_rect_ina_dsc);
|
|
|
|
lv_draw_label_dsc_init(&draw_label_ina_dsc);
|
|
|
|
lv_obj_init_draw_rect_dsc(btnm, LV_BTNMATRIX_PART_BTN, &draw_rect_ina_dsc);
|
|
|
|
lv_obj_init_draw_label_dsc(btnm, LV_BTNMATRIX_PART_BTN, &draw_label_ina_dsc);
|
|
|
|
draw_label_ina_dsc.flag = txt_flag;
|
|
|
|
btnm->state = state_ori;
|
|
|
|
disabled_inited = true;
|
|
|
|
}
|
2020-01-20 16:11:38 +01:00
|
|
|
draw_rect_dsc_act = &draw_rect_ina_dsc;
|
|
|
|
draw_label_dsc_act = &draw_label_ina_dsc;
|
2019-12-31 11:13:09 +01:00
|
|
|
}
|
|
|
|
/*Simple released or checked buttons button*/
|
|
|
|
else if(btn_i != ext->btn_id_pr && btn_i != ext->btn_id_focused) {
|
|
|
|
draw_rect_dsc_act = tgl_state ? &draw_rect_chk_dsc : &draw_rect_rel_dsc;
|
|
|
|
draw_label_dsc_act = tgl_state ? &draw_label_chk_dsc : &draw_label_rel_dsc;
|
|
|
|
}
|
|
|
|
/*Focused and/or pressed + checked or released button*/
|
|
|
|
else {
|
2020-02-22 01:09:29 +01:00
|
|
|
btnm->state = LV_STATE_DEFAULT;
|
2020-02-21 16:56:05 +01:00
|
|
|
if(tgl_state) btnm->state = LV_STATE_CHECKED;
|
|
|
|
if(ext->btn_id_pr == btn_i) btnm->state |= LV_STATE_PRESSED;
|
2020-04-23 09:50:50 +02:00
|
|
|
if(ext->btn_id_focused == btn_i) {
|
|
|
|
btnm->state |= LV_STATE_FOCUSED;
|
|
|
|
if(state_ori & LV_STATE_EDITED) btnm->state |= LV_STATE_EDITED;
|
|
|
|
}
|
2020-01-28 22:15:45 +01:00
|
|
|
|
2019-12-31 11:13:09 +01:00
|
|
|
lv_draw_rect_dsc_init(&draw_rect_tmp_dsc);
|
|
|
|
lv_draw_label_dsc_init(&draw_label_tmp_dsc);
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_obj_init_draw_rect_dsc(btnm, LV_BTNMATRIX_PART_BTN, &draw_rect_tmp_dsc);
|
|
|
|
lv_obj_init_draw_label_dsc(btnm, LV_BTNMATRIX_PART_BTN, &draw_label_tmp_dsc);
|
2020-02-09 10:48:34 +01:00
|
|
|
draw_label_tmp_dsc.flag = txt_flag;
|
2019-12-31 11:13:09 +01:00
|
|
|
draw_rect_dsc_act = &draw_rect_tmp_dsc;
|
|
|
|
draw_label_dsc_act = &draw_label_tmp_dsc;
|
2020-01-28 22:15:45 +01:00
|
|
|
|
2020-02-21 16:56:05 +01:00
|
|
|
btnm->state = state_ori;
|
2019-12-31 11:13:09 +01:00
|
|
|
}
|
|
|
|
|
2020-01-20 16:11:38 +01:00
|
|
|
lv_style_int_t border_part_ori = draw_rect_dsc_act->border_side;
|
2018-12-26 08:24:18 +01:00
|
|
|
|
|
|
|
/*Remove borders on the edges if `LV_BORDER_INTERNAL`*/
|
2019-12-31 06:10:50 +01:00
|
|
|
if(border_part_ori & LV_BORDER_SIDE_INTERNAL) {
|
2019-12-22 22:40:02 +01:00
|
|
|
/*Top/Bottom lines*/
|
|
|
|
if(area_tmp.y1 == btnm->coords.y1 + padding_top) {
|
2020-01-20 16:11:38 +01:00
|
|
|
draw_rect_dsc_act->border_side &= ~LV_BORDER_SIDE_TOP;
|
2018-12-26 08:24:18 +01:00
|
|
|
}
|
2019-12-22 22:40:02 +01:00
|
|
|
if(area_tmp.y2 == btnm->coords.y2 - padding_bottom) {
|
2020-01-20 16:11:38 +01:00
|
|
|
draw_rect_dsc_act->border_side &= ~LV_BORDER_SIDE_BOTTOM;
|
2018-12-26 08:24:18 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:40:02 +01:00
|
|
|
/*Left/right columns*/
|
|
|
|
if(txt_i == 0) { /*First button*/
|
2020-01-20 16:11:38 +01:00
|
|
|
draw_rect_dsc_act->border_side &= ~LV_BORDER_SIDE_LEFT;
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(strcmp(ext->map_p[txt_i - 1], "\n") == 0) {
|
2020-01-20 16:11:38 +01:00
|
|
|
draw_rect_dsc_act->border_side &= ~LV_BORDER_SIDE_LEFT;
|
2018-12-26 08:24:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(ext->map_p[txt_i + 1][0] == '\0' || strcmp(ext->map_p[txt_i + 1], "\n") == 0) {
|
2020-01-20 16:11:38 +01:00
|
|
|
draw_rect_dsc_act->border_side &= ~LV_BORDER_SIDE_RIGHT;
|
2018-12-26 08:24:18 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 22:40:02 +01:00
|
|
|
|
2019-12-31 11:13:09 +01:00
|
|
|
lv_draw_rect(&area_tmp, clip_area, draw_rect_dsc_act);
|
2017-11-21 10:35:57 +01:00
|
|
|
|
2020-01-20 16:11:38 +01:00
|
|
|
draw_rect_dsc_act->border_side = border_part_ori;
|
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
/*Calculate the size of the text*/
|
2019-12-31 11:13:09 +01:00
|
|
|
const lv_font_t * font = draw_label_dsc_act->font;
|
2020-01-06 22:14:04 +01:00
|
|
|
lv_style_int_t letter_space = draw_label_dsc_act->letter_space;
|
|
|
|
lv_style_int_t line_space = draw_label_dsc_act->line_space;
|
2019-12-22 22:40:02 +01:00
|
|
|
const char * txt = ext->map_p[txt_i];
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_point_t txt_size;
|
2020-05-13 14:11:16 +02:00
|
|
|
_lv_txt_get_size(&txt_size, txt, font, letter_space,
|
2020-05-18 16:57:23 +02:00
|
|
|
line_space, lv_area_get_width(&area_btnm), txt_flag);
|
2016-12-19 11:57:42 +01:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
area_tmp.x1 += (btn_w - txt_size.x) / 2;
|
|
|
|
area_tmp.y1 += (btn_h - txt_size.y) / 2;
|
|
|
|
area_tmp.x2 = area_tmp.x1 + txt_size.x;
|
|
|
|
area_tmp.y2 = area_tmp.y1 + txt_size.y;
|
2016-12-19 11:57:42 +01:00
|
|
|
|
2019-12-31 11:13:09 +01:00
|
|
|
lv_draw_label(&area_tmp, clip_area, draw_label_dsc_act, txt, NULL);
|
2018-06-19 09:49:58 +02:00
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(mode == LV_DESIGN_DRAW_POST) {
|
2020-01-20 16:11:38 +01:00
|
|
|
ancestor_design_f(btnm, clip_area, mode);
|
2016-12-19 11:57:42 +01:00
|
|
|
}
|
2019-09-06 19:53:39 +02:00
|
|
|
return LV_DESIGN_RES_OK;
|
2016-09-30 13:35:54 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 15:01:40 +01:00
|
|
|
/**
|
|
|
|
* Signal function of the button matrix
|
|
|
|
* @param btnm pointer to a button matrix 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
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * param)
|
2017-11-10 15:01:40 +01:00
|
|
|
{
|
|
|
|
lv_res_t res;
|
2019-12-22 22:40:02 +01:00
|
|
|
if(sign == LV_SIGNAL_GET_STYLE) {
|
2020-01-07 23:43:57 +01:00
|
|
|
lv_get_style_info_t * info = param;
|
2020-02-14 12:36:44 +01:00
|
|
|
info->result = lv_btnmatrix_get_style(btnm, info->part);
|
2020-01-07 23:43:57 +01:00
|
|
|
if(info->result != NULL) return LV_RES_OK;
|
|
|
|
else return ancestor_signal(btnm, sign, param);
|
2019-12-22 22:40:02 +01:00
|
|
|
}
|
2017-11-10 15:01:40 +01:00
|
|
|
|
|
|
|
/* Include the ancient signal function */
|
|
|
|
res = ancestor_signal(btnm, sign, param);
|
|
|
|
if(res != LV_RES_OK) return res;
|
2019-09-26 12:54:40 +02:00
|
|
|
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
|
2017-11-10 15:01:40 +01:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2017-11-23 21:28:36 +01:00
|
|
|
lv_point_t p;
|
2017-11-10 15:01:40 +01:00
|
|
|
if(sign == LV_SIGNAL_CLEANUP) {
|
2020-02-21 16:33:42 +01:00
|
|
|
lv_obj_clean_style_list(btnm, LV_BTNMATRIX_PART_BTN);
|
2017-11-23 21:28:36 +01:00
|
|
|
lv_mem_free(ext->button_areas);
|
2019-02-12 16:55:30 +08:00
|
|
|
lv_mem_free(ext->ctrl_bits);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_STYLE_CHG) {
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_set_map(btnm, ext->map_p);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_COORD_CHG) {
|
2020-01-07 23:43:57 +01:00
|
|
|
if(lv_obj_get_width(btnm) != lv_area_get_width(param) || lv_obj_get_height(btnm) != lv_area_get_height(param)) {
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_set_map(btnm, ext->map_p);
|
2020-01-07 23:43:57 +01:00
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_PRESSED) {
|
2020-02-04 02:09:15 +01:00
|
|
|
invalidate_button_area(btnm, ext->btn_id_pr);
|
|
|
|
|
|
|
|
lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
|
|
|
|
if(indev_type == LV_INDEV_TYPE_POINTER || indev_type == LV_INDEV_TYPE_BUTTON) {
|
2019-03-12 14:29:37 +01:00
|
|
|
uint16_t btn_pr;
|
|
|
|
/*Search the pressed area*/
|
|
|
|
lv_indev_get_point(param, &p);
|
|
|
|
btn_pr = get_button_from_point(btnm, &p);
|
2020-02-15 15:13:57 -05:00
|
|
|
/*Handle the case where there is no button there*/
|
|
|
|
if(btn_pr != LV_BTNMATRIX_BTN_NONE) {
|
|
|
|
if(button_is_inactive(ext->ctrl_bits[btn_pr]) == false &&
|
2020-02-26 19:48:27 +01:00
|
|
|
button_is_hidden(ext->ctrl_bits[btn_pr]) == false) {
|
2020-02-15 15:13:57 -05:00
|
|
|
invalidate_button_area(btnm, ext->btn_id_pr) /*Invalidate the old area*/;
|
|
|
|
ext->btn_id_pr = btn_pr;
|
|
|
|
ext->btn_id_act = btn_pr;
|
|
|
|
invalidate_button_area(btnm, ext->btn_id_pr); /*Invalidate the new area*/
|
|
|
|
}
|
2020-02-05 03:29:16 +01:00
|
|
|
}
|
2020-02-15 02:19:44 +01:00
|
|
|
}
|
|
|
|
#if LV_USE_GROUP
|
2020-02-26 19:48:27 +01:00
|
|
|
else if(indev_type == LV_INDEV_TYPE_KEYPAD || (indev_type == LV_INDEV_TYPE_ENCODER &&
|
|
|
|
lv_group_get_editing(lv_obj_get_group(btnm)))) {
|
2020-02-04 02:09:15 +01:00
|
|
|
ext->btn_id_pr = ext->btn_id_focused;
|
|
|
|
invalidate_button_area(btnm, ext->btn_id_focused);
|
2019-03-12 14:29:37 +01:00
|
|
|
}
|
2020-02-15 02:19:44 +01:00
|
|
|
#endif
|
2020-02-04 02:09:15 +01:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
if(ext->btn_id_act != LV_BTNMATRIX_BTN_NONE) {
|
2019-03-19 06:30:05 +01:00
|
|
|
if(button_is_click_trig(ext->ctrl_bits[ext->btn_id_act]) == false &&
|
2019-04-04 07:15:40 +02:00
|
|
|
button_is_inactive(ext->ctrl_bits[ext->btn_id_act]) == false &&
|
|
|
|
button_is_hidden(ext->ctrl_bits[ext->btn_id_act]) == false) {
|
2019-03-20 06:08:01 +01:00
|
|
|
uint32_t b = ext->btn_id_act;
|
2019-06-14 06:56:54 +02:00
|
|
|
res = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b);
|
2019-03-19 06:30:05 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_PRESSING) {
|
2020-03-10 20:55:32 +01:00
|
|
|
uint16_t btn_pr = LV_BTNMATRIX_BTN_NONE;
|
2017-11-10 15:01:40 +01:00
|
|
|
/*Search the pressed area*/
|
2020-03-10 20:55:32 +01:00
|
|
|
lv_indev_t * indev = lv_indev_get_act();
|
|
|
|
lv_indev_type_t indev_type = lv_indev_get_type(indev);
|
|
|
|
if(indev_type == LV_INDEV_TYPE_ENCODER || indev_type == LV_INDEV_TYPE_KEYPAD) return LV_RES_OK;
|
|
|
|
|
|
|
|
lv_indev_get_point(indev, &p);
|
2017-11-10 15:01:40 +01:00
|
|
|
btn_pr = get_button_from_point(btnm, &p);
|
2020-02-15 15:13:57 -05:00
|
|
|
/*Invalidate to old and the new areas*/
|
|
|
|
if(btn_pr != ext->btn_id_pr) {
|
2020-02-14 12:36:44 +01:00
|
|
|
if(ext->btn_id_pr != LV_BTNMATRIX_BTN_NONE) {
|
2019-02-12 16:55:30 +08:00
|
|
|
invalidate_button_area(btnm, ext->btn_id_pr);
|
2017-11-10 15:01:40 +01:00
|
|
|
}
|
2020-02-15 15:13:57 -05:00
|
|
|
lv_indev_reset_long_press(param); /*Start the log press time again on the new button*/
|
|
|
|
if(btn_pr != LV_BTNMATRIX_BTN_NONE &&
|
|
|
|
button_is_inactive(ext->ctrl_bits[btn_pr]) == false &&
|
|
|
|
button_is_hidden(ext->ctrl_bits[btn_pr]) == false) {
|
|
|
|
/* Send VALUE_CHANGED for the newly pressed button */
|
2019-03-20 06:08:01 +01:00
|
|
|
uint32_t b = ext->btn_id_act;
|
2019-06-14 06:56:54 +02:00
|
|
|
res = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b);
|
2019-04-28 17:26:18 +02:00
|
|
|
if(res == LV_RES_OK) {
|
|
|
|
invalidate_button_area(btnm, btn_pr);
|
|
|
|
}
|
2017-11-10 15:01:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
ext->btn_id_pr = btn_pr;
|
2019-03-15 05:10:43 +01:00
|
|
|
ext->btn_id_act = btn_pr;
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_RELEASED) {
|
2020-02-14 12:36:44 +01:00
|
|
|
if(ext->btn_id_pr != LV_BTNMATRIX_BTN_NONE) {
|
2019-03-11 06:18:44 +01:00
|
|
|
/*Toggle the button if enabled*/
|
2020-01-02 22:01:42 -08:00
|
|
|
if(button_is_tgl_enabled(ext->ctrl_bits[ext->btn_id_pr]) &&
|
2020-02-26 19:48:27 +01:00
|
|
|
!button_is_inactive(ext->ctrl_bits[ext->btn_id_pr])) {
|
2019-03-19 06:30:05 +01:00
|
|
|
if(button_get_tgl_state(ext->ctrl_bits[ext->btn_id_pr])) {
|
2020-02-14 17:03:25 +01:00
|
|
|
ext->ctrl_bits[ext->btn_id_pr] &= (~LV_BTNMATRIX_CTRL_CHECK_STATE);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-02-14 17:03:25 +01:00
|
|
|
ext->ctrl_bits[ext->btn_id_pr] |= LV_BTNMATRIX_CTRL_CHECK_STATE;
|
2017-11-10 15:01:40 +01:00
|
|
|
}
|
2020-03-04 16:29:21 +01:00
|
|
|
if(ext->one_check) make_one_button_toggled(btnm, ext->btn_id_pr);
|
2017-11-10 15:01:40 +01:00
|
|
|
}
|
2018-12-26 08:24:18 +01:00
|
|
|
|
2019-03-15 05:10:43 +01:00
|
|
|
/*Invalidate to old pressed area*/;
|
|
|
|
invalidate_button_area(btnm, ext->btn_id_pr);
|
2020-02-12 08:54:03 +01:00
|
|
|
invalidate_button_area(btnm, ext->btn_id_focused);
|
|
|
|
|
|
|
|
|
|
|
|
lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
|
|
|
|
if(indev_type == LV_INDEV_TYPE_KEYPAD || indev_type == LV_INDEV_TYPE_ENCODER) {
|
|
|
|
ext->btn_id_focused = ext->btn_id_pr;
|
|
|
|
}
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
ext->btn_id_pr = LV_BTNMATRIX_BTN_NONE;
|
2019-03-19 06:30:05 +01:00
|
|
|
|
|
|
|
if(button_is_click_trig(ext->ctrl_bits[ext->btn_id_act]) == true &&
|
2019-04-04 07:15:40 +02:00
|
|
|
button_is_inactive(ext->ctrl_bits[ext->btn_id_act]) == false &&
|
|
|
|
button_is_hidden(ext->ctrl_bits[ext->btn_id_act]) == false) {
|
2019-03-20 06:08:01 +01:00
|
|
|
uint32_t b = ext->btn_id_act;
|
2019-06-14 06:56:54 +02:00
|
|
|
res = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b);
|
2019-03-19 06:30:05 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_LONG_PRESS_REP) {
|
2020-02-14 12:36:44 +01:00
|
|
|
if(ext->btn_id_act != LV_BTNMATRIX_BTN_NONE) {
|
2019-03-19 06:30:05 +01:00
|
|
|
if(button_is_repeat_disabled(ext->ctrl_bits[ext->btn_id_act]) == false &&
|
2019-04-04 07:15:40 +02:00
|
|
|
button_is_inactive(ext->ctrl_bits[ext->btn_id_act]) == false &&
|
|
|
|
button_is_hidden(ext->ctrl_bits[ext->btn_id_act]) == false) {
|
2019-03-20 06:08:01 +01:00
|
|
|
uint32_t b = ext->btn_id_act;
|
2019-06-14 06:56:54 +02:00
|
|
|
res = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b);
|
2019-03-19 06:30:05 +01:00
|
|
|
}
|
2017-11-10 15:01:40 +01:00
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_PRESS_LOST) {
|
2020-02-14 12:36:44 +01:00
|
|
|
ext->btn_id_pr = LV_BTNMATRIX_BTN_NONE;
|
|
|
|
ext->btn_id_act = LV_BTNMATRIX_BTN_NONE;
|
2017-11-10 15:01:40 +01:00
|
|
|
lv_obj_invalidate(btnm);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_FOCUS) {
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_GROUP
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_indev_t * indev = lv_indev_get_act();
|
2019-03-29 16:10:18 +01:00
|
|
|
lv_indev_type_t indev_type = lv_indev_get_type(indev);
|
2019-10-19 11:01:10 +02:00
|
|
|
|
|
|
|
/*If not focused by an input device assume the last input device*/
|
2019-12-30 15:01:15 +01:00
|
|
|
if(indev == NULL) {
|
|
|
|
indev = lv_indev_get_next(NULL);
|
|
|
|
indev_type = lv_indev_get_type(indev);
|
|
|
|
}
|
2019-10-19 11:01:10 +02:00
|
|
|
|
2020-02-26 19:48:27 +01:00
|
|
|
if(indev_type == LV_INDEV_TYPE_ENCODER) {
|
2018-09-26 14:21:39 +02:00
|
|
|
/*In navigation mode don't select any button but in edit mode select the fist*/
|
2020-02-04 02:09:15 +01:00
|
|
|
if(lv_group_get_editing(lv_obj_get_group(btnm))) {
|
2020-01-28 15:24:18 +01:00
|
|
|
ext->btn_id_focused = 0;
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-02-14 12:36:44 +01:00
|
|
|
ext->btn_id_focused = LV_BTNMATRIX_BTN_NONE;
|
2020-02-04 02:09:15 +01:00
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(indev_type == LV_INDEV_TYPE_KEYPAD) {
|
2020-02-04 02:09:15 +01:00
|
|
|
ext->btn_id_focused = 0;
|
2018-09-12 18:55:28 +03:00
|
|
|
}
|
2019-03-12 14:29:37 +01:00
|
|
|
|
2020-01-28 15:24:18 +01:00
|
|
|
ext->btn_id_act = ext->btn_id_focused;
|
2020-02-04 02:09:15 +01:00
|
|
|
#endif
|
2020-01-28 15:24:18 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_DEFOCUS || sign == LV_SIGNAL_LEAVE) {
|
2020-02-14 12:36:44 +01:00
|
|
|
if(ext->btn_id_focused != LV_BTNMATRIX_BTN_NONE) invalidate_button_area(btnm, ext->btn_id_focused);
|
|
|
|
if(ext->btn_id_pr != LV_BTNMATRIX_BTN_NONE) invalidate_button_area(btnm, ext->btn_id_pr);
|
|
|
|
ext->btn_id_focused = LV_BTNMATRIX_BTN_NONE;
|
|
|
|
ext->btn_id_pr = LV_BTNMATRIX_BTN_NONE;
|
|
|
|
ext->btn_id_act = LV_BTNMATRIX_BTN_NONE;
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_CONTROL) {
|
2018-06-19 09:49:58 +02:00
|
|
|
char c = *((char *)param);
|
2019-04-08 14:36:20 +02:00
|
|
|
if(c == LV_KEY_RIGHT) {
|
2020-02-14 12:36:44 +01:00
|
|
|
if(ext->btn_id_focused == LV_BTNMATRIX_BTN_NONE)
|
2020-01-28 15:24:18 +01:00
|
|
|
ext->btn_id_focused = 0;
|
2019-04-04 07:15:40 +02:00
|
|
|
else
|
2020-01-28 15:24:18 +01:00
|
|
|
ext->btn_id_focused++;
|
|
|
|
if(ext->btn_id_focused >= ext->btn_cnt - 1) ext->btn_id_focused = ext->btn_cnt - 1;
|
|
|
|
ext->btn_id_act = ext->btn_id_focused;
|
2017-11-10 15:01:40 +01:00
|
|
|
lv_obj_invalidate(btnm);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(c == LV_KEY_LEFT) {
|
2020-02-14 12:36:44 +01:00
|
|
|
if(ext->btn_id_focused == LV_BTNMATRIX_BTN_NONE) ext->btn_id_focused = 0;
|
2020-01-28 15:24:18 +01:00
|
|
|
if(ext->btn_id_focused > 0) ext->btn_id_focused--;
|
|
|
|
ext->btn_id_act = ext->btn_id_focused;
|
2017-11-10 15:01:40 +01:00
|
|
|
lv_obj_invalidate(btnm);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(c == LV_KEY_DOWN) {
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_style_int_t pad_inner = lv_obj_get_style_pad_inner(btnm, LV_BTNMATRIX_PART_BG);
|
2018-03-07 11:37:38 +01:00
|
|
|
/*Find the area below the the current*/
|
2020-02-14 12:36:44 +01:00
|
|
|
if(ext->btn_id_focused == LV_BTNMATRIX_BTN_NONE) {
|
2020-01-28 15:24:18 +01:00
|
|
|
ext->btn_id_focused = 0;
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-03-07 11:37:38 +01:00
|
|
|
uint16_t area_below;
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_coord_t pr_center =
|
2020-01-28 15:24:18 +01:00
|
|
|
ext->button_areas[ext->btn_id_focused].x1 + (lv_area_get_width(&ext->button_areas[ext->btn_id_focused]) >> 1);
|
2018-03-07 11:37:38 +01:00
|
|
|
|
2020-01-28 15:24:18 +01:00
|
|
|
for(area_below = ext->btn_id_focused; area_below < ext->btn_cnt; area_below++) {
|
|
|
|
if(ext->button_areas[area_below].y1 > ext->button_areas[ext->btn_id_focused].y1 &&
|
2019-04-04 07:15:40 +02:00
|
|
|
pr_center >= ext->button_areas[area_below].x1 &&
|
2020-02-05 03:29:16 +01:00
|
|
|
pr_center <= ext->button_areas[area_below].x2 + pad_inner &&
|
|
|
|
button_is_inactive(ext->ctrl_bits[area_below]) == false &&
|
|
|
|
button_is_hidden(ext->ctrl_bits[area_below]) == false) {
|
2018-03-07 11:37:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-03-21 13:15:02 +01:00
|
|
|
|
2020-01-28 15:24:18 +01:00
|
|
|
if(area_below < ext->btn_cnt) ext->btn_id_focused = area_below;
|
2018-03-07 11:37:38 +01:00
|
|
|
}
|
2020-01-28 15:24:18 +01:00
|
|
|
ext->btn_id_act = ext->btn_id_focused;
|
2018-03-07 11:37:38 +01:00
|
|
|
lv_obj_invalidate(btnm);
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(c == LV_KEY_UP) {
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_style_int_t pad_inner = lv_obj_get_style_pad_inner(btnm, LV_BTNMATRIX_PART_BG);
|
2018-03-07 11:37:38 +01:00
|
|
|
/*Find the area below the the current*/
|
2020-02-14 12:36:44 +01:00
|
|
|
if(ext->btn_id_focused == LV_BTNMATRIX_BTN_NONE) {
|
2020-01-28 15:24:18 +01:00
|
|
|
ext->btn_id_focused = 0;
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-03-21 13:15:02 +01:00
|
|
|
int16_t area_above;
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_coord_t pr_center =
|
2020-01-28 15:24:18 +01:00
|
|
|
ext->button_areas[ext->btn_id_focused].x1 + (lv_area_get_width(&ext->button_areas[ext->btn_id_focused]) >> 1);
|
2018-03-07 11:37:38 +01:00
|
|
|
|
2020-01-28 15:24:18 +01:00
|
|
|
for(area_above = ext->btn_id_focused; area_above >= 0; area_above--) {
|
|
|
|
if(ext->button_areas[area_above].y1 < ext->button_areas[ext->btn_id_focused].y1 &&
|
2019-12-22 22:40:02 +01:00
|
|
|
pr_center >= ext->button_areas[area_above].x1 - pad_inner &&
|
2020-02-05 03:29:16 +01:00
|
|
|
pr_center <= ext->button_areas[area_above].x2 &&
|
|
|
|
button_is_inactive(ext->ctrl_bits[area_above]) == false &&
|
|
|
|
button_is_hidden(ext->ctrl_bits[area_above]) == false) {
|
2018-03-07 11:37:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-01-28 15:24:18 +01:00
|
|
|
if(area_above >= 0) ext->btn_id_focused = area_above;
|
2018-03-07 11:37:38 +01:00
|
|
|
}
|
2020-01-28 15:24:18 +01:00
|
|
|
ext->btn_id_act = ext->btn_id_focused;
|
2018-03-07 11:37:38 +01:00
|
|
|
lv_obj_invalidate(btnm);
|
2018-02-28 15:37:41 +01:00
|
|
|
}
|
2020-02-26 19:48:27 +01:00
|
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_GET_EDITABLE) {
|
2018-09-12 18:55:28 +03:00
|
|
|
bool * editable = (bool *)param;
|
2019-04-04 07:15:40 +02:00
|
|
|
*editable = true;
|
2017-11-10 15:01:40 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-01-10 11:10:07 +01:00
|
|
|
/**
|
|
|
|
* Get the style descriptor of a part of the object
|
|
|
|
* @param btnm pointer the object
|
2020-02-14 12:36:44 +01:00
|
|
|
* @param part the part of the object. (LV_BTNMATRIX_PART_...)
|
2020-01-10 11:10:07 +01:00
|
|
|
* @return pointer to the style descriptor of the specified part
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
static lv_style_list_t * lv_btnmatrix_get_style(lv_obj_t * btnm, uint8_t part)
|
2019-12-31 06:10:50 +01:00
|
|
|
{
|
|
|
|
LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2019-12-31 06:10:50 +01:00
|
|
|
|
2020-01-16 14:26:36 +01:00
|
|
|
lv_style_list_t * style_dsc_p;
|
2019-12-31 06:10:50 +01:00
|
|
|
|
2019-12-31 22:13:32 +01:00
|
|
|
switch(part) {
|
2020-02-26 19:48:27 +01:00
|
|
|
case LV_BTNMATRIX_PART_BG:
|
|
|
|
style_dsc_p = &btnm->style_list;
|
|
|
|
break;
|
|
|
|
case LV_BTNMATRIX_PART_BTN:
|
|
|
|
style_dsc_p = &ext->style_btn;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
style_dsc_p = NULL;
|
2019-12-31 06:10:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return style_dsc_p;
|
|
|
|
}
|
2020-01-10 11:10:07 +01:00
|
|
|
|
2016-09-30 13:35:54 +02:00
|
|
|
/**
|
2019-02-12 16:55:30 +08:00
|
|
|
* Create the required number of buttons and control bytes according to a map
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param btnm pointer to button matrix object
|
2016-09-30 13:35:54 +02:00
|
|
|
* @param map_p pointer to a string array
|
|
|
|
*/
|
2019-02-19 15:06:23 +08:00
|
|
|
static void allocate_btn_areas_and_controls(const lv_obj_t * btnm, const char ** map)
|
2016-09-30 13:35:54 +02:00
|
|
|
{
|
2018-06-19 09:49:58 +02:00
|
|
|
/*Count the buttons in the map*/
|
|
|
|
uint16_t btn_cnt = 0;
|
2019-04-04 07:15:40 +02:00
|
|
|
uint16_t i = 0;
|
2018-06-19 09:49:58 +02:00
|
|
|
while(strlen(map[i]) != 0) {
|
|
|
|
if(strcmp(map[i], "\n") != 0) { /*Do not count line breaks*/
|
2019-04-04 07:15:40 +02:00
|
|
|
btn_cnt++;
|
2018-06-19 09:49:58 +02:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2018-06-19 09:49:58 +02:00
|
|
|
|
|
|
|
if(ext->button_areas != NULL) {
|
|
|
|
lv_mem_free(ext->button_areas);
|
|
|
|
ext->button_areas = NULL;
|
|
|
|
}
|
2019-02-12 16:55:30 +08:00
|
|
|
if(ext->ctrl_bits != NULL) {
|
|
|
|
lv_mem_free(ext->ctrl_bits);
|
|
|
|
ext->ctrl_bits = NULL;
|
|
|
|
}
|
2018-06-19 09:49:58 +02:00
|
|
|
|
|
|
|
ext->button_areas = lv_mem_alloc(sizeof(lv_area_t) * btn_cnt);
|
2019-09-24 23:14:17 +02:00
|
|
|
LV_ASSERT_MEM(ext->button_areas);
|
2020-02-14 12:36:44 +01:00
|
|
|
ext->ctrl_bits = lv_mem_alloc(sizeof(lv_btnmatrix_ctrl_t) * btn_cnt);
|
2019-09-24 23:14:17 +02:00
|
|
|
LV_ASSERT_MEM(ext->ctrl_bits);
|
2019-02-12 16:55:30 +08:00
|
|
|
if(ext->button_areas == NULL || ext->ctrl_bits == NULL) btn_cnt = 0;
|
2018-07-25 13:33:53 +02:00
|
|
|
|
2020-05-13 14:11:16 +02:00
|
|
|
_lv_memset_00(ext->ctrl_bits, sizeof(lv_btnmatrix_ctrl_t) * btn_cnt);
|
2019-03-12 06:20:45 +01:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
ext->btn_cnt = btn_cnt;
|
2016-09-30 13:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-02-12 16:55:30 +08:00
|
|
|
/**
|
|
|
|
* Get the width of a button in units (default is 1).
|
|
|
|
* @param ctrl_bits least significant 3 bits used (1..7 valid values)
|
|
|
|
* @return the width of the button in units
|
|
|
|
*/
|
2020-02-14 12:36:44 +01:00
|
|
|
static uint8_t get_button_width(lv_btnmatrix_ctrl_t ctrl_bits)
|
2019-02-12 16:55:30 +08:00
|
|
|
{
|
2020-02-14 12:36:44 +01:00
|
|
|
uint8_t w = ctrl_bits & LV_BTNMATRIX_WIDTH_MASK;
|
2019-04-04 07:15:40 +02:00
|
|
|
return w != 0 ? w : 1;
|
2017-10-20 15:37:50 +02:00
|
|
|
}
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
static bool button_is_hidden(lv_btnmatrix_ctrl_t ctrl_bits)
|
2017-10-20 15:37:50 +02:00
|
|
|
{
|
2020-02-25 15:32:35 +01:00
|
|
|
return (ctrl_bits & LV_BTNMATRIX_CTRL_HIDDEN) ? true : false;
|
2017-10-20 15:37:50 +02:00
|
|
|
}
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
static bool button_is_repeat_disabled(lv_btnmatrix_ctrl_t ctrl_bits)
|
2017-11-03 13:39:37 +01:00
|
|
|
{
|
2020-02-25 15:32:35 +01:00
|
|
|
return (ctrl_bits & LV_BTNMATRIX_CTRL_NO_REPEAT) ? true : false;
|
2019-02-12 16:55:30 +08:00
|
|
|
}
|
2017-11-03 13:39:37 +01:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
static bool button_is_inactive(lv_btnmatrix_ctrl_t ctrl_bits)
|
2019-02-12 16:55:30 +08:00
|
|
|
{
|
2020-03-04 16:29:21 +01:00
|
|
|
return (ctrl_bits & LV_BTNMATRIX_CTRL_DISABLED) ? true : false;
|
2019-03-19 06:30:05 +01:00
|
|
|
}
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
static bool button_is_click_trig(lv_btnmatrix_ctrl_t ctrl_bits)
|
2019-03-19 06:30:05 +01:00
|
|
|
{
|
2020-02-25 15:32:35 +01:00
|
|
|
return (ctrl_bits & LV_BTNMATRIX_CTRL_CLICK_TRIG) ? true : false;
|
2017-11-03 13:39:37 +01:00
|
|
|
}
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
static bool button_is_tgl_enabled(lv_btnmatrix_ctrl_t ctrl_bits)
|
2019-03-11 06:18:44 +01:00
|
|
|
{
|
2020-02-25 15:32:35 +01:00
|
|
|
return (ctrl_bits & LV_BTNMATRIX_CTRL_CHECKABLE) ? true : false;
|
2019-03-11 06:18:44 +01:00
|
|
|
}
|
2017-11-03 13:39:37 +01:00
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
static bool button_get_tgl_state(lv_btnmatrix_ctrl_t ctrl_bits)
|
2017-10-20 15:37:50 +02:00
|
|
|
{
|
2020-02-25 15:32:35 +01:00
|
|
|
return (ctrl_bits & LV_BTNMATRIX_CTRL_CHECK_STATE) ? true : false;
|
2017-10-20 15:37:50 +02:00
|
|
|
}
|
|
|
|
|
2017-07-20 12:26:34 +02:00
|
|
|
/**
|
|
|
|
* Gives the button id of a button under a given point
|
|
|
|
* @param btnm pointer to a button matrix object
|
|
|
|
* @param p a point with absolute coordinates
|
2020-02-14 12:36:44 +01:00
|
|
|
* @return the id of the button or LV_BTNMATRIX_BTN_NONE.
|
2017-07-20 12:26:34 +02:00
|
|
|
*/
|
2017-11-23 21:28:36 +01:00
|
|
|
static uint16_t get_button_from_point(lv_obj_t * btnm, lv_point_t * p)
|
2017-01-09 06:48:04 +01:00
|
|
|
{
|
2017-11-23 21:28:36 +01:00
|
|
|
lv_area_t btnm_cords;
|
|
|
|
lv_area_t btn_area;
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2017-01-09 06:48:04 +01:00
|
|
|
uint16_t i;
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_obj_get_coords(btnm, &btnm_cords);
|
2017-01-09 06:48:04 +01:00
|
|
|
|
2020-04-29 10:25:24 +02:00
|
|
|
lv_coord_t w = lv_obj_get_width(btnm);
|
|
|
|
lv_coord_t h = lv_obj_get_height(btnm);
|
|
|
|
lv_style_int_t pleft = lv_obj_get_style_pad_left(btnm, LV_BTNMATRIX_PART_BG);
|
|
|
|
lv_style_int_t pright = lv_obj_get_style_pad_right(btnm, LV_BTNMATRIX_PART_BG);
|
|
|
|
lv_style_int_t ptop = lv_obj_get_style_pad_top(btnm, LV_BTNMATRIX_PART_BG);
|
|
|
|
lv_style_int_t pbottom = lv_obj_get_style_pad_bottom(btnm, LV_BTNMATRIX_PART_BG);
|
|
|
|
lv_style_int_t pinner = lv_obj_get_style_pad_inner(btnm, LV_BTNMATRIX_PART_BG);
|
|
|
|
|
|
|
|
/*Get the half inner padding. Button look larger with this value. (+1 for rounding error)*/
|
|
|
|
pinner = (pinner / 2) + 1 + (pinner & 1);
|
|
|
|
|
2017-11-10 15:01:40 +01:00
|
|
|
for(i = 0; i < ext->btn_cnt; i++) {
|
2017-11-28 16:15:13 +01:00
|
|
|
lv_area_copy(&btn_area, &ext->button_areas[i]);
|
2020-04-29 10:25:24 +02:00
|
|
|
if(btn_area.x1 <= pleft) btn_area.x1 = btnm_cords.x1;
|
|
|
|
else btn_area.x1 += btnm_cords.x1 - pinner;
|
|
|
|
|
|
|
|
if(btn_area.y1 <= ptop) btn_area.y1 = btnm_cords.y1;
|
|
|
|
else btn_area.y1 += btnm_cords.y1 - pinner;
|
|
|
|
|
|
|
|
if(btn_area.x2 >= w - pright - 2) btn_area.x2 = btnm_cords.x2; /*-2 for rounding error*/
|
|
|
|
else btn_area.x2 += btnm_cords.x1 + pinner;
|
|
|
|
|
|
|
|
if(btn_area.y2 >= h - pbottom - 2) btn_area.y2 = btnm_cords.y2; /*-2 for rounding error*/
|
|
|
|
else btn_area.y2 += btnm_cords.y1 + pinner;
|
|
|
|
|
2020-05-13 14:11:16 +02:00
|
|
|
if(_lv_area_is_point_on(&btn_area, p, 0) != false) {
|
2017-01-09 06:48:04 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
if(i == ext->btn_cnt) i = LV_BTNMATRIX_BTN_NONE;
|
2017-01-09 06:48:04 +01:00
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2019-02-12 16:55:30 +08:00
|
|
|
static void invalidate_button_area(const lv_obj_t * btnm, uint16_t btn_idx)
|
|
|
|
{
|
2020-02-14 12:36:44 +01:00
|
|
|
if(btn_idx == LV_BTNMATRIX_BTN_NONE) return;
|
2019-03-17 04:55:18 +01:00
|
|
|
|
2019-02-12 16:55:30 +08:00
|
|
|
lv_area_t btn_area;
|
|
|
|
lv_area_t btnm_area;
|
|
|
|
|
2020-02-14 12:36:44 +01:00
|
|
|
lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
2019-02-12 16:55:30 +08:00
|
|
|
lv_area_copy(&btn_area, &ext->button_areas[btn_idx]);
|
|
|
|
lv_obj_get_coords(btnm, &btnm_area);
|
|
|
|
|
|
|
|
/* Convert relative coordinates to absolute */
|
|
|
|
btn_area.x1 += btnm_area.x1;
|
|
|
|
btn_area.y1 += btnm_area.y1;
|
|
|
|
btn_area.x2 += btnm_area.x1;
|
|
|
|
btn_area.y2 += btnm_area.y1;
|
|
|
|
|
2020-01-20 14:47:05 +01:00
|
|
|
lv_obj_invalidate_area(btnm, &btn_area);
|
2019-02-12 16:55:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares two button matrix maps for equality
|
|
|
|
* @param map1 map to compare
|
|
|
|
* @param map2 map to compare
|
|
|
|
* @return true if maps are identical in length and content
|
|
|
|
*/
|
|
|
|
static bool maps_are_identical(const char ** map1, const char ** map2)
|
|
|
|
{
|
2019-04-04 07:15:40 +02:00
|
|
|
if(map1 == map2) return true;
|
|
|
|
if(map1 == NULL || map2 == NULL) return map1 == map2;
|
2019-02-12 16:55:30 +08:00
|
|
|
|
|
|
|
uint16_t i = 0;
|
2019-04-04 07:15:40 +02:00
|
|
|
while(map1[i][0] != '\0' && map2[i][0] != '\0') {
|
|
|
|
if(strcmp(map1[i], map2[i]) != 0) return false;
|
2019-02-27 06:17:37 +01:00
|
|
|
i++;
|
2019-02-12 16:55:30 +08:00
|
|
|
}
|
|
|
|
return map1[i][0] == '\0' && map2[i][0] == '\0';
|
|
|
|
}
|
|
|
|
|
2019-03-20 18:33:33 -04:00
|
|
|
/**
|
|
|
|
* Enforces a single button being toggled on the button matrix.
|
|
|
|
* It simply clears the toggle flag on other buttons.
|
|
|
|
* @param btnm Button matrix object
|
|
|
|
* @param btn_idx Button that should remain toggled
|
|
|
|
*/
|
2019-04-04 07:15:40 +02:00
|
|
|
static void make_one_button_toggled(lv_obj_t * btnm, uint16_t btn_idx)
|
2019-03-20 18:33:33 -04:00
|
|
|
{
|
2019-04-04 07:15:40 +02:00
|
|
|
/*Save whether the button was toggled*/
|
2020-02-14 17:03:25 +01:00
|
|
|
bool was_toggled = lv_btnmatrix_get_btn_ctrl(btnm, btn_idx, LV_BTNMATRIX_CTRL_CHECK_STATE);
|
2019-03-20 18:33:33 -04:00
|
|
|
|
2020-02-14 17:03:25 +01:00
|
|
|
lv_btnmatrix_clear_btn_ctrl_all(btnm, LV_BTNMATRIX_CTRL_CHECK_STATE);
|
2019-03-20 18:33:33 -04:00
|
|
|
|
2020-02-14 17:03:25 +01:00
|
|
|
if(was_toggled) lv_btnmatrix_set_btn_ctrl(btnm, btn_idx, LV_BTNMATRIX_CTRL_CHECK_STATE);
|
2019-03-20 18:33:33 -04:00
|
|
|
}
|
2016-09-30 13:35:54 +02:00
|
|
|
|
|
|
|
#endif
|