/*
ILI9341 Pin	Arduino Mega Pin
VCC	5V
GND	GND
CS	10
RESET	9
DC	8
SDI (MOSI)	51
SCK	52
LED (optional)	5V
*/

#include <Keypad.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
const byte ROWS = 5;
const byte COLS = 14;
const unsigned long BLINK_INTERVAL = 500; // Blink interval in milliseconds
unsigned long previousMillis = 0;
bool capsOn = false;
#define SCREEN_WIDTH 240 // OLED display width, in pixels
#define SCREEN_HEIGHT 320 // OLED display height, in pixels
#define TFT_DC 8
#define TFT_CS 10
int cursor_Width = 1;
int cursor_Height = 8;
int currentColor = ILI9341_BLACK;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// 'Monkey-Selfie2', 240x320px
const unsigned long int logo_bmp [] PROGMEM = {
	
};

byte rowPins[ROWS] = { 22, 24, 26, 28, 30 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49 }; //connect to the column pinouts of the keypad

char lowerCase[ROWS][COLS] = {
    { '`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 8 },
    { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\\', '\0' },
    { 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '"', '\n', '\0' },
    { 14, 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', '\0', '\0' },
    { ' ' }
};

char upperCase[ROWS][COLS] = {
    { '`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 8 },
    { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[', ']', '\\', '\0' },
    { 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '"', '\n', '\0' },
    { 14, 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', '\0', '\0' },
    { ' ' }
};

//initialize an instance of class NewKeypad

Keypad customKeypad = Keypad(makeKeymap(lowerCase), rowPins, colPins, ROWS, COLS);

void setup()
{
    Serial.begin(9600);
    tft.begin();
   /* tft.setCursor(26, 120);
    tft.setTextColor(ILI9341_RED);
    tft.setTextSize(3);
    tft.println("Wordman");
    tft.setCursor(26, 150);
    tft.setTextColor(ILI9341_YELLOW);
    tft.print("PRO");
    delay(3000);
    tft.fillScreen(ILI9341_BLACK);
    tft.drawRGBBitmap(0, 0, logo_bmp, SCREEN_WIDTH, SCREEN_HEIGHT);
    delay(3000);
    tft.fillScreen(ILI9341_BLACK); */
    tft.setCursor(0, 0);
    tft.setTextSize(1);
    tft.setTextColor(ILI9341_WHITE);
}
void buttonPress()
{
}

void loop()
{
    unsigned long currentMillis = millis();

    if (currentMillis - previousMillis >= BLINK_INTERVAL) {
        previousMillis = currentMillis;

        // Toggle the current color between black and white
        if (currentColor == ILI9341_BLACK) {
            currentColor = ILI9341_WHITE;
        }
        else {
            currentColor = ILI9341_BLACK;
        }

        // Fill the square with the current color
        tft.fillRect(tft.getCursorX(), tft.getCursorY(), cursor_Width, cursor_Height, currentColor);
    }

    char currentKey = customKeypad.getKey();

    if (currentKey == 14) {
        Serial.println("upper case is initiated");
        capsOn = !capsOn; // Toggle the capsOn state
    }
    else if (currentKey == 8) {
        if (tft.getCursorX() >= cursor_Width) {
            tft.setCursor(tft.getCursorX() - cursor_Width, tft.getCursorY());
            tft.fillRect(tft.getCursorX(), tft.getCursorY(), cursor_Width, cursor_Height, ILI9341_BLACK);
        }
    }
    else if (currentKey && tft.getCursorX() < SCREEN_WIDTH - cursor_Width) {
        if(capsOn){
        currentKey = toupper(currentKey); // Convert the character to uppercase
        }
        tft.print(currentKey);
        tft.fillRect(tft.getCursorX(), tft.getCursorY(), cursor_Width, cursor_Height, ILI9341_BLACK);
        tft.setCursor(tft.getCursorX() + cursor_Width, tft.getCursorY());
    }
}