diff --git a/src/widgets/roller/lv_roller.c b/src/widgets/roller/lv_roller.c index 7780f0c15f..2e71c03b1f 100644 --- a/src/widgets/roller/lv_roller.c +++ b/src/widgets/roller/lv_roller.c @@ -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 **********************/ diff --git a/src/widgets/roller/lv_roller.h b/src/widgets/roller/lv_roller.h index 7f960c7dcc..b4f3089b70 100644 --- a/src/widgets/roller/lv_roller.h +++ b/src/widgets/roller/lv_roller.h @@ -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 /** diff --git a/tests/src/test_cases/widgets/test_roller.c b/tests/src/test_cases/widgets/test_roller.c index 089a82bf60..1818ba8b3a 100644 --- a/tests/src/test_cases/widgets/test_roller.c +++ b/tests/src/test_cases/widgets/test_roller.c @@ -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};