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