summaryrefslogtreecommitdiff
path: root/bsp/gd32/arm/gd32470i-eval/board/ports/drv_lcd.c
blob: 726e30d2a8bee0adea4994db0a04e82cbbb57693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*
 * Copyright (c) 2006-2025, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-05-23     godmial      Refactor to conform to RT-Thread coding style.
 */

#include <board.h>
#include "gd32f4xx.h"
#include "gd32f4xx_tli.h"
#include "drv_lcd.h"
#include "font.h"


#if defined(__CC_ARM) || defined(__ARMCC_VERSION)
/* Keil MDK Compiler */
uint16_t ltdc_lcd_framebuf0[800][480] __attribute__((at(LCD_FRAME_BUF_ADDR)));
#elif defined(__GNUC__)
/* GCC Compiler (used by RT-Thread) */
uint16_t ltdc_lcd_framebuf0[10][10];
#endif

static void tli_gpio_config(void);
static void tli_config(void);
static void ipa_config(void);
static void lcd_disp_en_config(void);
static void lcd_disp_off(void);
static void lcd_disp_on(void);


/**
 * @brief    Configure and initialize the LCD display.
 *
 * @note     This function enables display power, initializes TLI GPIO and IPA,
 *           and sets up TLI and its layer configuration.
 *
 * @param    None
 *
 * @return   None
 *
 * @warning  Should be called during system initialization phase.
 */
void lcd_disp_config(void)
{
    lcd_disp_en_config();
    lcd_disp_off();

    /* configure the GPIO of TLI */
    tli_gpio_config();
    lcd_disp_on();

    tli_config();
    tli_layer_enable(LAYER0);
    tli_reload_config(TLI_FRAME_BLANK_RELOAD_EN);
    tli_enable();
    ipa_config();
}

/**
 * @brief    Initialize the TLI peripheral and layer configuration.
 *
 * @note     This function sets up the TLI clock, background color, timing parameters,
 *           and layer 0 settings including framebuffer and blending configuration.
 *
 * @param    None
 *
 * @return   None
 *
 * @warning  Must be called after clock and GPIO initialization.
 */
