#include <U8g2lib.h>
#include <Keypad.h>
//#include <WiFi.h>
//#include "WiFiClient.h"
//------------------------------------------
#define SCL 22
#define SDA 21
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, SCL, SDA, U8X8_PIN_NONE);
//定义菜单大小
#define MENU_SIZE 5
//定义菜单
char *menu[MENU_SIZE] = {"Clock", "Setting", "Info", "Set clock", "Temp"};
//---------------------------------------
//定义NTP
//#define NTP "ntp.aliyun.com"
//定义时间信息
//struct tm timeinfo;
//定义WiFi账号
//const char *ssid = "Xiaomi 14 Pro";
//const char *password = "glc060204";
//----------------------------------------
//4*4矩阵键盘
const byte ROWS = 4;
const byte COLS = 4;
//定义行列引脚
byte rowPins[ROWS] = {13, 12, 14, 27};
byte colPins[COLS] = {5, 18, 19, 15};
//定义键盘布局
char keys[ROWS][COLS] {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// 初始化矩阵键盘的参数
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
//-------------------------------------------------
//初始化菜单状态
int menu_state = 0;
// 定义当前选项
unsigned int current_option = 0;
//定义当前状态
enum State {MENU, SELECTING};
State currentState = MENU;
//unsigned int current_state= 0;
//---------------
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
u8g2.begin();
displaymenu();
}
void loop() {
// put your main code here, to run repeatedly:
keypad_control();
}
void displaymenu() {
//u8g2.clearBuffer();
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_6x12_tr);
if (current_option == 0) {
u8g2.drawStr(5, 12, "> Clock");
u8g2.drawStr(20, 24, "Setting");
u8g2.drawStr(20, 36, "Info");
u8g2.drawStr(20, 48, "Set clock");
u8g2.drawStr(20, 60, "Temp");
}
else if (current_option == 1) {
u8g2.drawStr(20, 12, "Clock");
u8g2.drawStr(5, 24, "> Setting");
u8g2.drawStr(20, 36, "Info");
u8g2.drawStr(20, 48, "Set clock");
u8g2.drawStr(20, 60, "Temp");
}
else if (current_option == 2) {
u8g2.drawStr(20, 12, "Clock");
u8g2.drawStr(20, 24, "Setting");
u8g2.drawStr(5, 36, "> Info");
u8g2.drawStr(20, 48, "Set clock");
u8g2.drawStr(20, 60, "Temp");
}
else if (current_option == 3) {
u8g2.drawStr(20, 12, "Clock");
u8g2.drawStr(20, 24, "Setting");
u8g2.drawStr(20, 36, "Info");
u8g2.drawStr(5, 48, "> Set clock");
u8g2.drawStr(20, 60, "Temp");
}
else if (current_option == 4) {
u8g2.drawStr(20, 12, "Clock");
u8g2.drawStr(20, 24, "Setting");
u8g2.drawStr(20, 36, "Info");
u8g2.drawStr(20, 48, "Set clock");
u8g2.drawStr(5, 60, "> Temp");
}
} while (u8g2.nextPage());
//u8g2.sendBuffer();
}
void keypad_control() {
char key = keypad.getKey();
if (key != NO_KEY) {
Serial.println(key);
if (key == 'A') { //向上
current_option++;
if (current_option > MENU_SIZE)
current_option = MENU_SIZE;
displaymenu();
}
else if (key == 'B') {
current_option--;
if (current_option < 0)
current_option = 0;
displaymenu();
}
}
}