#include <Wire.h>
#include <Key.h>
#include <Keypad.h>




#include <LiquidCrystal_I2C.h>


int pos = 0;
int target = 0;
int target1 = 0;
byte digitCount = 0;
char incomingByte;
char incomingByteB;
char incomingByteC;
char incomingByteD;

//SETUP THE LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);

//SETUP THE KEYPAD
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] ={
     '1','2','3','\a',
     '4','5','6','\b',
     '7','8','9','\e',
     '*','0','#','\f'
};

//byte rowPins[ROWS] = {22, 24, 26, 28}; //connect to the row pinouts of the keypad
//byte colPins[COLS] = {30, 32, 34, 36}; //connect to the column pinouts of the keypad

byte rowPins[ROWS] = {5, 4, 6, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 9, 10, 11}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );



void setup() {

  Serial.begin(115200);
  Serial1.begin(115200);
  Serial3.begin(115200);

//LETS SETUP THE LCD TEXT
  lcd.init();
  lcd.backlight();
  lcd.print("Sollposition in mm");
  lcd.setCursor(0,1); 
  lcd.print("--------------------");
  lcd.setCursor(0, 2);
  lcd.print("===>       mm");
  lcd.setCursor(0, 3);
  lcd.print("STOP POSITION= 0");
  lcd.setCursor(5, 2);
  

}
void loop() {
//this simply sends serial3(esplink wifi) input to serial1 (GRBL) and sends the GRBL(serial1) outputs to Serial3(esplink wifi)
  if (Serial3.available() > 0) {
    // read the incoming byte from esplink
    incomingByte = Serial3.read();

    // send to grbl
       Serial1.print(incomingByte);
       Serial3.print(incomingByte);
        }
if (Serial1.available() > 0) {
    // read the incoming from grbl
    incomingByteB = Serial1.read();

    // send to esplink
       Serial3.print(incomingByteB);
        }

        if (Serial.available() > 0) {
    // read the incoming from usb
    incomingByteC = Serial.read();

    // send to grbl
       Serial1.print(incomingByteC);
        }
        if (Serial.available() > 0) {
    // read the incoming from grbl
    incomingByteD = Serial.read();

    // send to esplink
       Serial3.println(incomingByteD);
        }


      

  //FUNCTION TO HOMME WHEN THE * IS PRESSED
  char keypressed = keypad.getKey();
if (keypressed == '*'){
Serial1.println("$HX");
Serial3.print("Homming....");
Serial.println("Homming....");
lcd.setCursor(14, 3);
     lcd.print("       ");
     lcd.setCursor(14, 3);
     lcd.print("HOMME");
     lcd.setCursor(5, 2);
 }
 //FUNCTION TO ADD 0.1MM WHEN THE B IS PRESSED
if (keypressed == '\b'){
Serial1.println("G91 X+0.1");
Serial3.print("+0.1");
Serial.println("+0.1");
lcd.setCursor(14, 3);
lcd.print("       ");
lcd.setCursor(14, 3);
     lcd.print(target+0.1);
     lcd.setCursor(5, 2);

 }
 //FUNCTION TO REMOVE 0.1MM WHEN C IS PRESSED
 if (keypressed == '\e'){
Serial1.println("G91 X-0.1");
Serial3.print("-0.1mm");
Serial.println("-0.1mm");
lcd.setCursor(14, 3);
lcd.print("       ");
lcd.setCursor(14, 3);
     lcd.print(target-0.1);
     lcd.setCursor(5, 2);
 }

 // FUNCTION TO FEED HOLD WHEN D IS PRESSED
 if (keypressed == '\f'){
Serial1.println("!");
Serial3.print("Feed Hold !!");
Serial.println("Feed Hold !!");
 }

 // FUNCTION TO RESUME AFTER FEED HOLD WHEN # IS PRESSED
 if (keypressed == '#'){
Serial1.println("~");
Serial3.print("Resuming");
Serial.println("Resuming");
 }
 // FUNCTION TO DELETE THE INPUT DIMENTION WHEN A IS PRESSED
 if (keypressed == '\a'){
digitCount=0;
 target1= 0;
lcd.setCursor(5, 2);
lcd.print("         ");
lcd.setCursor(5, 2);
 }
 //FUNCTION TO INPUT DIRECTION TO MOVE AFTER 4 DIGITS IS ENTERED
if (keypressed >= '0'){// && Key <= '9') {
  lcd.print(keypressed);
      target1 = (target1 * 10) + keypressed -'0';
          digitCount++;
         delay(300);
                
       
   if (digitCount == 4) {
     target = target1;
          Serial1.print("G90X");
          Serial1.println(target1);
          Serial3.println(target1);
          Serial.println(target1);
            lcd.setCursor(5, 2);
     lcd.print(target);
     Serial3.print(target);
      Serial.print(target);
   
     digitCount = 0;
     target1= 0;
     lcd.setCursor(5, 2);
     lcd.print("     "); } } }