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

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

//Variable
int Joy = 0;

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

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


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

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

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

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

void loop() {
//The wired code should also be inside the bluetooth if, cause then it would enter to it and never go out
  if (deviceConnected) { //Enter to the following only if it is connected
  //---------BLUETOOTH---------------------------------------------------------------
    //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
        Wire.beginTransmission(8); //Begin communitacion with address of slave 1 (wired)
        Wire.write('0'); //Send 0 to the wired slave
        Wire.endTransmission(); //End transmission with wired slave
        Joy = 0;
      }
      else if (ValorX==511){ //UP
        Wire.beginTransmission(8); //Begin communitacion with address of slave 1 (wired)
        Wire.write('1'); //Send 1 to the wired slave
        Wire.endTransmission(); //End transmission with wired slave
        Joy = 1;
      }
      else if(ValorY==511){ //RIGHT
        Wire.beginTransmission(8); //Begin communitacion with address of slave 1 (wired)
        Wire.write('1'); //Send 1 to the wired slave
        Wire.endTransmission(); //End transmission with wired slave
        Joy = 2;
      }
      else if(ValorX==0){ //DOWN
        Wire.beginTransmission(8); //Begin communitacion with address of slave 1 (wired)
        Wire.write('1'); //Send 1 to the wired slave
        Wire.endTransmission(); //End transmission with wired slave
        Joy = 3;
      }
      else if(ValorY==0){ //LEFT
        Wire.beginTransmission(8); //Begin communitacion with address of slave 1 (wired)
        Wire.write('1'); //Send 1 to the wired slave
        Wire.endTransmission(); //End transmission with wired slave
        Joy = 4;
      }
      else{
        Wire.beginTransmission(8); //Begin communitacion with address of slave 1 (wired)
        Wire.write('0'); //Send 0 to the wired slave
        Wire.endTransmission(); //End transmission with wired slave
        Joy = 0;
      }


    pCharacteristic->setValue(Joy);
    pCharacteristic->notify();
    delay(100); // bluetooth stack will go into congestion, if too many packets are sent
  }
}