

#include "PS2Keyboard.h"
//conectare convertor nivele LV la modulul ESP32
const int DataPin = 4;
const int IRQpin =  0;

PS2Keyboard keyboard;

#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WebSerial.h>
#define LED 14
AsyncWebServer server(80);
const char* ssid = "nume router";          
const char* password = "password router";

#define data_Length 30 
char Data[data_Length];
byte data_count = 0;

#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define OLED_ADDR   0x3C
Adafruit_SSD1306 display(-1);


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]);
    Serial.println(d);
    display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("Primit=");
  display.print(d); //afiseaza text tranmmsis pe oled
  display.display();
  }
//led pe GPOI14 comandat separat de mesjele ON si OFF  
  if (d == "on"){
    digitalWrite(LED, HIGH);
  }
  if (d=="off"){
    digitalWrite(LED, LOW);
  }
}

void setup() {
 Serial.begin(115200);
 keyboard.begin(DataPin, IRQpin);
 
  pinMode(LED, OUTPUT);
  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.begin(&server);
  WebSerial.msgCallback(recvMsg);
  server.begin();
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("webserial IP=");
  display.print(WiFi.localIP());
  display.print("/webserial"); 
  display.display();
  delay(5000);
    }
 
void loop() { 
  if (keyboard.available()) {
    char c = keyboard.read();  
   
  display.setTextSize(2);
  display.setTextColor(WHITE);
  
  display.print(c); 
  display.display(); 
  //la apasarea tastet z sterge afisajul Oled
  if (c == 'z'){
    display.clearDisplay();
    display.setCursor(0,0);
    }

    Data[data_count] = c;
    data_count++;

    Serial.print(Data); 
    WebSerial.print(Data);
    c = c++; 

    while (data_count != 0) {
    Data[data_count--] = 0;
    }
    return;
    //clearData();
    }
    }
 
  
