#include <TFT_eSPI.h>
#include <Arduino.h>
#include "OneButton.h"
#define PIN_INPUT 0
#define PIN_LED 2
OneButton button(PIN_INPUT, true);
//TFT
TFT_eSPI tft = TFT_eSPI();
//全局变量
int sWidth = TFT_WIDTH; //屏幕宽度
int sHeight = TFT_HEIGHT; //屏幕高度
int bgColor = TFT_BLACK; //背景颜色
int fgColor = TFT_ORANGE; //标题颜色
unsigned char keyValue = 0;
//定义按键结构体
typedef struct
{
unsigned char index;
unsigned char up;
unsigned char down;
unsigned char left;
unsigned char right;
void (*operation)(void);
} KEY_TABLE;
//定义日期时间结构体变量
unsigned char funIndex = 0;
void (*current)(void);
void menu11(void);
void menu12(void);
void menu21(void);
void menu22(void);
void menu23(void);
void menu31(void);
void menu32(void);
void menu33(void);
void menu34(void);
void showNum(String text, int16_t x, int16_t y, uint16_t color) {
tft.setTextSize(1);
tft.setTextColor(color);
tft.drawString(text, x, y, 6);
//tft.setFreeFont(&Orbitron_Light_32);
//tft.drawString(text, x, y);
}
//定义按键操作数据
KEY_TABLE table[9] =
{
{0, 0, 1, 0, 2, (*menu11)},
{1, 0, 1, 1, 4, (*menu12)},
{2, 2, 3, 0, 5, (*menu21)},
{3, 2, 3, 0, 7, (*menu22)},
{4, 4, 4, 1, 4, (*menu23)},
{5, 5, 6, 2, 5, (*menu31)},
{6, 5, 6, 2, 6, (*menu32)},
{7, 7, 8, 3, 7, (*menu33)},
{8, 7, 8, 3, 8, (*menu34)},
};
//一级菜单1
void menu11(void)
{
tft.fillScreen(fgColor);
showNum("1", 5, 50,bgColor );
}
//一级菜单2
void menu12(void)
{
tft.fillScreen(fgColor);
showNum("2", 5, 100,bgColor );
}
//二级菜单1
void menu21(void)
{
tft.fillScreen(fgColor);
showNum("21", 5, 100,bgColor );
}
//二级菜单2
void menu22(void)
{
tft.fillScreen(fgColor);
showNum("22", 5, 100,bgColor );
}
//二级菜单3
void menu23(void)
{
tft.fillScreen(fgColor);
showNum("23", 5, 100,bgColor );
}
//三级菜单1
void menu31(void)
{
tft.fillScreen(fgColor);
showNum("31", 5, 100,bgColor );
}
//三级菜单2
void menu32(void)
{
tft.fillScreen(fgColor);
showNum("32", 5, 100,bgColor );
}
//三级菜单3
void menu33(void)
{
tft.fillScreen(fgColor);
showNum("33", 5, 100,bgColor );
}
//三级菜单4
void menu34(void)
{
tft.fillScreen(fgColor);
showNum("34", 5, 100,bgColor );
}
//按键扫描函数
//*******************按键控制*************************
void doubleclick()
{
funIndex = table[funIndex].down;
}
void click()
{
funIndex = table[funIndex].right;
}
void longclick()
{
funIndex = table[funIndex].left;
}
//*********************END***************************
void setup()
{
//初始化TFT
tft.init();
//缺少这个位图颜色会反转
tft.setSwapBytes(true);
tft.setTextSize(1);
tft.setTextColor(bgColor);
tft.fillScreen(fgColor);
button.attachClick(click);
button.attachDoubleClick(doubleclick);
button.attachLongPressStart(longclick);
menu11();
}
void loop()
{
//keyValue = keyScan();
button.tick();
current = table[funIndex].operation;//根据需要获取对应需要执行的函数
(*current)();//执行获取到的函数
}
单按键控制进入多级菜单 仅作为个人代码记录