#define ILI9341_YELLOW      0xFFE0  //< 255, 255,   0
#define ILI9341_WHITE       0xFFFF  //< 255, 255, 255
#define ILI9341_ORANGE      0xFD20  //< 255, 165,   0
#define ILI9341_GREENYELLOW 0xAFE5  //< 173, 255,  41
#define ILI9341_RED         0xF800  //< 255,   0,   0
#define ILI9341_BLACK       0x0000  //<   0,   0,   0
#define ILI9341_LIME        0x07E0  ///<   0, 255, 255
#define ILI9341_YELLOW      0xFFE0  ///< 255, 255,   0
#define ILI9341_SPARKGAPRED 0xF008
//RGB565 Color Picker


#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <Keypad.h>

// Define the keypad pins and layout
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'.','0','#'}
};

byte rowPins[ROWS] = {33, 25, 26, 27}; // Connect keypad row pins to these Arduino pins
byte colPins[COLS] = {34, 35, 32}; // Connect keypad column pins to these Arduino pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

// Define the button pins
const byte VSET_PIN = 12;
const byte ISET_PIN = 14;
const byte CLEAR_PIN = 13;

// Define the setpoint variables
float VOLTAGEsetpoint = 0.0;
float CURRENTsetpoint = 0.0;

void setup() {
  Serial.begin(9600);

  // Set the button pins as inputs with pull-up resistors
  pinMode(VSET_PIN, INPUT_PULLUP);
  pinMode(ISET_PIN, INPUT_PULLUP);
  pinMode(CLEAR_PIN, INPUT_PULLUP);
}

void loop() {
  char key = keypad.getKey();

  // Check if a key is pressed
  if (key != NO_KEY) {
    // Check if the entered key is a number or decimal point
    if (isdigit(key) || key == '*') {
      // Add the entered digit or decimal point to the appropriate setpoint variable
      if (digitalRead(VSET_PIN) == LOW) {
        // If V-SET button is pressed, add the entered digit to VOLTAGEsetpoint
        if (key == '*') {
          VOLTAGEsetpoint += 0.1; // Add decimal point
        } else {
          VOLTAGEsetpoint = VOLTAGEsetpoint * 10 + (key - '0');
        }
      } else if (digitalRead(ISET_PIN) == LOW) {
        // If I-SET button is pressed, add the entered digit to CURRENTsetpoint
        if (key == '*') {
          CURRENTsetpoint += 0.1; // Add decimal point
        } else {
          CURRENTsetpoint = CURRENTsetpoint * 10 + (key - '0');
        }
      }
    }
    // Check if the entered key is the enter key
    else if (key == '#') {
      // If enter key is pressed, print the appropriate setpoint variable and reset it to 0
      if (digitalRead(VSET_PIN) == LOW) {
        Serial.print("Entered voltage setpoint: ");
        Serial.println(VOLTAGEsetpoint);
        VOLTAGEsetpoint = 0.0;
      } else if (digitalRead(ISET_PIN) == LOW) {
        Serial.print("Entered current setpoint: ");
        Serial.println(CURRENTsetpoint);
        CURRENTsetpoint = 0.0;
      }
    }
  }

  // Check if the clear button is pressed
  if (digitalRead(CLEAR_PIN) == LOW) {
    VOLTAGEsetpoint = 0.0;
    CURRENTsetpoint = 0.0;
  }
}