static void tli_config(void)
{
    tli_parameter_struct       tli_init_struct;
    tli_layer_parameter_struct tli_layer_init_struct;

    rcu_periph_clock_enable(RCU_TLI);
    tli_gpio_config();
    /* configure PLLSAI to generate TLI clock */
    /* if(ERROR == rcu_pllsai_config(216, 2, 3)) */
    {
    if (ERROR == rcu_pllsai_config(192, 2, 3))
    {
        while (1);
    }
    rcu_tli_clock_div_config(RCU_PLLSAIR_DIV2);

    rcu_osci_on(RCU_PLLSAI_CK);

    if (ERROR == rcu_osci_stab_wait(RCU_PLLSAI_CK))
    {
        while (1);
    }

    /* configure TLI parameter struct */
    tli_init_struct.signalpolarity_hs      = TLI_HSYN_ACTLIVE_LOW;
    tli_init_struct.signalpolarity_vs      = TLI_VSYN_ACTLIVE_LOW;
    tli_init_struct.signalpolarity_de      = TLI_DE_ACTLIVE_LOW;
    tli_init_struct.signalpolarity_pixelck = TLI_PIXEL_CLOCK_TLI;
    /* LCD display timing configuration */
    tli_init_struct.synpsz_hpsz   = HORIZONTAL_SYNCHRONOUS_PULSE - 1;
    tli_init_struct.synpsz_vpsz   = VERTICAL_SYNCHRONOUS_PULSE - 1;
    tli_init_struct.backpsz_hbpsz = HORIZONTAL_SYNCHRONOUS_PULSE + HORIZONTAL_BACK_PORCH - 1;
    tli_init_struct.backpsz_vbpsz = VERTICAL_SYNCHRONOUS_PULSE + VERTICAL_BACK_PORCH - 1;
    tli_init_struct.activesz_hasz = HORIZONTAL_SYNCHRONOUS_PULSE + HORIZONTAL_BACK_PORCH + ACTIVE_WIDTH - 1;
    tli_init_struct.activesz_vasz = VERTICAL_SYNCHRONOUS_PULSE + VERTICAL_BACK_PORCH + ACTIVE_HEIGHT - 1;
    tli_init_struct.totalsz_htsz  = HORIZONTAL_SYNCHRONOUS_PULSE + HORIZONTAL_BACK_PORCH + ACTIVE_WIDTH + HORIZONTAL_FRONT_PORCH - 1;
    tli_init_struct.totalsz_vtsz  = VERTICAL_SYNCHRONOUS_PULSE + VERTICAL_BACK_PORCH + ACTIVE_HEIGHT + VERTICAL_FRONT_PORCH - 1;
    /* configure LCD background R,G,B values */
    tli_init_struct.backcolor_red   = 0xFF;
    tli_init_struct.backcolor_green = 0xFF;
    tli_init_struct.backcolor_blue  = 0xFF;
    tli_init(&tli_init_struct);

#if 1
    /* TLI layer0 configuration */
    /* TLI window size configuration */
    tli_layer_init_struct.layer_window_leftpos   = HORIZONTAL_SYNCHRONOUS_PULSE + HORIZONTAL_BACK_PORCH;
    tli_layer_init_struct.layer_window_rightpos  = (ACTIVE_WIDTH + HORIZONTAL_SYNCHRONOUS_PULSE + HORIZONTAL_BACK_PORCH - 1);
    tli_layer_init_struct.layer_window_toppos    = VERTICAL_SYNCHRONOUS_PULSE + VERTICAL_BACK_PORCH;
    tli_layer_init_struct.layer_window_bottompos = (ACTIVE_HEIGHT + VERTICAL_SYNCHRONOUS_PULSE + VERTICAL_BACK_PORCH - 1);
    /* TLI window pixel format configuration */
    tli_layer_init_struct.layer_ppf = LAYER_PPF_RGB565;
    /* TLI window specified alpha configuration */
    tli_layer_init_struct.layer_sa = 255;
    /* TLI layer default alpha R,G,B value configuration */
    tli_layer_init_struct.layer_default_blue  = 0x00;
    tli_layer_init_struct.layer_default_green = 0x00;
    tli_layer_init_struct.layer_default_red   = 0x00;
    tli_layer_init_struct.layer_default_alpha = 0x00;
    /* TLI window blend configuration */
    tli_layer_init_struct.layer_acf1 = LAYER_ACF1_SA;
    tli_layer_init_struct.layer_acf2 = LAYER_ACF2_SA;
    /* TLI layer frame buffer base address configuration */
    tli_layer_init_struct.layer_frame_bufaddr           = (uint32_t)ltdc_lcd_framebuf0;
    tli_layer_init_struct.layer_frame_line_length       = ((ACTIVE_WIDTH * 2) + 3);
    tli_layer_init_struct.layer_frame_buf_stride_offset = (ACTIVE_WIDTH * 2);
    tli_layer_init_struct.layer_frame_total_line_number = ACTIVE_HEIGHT;
    tli_layer_init(LAYER0, &tli_layer_init_struct);
    tli_dither_config(TLI_DITHER_ENABLE);
#endif
}

/**
 * @brief    Initialize the IPA peripheral.
 *
 * @note     Enable clock and interrupt for the IPA module.
 *
 * @param    None
 *
 * @return   None
 */
static void ipa_config(void)
{
    rcu_periph_clock_enable(RCU_IPA);
    nvic_irq_enable(IPA_IRQn, 0, 2);
}

/**
 * @brief    Configure the GPIO pins used for TLI display.
 *
 * @note     Includes alternate function mapping and output mode setup
 *           for RGB and sync signals.
 *
 * @param    None
 *
 * @return   None
 */
static void tli_gpio_config(void)
{
    /* enable the periphral clock */
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_GPIOC);
    rcu_periph_clock_enable(RCU_GPIOD);
    rcu_periph_clock_enable(RCU_GPIOF);
    rcu_periph_clock_enable(RCU_GPIOG);

    /* configure HSYNC(PC6), VSYNC(PA4), PCLK(PG7), DE(PF10) */
    /* configure LCD_R7(PG6), LCD_R6(PA8), LCD_R5(PA12), LCD_R4(PA11), LCD_R3(PB0),
                 LCD_G7(PD3), LCD_G6(PC7), LCD_G5(PB11), LCD_G4(PB10), LCD_G3(PG10), LCD_G2(PA6),
                 LCD_B7(PB9), LCD_B6(PB8), LCD_B5(PA3), LCD_B4(PG12), LCD_B3(PG11) */
    gpio_af_set(GPIOA, GPIO_AF_14, GPIO_PIN_3);
    gpio_af_set(GPIOA, GPIO_AF_14, GPIO_PIN_4);
    gpio_af_set(GPIOA, GPIO_AF_14, GPIO_PIN_6);
    gpio_af_set(GPIOA, GPIO_AF_14, GPIO_PIN_8);
    gpio_af_set(GPIOA, GPIO_AF_14, GPIO_PIN_11);
    gpio_af_set(GPIOA, GPIO_AF_14, GPIO_PIN_12);

    gpio_af_set(GPIOB, GPIO_AF_9, GPIO_PIN_0);
    gpio_af_set(GPIOB, GPIO_AF_14, GPIO_PIN_8);
    gpio_af_set(GPIOB, GPIO_AF_14, GPIO_PIN_9);
    gpio_af_set(GPIOB, GPIO_AF_14, GPIO_PIN_10);
    gpio_af_set(GPIOB, GPIO_AF_14, GPIO_PIN_11);

    gpio_af_set(GPIOC, GPIO_AF_14, GPIO_PIN_6);
    gpio_af_set(GPIOC, GPIO_AF_14, GPIO_PIN_7);

    gpio_af_set(GPIOD, GPIO_AF_14, GPIO_PIN_3);

    gpio_af_set(GPIOF, GPIO_AF_14, GPIO_PIN_10);

    gpio_af_set(GPIOG, GPIO_AF_14, GPIO_PIN_6);
    gpio_af_set(GPIOG, GPIO_AF_14, GPIO_PIN_7);
    gpio_af_set(GPIOG, GPIO_AF_9, GPIO_PIN_10);
    gpio_af_set(GPIOG, GPIO_AF_14, GPIO_PIN_11);
    gpio_af_set(GPIOG, GPIO_AF_9, GPIO_PIN_12);

    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_6 | GPIO_PIN_8 | GPIO_PIN_11 | GPIO_PIN_12);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_6 | GPIO_PIN_8 | GPIO_PIN_11 | GPIO_PIN_12);

    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_0 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11);

    gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6 | GPIO_PIN_7);
    gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7);

    gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3);
    gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);

    gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
    gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);

    gpio_mode_set(GPIOG, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12);
    gpio_output_options_set(GPIOG, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12);
}

