feat(roller): function to get option string (#8730)

This commit is contained in:
bryghtlabs-richard
2025-09-28 23:24:26 -05:00
committed by GitHub
parent b2c86ef470
commit c727db10ce
3 changed files with 68 additions and 20 deletions
+32 -20
View File
@@ -287,26 +287,7 @@ void lv_roller_get_selected_str(const lv_obj_t * obj, char * buf, uint32_t buf_s
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_roller_t * roller = (lv_roller_t *)obj;
lv_obj_t * label = get_label(obj);
uint32_t i;
uint32_t line = 0;
const char * opt_txt = lv_label_get_text(label);
size_t txt_len = lv_strlen(opt_txt);
for(i = 0; i < txt_len && line != roller->sel_opt_id; i++) {
if(opt_txt[i] == '\n') line++;
}
uint32_t c;
for(c = 0; i < txt_len && opt_txt[i] != '\n'; c++, i++) {
if(buf_size && c >= buf_size - 1) {
LV_LOG_WARN("the buffer was too small");
break;
}
buf[c] = opt_txt[i];
}
buf[c] = '\0';
lv_roller_get_option_str(obj, roller->sel_opt_id, buf, buf_size);
}
/**
@@ -353,6 +334,37 @@ lv_observer_t * lv_roller_bind_value(lv_obj_t * obj, lv_subject_t * subject)
}
#endif /*LV_USE_OBSERVER*/
lv_result_t lv_roller_get_option_str(const lv_obj_t * obj, uint32_t option, char * buf, uint32_t buf_size)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_obj_t * label = get_label(obj);
uint32_t i;
uint32_t line = 0;
const char * opt_txt = lv_label_get_text(label);
size_t txt_len = lv_strlen(opt_txt);
for(i = 0; i < txt_len && line != option; i++) {
if(opt_txt[i] == '\n') line++;
}
if(line != option) {
return LV_RESULT_INVALID;
}
uint32_t c;
for(c = 0; i < txt_len && opt_txt[i] != '\n'; c++, i++) {
if(buf_size && c >= buf_size - 1) {
LV_LOG_WARN("the buffer was too small");
break;
}
buf[c] = opt_txt[i];
}
buf[c] = '\0';
return LV_RESULT_OK;
}
/**********************
* STATIC FUNCTIONS
**********************/
+9
View File
@@ -129,6 +129,15 @@ const char * lv_roller_get_options(const lv_obj_t * obj);
*/
uint32_t lv_roller_get_option_count(const lv_obj_t * obj);
/**
* Get an option as a string.
* @param obj pointer to roller object
* @param option index of chosen option
* @param buf pointer to an array to store the string
* @param buf_size size of `buf` in bytes. 0: to ignore it.
* @return LV_RESULT_OK if option found
*/
lv_result_t lv_roller_get_option_str(const lv_obj_t * obj, uint32_t option, char * buf, uint32_t buf_size);
#if LV_USE_OBSERVER
/**
@@ -57,6 +57,33 @@ void test_roller_get_options(void)
TEST_ASSERT_EQUAL_STRING(default_roller_options, lv_roller_get_options(roller));
}
void test_roller_get_option_str(void)
{
lv_result_t res;
char option_str[OPTION_BUFFER_SZ] = {0x00};
char * expected_strs[] = {"One", "Two", "Three"};
uint16_t expected_str_count = sizeof(expected_strs) / sizeof(expected_strs[0]);
/* Select the last option, index starts at 0 */
uint16_t option_count = lv_roller_get_option_count(roller);
TEST_ASSERT_EQUAL(expected_str_count, option_count);
for(uint16_t i = 0; i < option_count; ++i) {
memset(option_str, 0x00, sizeof(option_str));
res = lv_roller_get_option_str(roller, i, option_str, OPTION_BUFFER_SZ);
TEST_ASSERT_EQUAL(res, LV_RESULT_OK);
TEST_ASSERT_EQUAL_STRING(expected_strs[i], option_str);
}
memset(option_str, 0x00, sizeof(option_str));
res = lv_roller_get_option_str(roller, option_count, option_str, OPTION_BUFFER_SZ);
TEST_ASSERT_EQUAL(res, LV_RESULT_INVALID);
memset(option_str, 0x00, sizeof(option_str));
res = lv_roller_get_option_str(roller, -1, option_str, OPTION_BUFFER_SZ);
TEST_ASSERT_EQUAL(res, LV_RESULT_INVALID);
}
void test_roller_get_selected_option(void)
{
char actual_str[OPTION_BUFFER_SZ] = {0x00};