/*
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"
#include "avr/pgmspace.h"
const byte ROWS = 5;
const byte COLS = 14;
const unsigned long BLINK_INTERVAL = 500; // Blink interval in milliseconds
unsigned long previousMillis = 0;
unsigned long lastPress;
bool capsOn = false;
bool capsLock = false;
#define SCREEN_WIDTH 240 // OLED display width, in pixels
#define SCREEN_HEIGHT 320 // OLED display height, in pixels
#define minCursorX 0
#define minCursorY 0
#define TFT_DC 8
#define TFT_CS 10
#define TFT_RST 9
#define TFT_MOSI 51
#define TFT_MISO 50
#define TFT_CLK 52
int cursor_Width = 1;
int cursor_Height = 8;
int currentColor = ILI9341_BLACK;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
const uint16_t 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' },
    { ' ' }
};
//initialize an instance of class NewKeypad



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);
}

Keypad customKeypad = Keypad(makeKeymap(lowerCase), rowPins, colPins, ROWS, COLS);
void loop()
{
	unsigned long currentMillis = millis();
	int cursorX = tft.getCursorX();
	int cursorY = tft.getCursorY();
	unsigned long timeDiff = currentMillis - lastPress;

	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 customKey = customKeypad.getKey();

	//shift key
	if (customKey == 14)
	{
		lastPress = currentMillis;

		//check if shift key was pressed twice
		if (timeDiff <= 300)
		{
			if (capsLock == false)
			{
				capsLock = true;
				capsOn = true;
			}
		}

		//press shift once
		else if (capsOn == false)
		{
			capsOn = true;
			capsLock = false;
		}

		//turn shift off if pressed again
		else
		{
			capsOn = false;
			capsLock = false;
		}
	}

	//backspace
	else if (customKey == 8)
	{
		if (cursorX == 0 && cursorY > 0)
		{
			tft.fillRect(tft.getCursorX(), tft.getCursorY(), 6, 8, ILI9341_BLACK);
			tft.setCursor(240, tft.getCursorY() - 8);	// move cursor back one character
		}
		else if (cursorX > 0)
		{
			tft.fillRect(cursorX, tft.getCursorY(), 6, 8, ILI9341_BLACK);
			tft.setCursor(tft.getCursorX() - 6, tft.getCursorY());	// move cursor back one character
			tft.fillRect(tft.getCursorX(), tft.getCursorY(), 6, 8, ILI9341_BLACK);
		}
	}

	//character key
	else if (customKey && cursorY <= SCREEN_HEIGHT)
	{
		tft.fillRect(tft.getCursorX(), tft.getCursorY(), cursor_Width, cursor_Height, ILI9341_BLACK);
		if (capsOn)
		{
			customKey = toupper(customKey);	// Convert the character to uppercase
		}

		tft.print(customKey);

		tft.fillRect(tft.getCursorX(), tft.getCursorY(), 6, 8, ILI9341_BLACK);
		//turn off caps after second character
		if (capsOn && capsLock == false)
		{
			capsOn = false;
		}
	}
}
FPS: 0
Power: 0.00W