/**
 * @brief    Configure the GPIO pin for DISP enable control.
 *
 * @note     This function enables the clock for GPIOD and sets up pin 13
 *           as push-pull output to control the display on/off signal.
 *
 * @param    None
 *
 * @return   None
 *
 * @warning  Must be called before attempting to control the LCD display.
 */
static void lcd_disp_en_config(void)
{
    /* enable the periphral clock */
    rcu_periph_clock_enable(RCU_GPIOD);
    gpio_mode_set(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_13);
    gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
}

/**
 * @brief    Turn off the LCD display by resetting the DISP GPIO.
 *
 * @note     This function clears the DISP control pin to disable
 *           the LCD backlight or power depending on hardware design.
 *
 * @param    None
 *
 * @return   None
 *
 * @warning  Only valid if the DISP GPIO has been previously configured.
 */
static void lcd_disp_off(void)
{
    gpio_bit_reset(GPIOD, GPIO_PIN_13);
}

/**
 * @brief    Turn on the LCD display by setting the DISP GPIO.
 *
 * @note     This function sets the DISP control pin to enable
 *           the LCD backlight or power depending on hardware design.
 *
 * @param    None
 *
 * @return   None
 *
 * @warning  Only valid if the DISP GPIO has been previously configured.
 */
static void lcd_disp_on(void)
{
    gpio_bit_set(GPIOD, GPIO_PIN_13);
}


