stm32f103 TFT屏幕 链表上下滚动代码
时间: 2023-06-28 07:10:57 浏览: 63
以下是一个基于STM32F103芯片的TFT屏幕上下滚动的链表代码示例。
首先,我们需要定义一个链表结构来存储我们要显示的文本。该结构包含一个指向下一个节点的指针以及一个指向文本字符串的指针。
```c
typedef struct _text_node {
char *text;
struct _text_node *next;
} text_node_t;
```
然后,我们需要编写一个函数来将新文本添加到链表的末尾。
```c
void add_text_node(text_node_t **head, char *text) {
text_node_t *new_node = (text_node_t*) malloc(sizeof(text_node_t));
new_node->text = text;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
return;
}
text_node_t *last_node = *head;
while (last_node->next != NULL) {
last_node = last_node->next;
}
last_node->next = new_node;
}
```
接下来,我们需要编写一个函数来在TFT屏幕上显示链表中的文本。该函数将从链表的当前节点开始,向下滚动并逐个显示每个节点。
```c
void display_text_list(text_node_t *head, int x, int y, int line_height, int num_lines) {
text_node_t *current_node = head;
int line = 0;
while (current_node != NULL && line < num_lines) {
// 清除当前行
tft_fill_rect(x, y + (line * line_height), tft_get_width(), line_height, TFT_BLACK);
// 显示文本
tft_draw_string(current_node->text, x, y + (line * line_height), TFT_WHITE);
// 下一个节点
current_node = current_node->next;
line++;
}
}
```
最后,我们需要编写一个函数来处理按下按钮的事件,该按钮将向上或向下滚动显示的文本。
```c
void scroll_text_list(text_node_t **head, int *scroll_index, int num_lines) {
if (*head == NULL) {
return;
}
if (*scroll_index < 0) {
*scroll_index = 0;
}
int num_nodes = 0;
text_node_t *current_node = *head;
while (current_node != NULL) {
num_nodes++;
current_node = current_node->next;
}
if ((*scroll_index + num_lines) > num_nodes) {
*scroll_index = num_nodes - num_lines;
}
display_text_list(*head, 0, 0, 20, num_lines);
}
```
这些函数可以组合使用,以实现在TFT屏幕上显示链表中的文本,并在按下按钮时向上或向下滚动。