mirror of
https://github.com/esphome/esphome.git
synced 2025-09-28 16:12:24 +01:00
Merge branch 'ble_server_automations_conditional_compile' into integration
This commit is contained in:
@@ -488,6 +488,7 @@ async def to_code_descriptor(descriptor_conf, char_var):
|
||||
cg.add(desc_var.set_value(value))
|
||||
if CONF_ON_WRITE in descriptor_conf:
|
||||
on_write_conf = descriptor_conf[CONF_ON_WRITE]
|
||||
cg.add_define("USE_ESP32_BLE_SERVER_DESCRIPTOR_ON_WRITE")
|
||||
await automation.build_automation(
|
||||
BLETriggers_ns.create_descriptor_on_write_trigger(desc_var),
|
||||
[(cg.std_vector.template(cg.uint8), "x"), (cg.uint16, "id")],
|
||||
@@ -505,12 +506,17 @@ async def to_code_characteristic(service_var, char_conf):
|
||||
)
|
||||
if CONF_ON_WRITE in char_conf:
|
||||
on_write_conf = char_conf[CONF_ON_WRITE]
|
||||
cg.add_define("USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE")
|
||||
await automation.build_automation(
|
||||
BLETriggers_ns.create_characteristic_on_write_trigger(char_var),
|
||||
[(cg.std_vector.template(cg.uint8), "x"), (cg.uint16, "id")],
|
||||
on_write_conf,
|
||||
)
|
||||
if CONF_VALUE in char_conf:
|
||||
# Check if the value is templated (Lambda)
|
||||
value_data = char_conf[CONF_VALUE].get(CONF_DATA)
|
||||
if isinstance(value_data, cv.Lambda):
|
||||
# Templated value - need the full action infrastructure
|
||||
action_conf = {
|
||||
CONF_ID: char_conf[CONF_ID],
|
||||
CONF_VALUE: char_conf[CONF_VALUE],
|
||||
@@ -522,6 +528,10 @@ async def to_code_characteristic(service_var, char_conf):
|
||||
{},
|
||||
)
|
||||
cg.add(value_action.play())
|
||||
else:
|
||||
# Static value - just set it directly without action infrastructure
|
||||
value = await parse_value(char_conf[CONF_VALUE], {})
|
||||
cg.add(char_var.set_value(value))
|
||||
for descriptor_conf in char_conf[CONF_DESCRIPTORS]:
|
||||
await to_code_descriptor(descriptor_conf, char_var)
|
||||
|
||||
@@ -560,12 +570,14 @@ async def to_code(config):
|
||||
else:
|
||||
cg.add(var.enqueue_start_service(service_var))
|
||||
if CONF_ON_CONNECT in config:
|
||||
cg.add_define("USE_ESP32_BLE_SERVER_ON_CONNECT")
|
||||
await automation.build_automation(
|
||||
BLETriggers_ns.create_server_on_connect_trigger(var),
|
||||
[(cg.uint16, "id")],
|
||||
config[CONF_ON_CONNECT],
|
||||
)
|
||||
if CONF_ON_DISCONNECT in config:
|
||||
cg.add_define("USE_ESP32_BLE_SERVER_ON_DISCONNECT")
|
||||
await automation.build_automation(
|
||||
BLETriggers_ns.create_server_on_disconnect_trigger(var),
|
||||
[(cg.uint16, "id")],
|
||||
@@ -594,6 +606,7 @@ async def ble_server_characteristic_set_value(config, action_id, template_arg, a
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
value = await parse_value(config[CONF_VALUE], args)
|
||||
cg.add(var.set_buffer(value))
|
||||
cg.add_define("USE_ESP32_BLE_SERVER_SET_VALUE_ACTION")
|
||||
return var
|
||||
|
||||
|
||||
@@ -612,6 +625,7 @@ async def ble_server_descriptor_set_value(config, action_id, template_arg, args)
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
value = await parse_value(config[CONF_VALUE], args)
|
||||
cg.add(var.set_buffer(value))
|
||||
cg.add_define("USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION")
|
||||
return var
|
||||
|
||||
|
||||
@@ -629,4 +643,5 @@ async def ble_server_descriptor_set_value(config, action_id, template_arg, args)
|
||||
)
|
||||
async def ble_server_characteristic_notify(config, action_id, template_arg, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
cg.add_define("USE_ESP32_BLE_SERVER_NOTIFY_ACTION")
|
||||
return cg.new_Pvariable(action_id, template_arg, paren)
|
||||
|
@@ -9,6 +9,7 @@ namespace esp32_ble_server_automations {
|
||||
|
||||
using namespace esp32_ble;
|
||||
|
||||
#ifdef USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE
|
||||
Trigger<std::vector<uint8_t>, uint16_t> *BLETriggers::create_characteristic_on_write_trigger(
|
||||
BLECharacteristic *characteristic) {
|
||||
Trigger<std::vector<uint8_t>, uint16_t> *on_write_trigger = // NOLINT(cppcoreguidelines-owning-memory)
|
||||
@@ -18,7 +19,9 @@ Trigger<std::vector<uint8_t>, uint16_t> *BLETriggers::create_characteristic_on_w
|
||||
[on_write_trigger](const std::vector<uint8_t> &data, uint16_t id) { on_write_trigger->trigger(data, id); });
|
||||
return on_write_trigger;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_ON_WRITE
|
||||
Trigger<std::vector<uint8_t>, uint16_t> *BLETriggers::create_descriptor_on_write_trigger(BLEDescriptor *descriptor) {
|
||||
Trigger<std::vector<uint8_t>, uint16_t> *on_write_trigger = // NOLINT(cppcoreguidelines-owning-memory)
|
||||
new Trigger<std::vector<uint8_t>, uint16_t>();
|
||||
@@ -27,21 +30,27 @@ Trigger<std::vector<uint8_t>, uint16_t> *BLETriggers::create_descriptor_on_write
|
||||
[on_write_trigger](const std::vector<uint8_t> &data, uint16_t id) { on_write_trigger->trigger(data, id); });
|
||||
return on_write_trigger;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP32_BLE_SERVER_ON_CONNECT
|
||||
Trigger<uint16_t> *BLETriggers::create_server_on_connect_trigger(BLEServer *server) {
|
||||
Trigger<uint16_t> *on_connect_trigger = new Trigger<uint16_t>(); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
server->on(BLEServerEvt::EmptyEvt::ON_CONNECT,
|
||||
[on_connect_trigger](uint16_t conn_id) { on_connect_trigger->trigger(conn_id); });
|
||||
return on_connect_trigger;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP32_BLE_SERVER_ON_DISCONNECT
|
||||
Trigger<uint16_t> *BLETriggers::create_server_on_disconnect_trigger(BLEServer *server) {
|
||||
Trigger<uint16_t> *on_disconnect_trigger = new Trigger<uint16_t>(); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
server->on(BLEServerEvt::EmptyEvt::ON_DISCONNECT,
|
||||
[on_disconnect_trigger](uint16_t conn_id) { on_disconnect_trigger->trigger(conn_id); });
|
||||
return on_disconnect_trigger;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
|
||||
void BLECharacteristicSetValueActionManager::set_listener(BLECharacteristic *characteristic,
|
||||
EventEmitterListenerID listener_id,
|
||||
const std::function<void()> &pre_notify_listener) {
|
||||
@@ -90,6 +99,7 @@ void BLECharacteristicSetValueActionManager::remove_listener_(BLECharacteristic
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace esp32_ble_server_automations
|
||||
} // namespace esp32_ble_server
|
||||
|
@@ -25,13 +25,22 @@ static constexpr EventEmitterListenerID INVALID_LISTENER_ID = 0;
|
||||
|
||||
class BLETriggers {
|
||||
public:
|
||||
#ifdef USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE
|
||||
static Trigger<std::vector<uint8_t>, uint16_t> *create_characteristic_on_write_trigger(
|
||||
BLECharacteristic *characteristic);
|
||||
#endif
|
||||
#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_ON_WRITE
|
||||
static Trigger<std::vector<uint8_t>, uint16_t> *create_descriptor_on_write_trigger(BLEDescriptor *descriptor);
|
||||
#endif
|
||||
#ifdef USE_ESP32_BLE_SERVER_ON_CONNECT
|
||||
static Trigger<uint16_t> *create_server_on_connect_trigger(BLEServer *server);
|
||||
#endif
|
||||
#ifdef USE_ESP32_BLE_SERVER_ON_DISCONNECT
|
||||
static Trigger<uint16_t> *create_server_on_disconnect_trigger(BLEServer *server);
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
|
||||
enum BLECharacteristicSetValueActionEvt {
|
||||
PRE_NOTIFY,
|
||||
};
|
||||
@@ -97,13 +106,17 @@ template<typename... Ts> class BLECharacteristicSetValueAction : public Action<T
|
||||
BLECharacteristic *parent_;
|
||||
EventEmitterListenerID listener_id_;
|
||||
};
|
||||
#endif // USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
|
||||
|
||||
#ifdef USE_ESP32_BLE_SERVER_NOTIFY_ACTION
|
||||
template<typename... Ts> class BLECharacteristicNotifyAction : public Action<Ts...> {
|
||||
public:
|
||||
BLECharacteristicNotifyAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
|
||||
void play(Ts... x) override {
|
||||
#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
|
||||
// Call the pre-notify event
|
||||
BLECharacteristicSetValueActionManager::get_instance()->emit_pre_notify(this->parent_);
|
||||
#endif
|
||||
// Notify the characteristic
|
||||
this->parent_->notify();
|
||||
}
|
||||
@@ -111,7 +124,9 @@ template<typename... Ts> class BLECharacteristicNotifyAction : public Action<Ts.
|
||||
protected:
|
||||
BLECharacteristic *parent_;
|
||||
};
|
||||
#endif // USE_ESP32_BLE_SERVER_NOTIFY_ACTION
|
||||
|
||||
#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION
|
||||
template<typename... Ts> class BLEDescriptorSetValueAction : public Action<Ts...> {
|
||||
public:
|
||||
BLEDescriptorSetValueAction(BLEDescriptor *descriptor) : parent_(descriptor) {}
|
||||
@@ -122,6 +137,7 @@ template<typename... Ts> class BLEDescriptorSetValueAction : public Action<Ts...
|
||||
protected:
|
||||
BLEDescriptor *parent_;
|
||||
};
|
||||
#endif // USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION
|
||||
|
||||
} // namespace esp32_ble_server_automations
|
||||
} // namespace esp32_ble_server
|
||||
|
@@ -160,6 +160,13 @@
|
||||
#define USE_ESP32_BLE_SERVER
|
||||
#define USE_ESP32_BLE_UUID
|
||||
#define USE_ESP32_BLE_ADVERTISING
|
||||
#define USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
|
||||
#define USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION
|
||||
#define USE_ESP32_BLE_SERVER_NOTIFY_ACTION
|
||||
#define USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE
|
||||
#define USE_ESP32_BLE_SERVER_DESCRIPTOR_ON_WRITE
|
||||
#define USE_ESP32_BLE_SERVER_ON_CONNECT
|
||||
#define USE_ESP32_BLE_SERVER_ON_DISCONNECT
|
||||
#define USE_ESP32_CAMERA_JPEG_ENCODER
|
||||
#define USE_I2C
|
||||
#define USE_IMPROV
|
||||
|
Reference in New Issue
Block a user