/**
 * @brief    Draw a pixel on the LCD at specified coordinates.
 *
 * @note     This function writes a color value directly to the framebuffer.
 *           Coordinates must be within the valid LCD dimensions.
 *
 * @param    x      Horizontal coordinate of the pixel.
 * @param    y      Vertical coordinate of the pixel.
 * @param    color  Color of the pixel in RGB565 format.
 *
 * @return   None
 */
void tli_draw_point(uint16_t x, uint16_t y, uint16_t color)
{
    *(ltdc_lcd_framebuf0[0] + (LCD_WIDTH * y + x)) = color;
}

/**
 * @brief    Draw a straight line between two points.
 *
 * @note     Uses Bresenham's line drawing algorithm to connect two points.
 *           Coordinates should be within LCD bounds.
 *
 * @param    sx      Start point X coordinate.
 * @param    sy      Start point Y coordinate.
 * @param    ex      End point X coordinate.
 * @param    ey      End point Y coordinate.
 * @param    color   Line color in RGB565 format.
 *
 * @return   None
 */
void tli_draw_line(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint16_t color)
{
    uint16_t t;
    int      xerr = 0, yerr = 0, delta_x, delta_y, distance;
    int      incx, incy, uRow, uCol;

    delta_x = ex - sx;
    delta_y = ey - sy;
    uRow    = sx;
    uCol    = sy;
    if (delta_x > 0)
        incx = 1;
    else if (delta_x == 0)
        incx = 0;
    else
    {
        incx    = -1;
        delta_x = -delta_x;
    }

    if (delta_y > 0)
        incy = 1;
    else if (delta_y == 0)
        incy = 0;
    else
    {
        incy    = -1;
        delta_y = -delta_y;
    }

    if (delta_x > delta_y)
        distance = delta_x;
    else
        distance = delta_y;
    for (t = 0; t <= distance + 1; t++)
    {
        tli_draw_point(uRow, uCol, color);
        xerr += delta_x;
        yerr += delta_y;
        if (xerr > distance)
        {
            xerr -= distance;
            uRow += incx;
        }

        if (yerr > distance)
        {
            yerr -= distance;
            uCol += incy;
        }
    }
}
/**
 * @brief    Draw a rectangle on the screen, optionally filled.
 *
 * @note     The rectangle is defined by top-left and bottom-right corners.
 *           If fill is set to 1, the rectangle will be filled with the color.
 *
 * @param    sx      X coordinate of the top-left corner.
 * @param    sy      Y coordinate of the top-left corner.
 * @param    ex      X coordinate of the bottom-right corner.
 * @param    ey      Y coordinate of the bottom-right corner.
 * @param    color   Color of the rectangle in RGB565 format.
 * @param    fill    Fill mode: 1 = filled, 0 = only borders.
 *
 * @return   None
 */
void tli_draw_rectangle(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint16_t color, uint16_t fill)
{
    int i = 0, j = 0;
    if (fill)
    {
        for (i = sx; i < ex; i++)
        {
            for (j = sy; j < ey; j++)
            {
                tli_draw_point(i, j, color);
            }
        }
    }
    else
    {
        tli_draw_line(sx, sy, ex, sy, color);
        tli_draw_line(sx, sy, sx, ey, color);
        tli_draw_line(sx, ey, ex, ey, color);
        tli_draw_line(ex, sy, ex, ey, color);
    }
}

/**
 * @brief    Draw eight symmetric points of a circle.
 *
 * @note     This function is used internally to draw points in all eight
 *           symmetrical positions around a circle center.
 *
 * @param    xc      X coordinate of the circle center.
 * @param    yc      Y coordinate of the circle center.
 * @param    x       Relative X offset from the center.
 * @param    y       Relative Y offset from the center.
 * @param    c       Color of the point in RGB565 format.
 *
 * @return   None
 */

