mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	cond
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)) |     cg.add(desc_var.set_value(value)) | ||||||
|     if CONF_ON_WRITE in descriptor_conf: |     if CONF_ON_WRITE in descriptor_conf: | ||||||
|         on_write_conf = descriptor_conf[CONF_ON_WRITE] |         on_write_conf = descriptor_conf[CONF_ON_WRITE] | ||||||
|  |         cg.add_define("USE_ESP32_BLE_SERVER_DESCRIPTOR_ON_WRITE") | ||||||
|         await automation.build_automation( |         await automation.build_automation( | ||||||
|             BLETriggers_ns.create_descriptor_on_write_trigger(desc_var), |             BLETriggers_ns.create_descriptor_on_write_trigger(desc_var), | ||||||
|             [(cg.std_vector.template(cg.uint8), "x"), (cg.uint16, "id")], |             [(cg.std_vector.template(cg.uint8), "x"), (cg.uint16, "id")], | ||||||
| @@ -505,23 +506,32 @@ async def to_code_characteristic(service_var, char_conf): | |||||||
|     ) |     ) | ||||||
|     if CONF_ON_WRITE in char_conf: |     if CONF_ON_WRITE in char_conf: | ||||||
|         on_write_conf = char_conf[CONF_ON_WRITE] |         on_write_conf = char_conf[CONF_ON_WRITE] | ||||||
|  |         cg.add_define("USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE") | ||||||
|         await automation.build_automation( |         await automation.build_automation( | ||||||
|             BLETriggers_ns.create_characteristic_on_write_trigger(char_var), |             BLETriggers_ns.create_characteristic_on_write_trigger(char_var), | ||||||
|             [(cg.std_vector.template(cg.uint8), "x"), (cg.uint16, "id")], |             [(cg.std_vector.template(cg.uint8), "x"), (cg.uint16, "id")], | ||||||
|             on_write_conf, |             on_write_conf, | ||||||
|         ) |         ) | ||||||
|     if CONF_VALUE in char_conf: |     if CONF_VALUE in char_conf: | ||||||
|         action_conf = { |         # Check if the value is templated (Lambda) | ||||||
|             CONF_ID: char_conf[CONF_ID], |         value_data = char_conf[CONF_VALUE].get(CONF_DATA) | ||||||
|             CONF_VALUE: char_conf[CONF_VALUE], |         if isinstance(value_data, cv.Lambda): | ||||||
|         } |             # Templated value - need the full action infrastructure | ||||||
|         value_action = await ble_server_characteristic_set_value( |             action_conf = { | ||||||
|             action_conf, |                 CONF_ID: char_conf[CONF_ID], | ||||||
|             char_conf[CONF_CHAR_VALUE_ACTION_ID_], |                 CONF_VALUE: char_conf[CONF_VALUE], | ||||||
|             cg.TemplateArguments(), |             } | ||||||
|             {}, |             value_action = await ble_server_characteristic_set_value( | ||||||
|         ) |                 action_conf, | ||||||
|         cg.add(value_action.play()) |                 char_conf[CONF_CHAR_VALUE_ACTION_ID_], | ||||||
|  |                 cg.TemplateArguments(), | ||||||
|  |                 {}, | ||||||
|  |             ) | ||||||
|  |             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]: |     for descriptor_conf in char_conf[CONF_DESCRIPTORS]: | ||||||
|         await to_code_descriptor(descriptor_conf, char_var) |         await to_code_descriptor(descriptor_conf, char_var) | ||||||
|  |  | ||||||
| @@ -560,12 +570,14 @@ async def to_code(config): | |||||||
|         else: |         else: | ||||||
|             cg.add(var.enqueue_start_service(service_var)) |             cg.add(var.enqueue_start_service(service_var)) | ||||||
|     if CONF_ON_CONNECT in config: |     if CONF_ON_CONNECT in config: | ||||||
|  |         cg.add_define("USE_ESP32_BLE_SERVER_ON_CONNECT") | ||||||
|         await automation.build_automation( |         await automation.build_automation( | ||||||
|             BLETriggers_ns.create_server_on_connect_trigger(var), |             BLETriggers_ns.create_server_on_connect_trigger(var), | ||||||
|             [(cg.uint16, "id")], |             [(cg.uint16, "id")], | ||||||
|             config[CONF_ON_CONNECT], |             config[CONF_ON_CONNECT], | ||||||
|         ) |         ) | ||||||
|     if CONF_ON_DISCONNECT in config: |     if CONF_ON_DISCONNECT in config: | ||||||
|  |         cg.add_define("USE_ESP32_BLE_SERVER_ON_DISCONNECT") | ||||||
|         await automation.build_automation( |         await automation.build_automation( | ||||||
|             BLETriggers_ns.create_server_on_disconnect_trigger(var), |             BLETriggers_ns.create_server_on_disconnect_trigger(var), | ||||||
|             [(cg.uint16, "id")], |             [(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) |     var = cg.new_Pvariable(action_id, template_arg, paren) | ||||||
|     value = await parse_value(config[CONF_VALUE], args) |     value = await parse_value(config[CONF_VALUE], args) | ||||||
|     cg.add(var.set_buffer(value)) |     cg.add(var.set_buffer(value)) | ||||||
|  |     cg.add_define("USE_ESP32_BLE_SERVER_SET_VALUE_ACTION") | ||||||
|     return var |     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) |     var = cg.new_Pvariable(action_id, template_arg, paren) | ||||||
|     value = await parse_value(config[CONF_VALUE], args) |     value = await parse_value(config[CONF_VALUE], args) | ||||||
|     cg.add(var.set_buffer(value)) |     cg.add(var.set_buffer(value)) | ||||||
|  |     cg.add_define("USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION") | ||||||
|     return var |     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): | async def ble_server_characteristic_notify(config, action_id, template_arg, args): | ||||||
|     paren = await cg.get_variable(config[CONF_ID]) |     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) |     return cg.new_Pvariable(action_id, template_arg, paren) | ||||||
|   | |||||||
| @@ -9,6 +9,7 @@ namespace esp32_ble_server_automations { | |||||||
|  |  | ||||||
| using namespace esp32_ble; | 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( | Trigger<std::vector<uint8_t>, uint16_t> *BLETriggers::create_characteristic_on_write_trigger( | ||||||
|     BLECharacteristic *characteristic) { |     BLECharacteristic *characteristic) { | ||||||
|   Trigger<std::vector<uint8_t>, uint16_t> *on_write_trigger =  // NOLINT(cppcoreguidelines-owning-memory) |   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); }); |       [on_write_trigger](const std::vector<uint8_t> &data, uint16_t id) { on_write_trigger->trigger(data, id); }); | ||||||
|   return on_write_trigger; |   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> *BLETriggers::create_descriptor_on_write_trigger(BLEDescriptor *descriptor) { | ||||||
|   Trigger<std::vector<uint8_t>, uint16_t> *on_write_trigger =  // NOLINT(cppcoreguidelines-owning-memory) |   Trigger<std::vector<uint8_t>, uint16_t> *on_write_trigger =  // NOLINT(cppcoreguidelines-owning-memory) | ||||||
|       new Trigger<std::vector<uint8_t>, uint16_t>(); |       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); }); |       [on_write_trigger](const std::vector<uint8_t> &data, uint16_t id) { on_write_trigger->trigger(data, id); }); | ||||||
|   return on_write_trigger; |   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> *BLETriggers::create_server_on_connect_trigger(BLEServer *server) { | ||||||
|   Trigger<uint16_t> *on_connect_trigger = new Trigger<uint16_t>();  // NOLINT(cppcoreguidelines-owning-memory) |   Trigger<uint16_t> *on_connect_trigger = new Trigger<uint16_t>();  // NOLINT(cppcoreguidelines-owning-memory) | ||||||
|   server->on(BLEServerEvt::EmptyEvt::ON_CONNECT, |   server->on(BLEServerEvt::EmptyEvt::ON_CONNECT, | ||||||
|              [on_connect_trigger](uint16_t conn_id) { on_connect_trigger->trigger(conn_id); }); |              [on_connect_trigger](uint16_t conn_id) { on_connect_trigger->trigger(conn_id); }); | ||||||
|   return on_connect_trigger; |   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> *BLETriggers::create_server_on_disconnect_trigger(BLEServer *server) { | ||||||
|   Trigger<uint16_t> *on_disconnect_trigger = new Trigger<uint16_t>();  // NOLINT(cppcoreguidelines-owning-memory) |   Trigger<uint16_t> *on_disconnect_trigger = new Trigger<uint16_t>();  // NOLINT(cppcoreguidelines-owning-memory) | ||||||
|   server->on(BLEServerEvt::EmptyEvt::ON_DISCONNECT, |   server->on(BLEServerEvt::EmptyEvt::ON_DISCONNECT, | ||||||
|              [on_disconnect_trigger](uint16_t conn_id) { on_disconnect_trigger->trigger(conn_id); }); |              [on_disconnect_trigger](uint16_t conn_id) { on_disconnect_trigger->trigger(conn_id); }); | ||||||
|   return on_disconnect_trigger; |   return on_disconnect_trigger; | ||||||
| } | } | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | #ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION | ||||||
| void BLECharacteristicSetValueActionManager::set_listener(BLECharacteristic *characteristic, | void BLECharacteristicSetValueActionManager::set_listener(BLECharacteristic *characteristic, | ||||||
|                                                           EventEmitterListenerID listener_id, |                                                           EventEmitterListenerID listener_id, | ||||||
|                                                           const std::function<void()> &pre_notify_listener) { |                                                           const std::function<void()> &pre_notify_listener) { | ||||||
| @@ -69,6 +78,7 @@ void BLECharacteristicSetValueActionManager::set_listener(BLECharacteristic *cha | |||||||
|   // Save the pair listener_id, pre_notify_listener_id to the map |   // Save the pair listener_id, pre_notify_listener_id to the map | ||||||
|   this->listeners_[characteristic] = std::make_pair(listener_id, pre_notify_listener_id); |   this->listeners_[characteristic] = std::make_pair(listener_id, pre_notify_listener_id); | ||||||
| } | } | ||||||
|  | #endif | ||||||
|  |  | ||||||
| }  // namespace esp32_ble_server_automations | }  // namespace esp32_ble_server_automations | ||||||
| }  // namespace esp32_ble_server | }  // namespace esp32_ble_server | ||||||
|   | |||||||
| @@ -23,13 +23,22 @@ using namespace event_emitter; | |||||||
|  |  | ||||||
| class BLETriggers { | class BLETriggers { | ||||||
|  public: |  public: | ||||||
|  | #ifdef USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE | ||||||
|   static Trigger<std::vector<uint8_t>, uint16_t> *create_characteristic_on_write_trigger( |   static Trigger<std::vector<uint8_t>, uint16_t> *create_characteristic_on_write_trigger( | ||||||
|       BLECharacteristic *characteristic); |       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); |   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); |   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); |   static Trigger<uint16_t> *create_server_on_disconnect_trigger(BLEServer *server); | ||||||
|  | #endif | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | #ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION | ||||||
| enum BLECharacteristicSetValueActionEvt { | enum BLECharacteristicSetValueActionEvt { | ||||||
|   PRE_NOTIFY, |   PRE_NOTIFY, | ||||||
| }; | }; | ||||||
| @@ -82,13 +91,17 @@ template<typename... Ts> class BLECharacteristicSetValueAction : public Action<T | |||||||
|   BLECharacteristic *parent_; |   BLECharacteristic *parent_; | ||||||
|   EventEmitterListenerID listener_id_; |   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...> { | template<typename... Ts> class BLECharacteristicNotifyAction : public Action<Ts...> { | ||||||
|  public: |  public: | ||||||
|   BLECharacteristicNotifyAction(BLECharacteristic *characteristic) : parent_(characteristic) {} |   BLECharacteristicNotifyAction(BLECharacteristic *characteristic) : parent_(characteristic) {} | ||||||
|   void play(Ts... x) override { |   void play(Ts... x) override { | ||||||
|  | #ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION | ||||||
|     // Call the pre-notify event |     // Call the pre-notify event | ||||||
|     BLECharacteristicSetValueActionManager::get_instance()->emit_pre_notify(this->parent_); |     BLECharacteristicSetValueActionManager::get_instance()->emit_pre_notify(this->parent_); | ||||||
|  | #endif | ||||||
|     // Notify the characteristic |     // Notify the characteristic | ||||||
|     this->parent_->notify(); |     this->parent_->notify(); | ||||||
|   } |   } | ||||||
| @@ -96,7 +109,9 @@ template<typename... Ts> class BLECharacteristicNotifyAction : public Action<Ts. | |||||||
|  protected: |  protected: | ||||||
|   BLECharacteristic *parent_; |   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...> { | template<typename... Ts> class BLEDescriptorSetValueAction : public Action<Ts...> { | ||||||
|  public: |  public: | ||||||
|   BLEDescriptorSetValueAction(BLEDescriptor *descriptor) : parent_(descriptor) {} |   BLEDescriptorSetValueAction(BLEDescriptor *descriptor) : parent_(descriptor) {} | ||||||
| @@ -107,6 +122,7 @@ template<typename... Ts> class BLEDescriptorSetValueAction : public Action<Ts... | |||||||
|  protected: |  protected: | ||||||
|   BLEDescriptor *parent_; |   BLEDescriptor *parent_; | ||||||
| }; | }; | ||||||
|  | #endif  // USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION | ||||||
|  |  | ||||||
| }  // namespace esp32_ble_server_automations | }  // namespace esp32_ble_server_automations | ||||||
| }  // namespace esp32_ble_server | }  // namespace esp32_ble_server | ||||||
|   | |||||||
| @@ -158,6 +158,13 @@ | |||||||
| #define USE_ESP32_BLE_SERVER | #define USE_ESP32_BLE_SERVER | ||||||
| #define USE_ESP32_BLE_UUID | #define USE_ESP32_BLE_UUID | ||||||
| #define USE_ESP32_BLE_ADVERTISING | #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_ESP32_CAMERA_JPEG_ENCODER | ||||||
| #define USE_I2C | #define USE_I2C | ||||||
| #define USE_IMPROV | #define USE_IMPROV | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user