Advertisement
thomazrb

Garantir que só o controle selecionado se conecte

May 8th, 2025 (edited)
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Defina o endereço MAC do controle autorizado (use o seu)
  2. const uint8_t allowed_btaddr[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  3. int LED_pin = 2; // ESP32 WROOM 32 normalmente pino 2 ou 15, ESP32 C3 normalmente o pino 8,
  4.  
  5. // ...
  6.  
  7. void onConnectedController(ControllerPtr ctl) {
  8.   bool foundEmptySlot = false;
  9.   for (int i = 0; i < BP32_MAX_GAMEPADS; i++) {
  10.     if (myControllers[i] == nullptr) {
  11.       Serial.printf("CALLBACK: Controller is connected, index=%d\n", i);
  12.       ControllerProperties properties = ctl->getProperties();
  13.       Serial.printf("Controller model: %s, VID=0x%04x, PID=0x%04x\n", ctl->getModelName().c_str(), properties.vendor_id,
  14.                     properties.product_id);
  15.       Serial.print("Controller BTAddr: ");
  16.       for (int i = 0; i < 6; i++) {
  17.           Serial.printf("%02X", properties.btaddr[i]);
  18.           if (i < 5) Serial.print(":");
  19.       }
  20.       Serial.println();
  21.  
  22.       //Verificando se o controle está autorizado:
  23.       bool isAllowed = true;
  24.       for (int j = 0; j < 6; j++) {
  25.         if (properties.btaddr[j] != allowed_btaddr[j]) {
  26.           isAllowed = false;
  27.           break;
  28.         }
  29.       }
  30.  
  31.       if (!isAllowed) {
  32.         Serial.println("Controller Not Allowed! Disconnecting...");
  33.         ctl->disconnect();
  34.         conectado = false;
  35.         return; // Sai da função sem adicionar ao array
  36.       }
  37.       myControllers[i] = ctl;
  38.       foundEmptySlot = true;
  39.       digitalWrite(LED_pin, HIGH);  // Liga o LED do ESP para indicar controle conectado
  40.       conectado = true;
  41.       break;
  42.     }
  43.   }
  44.   if (!foundEmptySlot) {
  45.     Serial.println("CALLBACK: Controller connected, but could not found empty slot");
  46.   }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement