mirror of
https://github.com/esphome/esphome.git
synced 2025-02-25 04:22:30 +01:00
Fix BT Classic scan for ESP-IDF
This commit is contained in:
parent
e7f36c9b3c
commit
f1ea245928
15 changed files with 61 additions and 27 deletions
|
@ -97,6 +97,7 @@ esphome/components/esp32_ble/* @Rapsssito @jesserockz
|
||||||
esphome/components/esp32_ble_client/* @jesserockz
|
esphome/components/esp32_ble_client/* @jesserockz
|
||||||
esphome/components/esp32_ble_server/* @Rapsssito @clydebarrow @jesserockz
|
esphome/components/esp32_ble_server/* @Rapsssito @clydebarrow @jesserockz
|
||||||
esphome/components/esp32_bt_classic/* @RoboMagus
|
esphome/components/esp32_bt_classic/* @RoboMagus
|
||||||
|
esphome/components/esp32_bt_common/* @RoboMagus
|
||||||
esphome/components/esp32_camera_web_server/* @ayufan
|
esphome/components/esp32_camera_web_server/* @ayufan
|
||||||
esphome/components/esp32_can/* @Sympatron
|
esphome/components/esp32_can/* @Sympatron
|
||||||
esphome/components/esp32_improv/* @jesserockz
|
esphome/components/esp32_improv/* @jesserockz
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome.components import binary_sensor
|
from esphome.components import binary_sensor
|
||||||
from esphome.const import CONF_MAC_ADDRESS
|
from esphome.const import CONF_MAC_ADDRESS, CONF_NUM_SCANS
|
||||||
from esphome.components.esp32_bt_classic import ESP32BtClassic
|
from esphome.components.esp32_bt_classic import ESP32BtClassic
|
||||||
from esphome.components.esp32_bt_classic.const import (
|
from esphome.components.esp32_bt_classic.const import CONF_ESP32_BTCLASSIC_ID
|
||||||
CONF_ESP32_BTCLASSIC_ID,
|
|
||||||
CONF_NUM_SCANS,
|
|
||||||
)
|
|
||||||
|
|
||||||
DEPENDENCIES = ["esp32_bt_classic"]
|
DEPENDENCIES = ["esp32_bt_classic"]
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ from esphome.const import CONF_ID
|
||||||
from esphome.core import CORE
|
from esphome.core import CORE
|
||||||
from esphome.components.esp32 import add_idf_sdkconfig_option, get_esp32_variant, const
|
from esphome.components.esp32 import add_idf_sdkconfig_option, get_esp32_variant, const
|
||||||
|
|
||||||
|
AUTO_LOAD = ["esp32_bt_common"]
|
||||||
DEPENDENCIES = ["esp32"]
|
DEPENDENCIES = ["esp32"]
|
||||||
CODEOWNERS = ["@jesserockz", "@Rapsssito"]
|
CODEOWNERS = ["@jesserockz", "@Rapsssito"]
|
||||||
CONFLICTS_WITH = ["esp32_ble_beacon"]
|
CONFLICTS_WITH = ["esp32_ble_beacon"]
|
||||||
|
|
|
@ -13,9 +13,7 @@
|
||||||
#include <freertos/task.h>
|
#include <freertos/task.h>
|
||||||
#include <nvs_flash.h>
|
#include <nvs_flash.h>
|
||||||
|
|
||||||
#ifdef USE_ARDUINO
|
#include "esphome/components/esp32_bt_common/bt_defs.h"
|
||||||
#include <esp32-hal-bt.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace esp32_ble {
|
namespace esp32_ble {
|
||||||
|
@ -124,7 +122,7 @@ bool ESP32BLE::ble_setup_() {
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
|
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
|
||||||
err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
|
err = esp_bt_controller_enable(BT_MODE);
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
ESP_LOGE(TAG, "esp_bt_controller_enable failed: %s", esp_err_to_name(err));
|
ESP_LOGE(TAG, "esp_bt_controller_enable failed: %s", esp_err_to_name(err));
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
|
|
||||||
#include "ble_event.h"
|
#include "ble_event.h"
|
||||||
#include "queue.h"
|
#include "esphome/components/esp32_bt_common/queue.h"
|
||||||
|
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
|
|
||||||
|
@ -118,7 +118,7 @@ class ESP32BLE : public Component {
|
||||||
std::vector<BLEStatusEventHandler *> ble_status_event_handlers_;
|
std::vector<BLEStatusEventHandler *> ble_status_event_handlers_;
|
||||||
BLEComponentState state_{BLE_COMPONENT_STATE_OFF};
|
BLEComponentState state_{BLE_COMPONENT_STATE_OFF};
|
||||||
|
|
||||||
Queue<BLEEvent> ble_events_;
|
esp32_bt_common::Queue<BLEEvent> ble_events_;
|
||||||
BLEAdvertising *advertising_;
|
BLEAdvertising *advertising_;
|
||||||
esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE};
|
esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE};
|
||||||
bool enable_on_boot_;
|
bool enable_on_boot_;
|
||||||
|
|
|
@ -5,6 +5,7 @@ from esphome.core import CORE, TimePeriod
|
||||||
from esphome.components.esp32 import add_idf_sdkconfig_option
|
from esphome.components.esp32 import add_idf_sdkconfig_option
|
||||||
from esphome.components import esp32_ble
|
from esphome.components import esp32_ble
|
||||||
|
|
||||||
|
AUTO_LOAD = ["esp32_bt_common"]
|
||||||
DEPENDENCIES = ["esp32"]
|
DEPENDENCIES = ["esp32"]
|
||||||
CONFLICTS_WITH = ["esp32_ble_tracker"]
|
CONFLICTS_WITH = ["esp32_ble_tracker"]
|
||||||
|
|
||||||
|
|
|
@ -12,9 +12,7 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include "esphome/core/hal.h"
|
#include "esphome/core/hal.h"
|
||||||
|
|
||||||
#ifdef USE_ARDUINO
|
#include "esphome/components/esp32_bt_common/bt_defs.h"
|
||||||
#include <esp32-hal-bt.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace esp32_ble_beacon {
|
namespace esp32_ble_beacon {
|
||||||
|
@ -109,7 +107,7 @@ void ESP32BLEBeacon::ble_setup() {
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
|
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
|
||||||
err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
|
err = esp_bt_controller_enable(BT_MODE);
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
ESP_LOGE(TAG, "esp_bt_controller_enable failed: %s", esp_err_to_name(err));
|
ESP_LOGE(TAG, "esp_bt_controller_enable failed: %s", esp_err_to_name(err));
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
#include "esphome/components/esp32_ble/ble.h"
|
#include "esphome/components/esp32_ble/ble.h"
|
||||||
#include "esphome/components/esp32_ble/ble_advertising.h"
|
#include "esphome/components/esp32_ble/ble_advertising.h"
|
||||||
#include "esphome/components/esp32_ble/ble_uuid.h"
|
#include "esphome/components/esp32_ble/ble_uuid.h"
|
||||||
#include "esphome/components/esp32_ble/queue.h"
|
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
#include "esphome/core/preferences.h"
|
#include "esphome/core/preferences.h"
|
||||||
|
|
|
@ -24,7 +24,7 @@ from .const import (
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
AUTO_LOAD = ["esp32_ble"]
|
AUTO_LOAD = ["esp32_bt_common"]
|
||||||
DEPENDENCIES = ["esp32"]
|
DEPENDENCIES = ["esp32"]
|
||||||
CODEOWNERS = ["@RoboMagus"]
|
CODEOWNERS = ["@RoboMagus"]
|
||||||
|
|
||||||
|
@ -161,3 +161,5 @@ async def to_code(config):
|
||||||
|
|
||||||
if CORE.using_esp_idf:
|
if CORE.using_esp_idf:
|
||||||
add_idf_sdkconfig_option("CONFIG_BT_ENABLED", True)
|
add_idf_sdkconfig_option("CONFIG_BT_ENABLED", True)
|
||||||
|
add_idf_sdkconfig_option("CONFIG_BTDM_CTRL_MODE_BTDM", True)
|
||||||
|
add_idf_sdkconfig_option("CONFIG_BT_CLASSIC_ENABLED", True)
|
||||||
|
|
|
@ -12,9 +12,7 @@
|
||||||
#include <freertos/task.h>
|
#include <freertos/task.h>
|
||||||
#include <nvs_flash.h>
|
#include <nvs_flash.h>
|
||||||
|
|
||||||
#ifdef USE_ARDUINO
|
#include "esphome/components/esp32_bt_common/bt_defs.h"
|
||||||
#include <esp32-hal-bt.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace esp32_bt_classic {
|
namespace esp32_bt_classic {
|
||||||
|
@ -63,7 +61,7 @@ bool ESP32BtClassic::bt_setup_() {
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
|
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
|
||||||
err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
|
err = esp_bt_controller_enable(BT_MODE);
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
ESP_LOGE(TAG, "esp_bt_controller_enable failed: %s", esp_err_to_name(err));
|
ESP_LOGE(TAG, "esp_bt_controller_enable failed: %s", esp_err_to_name(err));
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -7,12 +7,12 @@
|
||||||
|
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
|
|
||||||
|
#include "esphome/components/esp32_bt_common/queue.h"
|
||||||
|
|
||||||
// IDF headers
|
// IDF headers
|
||||||
#include <esp_bt_defs.h>
|
#include <esp_bt_defs.h>
|
||||||
#include <esp_gap_bt_api.h>
|
#include <esp_gap_bt_api.h>
|
||||||
|
|
||||||
#include "esphome/components/esp32_ble/queue.h"
|
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
@ -139,7 +139,7 @@ class ESP32BtClassic : public Component, public BtClassicItf {
|
||||||
std::vector<BtClassicScanResultListner *> scan_result_listners_;
|
std::vector<BtClassicScanResultListner *> scan_result_listners_;
|
||||||
|
|
||||||
// Ble-Queue which thread safety precautions:
|
// Ble-Queue which thread safety precautions:
|
||||||
esp32_ble::Queue<BtGapEvent> bt_events_;
|
esp32_bt_common::Queue<BtGapEvent> bt_events_;
|
||||||
|
|
||||||
const uint32_t scan_delay_{100}; // (ms) minimal time between consecutive scans
|
const uint32_t scan_delay_{100}; // (ms) minimal time between consecutive scans
|
||||||
};
|
};
|
||||||
|
|
6
esphome/components/esp32_bt_common/__init__.py
Normal file
6
esphome/components/esp32_bt_common/__init__.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import esphome.codegen as cg
|
||||||
|
|
||||||
|
DEPENDENCIES = ["esp32"]
|
||||||
|
CODEOWNERS = ["@RoboMagus"]
|
||||||
|
|
||||||
|
esp32_bt_common_ns = cg.esphome_ns.namespace("esp32_bt_common")
|
26
esphome/components/esp32_bt_common/bt_defs.h
Normal file
26
esphome/components/esp32_bt_common/bt_defs.h
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef USE_ESP32
|
||||||
|
|
||||||
|
#include <esp_bt.h>
|
||||||
|
|
||||||
|
// Define the configured BT operation mode:
|
||||||
|
#ifdef USE_ARDUINO
|
||||||
|
#include <esp32-hal-bt.h>
|
||||||
|
#else
|
||||||
|
#ifdef CONFIG_BTDM_CONTROLLER_MODE_BTDM
|
||||||
|
#define BT_MODE ESP_BT_MODE_BTDM
|
||||||
|
#elif defined(CONFIG_BTDM_CONTROLLER_MODE_BR_EDR_ONLY)
|
||||||
|
#define BT_MODE ESP_BT_MODE_CLASSIC_BT
|
||||||
|
#else
|
||||||
|
#define BT_MODE ESP_BT_MODE_BLE
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace esp32_bt_common {
|
||||||
|
|
||||||
|
} // namespace esp32_bt_common
|
||||||
|
} // namespace esphome
|
||||||
|
|
||||||
|
#endif
|
|
@ -9,7 +9,7 @@
|
||||||
#include <freertos/semphr.h>
|
#include <freertos/semphr.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* BLE events come in from a separate Task (thread) in the ESP32 stack. Rather
|
* Bluetooth events come in from a separate Task (thread) in the ESP32 stack. Rather
|
||||||
* than trying to deal with various locking strategies, all incoming GAP and GATT
|
* than trying to deal with various locking strategies, all incoming GAP and GATT
|
||||||
* events will simply be placed on a semaphore guarded queue. The next time the
|
* events will simply be placed on a semaphore guarded queue. The next time the
|
||||||
* component runs loop(), these events are popped off the queue and handed at
|
* component runs loop(), these events are popped off the queue and handed at
|
||||||
|
@ -17,7 +17,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace esp32_ble {
|
namespace esp32_bt_common {
|
||||||
|
|
||||||
template<class T> class Queue {
|
template<class T> class Queue {
|
||||||
public:
|
public:
|
||||||
|
@ -50,7 +50,7 @@ template<class T> class Queue {
|
||||||
SemaphoreHandle_t m_;
|
SemaphoreHandle_t m_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace esp32_ble
|
} // namespace esp32_bt_common
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -89,6 +89,8 @@ mqtt:
|
||||||
vbus:
|
vbus:
|
||||||
- uart_id: uart_2
|
- uart_id: uart_2
|
||||||
|
|
||||||
|
esp32_bt_classic:
|
||||||
|
|
||||||
binary_sensor:
|
binary_sensor:
|
||||||
- platform: gpio
|
- platform: gpio
|
||||||
pin: GPIO0
|
pin: GPIO0
|
||||||
|
@ -223,6 +225,10 @@ binary_sensor:
|
||||||
name: VBus Custom Binary Sensor
|
name: VBus Custom Binary Sensor
|
||||||
lambda: return x[0] & 1;
|
lambda: return x[0] & 1;
|
||||||
|
|
||||||
|
- platform: bt_classic_presence
|
||||||
|
name: BT Classic Presence T0
|
||||||
|
mac_address: 1a:2b:3c:4d:5e:6f
|
||||||
|
|
||||||
tlc5947:
|
tlc5947:
|
||||||
data_pin:
|
data_pin:
|
||||||
number: GPIO12
|
number: GPIO12
|
||||||
|
|
Loading…
Add table
Reference in a new issue