This commit is contained in:
Martin Bauer
2024-04-14 18:04:52 +02:00
parent a9ad6e9245
commit 269c6054eb
17 changed files with 763 additions and 15 deletions

View File

@@ -0,0 +1,31 @@
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.const import (
CONF_ACTIVE,
CONF_ID,
CONF_INTERVAL,
CONF_DURATION,
)
DEPENDENCIES = ["esp32"]
nimble_tracker_ns = cg.esphome_ns.namespace("my_nimble_tracker")
NimbleTracker = nimble_tracker_ns.class_("MyNimbleTracker", cg.Component)
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(NimbleTracker),
cv.Optional(CONF_ACTIVE, default=True): cv.boolean,
}).extend(cv.COMPONENT_SCHEMA)
cg.add_library(
name="NimBLE",
repository="https://github.com/h2zero/NimBLE-Arduino.git",
version="release/1.4",
)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
#cg.add(var.set_my_required_key(config[CONF_MY_REQUIRED_KEY]))

View File

@@ -0,0 +1,93 @@
#include "my_nimble_tracker.h"
#include "esphome/core/log.h"
#include "esp_log.h"
#include "nvs_flash.h"
/* BLE */
/*
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
#include "host/ble_hs.h"
#include "host/util/util.h"
#include "console/console.h"
#include "services/gap/ble_svc_gap.h"
#include "ble_prox_cent.h"
*/
namespace esphome {
namespace my_nimble_tracker {
static const char *const TAG = "my_nimble_tracker";
/*
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice *advertisedDevice)
{
ESP_LOGI(TAG, "Received advertisement for %s", advertisedDevice->getAddress().toString().c_str());
delay(1);
}
};
*/
void MyNimbleTracker::setup()
{
/*
NimBLEDevice::init("my_nimble_tracker");
pBLEScan_ = NimBLEDevice::getScan(); //create new scan
pBLEScan_->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan_->setActiveScan(false); //active scan uses more power, but get results faster
pBLEScan_->setInterval(1200);
pBLEScan_->setWindow(500); // less or equal setInterval value
*/
#if 0
int rc;
/* Initialize NVS — it is used to store PHY calibration data */
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
nimble_port_init();
/* Configure the host. */
ble_hs_cfg.reset_cb = ble_prox_cent_on_reset;
ble_hs_cfg.sync_cb = ble_prox_cent_on_sync;
ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
/* Initialize data structures to track connected peers. */
rc = peer_init(MYNEWT_VAL(BLE_MAX_CONNECTIONS), 64, 64, 64);
assert(rc == 0);
/* Set the default device name. */
rc = ble_svc_gap_device_name_set("nimble-prox-cent");
assert(rc == 0);
/* XXX Need to have template for store */
ble_store_config_init();
nimble_port_freertos_init(ble_prox_cent_host_task);
#endif
}
void MyNimbleTracker::loop()
{
/*
auto completion_func = [](NimBLEScanResults) { };
//ESP_LOGI(TAG, "Entering loop");
if(!pBLEScan_->isScanning()) {
ESP_LOGI(TAG, "Starting scan");
pBLEScan_->start(4, completion_func, false);
}
vTaskDelay(1);
//ESP_LOGI(TAG, "Exiting loop");
delay(1);
*/
}
} // namespace my_nimble_tracker
} // namespace esphome

View File

@@ -0,0 +1,20 @@
#pragma once
#include "esphome/core/component.h"
class NimBLEScan;
namespace esphome {
namespace my_nimble_tracker {
class MyNimbleTracker : public Component {
public:
void setup() override;
void loop() override;
private:
NimBLEScan *pBLEScan_;
};
}
}