#include <Wire.h>
#include <GyverOLED.h>
#include <GyverDS3231.h>
GyverDS3231 ds;
GyverOLED<SSD1306_128x64, OLED_NO_BUFFER> oled;
#define BTN_1 14
#define BTN_2 15
#define BTN_3 16
#define BTN_4 17
#define MENU_MAIN 0
#define MENU_FAN 1
#define MENU_TRAFFIC 2
#define MENU_DS3231 3
int currentMenu = MENU_MAIN;
int submenuItemIndex = 0;
int previousMenu = MENU_MAIN;
int previousSubmenuItemIndex = 0;
char newkey, oldkey;
int ledState = 0;
int nowtimedis = 0;
void switchkey() {
newkey = ReadKey();
if (newkey != oldkey) {
oled.update();
// 處理子選單索引變更
if (newkey == 1) {
submenuItemIndex = (2 + submenuItemIndex) % 3;
}
if (newkey == 8) {
submenuItemIndex = (1 + submenuItemIndex) % 3;
}
// 按下選擇按鍵 (newkey == 2)
if (newkey == 2) {
switch (currentMenu) {
case MENU_MAIN:
previousMenu = currentMenu;
previousSubmenuItemIndex = submenuItemIndex;
switch (submenuItemIndex) {
case 0:
currentMenu = MENU_FAN;
break;
case 1:
currentMenu = MENU_TRAFFIC;
break;
case 2:
currentMenu = MENU_DS3231;
break;
}
submenuItemIndex = 0; // 初始化子菜單索引
break;
case MENU_FAN:
switch (submenuItemIndex) {
case 0:
// 處理風扇速度1邏輯
break;
case 1:
// 處理風扇速度2邏輯
break;
case 2:
// 返回主選單
currentMenu = previousMenu;
submenuItemIndex = previousSubmenuItemIndex;
break;
}
break;
case MENU_TRAFFIC:
switch (submenuItemIndex) {
case 0:
// 處理交通模式1邏輯
break;
case 1:
// 處理交通模式2邏輯
break;
case 2:
// 返回主選單
currentMenu = previousMenu;
submenuItemIndex = previousSubmenuItemIndex;
break;
}
break;
case MENU_DS3231:
switch (submenuItemIndex) {
case 0:
nowtimedis = 1; // 顯示時間邏輯
oled.clear();
break;
case 1:
// 處理設定時間邏輯
break;
case 2:
// 返回主選單
currentMenu = previousMenu;
submenuItemIndex = previousSubmenuItemIndex;
break;
}
break;
}
}
// 按下返回鍵 (newkey == 4)
if (newkey == 4) {
if (nowtimedis == 1) {
// 如果顯示時間功能正在執行,按下返回鍵應該停止顯示時間,並回到 DS3231 菜單
nowtimedis = 0;
oled.clear();
// 回到 DS3231 菜單
currentMenu = MENU_DS3231;
submenuItemIndex = 0; // 重置子菜單索引
} else if (currentMenu != MENU_MAIN) {
// 如果不在主選單,按下返回鍵應回到上一個菜單
currentMenu = previousMenu;
submenuItemIndex = previousSubmenuItemIndex;
}
oled.clear(); // 清除顯示
}
}
oldkey = newkey; // 更新按鍵狀態
}
char ReadKey() {
char key = 0;
if (digitalRead(BTN_1) == LOW) key |= 1;
if (digitalRead(BTN_2) == LOW) key |= 2;
if (digitalRead(BTN_3) == LOW) key |= 4;
if (digitalRead(BTN_4) == LOW) key |= 8;
return key;
}
void updateDisplay() {
oled.setScale(2);
oled.invertText(false);
oled.setCursorXY(0, 0); // 設置光標到頂部
switch (currentMenu) {
case MENU_MAIN:
oled.print(" MENU ");
displaySubMenuItems("LEDCON1", "LEDCON2", "DS3231 ");
break;
case MENU_FAN:
oled.print("LEDCON1");
displaySubMenuItems("SPEED 1", "SPEED 2", "RETURN ");
break;
case MENU_TRAFFIC:
oled.print("LEDCON2");
displaySubMenuItems("MODE 1 ", "MODE 2 ", "RETURN ");
break;
case MENU_DS3231:
oled.print("DS3231 ");
displaySubMenuItems("NOWTIME", "SETTIME", "RETURN ");
break;
}
oled.update(); // 刷新顯示
}
void displaySubMenuItems(const char *item1, const char *item2, const char *item3) {
char items[3][10];
strcpy(items[0], item1);
strcpy(items[1], item2);
strcpy(items[2], item3);
for (int i = 0; i < 3; i++) {
oled.setCursorXY(0, (i + 1) * 16); // 根據索引設置子菜單項目的位置
if (submenuItemIndex == i) {
oled.invertText(true); // 反轉顯示當前選中的子菜單項目
} else {
oled.invertText(false); // 正常顯示其他項目
}
oled.print(items[i]);
}
}
void disTimeTemp() {
Datime now(ds);
Serial.println(now.second);
Serial.println(now.minute);
float temperature = ds.getTemp(); // 修正為正確的溫度讀取方法
oled.setScale(2);
oled.setCursorXY(0, 0);
oled.print(now.year);
oled.print("-");
oled.print(now.month);
oled.print("-");
oled.print(now.day);
oled.setCursorXY(0, 16);
oled.print("T:");
if (now.hour < 10) {
oled.print('0');
oled.print(now.hour);
} else {
oled.print(now.hour);
}
oled.print(":");
if (now.minute < 10) {
oled.print('0');
oled.print(now.minute);
} else {
oled.print(now.minute);
}
oled.print(":");
if (now.second < 10) {
oled.print('0');
oled.print(now.second);
} else {
oled.print(now.second);
}
oled.setCursorXY(0, 32);
oled.print("Temp:");
oled.print(temperature);
oled.update();
}
void controlLEDs() {
// 控制LEDs的邏輯在此實現
}
void setup() {
pinMode(BTN_1, INPUT_PULLUP);
pinMode(BTN_2, INPUT_PULLUP);
pinMode(BTN_3, INPUT_PULLUP);
pinMode(BTN_4, INPUT_PULLUP);
Serial.begin(115200);
oled.init();
oled.clear();
oled.update();
Wire.begin();
if (!ds.begin()) ds.setBuildTime();
}
void loop() {
switchkey();
if (nowtimedis == 0) updateDisplay();
else disTimeTemp();
controlLEDs();
if (ds.tick()) {
Serial.println(ds.toString());
float temp = ds.getTemp();
Serial.println(temp);
}
}