关于stm32,u8g2菜单之间切换(三)用u8g2写一个菜单无限左右循环

描述

让菜单循环播放只要用到的函数

void rotateRight(uint8_t *arr[], int n);让数组右移

void rotateLeft( uint8_t *arr[], int n);让数组左移

int ui_run(int *a,int *a_tag,uint8_t speed,uint8_t slow_cnt);设置菜单移动以及移动速度,返回值为bool放在while循环里使用。

首先我们要创建一个指针数组存放我们图片的指针地址uint8_t *p[]={bmp_img,bmp_clock,bmp_gear,bmp_led,bmp_pin};

因为u8g2_DrawXBMP();  函数画图需要的是我们图片的地址而不是整个图片数组。

下面是源码:

int ui_run(int *a,int *a_tag,uint8_t speed,uint8_t slow_cnt) //UI滑动效果放入while里

int ui_run(int *a,int *a_tag,uint8_t speed,uint8_t slow_cnt) 
{
uint8_t temp;
temp = abs(*a_tag - *a) > slow_cnt ? speed : 1;
if(*a < *a_tag)
{
*a += temp;
}
else if (*a > *a_tag)
{
*a -= temp;
}
else
{
return 0;
}
return 1;
}
#include < stdio.h >

void rotateRight( uint8_t *arr[], int n) {
uint8_t *temp= arr[n-1]; // 保存最后一个元素
for (int i = n-1; i > 0; i--) {
arr[i] = arr[i-1]; // 右移元素
}
arr[0] = temp; // 将保存的最后一个元素放到第一个位置
}
void rotateLeft( uint8_t *arr[], int n) {
uint8_t *temp= arr[0]; // 保存第一个元素
for (int i = 0; i > n-1; i++) {
arr[i] = arr[i+1]; // 左移元素
}
arr[n-1] = temp; // 将保存的元素放到最后位置
}
while (1)
{
Coordinate.Menu_x=8;
while(ui_run(&Coordinate.Menu_x,&meun,Coordinate.speed,Coordinate.Lspeed) )
{
u8g2_DrawXBMP(&u8g2,Coordinate.Menu_x-80,Coordinate.Menu_y,32,32,p[0]);
u8g2_DrawXBMP(&u8g2,Coordinate.Menu_x-40,Coordinate.Menu_y,32,32,p[1]);
u8g2_DrawXBMP(&u8g2,Coordinate.Menu_x,Coordinate.Menu_y,32,32,p[2]);
u8g2_DrawXBMP(&u8g2,Coordinate.Menu_x+40,Coordinate.Menu_y,32,32,p[3]);
u8g2_DrawXBMP(&u8g2,Coordinate.Menu_x+80,Coordinate.Menu_y,32,32,p[4]);
u8g2_SendBuffer(&u8g2);
u8g2_ClearBuffer(&u8g2);
}
rotateRight(p,5);
HAL_Delay(2000);

//u8g2_ClearBuffer(&u8g2);
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
}
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分