//Official Server
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <BLEServer.h>

//Pin numbers
int LED = D9;
int JoyX = A2;
int JoyY = A1;

//Variable
int Joy = 0;

//BLE Server name (name of the other Xiao)
#define bleServerName "Danisita_BBQ"

//Look for the object in the BLECharacteristic class
BLECharacteristic *pCharacteristic; 
//Boolean variable for the connection of the device
bool deviceConnected = false; 


class MyServerCallbacks: public BLEServerCallbacks { //Class for callbacks
  void onConnect(BLEServer* pServer) { //Function for a connected device
    deviceConnected = true; //Establish positive value when connected device
    digitalWrite(LED, HIGH);
  };
  
  void onDisconnect(BLEServer* pServer) { //Function for disconnected device
    //Establish the negative value when a device is connected
    deviceConnected = false; 
    digitalWrite(LED, LOW);
  }
};

void setup() {
  Serial.begin(115200); //Serial communication to observe in serial monitor

  //pinModes
  pinMode(LED,OUTPUT);
  pinMode(JoyX, INPUT);
  pinMode(JoyY, INPUT);

//Give the established name to the server
  BLEDevice::init(bleServerName); 
  //Create the server
  BLEServer *pServer = BLEDevice::createServer(); 
  //Function to know if connected
  pServer->setCallbacks(new MyServerCallbacks()); 
  
  //Service UUID
  BLEService *pService = pServer->createService(BLEUUID((uint16_t)0x181A));
  //Give the characteristics to the service
  pCharacteristic = pService->createCharacteristic( 
    //Characteristic UUID
    BLEUUID((uint16_t)0x2A59), 
    //Establish the notifiy property to the server
    BLECharacteristic::PROPERTY_NOTIFY 
  );
  //Give a descriptor to allow the client to receive notifications
  pCharacteristic->addDescriptor(new BLE2902()); 
  
  pService->start(); //Begin the service
  //Access the advertising class to allow it
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); 
  //Method to get the UUID of the service
  pAdvertising->addServiceUUID(pService->getUUID()); 
  //Allow scanning
  pAdvertising->setScanResponse(true); 
  //Method to establish the minimum interval to be advertised
  pAdvertising->setMinPreferred(0x0); 
  pAdvertising->setMinPreferred(0x1F); 
  //Begin the advertising of the server
  BLEDevice::startAdvertising(); 
}

void loop() {
  if (deviceConnected) { //Enter to the following only if it is connected
    //Read analog values from joystick
      int ValorX = analogRead(JoyX)/8;
      int ValorY = analogRead(JoyY)/8;
      //Assign string values to the "Joy" variable
      if((ValorX>=490 && ValorX<=494)&&(ValorY>=468 && ValorY<=474)){ //STOP
        Joy = 0;
      }
      else if (ValorX==511){ //UP
        Joy = 1;
      }
      else if(ValorY==511){ //RIGHT
        Joy = 2;
      }
      else if(ValorX==0){ //DOWN
        Joy = 3;
      }
      else if(ValorY==0){ //LEFT
        Joy = 4;
      }
      else{
        Joy = 0;
      }

    pCharacteristic->setValue(Joy);
    pCharacteristic->notify();
    delay(10); //Bluetooth will go into congestion, if too many packets
  }
}