 
/*
 * ESP32 S cu tastatura PS2 cu convertor logic
 * trimite si primeste text  cu aplic BT S2 terminal 
instalata pe Android si afiseaza pe Oled

 afiseaza pe Oled text editat cu PS2 Key
 afiseaza pe tetefon corect daca se debifeaza
 in aplicatie fereastra Autol casuta After Receive 
 setare ASCII
 comanda led RGB daca primeste carecaterele R G B
*/
#include "BluetoothSerial.h"  
BluetoothSerial SerialBT;
char c = ' ';
char d= ' ';

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

#include "PS2Keyboard.h"
const int DataPin = 4;//albastru
const int IRQpin =  0;//galben
PS2Keyboard keyboard;
 int ledr=12;
 int ledg=14;
 int ledb=27;

#include <ESP32Servo.h> 
Servo myservo;    
  int state = 0;
 
void setup() 
{
   Serial.begin(115200);
   SerialBT.begin("ESP32 BT");
   keyboard.begin(DataPin, IRQpin);

   myservo.attach(13);
   
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("ESP32 BT Text Terminal PS2");
  display.display();
  delay(1000);  
  //pinMode(LED_BUILTIN, OUTPUT);
  pinMode(ledr, OUTPUT);
  pinMode(ledg, OUTPUT);
  pinMode(ledb, OUTPUT);
}
 
void loop()
{
    if (SerialBT.available()) {  
        c = SerialBT.read(); 
        //primeste caractere de la telefon
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.print(c);   
  display.display();

if (c == 'R'){
    //aprinde ledr  cand primeste caracterul 'R'
    digitalWrite(ledr, HIGH);
    myservo.write(45);
    }
 else {
  digitalWrite(ledr, LOW);
 }  

if (c == 'G'){
    //aprinde ledg  cand primesste caracterul 'G'
    digitalWrite(ledg, HIGH);
    myservo.write(90);
    }
 else {
  digitalWrite(ledg, LOW);
 }  

 if (c == 'B'){
    //aprinde ledb  cand primesste caracterul 'B'
    digitalWrite(ledb, HIGH); 
    myservo.write(145);   
    }
 else {
  digitalWrite(ledb, LOW);
 }  
 
  if (c == '/'){
    //sterge oled cand se tipareste '/'
    display.clearDisplay();
    display.setCursor(0,0);
    }
    }
    
    if (keyboard.available()) {
      //de la tastatura PS2 trimite ASCII
    char  d = keyboard.read();       
        SerialBT.print(d);
        display.print(d); 
        display.display();
        if (d == '/'){
          //sterge oled cand se tipareste '/'
    display.clearDisplay();
    display.setCursor(0,0);
    }
    }
    }
