#include <U8g2lib.h>
#include <Wire.h>
// 定义屏幕驱动和尺寸
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// 菜单列表结构体
typedef struct {
char str[3];
} MENU_LIST;
// 菜单列表数组
MENU_LIST menuList[] = {
{"1"}, {"2"}, {"3"}, {"4"}, {"5"}, {"6"}, {"7"}, {"8"}, {"9"}, {"10"}
};
// 定义全局变量
short x = 0, x_target = 0;
short y = 0, y_target = 0;
int state = 0;
int menuLength = sizeof(menuList) / sizeof(MENU_LIST);
int menuIndex = 0;
void setup(void) {
// 初始化显示器
u8g2.begin();
u8g2.setFont(u8g2_font_ncenB08_tr);
}
void loop(void) {
// 清空显示缓存
u8g2.firstPage();
do {
// 显示菜单内容
for (int i = 0; i < menuLength ; i++) {
u8g2.drawStr(x + 42, y + i * 10, menuList[(i + menuIndex) % menuLength].str);
}
// 更新菜单移动状态
if ((cd_run(y, y_target)) == 0) {
y_target -= 10;
state = 0;
}
} while (u8g2.nextPage());
// 延迟一定时间再次刷新菜单
delay(20);
}
// 定义菜单移动算法
int cd_run(short &a, short a_target) {
if (a < a_target) {
a += (a_target - a) / 2 + 1;
} else if (a > a_target) {
a -= (a - a_target) / 2 + 1;
} else {
return 0;
}
return 1;
}