mirror of
https://github.com/esphome/esphome.git
synced 2025-09-26 07:02:21 +01:00
🏗 Merge C++ into python codebase (#504)
## Description: Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97 Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍 Progress: - Core support (file copy etc): 80% - Base Abstractions (light, switch): ~50% - Integrations: ~10% - Working? Yes, (but only with ported components). Other refactors: - Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`) - Rework coroutine syntax - Move from `component/platform.py` to `domain/component.py` structure as with HA - Move all defaults out of C++ and into config validation. - Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration. - Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit. Future work: - Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block - Enable loading from `custom_components` folder. **Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97 **Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here> ## Checklist: - [ ] The code change is tested and works locally. - [ ] Tests have been added to verify that the new code works (under `tests/` folder). If user exposed functionality or configuration variables are added/changed: - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
This commit is contained in:
29
esphome/components/esp32_ble_beacon/__init__.py
Normal file
29
esphome/components/esp32_ble_beacon/__init__.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID, CONF_TYPE, CONF_UUID, ESP_PLATFORM_ESP32
|
||||
|
||||
ESP_PLATFORMS = [ESP_PLATFORM_ESP32]
|
||||
CONFLICTS_WITH = ['esp32_ble_tracker']
|
||||
|
||||
esp32_ble_beacon_ns = cg.esphome_ns.namespace('esp32_ble_beacon')
|
||||
ESP32BLEBeacon = esp32_ble_beacon_ns.class_('ESP32BLEBeacon', cg.Component)
|
||||
|
||||
CONF_MAJOR = 'major'
|
||||
CONF_MINOR = 'minor'
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema({
|
||||
cv.GenerateID(): cv.declare_variable_id(ESP32BLEBeacon),
|
||||
cv.Required(CONF_TYPE): cv.one_of('IBEACON', upper=True),
|
||||
cv.Required(CONF_UUID): cv.uuid,
|
||||
cv.Optional(CONF_MAJOR, default=10167): cv.uint16_t,
|
||||
cv.Optional(CONF_MINOR, default=61958): cv.uint16_t,
|
||||
}).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
||||
def to_code(config):
|
||||
uuid = config[CONF_UUID].hex
|
||||
uuid_arr = [cg.RawExpression('0x{}'.format(uuid[i:i + 2])) for i in range(0, len(uuid), 2)]
|
||||
var = cg.new_Pvariable(config[CONF_ID], uuid_arr)
|
||||
yield cg.register_component(var, config)
|
||||
cg.add(var.set_major(config[CONF_MAJOR]))
|
||||
cg.add(var.set_minor(config[CONF_MINOR]))
|
134
esphome/components/esp32_ble_beacon/esp32_ble_beacon.cpp
Normal file
134
esphome/components/esp32_ble_beacon/esp32_ble_beacon.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
#include "esp32_ble_beacon.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
|
||||
#include <nvs_flash.h>
|
||||
#include <freertos/FreeRTOSConfig.h>
|
||||
#include <esp_bt_main.h>
|
||||
#include <esp_bt.h>
|
||||
#include <freertos/task.h>
|
||||
#include <esp_gap_ble_api.h>
|
||||
|
||||
namespace esphome {
|
||||
namespace esp32_ble_beacon {
|
||||
|
||||
static const char *TAG = "esp32_ble_beacon";
|
||||
|
||||
static esp_ble_adv_params_t ble_adv_params = {
|
||||
.adv_int_min = 0x20,
|
||||
.adv_int_max = 0x40,
|
||||
.adv_type = ADV_TYPE_NONCONN_IND,
|
||||
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
|
||||
.peer_addr = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||
.peer_addr_type = BLE_ADDR_TYPE_PUBLIC,
|
||||
.channel_map = ADV_CHNL_ALL,
|
||||
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
|
||||
};
|
||||
|
||||
#define ENDIAN_CHANGE_U16(x) ((((x) &0xFF00) >> 8) + (((x) &0xFF) << 8))
|
||||
|
||||
static esp_ble_ibeacon_head_t ibeacon_common_head = {
|
||||
.flags = {0x02, 0x01, 0x06}, .length = 0x1A, .type = 0xFF, .company_id = 0x004C, .beacon_type = 0x1502};
|
||||
|
||||
void ESP32BLEBeacon::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up ESP32 BLE beacon...");
|
||||
global_esp32_ble_beacon = this;
|
||||
|
||||
xTaskCreatePinnedToCore(ESP32BLEBeacon::ble_core_task,
|
||||
"ble_task", // name
|
||||
10000, // stack size (in words)
|
||||
nullptr, // input params
|
||||
1, // priority
|
||||
nullptr, // Handle, not needed
|
||||
0 // core
|
||||
);
|
||||
}
|
||||
|
||||
float ESP32BLEBeacon::get_setup_priority() const { return setup_priority::DATA; }
|
||||
void ESP32BLEBeacon::ble_core_task(void *params) {
|
||||
ble_setup();
|
||||
|
||||
while (true) {
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
void ESP32BLEBeacon::ble_setup() {
|
||||
// Initialize non-volatile storage for the bluetooth controller
|
||||
esp_err_t err = nvs_flash_init();
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "nvs_flash_init failed: %d", err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!btStart()) {
|
||||
ESP_LOGE(TAG, "btStart failed: %d", esp_bt_controller_get_status());
|
||||
return;
|
||||
}
|
||||
|
||||
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
|
||||
|
||||
err = esp_bluedroid_init();
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_bluedroid_init failed: %d", err);
|
||||
return;
|
||||
}
|
||||
err = esp_bluedroid_enable();
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_bluedroid_enable failed: %d", err);
|
||||
return;
|
||||
}
|
||||
err = esp_ble_gap_register_callback(ESP32BLEBeacon::gap_event_handler);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_ble_gap_register_callback failed: %d", err);
|
||||
return;
|
||||
}
|
||||
|
||||
esp_ble_ibeacon_t ibeacon_adv_data;
|
||||
memcpy(&ibeacon_adv_data.ibeacon_head, &ibeacon_common_head, sizeof(esp_ble_ibeacon_head_t));
|
||||
memcpy(&ibeacon_adv_data.ibeacon_vendor.proximity_uuid, global_esp32_ble_beacon->uuid_.data(),
|
||||
sizeof(ibeacon_adv_data.ibeacon_vendor.proximity_uuid));
|
||||
ibeacon_adv_data.ibeacon_vendor.minor = ENDIAN_CHANGE_U16(global_esp32_ble_beacon->minor_);
|
||||
ibeacon_adv_data.ibeacon_vendor.major = ENDIAN_CHANGE_U16(global_esp32_ble_beacon->major_);
|
||||
ibeacon_adv_data.ibeacon_vendor.measured_power = 0xC5;
|
||||
|
||||
esp_ble_gap_config_adv_data_raw((uint8_t *) &ibeacon_adv_data, sizeof(ibeacon_adv_data));
|
||||
}
|
||||
|
||||
void ESP32BLEBeacon::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
|
||||
esp_err_t err;
|
||||
switch (event) {
|
||||
case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT: {
|
||||
err = esp_ble_gap_start_advertising(&ble_adv_params);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_ble_gap_start_advertising failed: %d", err);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: {
|
||||
err = param->adv_start_cmpl.status;
|
||||
if (err != ESP_BT_STATUS_SUCCESS) {
|
||||
ESP_LOGE(TAG, "BLE adv start failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: {
|
||||
err = param->adv_start_cmpl.status;
|
||||
if (err != ESP_BT_STATUS_SUCCESS) {
|
||||
ESP_LOGE(TAG, "BLE adv stop failed: %s", esp_err_to_name(err));
|
||||
} else {
|
||||
ESP_LOGD(TAG, "BLE stopped advertising successfully");
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ESP32BLEBeacon *global_esp32_ble_beacon = nullptr;
|
||||
|
||||
} // namespace esp32_ble_beacon
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
57
esphome/components/esp32_ble_beacon/esp32_ble_beacon.h
Normal file
57
esphome/components/esp32_ble_beacon/esp32_ble_beacon.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
|
||||
#include <esp_gap_ble_api.h>
|
||||
|
||||
namespace esphome {
|
||||
namespace esp32_ble_beacon {
|
||||
|
||||
typedef struct {
|
||||
uint8_t flags[3];
|
||||
uint8_t length;
|
||||
uint8_t type;
|
||||
uint16_t company_id;
|
||||
uint16_t beacon_type;
|
||||
} __attribute__((packed)) esp_ble_ibeacon_head_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t proximity_uuid[16];
|
||||
uint16_t major;
|
||||
uint16_t minor;
|
||||
uint8_t measured_power;
|
||||
} __attribute__((packed)) esp_ble_ibeacon_vendor_t;
|
||||
|
||||
typedef struct {
|
||||
esp_ble_ibeacon_head_t ibeacon_head;
|
||||
esp_ble_ibeacon_vendor_t ibeacon_vendor;
|
||||
} __attribute__((packed)) esp_ble_ibeacon_t;
|
||||
|
||||
class ESP32BLEBeacon : public Component {
|
||||
public:
|
||||
explicit ESP32BLEBeacon(const std::array<uint8_t, 16> &uuid) : uuid_(uuid) {}
|
||||
|
||||
void setup() override;
|
||||
float get_setup_priority() const override;
|
||||
|
||||
void set_major(uint16_t major) { this->major_ = major; }
|
||||
void set_minor(uint16_t minor) { this->minor_ = minor; }
|
||||
|
||||
protected:
|
||||
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
|
||||
static void ble_core_task(void *params);
|
||||
static void ble_setup();
|
||||
|
||||
std::array<uint8_t, 16> uuid_;
|
||||
uint16_t major_{};
|
||||
uint16_t minor_{};
|
||||
};
|
||||
|
||||
extern ESP32BLEBeacon *global_esp32_ble_beacon;
|
||||
|
||||
} // namespace esp32_ble_beacon
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user