thomazrb icon

Garantir que só o controle selecionado se conecte

thomazrb | PRO | 05/08/25 01:36:36 PM UTC (Edited) | 0 ⭐ | 10917 👁️ | Never ⏰ | [Arduino, Robótica, Bluepad32]
Arduino |

1.67 KB

|

None

|

0 👍

/

0 👎

// Defina o endereço MAC do controle autorizado (use o seu)
const uint8_t allowed_btaddr[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
int LED_pin = 2; // ESP32 WROOM 32 normalmente pino 2 ou 15, ESP32 C3 normalmente o pino 8,
 
// ...
 
void onConnectedController(ControllerPtr ctl) {
  bool foundEmptySlot = false;
  for (int i = 0; i < BP32_MAX_GAMEPADS; i++) {
    if (myControllers[i] == nullptr) {
      Serial.printf("CALLBACK: Controller is connected, index=%d\n", i);
      ControllerProperties properties = ctl->getProperties();
      Serial.printf("Controller model: %s, VID=0x%04x, PID=0x%04x\n", ctl->getModelName().c_str(), properties.vendor_id,
                    properties.product_id);
      Serial.print("Controller BTAddr: ");
      for (int i = 0; i < 6; i++) {
          Serial.printf("%02X", properties.btaddr[i]);
          if (i < 5) Serial.print(":");
      }
      Serial.println();
 
      //Verificando se o controle está autorizado:
      bool isAllowed = true;
      for (int j = 0; j < 6; j++) {
        if (properties.btaddr[j] != allowed_btaddr[j]) {
          isAllowed = false;
          break;
        }
      }
 
      if (!isAllowed) {
        Serial.println("Controller Not Allowed! Disconnecting...");
        ctl->disconnect();
        conectado = false;
        return; // Sai da função sem adicionar ao array
      }
      myControllers[i] = ctl;
      foundEmptySlot = true;
      digitalWrite(LED_pin, HIGH);  // Liga o LED do ESP para indicar controle conectado
      conectado = true;
      break;
    }
  }
  if (!foundEmptySlot) {
    Serial.println("CALLBACK: Controller connected, but could not found empty slot");
  }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