static void _draw_circle_8(int xc, int yc, int x, int y, uint16_t c)
{
    tli_draw_point(xc + x, yc + y, c);

    tli_draw_point(xc - x, yc + y, c);

    tli_draw_point(xc + x, yc - y, c);

    tli_draw_point(xc - x, yc - y, c);

    tli_draw_point(xc + y, yc + x, c);

    tli_draw_point(xc - y, yc + x, c);

    tli_draw_point(xc + y, yc - x, c);

    tli_draw_point(xc - y, yc - x, c);
}

/**
 * @brief    Draw a circle on the screen with optional fill.
 *
 * @note     Uses the midpoint circle algorithm to render the circle.
 *           When fill is enabled, the circle is drawn as a filled disk.
 *
 * @param    xc      X coordinate of the circle center.
 * @param    yc      Y coordinate of the circle center.
 * @param    c       Color of the circle in RGB565 format.
 * @param    r       Radius of the circle.
 * @param    fill    Fill mode: 1 = filled, 0 = outline only.
 *
 * @return   None
 */
void tli_draw_circle(int xc, int yc, uint16_t c, int r, int fill)
{
    int x = 0, y = r, yi, d;

    d = 3 - 2 * r;

    if (fill)
    {
        while (x <= y)
        {
            for (yi = x; yi <= y; yi++)
                _draw_circle_8(xc, yc, x, yi, c);

            if (d < 0)
            {
                d = d + 4 * x + 6;
            }
            else
            {
                d = d + 4 * (x - y) + 10;
                y--;
            }
            x++;
        }
    }
    else
    {
        while (x <= y)
        {
            _draw_circle_8(xc, yc, x, y, c);
            if (d < 0)
            {
                d = d + 4 * x + 6;
            }
            else
            {
                d = d + 4 * (x - y) + 10;
                y--;
            }
            x++;
        }
    }
}

/**
 * @brief    Swap the values of two 16-bit unsigned integers.
 *
 * @note     This utility function is used internally to simplify sorting
 *           or reordering logic, such as coordinate alignment.
 *
 * @param    a   Pointer to the first variable.
 * @param    b   Pointer to the second variable.
 *
 * @return   None
 */
static void _swap(uint16_t *a, uint16_t *b)
{
    uint16_t tmp;
    tmp = *a;
    *a  = *b;
    *b  = tmp;
}

/**
 * @brief    Draw a triangle with optional fill.
 *
 * @note     The triangle is defined by three vertex points. When fill is set,
 *           it is rendered as a filled triangle using scan-line rasterization.
 *
 * @param    x0      X coordinate of the first vertex.
 * @param    y0      Y coordinate of the first vertex.
 * @param    x1      X coordinate of the second vertex (typically the top).
 * @param    y1      Y coordinate of the second vertex.
 * @param    x2      X coordinate of the third vertex.
 * @param    y2      Y coordinate of the third vertex.
 * @param    color   Color of the triangle in RGB565 format.
 * @param    fill    Fill mode: 1 = filled, 0 = outline only.
 *
 * @return   None
 */
void tli_draw_triangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color, uint16_t fill)
{
    uint16_t a, b, y, last;
    int      dx01, dy01, dx02, dy02, dx12, dy12;
    long     sa = 0;
    long     sb = 0;

    if (fill == 0)
    {
        tli_draw_line(x0, y0, x1, y1, color);
        tli_draw_line(x1, y1, x2, y2, color);
        tli_draw_line(x2, y2, x0, y0, color);
    }
    else
    {
        if (y0 > y1)
        {
            _swap(&y0, &y1);
            _swap(&x0, &x1);
        }

        if (y1 > y2)
        {
            _swap(&y2, &y1);
            _swap(&x2, &x1);
        }

        if (y0 > y1)
        {
            _swap(&y0, &y1);
            _swap(&x0, &x1);
        }

        if (y0 == y2)
        {
            a = b = x0;
            if (x1 < a)
            {
                a = x1;
            }
            else if (x1 > b)
            {
                b = x1;
            }

            if (x2 < a)
            {
                a = x2;
            }
            else if (x2 > b)
            {
                b = x2;
            }
            tli_draw_rectangle(a, y0, b, y0, color, 0);
            return;
        }
        dx01 = x1 - x0;
        dy01 = y1 - y0;
        dx02 = x2 - x0;
        dy02 = y2 - y0;
        dx12 = x2 - x1;
        dy12 = y2 - y1;

        if (y1 == y2)
        {
            last = y1;
        }
        else
        {
            last = y1 - 1;
        }

        for (y = y0; y <= last; y++)
        {
            a   = x0 + sa / dy01;
            b   = x0 + sb / dy02;
            sa += dx01;
            sb += dx02;
            if (a > b)
            {
                _swap(&a, &b);
            }
            tli_draw_rectangle(a, y, b, y, color, 0);
        }
        sa = dx12 * (y - y1);
        sb = dx02 * (y - y0);
        for (; y <= y2; y++)
        {
            a   = x1 + sa / dy12;
            b   = x0 + sb / dy02;
            sa += dx12;
            sb += dx02;
            if (a > b)
            {
                _swap(&a, &b);
            }

            tli_draw_rectangle(a, y, b, y, color, 0);
        }
    }
}


