draw_panel.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2006-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-05-23 godmial Refactor to conform to RT-Thread coding style.
  9. */
  10. #include <board.h>
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include "drv_lcd.h"
  14. #include "drv_touch.h"
  15. #define COLOR_MAX 14
  16. /* Maximum magnification of pixels */
  17. #define PIXEL_SIZE_MAX 20
  18. typedef struct button_struct
  19. {
  20. uint16_t x;
  21. uint16_t y;
  22. uint16_t w;
  23. uint16_t h;
  24. uint16_t color;
  25. uint16_t value;
  26. } widget_object_t;
  27. uint16_t color_buf[COLOR_MAX] = {GRAYBLUE, BLACK, BLUE, BRED, GRED, GBLUE, RED, MAGENTA, GREEN, YELLOW, CYAN, BROWN, BRRED, GRAY};
  28. widget_object_t button_clear, button_color, button_pixel, button_eraser;
  29. /**
  30. * @brief Initialize a widget button with given properties.
  31. *
  32. * @param obj Pointer to the widget object.
  33. * @param x X coordinate of the top-left corner.
  34. * @param y Y coordinate of the top-left corner.
  35. * @param w Width of the widget.
  36. * @param h Height of the widget.
  37. * @param color Color value of the widget.
  38. * @param value Initial value of the widget.
  39. *
  40. * @return None.
  41. */
  42. void widget_object_init(widget_object_t *obj, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color, uint16_t value)
  43. {
  44. obj->x = x;
  45. obj->y = y;
  46. obj->h = h;
  47. obj->w = w;
  48. obj->color = color;
  49. obj->value = value;
  50. }
  51. /**
  52. * @brief Initialize and display the user interface.
  53. *
  54. * This function draws the control buttons for clear, color,
  55. * pixel size, and eraser on the screen.
  56. *
  57. * @return None.
  58. */
  59. void ui_init(void)
  60. {
  61. char temp_buf[20] = {0};
  62. tli_show_button(button_clear.x, button_clear.y, button_clear.w, button_clear.h, 12, button_clear.color);
  63. tli_show_string(button_clear.x + 20, button_clear.y + 20, WHITE, button_clear.color, 2, "clear", 0);
  64. tli_show_button(button_color.x, button_color.y, button_color.w, button_color.h, 12, button_color.color);
  65. tli_show_string(button_color.x + 20, button_color.y + 20, WHITE, button_color.color, 2, "color", 0);
  66. tli_show_button(button_pixel.x, button_pixel.y, button_pixel.w, button_pixel.h, 12, button_pixel.color);
  67. if (button_pixel.value == 0)
  68. {
  69. tli_show_string(button_pixel.x + 20, button_pixel.y + 20, WHITE, button_pixel.color, 2, "pixel", 0);
  70. button_pixel.value = 1;
  71. }
  72. else
  73. {
  74. sprintf(temp_buf, "%d", button_pixel.value);
  75. tli_show_string(button_pixel.x + (button_pixel.w / 2) - (strlen(temp_buf) / 2 * 16), button_pixel.y + 20, WHITE, button_pixel.color, 2, (uint8_t *)temp_buf, 0);
  76. }
  77. tli_show_button(button_eraser.x, button_eraser.y, button_eraser.w, button_eraser.h, 12, button_eraser.color);
  78. tli_show_string(button_eraser.x + (button_eraser.w / 2) - (strlen("eraser") / 2 * 16), button_eraser.y + 20, WHITE, button_eraser.color, 2, "eraser", 0);
  79. }
  80. /**
  81. * @brief Main routine to test the drawing panel with touch interaction.
  82. *
  83. * Initializes LCD and touch interface, sets up buttons and handles user input
  84. * to draw or erase on the screen based on touch events.
  85. *
  86. * @return Always returns 0.
  87. */
  88. int draw_panel_test(void)
  89. {
  90. int touch_state = 0;
  91. char color_num = 0;
  92. char pixel_size = 0;
  93. lcd_disp_config();
  94. FT5206_Init();
  95. tli_draw_rectangle(0, 0, 800, 480, WHITE, 1);
  96. widget_object_init(&button_clear, 800 - 130, 480 - 80, 120, 70, BLUE, 0);
  97. widget_object_init(&button_color, 10, 480 - 80, 120, 70, BLUE, 0);
  98. widget_object_init(&button_pixel, 400 - 60, 480 - 80, 120, 70, BLUE, pixel_size);
  99. widget_object_init(&button_eraser, 800 - 130 - 130, 480 - 80, 120, 70, BLUE, 0);
  100. ui_init();
  101. while (1)
  102. {
  103. touch_state = FT5206_Scan(0);
  104. if (touch_state == 1)
  105. {
  106. if (tp_dev.x[0] >= button_color.x && tp_dev.x[0] <= (button_color.x + button_color.w))
  107. {
  108. if (tp_dev.y[0] >= button_color.y && tp_dev.y[0] <= (button_color.y + button_color.h))
  109. {
  110. letgo_scan(0, button_color.x, button_color.y, (button_color.x + button_color.w), (button_color.y + button_color.h));
  111. color_num = (color_num + 1) % COLOR_MAX;
  112. widget_object_init(&button_color, button_color.x, button_color.y, button_color.w, button_color.h, color_buf[color_num], 0);
  113. ui_init();
  114. }
  115. }
  116. if (tp_dev.x[0] >= button_clear.x && tp_dev.x[0] <= (button_clear.x + button_clear.w))
  117. {
  118. if (tp_dev.y[0] >= button_clear.y && tp_dev.y[0] <= (button_clear.y + button_clear.h))
  119. {
  120. letgo_scan(0, button_clear.x, button_clear.y, (button_clear.x + button_clear.w), (button_clear.y + button_clear.h));
  121. tli_draw_rectangle(0, 0, 800, 480, WHITE, 1);
  122. ui_init();
  123. }
  124. }
  125. if (tp_dev.x[0] >= button_pixel.x && tp_dev.x[0] <= (button_pixel.x + button_pixel.w))
  126. {
  127. if (tp_dev.y[0] >= button_pixel.y && tp_dev.y[0] <= (button_pixel.y + button_pixel.h))
  128. {
  129. letgo_scan(0, button_pixel.x, button_pixel.y, (button_pixel.x + button_pixel.w), (button_pixel.y + button_pixel.h));
  130. pixel_size++;
  131. if (pixel_size > PIXEL_SIZE_MAX) pixel_size = 1;
  132. widget_object_init(&button_pixel, button_pixel.x, button_pixel.y, button_pixel.w, button_pixel.h, button_pixel.color, pixel_size);
  133. ui_init();
  134. }
  135. }
  136. if (tp_dev.x[0] >= button_eraser.x && tp_dev.x[0] <= (button_eraser.x + button_eraser.w))
  137. {
  138. if (tp_dev.y[0] >= button_eraser.y && tp_dev.y[0] <= (button_eraser.y + button_eraser.h))
  139. {
  140. letgo_scan(0, button_eraser.x, button_eraser.y, (button_eraser.x + button_eraser.w), (button_eraser.y + button_eraser.h));
  141. button_eraser.value = !button_eraser.value;
  142. if (button_eraser.value)
  143. {
  144. widget_object_init(&button_eraser, 800 - 130 - 130, 480 - 80, 120, 70, BLACK, button_eraser.value);
  145. }
  146. else
  147. {
  148. widget_object_init(&button_eraser, 800 - 130 - 130, 480 - 80, 120, 70, BLUE, button_eraser.value);
  149. }
  150. ui_init();
  151. }
  152. }
  153. if (!button_eraser.value)
  154. {
  155. tli_point_enlarge(tp_dev.x[0], tp_dev.y[0], color_buf[color_num], button_pixel.value);
  156. }
  157. else
  158. {
  159. tli_point_enlarge(tp_dev.x[0], tp_dev.y[0], WHITE, button_pixel.value);
  160. }
  161. }
  162. }
  163. }
  164. INIT_COMPONENT_EXPORT(draw_panel_test);