1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3 #include "../../lvgl_private.h"
4 
5 #include "unity/unity.h"
6 
setUp(void)7 void setUp(void)
8 {
9     /* Function run before every test */
10     lv_obj_set_flex_flow(lv_screen_active(), LV_FLEX_FLOW_ROW_WRAP);
11     lv_obj_set_flex_align(lv_screen_active(), LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_SPACE_EVENLY);
12 }
13 
tearDown(void)14 void tearDown(void)
15 {
16     /* Function run after every test */
17     lv_obj_clean(lv_screen_active());
18 }
19 
canvas_create(void)20 static lv_obj_t * canvas_create(void)
21 {
22     lv_obj_t * canvas = lv_canvas_create(lv_screen_active());
23     lv_obj_set_size(canvas, 500, 360);
24 
25     lv_draw_buf_t * draw_buf = lv_draw_buf_create(500, 360, LV_COLOR_FORMAT_ARGB8888, LV_STRIDE_AUTO);
26     lv_draw_buf_clear(draw_buf, NULL);
27     lv_canvas_set_draw_buf(canvas, draw_buf);
28 
29     return canvas;
30 }
31 
canvas_destroy(lv_obj_t * canvas)32 static void canvas_destroy(lv_obj_t * canvas)
33 {
34     lv_draw_buf_destroy(lv_canvas_get_draw_buf(canvas));
35     lv_obj_delete(canvas);
36 }
37 
test_draw_sin_wave(void)38 void test_draw_sin_wave(void)
39 {
40     const char * string = "lol~ I'm wavvvvvvving~";
41     const uint32_t string_len = lv_strlen(string);
42 
43     LV_FONT_DECLARE(test_font_montserrat_ascii_4bpp);
44     lv_obj_t * canvas = canvas_create();
45 
46     lv_layer_t layer;
47     lv_canvas_init_layer(canvas, &layer);
48 
49     lv_draw_letter_dsc_t letter_dsc;
50     lv_draw_letter_dsc_init(&letter_dsc);
51     letter_dsc.color = lv_color_hex(0xff0000);
52     letter_dsc.font = &test_font_montserrat_ascii_4bpp;
53 
54     {
55 #define CURVE1_X(t) (t * 2 + 20)
56 #define CURVE1_Y(t) (lv_trigo_sin(t) * 40 / 32767 + 80)
57         int32_t pre_x = CURVE1_X(-1);
58         int32_t pre_y = CURVE1_Y(-1);
59 
60         for(int16_t i = 0; i < 30; i++) {
61             const int32_t angle = i * 10;
62             const int32_t x = CURVE1_X(angle);
63             const int32_t y = CURVE1_Y(angle);
64             letter_dsc.unicode = (uint32_t)string[i % string_len];
65             letter_dsc.rotation = lv_atan2(y - pre_y, x - pre_x);
66             letter_dsc.rotation = (letter_dsc.rotation > 180 ? letter_dsc.rotation - 360 : letter_dsc.rotation) * 5;
67             lv_draw_letter(&layer, &letter_dsc, &(lv_point_t) {
68                 .x = x, .y = y
69             });
70             pre_x = x;
71             pre_y = y;
72         }
73     }
74 
75     {
76 #define CURVE2_X(t) (t * 3 + 20)
77 #define CURVE2_Y(t) (lv_trigo_sin((t) * 4) * 40 / 32767 + 230)
78 
79         int32_t pre_x = CURVE2_X(-1);
80         int32_t pre_y = CURVE2_Y(-1);
81         for(int16_t i = 0; i < 30; i++) {
82             const int32_t angle = i * 5;
83             const int32_t x = CURVE2_X(angle);
84             const int32_t y = CURVE2_Y(angle);
85 
86             letter_dsc.unicode = (uint32_t)string[i % string_len];
87             letter_dsc.rotation = lv_atan2(y - pre_y, x - pre_x) * 10;
88             letter_dsc.color = lv_color_hsv_to_rgb(i * 10, 100, 100);
89             lv_draw_letter(&layer, &letter_dsc, &(lv_point_t) {
90                 .x = x, .y = y
91             });
92 
93             pre_x = x;
94             pre_y = y;
95         }
96     }
97 
98     lv_canvas_finish_layer(canvas, &layer);
99 
100 #ifndef NON_AMD64_BUILD
101     TEST_ASSERT_EQUAL_SCREENSHOT("draw/letter_0.png");
102 #endif
103 
104     canvas_destroy(canvas);
105 }
106 
107 #endif
108