/**
 * @brief    Enlarge a single point by drawing a square block.
 *
 * @note     The enlarged point is drawn as a 2D cross pattern made of four
 *           rectangles centered at (x, y) with specified magnification.
 *
 * @param    x         X coordinate of the center point.
 * @param    y         Y coordinate of the center point.
 * @param    color     Color to fill the enlarged point.
 * @param    magnify   Enlargement factor (minimum value is 1).
 *
 * @return   None
 */
void tli_point_enlarge(uint16_t x, uint16_t y, uint16_t color, char magnify)
{
    tli_draw_rectangle(x - magnify, y - magnify, x, y, color, 1);

    tli_draw_rectangle(x, y - magnify, x + magnify, y, color, 1);

    tli_draw_rectangle(x - magnify, y, x, y + magnify, color, 1);

    tli_draw_rectangle(x, y, x + magnify, y + magnify, color, 1);
}

/**
 * @brief    Display a single ASCII character at the specified position.
 *
 * @note     Uses a fixed-size 16x8 font to render the character with optional
 *           enlargement and overlay mode.
 *
 * @param    x      X coordinate of the character start position.
 * @param    y      Y coordinate of the character start position.
 * @param    fc     Foreground color.
 * @param    bc     Background color.
 * @param    num    ASCII character to display.
 * @param    size   Enlargement factor (minimum 1).
 * @param    mode   Overlay mode: 0 = non-overlay, 1 = overlay.
 *
 * @return   None
 */
void tli_show_char(uint16_t x, uint16_t y, uint16_t fc, uint16_t bc, uint8_t num, uint8_t size, uint8_t mode)
{
    uint8_t  temp;
    uint8_t  pos, t;
    uint16_t x0 = 0;
    uint16_t y0 = 0;
    num         = num - ' ';

    for (pos = 0; pos < 16; pos++)
    {
        temp = asc2_1608[num][pos];
        for (t = 0; t < 16 / 2; t++)
        {
            if (!mode)
            {
                if (temp & 0x01)
                    tli_point_enlarge(x + x0, y + y0, fc, size);
                else
                    tli_point_enlarge(x + x0, y + y0, bc, size);
            }
            else
            {
                if (temp & 0x01) tli_point_enlarge(x + x0, y + y0, fc, size);
            }
            temp >>= 1;
            x0     = x0 + size;
        }
        x0 = 0;
        y0 = y0 + size;
    }
}

/**
 * @brief    Display a null-terminated ASCII string on the screen.
 *
 * @note     Automatically handles line boundaries and skips illegal characters.
 *
 * @param    x      X coordinate of the start position.
 * @param    y      Y coordinate of the start position.
 * @param    fc     Foreground color.
 * @param    bc     Background color.
 * @param    size   Font enlargement factor (base size is 16x8).
 * @param    p      Pointer to the string.
 * @param    mode   Overlay mode: 0 = non-overlay, 1 = overlay.
 *
 * @return   None
 */
