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

feat(barcode): add vertical display mode (#4564)

Signed-off-by: liujp <liujp@xiaomi.com>
Co-authored-by: liujp <liujp@xiaomi.com>
This commit is contained in:
Lemon 2023-09-25 03:41:24 +08:00 committed by GitHub
parent f0c58a1719
commit 2f67d804ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 9 deletions

View File

@ -15,8 +15,9 @@ Use :c:expr:`lv_barcode_create()` to create a barcode object, and use
:c:expr:`lv_barcode_update()` to generate a barcode.
Call :c:expr:`lv_barcode_set_scale()` or :c:expr:`lv_barcode_set_dark/light_color()`
to adjust scaling and color, and call :c:expr:`lv_barcode_update()` again to
regenerate the barcode.
to adjust scaling and color, call :c:expr:`lv_barcode_set_direction()` will set
direction to display, and call :c:expr:`lv_barcode_update()` again to regenerate
the barcode.
Notes
-----
@ -26,6 +27,7 @@ Notes
display will be incomplete due to truncation.
- The scale adjustment can only be an integer multiple, for example,
:c:expr:`lv_barcode_set_scale(barcode, 2)` means 2x scaling.
- The direction adjustment can be `LV_DIR_HOR` or `LV_DIR_VER`
Example
-------

View File

@ -25,7 +25,7 @@
**********************/
static void lv_barcode_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
static void lv_barcode_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
static bool lv_barcode_change_buf_size(lv_obj_t * obj, lv_coord_t w);
static bool lv_barcode_change_buf_size(lv_obj_t * obj, lv_coord_t w, lv_coord_t h);
/**********************
* STATIC VARIABLES
@ -83,6 +83,14 @@ void lv_barcode_set_scale(lv_obj_t * obj, uint16_t scale)
barcode->scale = scale;
}
void lv_barcode_set_direction(lv_obj_t * obj, lv_dir_t direction)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_barcode_t * barcode = (lv_barcode_t *)obj;
barcode->direction = direction;
}
lv_result_t lv_barcode_update(lv_obj_t * obj, const char * data)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
@ -112,7 +120,10 @@ lv_result_t lv_barcode_update(lv_obj_t * obj, const char * data)
LV_ASSERT(barcode->scale > 0);
uint16_t scale = barcode->scale;
if(!lv_barcode_change_buf_size(obj, barcode_w * scale)) {
lv_coord_t buf_w = (barcode->direction == LV_DIR_HOR) ? barcode_w * scale : 1;
lv_coord_t buf_h = (barcode->direction == LV_DIR_VER) ? barcode_w * scale : 1;
if(!lv_barcode_change_buf_size(obj, buf_w, buf_h)) {
goto failed;
}
@ -123,7 +134,12 @@ lv_result_t lv_barcode_update(lv_obj_t * obj, const char * data)
lv_color_t color;
color = lv_color_from_int(out_buf[x] ? 0 : 1);
for(uint16_t i = 0; i < scale; i++) {
lv_canvas_set_px(obj, x * scale + i, 0, color, LV_OPA_COVER);
if(barcode->direction == LV_DIR_HOR) {
lv_canvas_set_px(obj, x * scale + i, 0, color, LV_OPA_COVER);
}
else {
lv_canvas_set_px(obj, 0, x * scale + i, color, LV_OPA_COVER);
}
}
}
@ -170,6 +186,7 @@ static void lv_barcode_constructor(const lv_obj_class_t * class_p, lv_obj_t * ob
barcode->dark_color = lv_color_black();
barcode->light_color = lv_color_white();
barcode->scale = 1;
barcode->direction = LV_DIR_HOR;
}
static void lv_barcode_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
@ -192,7 +209,7 @@ static void lv_barcode_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj
img->data = NULL;
}
static bool lv_barcode_change_buf_size(lv_obj_t * obj, lv_coord_t w)
static bool lv_barcode_change_buf_size(lv_obj_t * obj, lv_coord_t w, lv_coord_t h)
{
LV_ASSERT_NULL(obj);
LV_ASSERT(w > 0);
@ -200,7 +217,7 @@ static bool lv_barcode_change_buf_size(lv_obj_t * obj, lv_coord_t w)
lv_image_dsc_t * img = lv_canvas_get_image(obj);
void * buf = (void *)img->data;
uint32_t buf_size = LV_CANVAS_BUF_SIZE_INDEXED_1BIT(w, 1);
uint32_t buf_size = LV_CANVAS_BUF_SIZE_INDEXED_1BIT(w, h);
buf = lv_realloc(buf, buf_size);
LV_ASSERT_MALLOC(buf);
@ -209,7 +226,7 @@ static bool lv_barcode_change_buf_size(lv_obj_t * obj, lv_coord_t w)
return false;
}
lv_canvas_set_buffer(obj, buf, w, 1, LV_COLOR_FORMAT_I1);
lv_canvas_set_buffer(obj, buf, w, h, LV_COLOR_FORMAT_I1);
LV_LOG_INFO("set canvas buffer: %p, width = %d", buf, (int)w);
return true;
}

View File

@ -30,6 +30,7 @@ typedef struct {
lv_color_t dark_color;
lv_color_t light_color;
uint16_t scale;
lv_dir_t direction;
} lv_barcode_t;
extern const lv_obj_class_t lv_barcode_class;
@ -66,6 +67,13 @@ void lv_barcode_set_light_color(lv_obj_t * obj, lv_color_t color);
*/
void lv_barcode_set_scale(lv_obj_t * obj, uint16_t scale);
/**
* Set the direction of a barcode object
* @param obj pointer to barcode object
* @param direction draw direction (`LV_DIR_HOR` or `LB_DIR_VER`)
*/
void lv_barcode_set_direction(lv_obj_t * obj, lv_dir_t direction);
/**
* Set the data of a barcode object
* @param obj pointer to barcode object

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -22,7 +22,6 @@ void test_barcode_normal(void)
lv_obj_t * barcode = lv_barcode_create(active_screen);
TEST_ASSERT_NOT_NULL(barcode);
lv_obj_set_height(barcode, 50);
lv_obj_center(barcode);
lv_color_t dark_color = lv_color_black();
@ -37,9 +36,26 @@ void test_barcode_normal(void)
TEST_ASSERT_EQUAL_COLOR(lv_barcode_get_light_color(barcode), light_color);
TEST_ASSERT_EQUAL(lv_barcode_get_scale(barcode), scale);
lv_barcode_set_direction(barcode, LV_DIR_HOR);
lv_result_t res = lv_barcode_update(barcode, "https://lvgl.io");
TEST_ASSERT_EQUAL(res, LV_RESULT_OK);
lv_image_dsc_t * image_dsc = lv_canvas_get_image(barcode);
TEST_ASSERT_NOT_NULL(image_dsc);
lv_obj_set_size(barcode, image_dsc->header.w, 50);
TEST_ASSERT_EQUAL_SCREENSHOT("barcode_1.png");
lv_barcode_set_direction(barcode, LV_DIR_VER);
res = lv_barcode_update(barcode, "https://lvgl.io");
TEST_ASSERT_EQUAL(res, LV_RESULT_OK);
image_dsc = lv_canvas_get_image(barcode);
TEST_ASSERT_NOT_NULL(image_dsc);
lv_obj_set_size(barcode, 50, image_dsc->header.h);
TEST_ASSERT_EQUAL_SCREENSHOT("barcode_2.png");
}
#else