

//merge cu ESP32 S cu libr
//Adafruit Keypad
//conexiuni coloane 2, 0, 4, 16,12,14,27,26
//conexiuni randuri 19, 18, 5, 17
//2 tastaturi 4x4
//coloane diferite
//aceleasi randuri
//caracter afisat la intersectia coloana cu rand
//trimite pe webserial corect

#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WebSerial.h>

AsyncWebServer server(80);
const char* ssid = "nume router";          
const char* password = "password router";
#include <Keypad.h>

#define text_Length 1 
//trebuie sa fie cu 1 mai lung decat password
//Length poate sa fie oricat
char Data[text_Length];

byte data_count = 0;
char key;

const byte ROWS = 4;
const byte COLS = 8;


char hexaKeys[ROWS][COLS] = {
  {'a', 'b', 'c', 'd', 'e', 'f', 'g','h'},
  {'i', 'j', 'k', 'l', 'm', 'n', 'o','p'},
  {'r', 's', 't', 'u', 'v', 'x', 'y','z'},
  {'1', '2', '3', '4', '5', '6', '7','8'}
};
 byte rowPins[ROWS]     = {19, 18, 5, 17}; // GIOP19, GIOP18, GIOP5, GIOP17 connect to the row pins
 byte colPins[COLS]= {2, 0, 4, 16,12,14,27,26}; 

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

void recvMsg(uint8_t *data, size_t len){
  WebSerial.println("Received Data...");
  String d = "";
  for(int i=0; i < len; i++){
    d += char(data[i]);
    //WebSerial.println(d);
    Serial.println(d);
  }
}

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.printf("WiFi Failed!\n");
    return;
  }
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
  // WebSerial is accessible at "<IP Address>/webserial" in browser
  WebSerial.begin(&server);
  WebSerial.msgCallback(recvMsg);
  server.begin();
}

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

  if (key) {
    Data[data_count] = key;
    data_count++;
    Serial.print("data ");
    Serial.println(Data);
    WebSerial.print(Data);    
  }
  while (data_count != 0) {
    Data[data_count--] = 0;
  }
  return;
    //clearData();
}
