/*
ESP32 HTTPClient Jokes API Example
https://wokwi.com/projects/342032431249883731
Copyright (C) 2022, Uri Shaked
*/
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>
//
#include "Fonts/FreeSansBold9pt7b.h"//
// #include <FreeDefaultFonts.h>
#define BLACK 0x0000 /* 0, 0, 0 */
#define NAVY 0x000F /* 0, 0, 128 */
#define DARKGREEN 0x03E0 /* 0, 128, 0 */
#define DARKCYAN 0x03EF /* 0, 128, 128 */
#define MAROON 0x7800 /* 128, 0, 0 */
#define PURPLE 0x780F /* 128, 0, 128 */
#define OLIVE 0x7BE0 /* 128, 128, 0 */
#define LIGHTGREY 0xC618 /* 192, 192, 192 */
#define DARKGREY 0x7BEF /* 128, 128, 128 */
#define BLUE 0x001F /* 0, 0, 255 */
#define GREEN 0x07E0 /* 0, 255, 0 */
#define CYAN 0x07FF /* 0, 255, 255 */
#define RED 0xF800 /* 255, 0, 0 */
#define MAGENTA 0xF81F /* 255, 0, 255 */
#define YELLOW 0xFFE0 /* 255, 255, 0 */
#define WHITE 0xFFFF /* 255, 255, 255 */
#define ORANGE 0xFDA0 /* 255, 180, 0 */
#define GREENYELLOW 0xB7E0 /* 180, 255, 0 */
#define PINK 0xFC9F
// أزرار التنقل
const int buttonUp = 5;
const int buttonDown = 17;
const int buttonSelect = 16;
const int buttonBack = 0;
int currentScreen = 0;
int currentOption = 0;
String mainMenu[] = {"Option 1", "Option 2", "Dropdown Menu", "Exit"};
String dropdownMenu[] = {"Item 1", "Item 2", "Item 3"};
bool inDropdown = false;
void setup() {
// إعداد الشاشة
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(2);
// إعداد الأزرار
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
pinMode(buttonSelect, INPUT_PULLUP);
pinMode(buttonBack, INPUT_PULLUP);
displayMainMenu();
}
void loop() {
if (digitalRead(buttonUp) == LOW) {
moveUp();
delay(200);
}
if (digitalRead(buttonDown) == LOW) {
moveDown();
delay(200);
}
if (digitalRead(buttonSelect) == LOW) {
selectOption();
delay(200);
}
if (digitalRead(buttonBack) == LOW && inDropdown) {
backToMainMenu();
delay(200);
}
}
void displayMainMenu() {
tft.fillScreen(BLACK);
for (int i = 0; i < 4; i++) {
if (i == currentOption) {
tft.setTextColor(RED, BLACK);
} else {
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
}
tft.setCursor(10, 30 + (i * 30));
tft.print(mainMenu[i]);
}
}
void displayDropdownMenu() {
tft.fillScreen(ILI9341_BLACK);
for (int i = 0; i < 3; i++) {
if (i == currentOption) {
tft.setTextColor(ILI9341_RED, ILI9341_BLACK);
} else {
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
}
tft.setCursor(10, 30 + (i * 30));
tft.print(dropdownMenu[i]);
}
}
void moveUp() {
currentOption = (currentOption == 0) ? (inDropdown ? 2 : 3) : currentOption - 1;
if (inDropdown) {
displayDropdownMenu();
} else {
displayMainMenu();
}
}
void moveDown() {
currentOption = (currentOption == (inDropdown ? 2 : 3)) ? 0 : currentOption + 1;
if (inDropdown) {
displayDropdownMenu();
} else {
displayMainMenu();
}
}
void selectOption() {
if (!inDropdown) {
if (currentOption == 2) { // Dropdown Menu
inDropdown = true;
currentOption = 0;
displayDropdownMenu();
} else if (currentOption == 3) { // Exit
tft.fillScreen(BLACK);
tft.setCursor(10, 30);
tft.print("Goodbye!");
delay(2000);
ESP.restart();
} else {
tft.fillScreen(BLACK);
tft.setCursor(10, 30);
tft.print("Selected: " + mainMenu[currentOption]);
delay(2000);
displayMainMenu();
}
} else {
tft.fillScreen(BLACK);
tft.setCursor(10, 30);
tft.print("Selected: " + dropdownMenu[currentOption]);
delay(2000);
displayDropdownMenu();
}
}
void backToMainMenu() {
inDropdown = false;
currentOption = 0;
displayMainMenu();
}