void tli_show_string(uint16_t x, uint16_t y, uint16_t fc, uint16_t bc, uint8_t size, uint8_t *p, uint8_t mode)
{
    while ((*p <= '~') && (*p >= ' '))
    {
        if (x > (LCD_WIDTH - 1) || y > (LCD_HEIGHT - 1)) return;

        tli_show_char(x, y, fc, bc, *p, size, mode);
        x += 16 * size / 2;
        p++;
    }
}


/**
 * @brief    Display a picture at a specified position on the screen.
 *
 * @note     The image data is expected to be in RGB565 format, with each pixel
 *           occupying two consecutive bytes in the array.
 *
 * @param    x      X coordinate of the top-left corner.
 * @param    y      Y coordinate of the top-left corner.
 * @param    w      Width of the image in pixels.
 * @param    h      Height of the image in pixels.
 * @param    pic    Pointer to the image data array.
 *
 * @return   None
 */
void tli_show_picture(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const unsigned char pic[])
{
    uint16_t i, j;
    uint32_t k  = 0;
    uint16_t x0 = x, y0 = y;
    for (i = y; i < h + y0; i++)
    {
        for (j = x; j < w + x0; j++)
        {
            tli_draw_point(j, i, pic[k * 2] << 8 | pic[k * 2 + 1]);
            k++;
        }
    }
}


/**
 * @brief    Draw a filled rounded rectangle button.
 *
 * @note     This function draws a button with four rounded corners and fills
 *           the background. The radius must be less than half of the height.
 *
 * @param    x        X coordinate of the top-left corner.
 * @param    y        Y coordinate of the top-left corner.
 * @param    width    Width of the button.
 * @param    height   Height of the button.
 * @param    radius   Radius of the corners.
 * @param    color    Fill color of the button.
 *
 * @return   None
 */
void tli_show_button(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t radius, uint16_t color)
{
    int i = 0;
    int j = 0;


    if (radius >= (height / 2)) return;


    tli_draw_circle(x + radius, y + radius, color, radius, 1);
    tli_draw_circle(x + width - radius - 1, y + radius, color, radius, 1);
    tli_draw_circle(x + radius, y + height - radius - 1, color, radius, 1);
    tli_draw_circle(x + width - radius - 1, y + height - radius - 1, color, radius, 1);

    tli_draw_line(x + radius, y, x + width - radius - 1, y, color);
    tli_draw_line(x + radius, y + height - 1, x + width - radius - 1, y + height - 1, color);
    tli_draw_line(x, y + radius, x, y + height - radius - 1, color);
    tli_draw_line(x + width - 1, y + radius, x + width - 1, y + height - radius - 1, color);

    tli_draw_rectangle(x + radius, y + radius, x + width - radius, y + height - radius, color, 1);
    tli_draw_rectangle(x + radius, y, x + width - radius, y + radius, color, 1);
    tli_draw_rectangle(x, y + radius, x + width, y + height - radius, color, 1);
    tli_draw_rectangle(x + radius, y + height - radius, x + width - radius, y + height, color, 1);
}

/**
 * @brief    Draw a switch (toggle) component with visual state.
 *
 * @note     The switch has a rounded rectangle base with a circular knob
 *           indicating on or off state. Automatically validates layout rules.
 *
 * @param    x           X coordinate of the top-left corner.
 * @param    y           Y coordinate of the top-left corner.
 * @param    width       Total width of the switch.
 * @param    height      Total height of the switch.
 * @param    on_color    Color of the switch when turned on.
 * @param    off_color   Color of the switch when turned off.
 * @param    sw          Switch state: 1 = on, 0 = off.
 *
 * @return   None
 */
void tli_show_switch(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t on_color, uint16_t off_color, char sw)
{
    int radius = height / 2 - 1;

    if (width < 2 * (radius + 1)) return;

    if (sw == 0)
    {
        tli_show_button(x, y, width, height, radius, off_color);
        tli_draw_circle(x + radius, y + radius, WHITE, radius / 2, 1);
    }
    else
    {
        tli_show_button(x, y, width, height, radius, on_color);
        tli_draw_circle(x + width - radius, y + radius, WHITE, radius / 2, 1);
    }
}