mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	[uart] Optimize UART components to eliminate temporary vector allocations (#11570)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
		| @@ -446,7 +446,7 @@ async def uart_write_to_code(config, action_id, template_arg, args): | ||||
|         templ = await cg.templatable(data, args, cg.std_vector.template(cg.uint8)) | ||||
|         cg.add(var.set_data_template(templ)) | ||||
|     else: | ||||
|         cg.add(var.set_data_static(data)) | ||||
|         cg.add(var.set_data_static(cg.ArrayInitializer(*data))) | ||||
|     return var | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -14,8 +14,12 @@ template<typename... Ts> class UARTWriteAction : public Action<Ts...>, public Pa | ||||
|     this->data_func_ = func; | ||||
|     this->static_ = false; | ||||
|   } | ||||
|   void set_data_static(const std::vector<uint8_t> &data) { | ||||
|     this->data_static_ = data; | ||||
|   void set_data_static(std::vector<uint8_t> &&data) { | ||||
|     this->data_static_ = std::move(data); | ||||
|     this->static_ = true; | ||||
|   } | ||||
|   void set_data_static(std::initializer_list<uint8_t> data) { | ||||
|     this->data_static_ = std::vector<uint8_t>(data); | ||||
|     this->static_ = true; | ||||
|   } | ||||
|  | ||||
|   | ||||
| @@ -33,4 +33,4 @@ async def to_code(config): | ||||
|     data = config[CONF_DATA] | ||||
|     if isinstance(data, bytes): | ||||
|         data = [HexInt(x) for x in data] | ||||
|     cg.add(var.set_data(data)) | ||||
|     cg.add(var.set_data(cg.ArrayInitializer(*data))) | ||||
|   | ||||
| @@ -11,7 +11,8 @@ namespace uart { | ||||
|  | ||||
| class UARTButton : public button::Button, public UARTDevice, public Component { | ||||
|  public: | ||||
|   void set_data(const std::vector<uint8_t> &data) { this->data_ = data; } | ||||
|   void set_data(std::vector<uint8_t> &&data) { this->data_ = std::move(data); } | ||||
|   void set_data(std::initializer_list<uint8_t> data) { this->data_ = std::vector<uint8_t>(data); } | ||||
|  | ||||
|   void dump_config() override; | ||||
|  | ||||
|   | ||||
| @@ -44,16 +44,16 @@ async def to_code(config): | ||||
|         if data_on := data.get(CONF_TURN_ON): | ||||
|             if isinstance(data_on, bytes): | ||||
|                 data_on = [HexInt(x) for x in data_on] | ||||
|             cg.add(var.set_data_on(data_on)) | ||||
|             cg.add(var.set_data_on(cg.ArrayInitializer(*data_on))) | ||||
|         if data_off := data.get(CONF_TURN_OFF): | ||||
|             if isinstance(data_off, bytes): | ||||
|                 data_off = [HexInt(x) for x in data_off] | ||||
|             cg.add(var.set_data_off(data_off)) | ||||
|             cg.add(var.set_data_off(cg.ArrayInitializer(*data_off))) | ||||
|     else: | ||||
|         data = config[CONF_DATA] | ||||
|         if isinstance(data, bytes): | ||||
|             data = [HexInt(x) for x in data] | ||||
|         cg.add(var.set_data_on(data)) | ||||
|         cg.add(var.set_data_on(cg.ArrayInitializer(*data))) | ||||
|         cg.add(var.set_single_state(True)) | ||||
|     if CONF_SEND_EVERY in config: | ||||
|         cg.add(var.set_send_every(config[CONF_SEND_EVERY])) | ||||
|   | ||||
| @@ -14,8 +14,10 @@ class UARTSwitch : public switch_::Switch, public UARTDevice, public Component { | ||||
|  public: | ||||
|   void loop() override; | ||||
|  | ||||
|   void set_data_on(const std::vector<uint8_t> &data) { this->data_on_ = data; } | ||||
|   void set_data_off(const std::vector<uint8_t> &data) { this->data_off_ = data; } | ||||
|   void set_data_on(std::vector<uint8_t> &&data) { this->data_on_ = std::move(data); } | ||||
|   void set_data_on(std::initializer_list<uint8_t> data) { this->data_on_ = std::vector<uint8_t>(data); } | ||||
|   void set_data_off(std::vector<uint8_t> &&data) { this->data_off_ = std::move(data); } | ||||
|   void set_data_off(std::initializer_list<uint8_t> data) { this->data_off_ = std::vector<uint8_t>(data); } | ||||
|   void set_send_every(uint32_t send_every) { this->send_every_ = send_every; } | ||||
|   void set_single_state(bool single) { this->single_state_ = single; } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user