#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
// Define pins for the TFT display
#define TFT_CS 15
#define TFT_RST 2
#define TFT_DC 4
#define TFT_SCK 18
#define TFT_MOSI 23
#define TFT_MISO 19
// Define pins for the switches
#define SWITCH_UP 32
#define SWITCH_DOWN 33
#define SWITCH_LEFT 25
#define SWITCH_RIGHT 26
#define SWITCH_SELECT 27
#define SWITCH_BACK 14
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
const int NUM_BOXES = 9;
const int BOXES_PER_SCREEN = 6;
const int BOX_WIDTH = 100; // Width of each box
const int BOX_HEIGHT = 100; // Height of each box
char boxLabels[NUM_BOXES] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'};
int cursorPosition = 0;
int scrollOffset = 0;
bool isMenuActive = true;
void setup() {
pinMode(SWITCH_UP, INPUT_PULLUP);
pinMode(SWITCH_DOWN, INPUT_PULLUP);
pinMode(SWITCH_LEFT, INPUT_PULLUP);
pinMode(SWITCH_RIGHT, INPUT_PULLUP);
pinMode(SWITCH_SELECT, INPUT_PULLUP);
pinMode(SWITCH_BACK, INPUT_PULLUP);
tft.begin();
tft.setRotation(3); // Adjust as necessary for your display orientation
tft.fillScreen(ILI9341_BLACK);
drawScreen();
}
void loop() {
if (digitalRead(SWITCH_UP) == LOW) {
delay(200); // Debounce
navigateUp();
}
if (digitalRead(SWITCH_DOWN) == LOW) {
delay(200); // Debounce
navigateDown();
}
if (digitalRead(SWITCH_LEFT) == LOW) {
delay(200); // Debounce
navigateLeft();
}
if (digitalRead(SWITCH_RIGHT) == LOW) {
delay(200); // Debounce
navigateRight();
}
if (digitalRead(SWITCH_SELECT) == LOW) {
delay(200); // Debounce
selectBox();
}
if (digitalRead(SWITCH_BACK) == LOW) {
delay(200); // Debounce
backToMenu();
}
}
void drawScreen() {
tft.fillScreen(ILI9341_BLACK);
for (int i = 0; i < BOXES_PER_SCREEN && (i + scrollOffset) < NUM_BOXES; ++i) {
int x = (i % 3) * BOX_WIDTH;
int y = (i / 3) * BOX_HEIGHT;
int boxIndex = i + scrollOffset;
if (boxIndex == cursorPosition) {
tft.fillRect(x, y, BOX_WIDTH, BOX_HEIGHT, ILI9341_BLUE); // Highlight selected box
tft.drawRect(x, y, BOX_WIDTH, BOX_HEIGHT, ILI9341_WHITE); // White border for highlighted box
} else {
tft.drawRect(x, y, BOX_WIDTH, BOX_HEIGHT, ILI9341_WHITE);
}
tft.setCursor(x + 40, y + 40); // Adjusted position for bigger blocks
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3); // Larger text size
tft.print(boxLabels[boxIndex]);
}
}
void navigateUp() {
if (isMenuActive && cursorPosition >= 3) {
cursorPosition -= 3;
if (cursorPosition < scrollOffset) {
scrollOffset -= 3;
}
drawScreen();
}
}
void navigateDown() {
if (isMenuActive && cursorPosition + 3 < NUM_BOXES) {
cursorPosition += 3;
if (cursorPosition >= scrollOffset + BOXES_PER_SCREEN) {
scrollOffset += 3;
}
drawScreen();
}
}
void navigateLeft() {
if (isMenuActive && cursorPosition % 3 > 0) {
cursorPosition -= 1;
drawScreen();
}
}
void navigateRight() {
if (isMenuActive && cursorPosition % 3 < 2 && cursorPosition + 1 < NUM_BOXES) {
cursorPosition += 1;
drawScreen();
}
}
void selectBox() {
if (isMenuActive) {
isMenuActive = false;
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(60, 120);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(5);
tft.print(boxLabels[cursorPosition]);
}
}
void backToMenu() {
if (!isMenuActive) {
isMenuActive = true;
drawScreen();
}
}