mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Mirage remote receiver & transmitter (#6479)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
		| @@ -1913,3 +1913,41 @@ async def abbwelcome_action(var, config, args): | |||||||
|             cg.add(var.set_data_template(template_)) |             cg.add(var.set_data_template(template_)) | ||||||
|         else: |         else: | ||||||
|             cg.add(var.set_data_static(data_)) |             cg.add(var.set_data_static(data_)) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | # Mirage | ||||||
|  | ( | ||||||
|  |     MirageData, | ||||||
|  |     MirageBinarySensor, | ||||||
|  |     MirageTrigger, | ||||||
|  |     MirageAction, | ||||||
|  |     MirageDumper, | ||||||
|  | ) = declare_protocol("Mirage") | ||||||
|  |  | ||||||
|  | MIRAGE_SCHEMA = cv.Schema( | ||||||
|  |     { | ||||||
|  |         cv.Required(CONF_CODE): cv.All([cv.hex_uint8_t], cv.Length(min=14, max=14)), | ||||||
|  |     } | ||||||
|  | ) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @register_binary_sensor("mirage", MirageBinarySensor, MIRAGE_SCHEMA) | ||||||
|  | def mirage_binary_sensor(var, config): | ||||||
|  |     cg.add(var.set_code(config[CONF_CODE])) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @register_trigger("mirage", MirageTrigger, MirageData) | ||||||
|  | def mirage_trigger(var, config): | ||||||
|  |     pass | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @register_dumper("mirage", MirageDumper) | ||||||
|  | def mirage_dumper(var, config): | ||||||
|  |     pass | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @register_action("mirage", MirageAction, MIRAGE_SCHEMA) | ||||||
|  | async def mirage_action(var, config, args): | ||||||
|  |     vec_ = cg.std_vector.template(cg.uint8) | ||||||
|  |     template_ = await cg.templatable(config[CONF_CODE], args, vec_, vec_) | ||||||
|  |     cg.add(var.set_code(template_)) | ||||||
|   | |||||||
							
								
								
									
										84
									
								
								esphome/components/remote_base/mirage_protocol.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								esphome/components/remote_base/mirage_protocol.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,84 @@ | |||||||
|  | #include "mirage_protocol.h" | ||||||
|  | #include "esphome/core/log.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace remote_base { | ||||||
|  |  | ||||||
|  | static const char *const TAG = "remote.mirage"; | ||||||
|  |  | ||||||
|  | constexpr uint32_t HEADER_MARK_US = 8360; | ||||||
|  | constexpr uint32_t HEADER_SPACE_US = 4248; | ||||||
|  | constexpr uint32_t BIT_MARK_US = 554; | ||||||
|  | constexpr uint32_t BIT_ONE_SPACE_US = 1592; | ||||||
|  | constexpr uint32_t BIT_ZERO_SPACE_US = 545; | ||||||
|  |  | ||||||
|  | constexpr unsigned int MIRAGE_IR_PACKET_BIT_SIZE = 120; | ||||||
|  |  | ||||||
|  | void MirageProtocol::encode(RemoteTransmitData *dst, const MirageData &data) { | ||||||
|  |   ESP_LOGI(TAG, "Transive Mirage: %s", format_hex_pretty(data.data).c_str()); | ||||||
|  |   dst->set_carrier_frequency(38000); | ||||||
|  |   dst->reserve(5 + ((data.data.size() + 1) * 2)); | ||||||
|  |   dst->mark(HEADER_MARK_US); | ||||||
|  |   dst->space(HEADER_SPACE_US); | ||||||
|  |   dst->mark(BIT_MARK_US); | ||||||
|  |   uint8_t checksum = 0; | ||||||
|  |   for (uint8_t item : data.data) { | ||||||
|  |     this->encode_byte_(dst, item); | ||||||
|  |     checksum += (item >> 4) + (item & 0xF); | ||||||
|  |   } | ||||||
|  |   this->encode_byte_(dst, checksum); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void MirageProtocol::encode_byte_(RemoteTransmitData *dst, uint8_t item) { | ||||||
|  |   for (uint8_t b = 0; b < 8; b++) { | ||||||
|  |     if (item & (1UL << b)) { | ||||||
|  |       dst->space(BIT_ONE_SPACE_US); | ||||||
|  |     } else { | ||||||
|  |       dst->space(BIT_ZERO_SPACE_US); | ||||||
|  |     } | ||||||
|  |     dst->mark(BIT_MARK_US); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | optional<MirageData> MirageProtocol::decode(RemoteReceiveData src) { | ||||||
|  |   if (!src.expect_item(HEADER_MARK_US, HEADER_SPACE_US)) { | ||||||
|  |     return {}; | ||||||
|  |   } | ||||||
|  |   if (!src.expect_mark(BIT_MARK_US)) { | ||||||
|  |     return {}; | ||||||
|  |   } | ||||||
|  |   size_t size = src.size() - src.get_index() - 1; | ||||||
|  |   if (size < MIRAGE_IR_PACKET_BIT_SIZE * 2) | ||||||
|  |     return {}; | ||||||
|  |   size = MIRAGE_IR_PACKET_BIT_SIZE * 2; | ||||||
|  |   uint8_t checksum = 0; | ||||||
|  |   MirageData out; | ||||||
|  |   while (size > 0) { | ||||||
|  |     uint8_t data = 0; | ||||||
|  |     for (uint8_t b = 0; b < 8; b++) { | ||||||
|  |       if (src.expect_space(BIT_ONE_SPACE_US)) { | ||||||
|  |         data |= (1UL << b); | ||||||
|  |       } else if (!src.expect_space(BIT_ZERO_SPACE_US)) { | ||||||
|  |         return {}; | ||||||
|  |       } | ||||||
|  |       if (!src.expect_mark(BIT_MARK_US)) { | ||||||
|  |         return {}; | ||||||
|  |       } | ||||||
|  |       size -= 2; | ||||||
|  |     } | ||||||
|  |     if (size > 0) { | ||||||
|  |       checksum += (data >> 4) + (data & 0xF); | ||||||
|  |       out.data.push_back(data); | ||||||
|  |     } else if (checksum != data) { | ||||||
|  |       return {}; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |   return out; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void MirageProtocol::dump(const MirageData &data) { | ||||||
|  |   ESP_LOGI(TAG, "Received Mirage: %s", format_hex_pretty(data.data).c_str()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | }  // namespace remote_base | ||||||
|  | }  // namespace esphome | ||||||
							
								
								
									
										39
									
								
								esphome/components/remote_base/mirage_protocol.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								esphome/components/remote_base/mirage_protocol.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | |||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include "esphome/core/component.h" | ||||||
|  | #include "remote_base.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace remote_base { | ||||||
|  |  | ||||||
|  | struct MirageData { | ||||||
|  |   std::vector<uint8_t> data; | ||||||
|  |  | ||||||
|  |   bool operator==(const MirageData &rhs) const { return data == rhs.data; } | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | class MirageProtocol : public RemoteProtocol<MirageData> { | ||||||
|  |  public: | ||||||
|  |   void encode(RemoteTransmitData *dst, const MirageData &data) override; | ||||||
|  |   optional<MirageData> decode(RemoteReceiveData src) override; | ||||||
|  |   void dump(const MirageData &data) override; | ||||||
|  |  | ||||||
|  |  protected: | ||||||
|  |   void encode_byte_(RemoteTransmitData *dst, uint8_t item); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | DECLARE_REMOTE_PROTOCOL(Mirage) | ||||||
|  |  | ||||||
|  | template<typename... Ts> class MirageAction : public RemoteTransmitterActionBase<Ts...> { | ||||||
|  |  public: | ||||||
|  |   TEMPLATABLE_VALUE(std::vector<uint8_t>, code) | ||||||
|  |  | ||||||
|  |   void encode(RemoteTransmitData *dst, Ts... x) override { | ||||||
|  |     MirageData data{}; | ||||||
|  |     data.data = this->code_.value(x...); | ||||||
|  |     MirageProtocol().encode(dst, data); | ||||||
|  |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | }  // namespace remote_base | ||||||
|  | }  // namespace esphome | ||||||
| @@ -143,6 +143,10 @@ remote_receiver: | |||||||
|       - logger.log: |       - logger.log: | ||||||
|           format: "on_toshiba_ac: %llu %llu" |           format: "on_toshiba_ac: %llu %llu" | ||||||
|           args: ["x.rc_code_1", "x.rc_code_2"] |           args: ["x.rc_code_1", "x.rc_code_2"] | ||||||
|  |   on_mirage: | ||||||
|  |     then: | ||||||
|  |       - lambda: |- | ||||||
|  |           ESP_LOGD("mirage", "Mirage data: %s", format_hex(x.data).c_str()); | ||||||
|  |  | ||||||
| binary_sensor: | binary_sensor: | ||||||
|   - platform: remote_receiver |   - platform: remote_receiver | ||||||
|   | |||||||
| @@ -142,6 +142,10 @@ remote_receiver: | |||||||
|       - logger.log: |       - logger.log: | ||||||
|           format: "on_toshiba_ac: %llu %llu" |           format: "on_toshiba_ac: %llu %llu" | ||||||
|           args: ["x.rc_code_1", "x.rc_code_2"] |           args: ["x.rc_code_1", "x.rc_code_2"] | ||||||
|  |   on_mirage: | ||||||
|  |     then: | ||||||
|  |       - lambda: |- | ||||||
|  |           ESP_LOGD("mirage", "Mirage data: %s", format_hex(x.data).c_str()); | ||||||
|  |  | ||||||
| binary_sensor: | binary_sensor: | ||||||
|   - platform: remote_receiver |   - platform: remote_receiver | ||||||
|   | |||||||
| @@ -176,6 +176,11 @@ button: | |||||||
|             0x00, |             0x00, | ||||||
|             0x05, |             0x05, | ||||||
|           ] |           ] | ||||||
|  |   - platform: template | ||||||
|  |     name: Mirage | ||||||
|  |     on_press: | ||||||
|  |       remote_transmitter.transmit_mirage: | ||||||
|  |         code: [0x56, 0x77, 0x00, 0x00, 0x22, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] | ||||||
|   - platform: template |   - platform: template | ||||||
|     name: Dooya |     name: Dooya | ||||||
|     on_press: |     on_press: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user