ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
mitsubishi_cn105_properties.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cmath>
5#include <cstdint>
6#include <limits>
7#include <type_traits>
8#include <utility>
9#include "mitsubishi_cn105.h"
10
12
13template<auto Unknown, size_t N> struct LookupMap {
14 using value_type = decltype(Unknown);
15 const std::array<value_type, N> table;
16
17 constexpr value_type lookup(uint8_t raw) const { return (raw < N) ? this->table[raw] : Unknown; }
18
19 constexpr bool reverse_lookup(value_type value, uint8_t &out) const {
20 static_assert(N <= std::numeric_limits<uint8_t>::max());
21 if (value == Unknown) {
22 return false;
23 }
24 for (uint8_t i = 0; i < static_cast<uint8_t>(N); ++i) {
25 if (this->table[i] == value) {
26 out = i;
27 return true;
28 }
29 }
30 return false;
31 }
32};
33
34template<auto Unknown, class T, std::size_t N> static constexpr auto make_map(const T (&values)[N]) {
35 return LookupMap<Unknown, N>{std::to_array(values)};
36}
37
38struct Property {
42
43 struct Power {
44 static constexpr auto ID = PropertyId::POWER;
45
46 static void decode_context(PropertyContext &ctx, const uint8_t *payload) {}
47
48 static void decode(Status &status, const uint8_t *payload, const PropertyContext &ctx) {
49 status.power_on = payload[2] != 0;
50 }
51
52 static void encode(uint8_t *payload, const Status &status, const PropertyContext &ctx) {
53 payload[1] |= 0x01;
54 payload[3] = status.power_on ? 0x01 : 0x00;
55 }
56 };
57
58 struct Operating {
59 static void decode_context(PropertyContext &ctx, const uint8_t *payload) {}
60
61 static void decode(Status &status, const uint8_t *payload, const PropertyContext &ctx) {
62 status.operating = payload[3] != 0;
63 }
64 };
65
66 struct Temperature {
67 struct Target {
68 static constexpr auto ID = PropertyId::TEMPERATURE;
69 static constexpr uint8_t TARGET_TEMPERATURE_ENC_A_OFFSET = 31;
70
71 static void decode_context(PropertyContext &ctx, const uint8_t *payload) {
72 ctx.use_temperature_encoding_b = payload[10] != 0;
73 }
74
75 static void decode(Status &status, const uint8_t *payload, const PropertyContext &ctx) {
76 status.target_temperature = Temperature::decode(-payload[4], payload[10], TARGET_TEMPERATURE_ENC_A_OFFSET);
77 }
78
79 static void encode(uint8_t *payload, const Status &status, const PropertyContext &ctx) {
80 payload[1] |= 0x04;
82 payload[14] = static_cast<uint8_t>(std::round(status.target_temperature * 2.0f) + 128);
83 } else {
84 payload[5] = static_cast<uint8_t>(TARGET_TEMPERATURE_ENC_A_OFFSET - std::round(status.target_temperature));
85 }
86 }
87 };
88
89 struct Room {
90 static void decode_context(PropertyContext &ctx, const uint8_t *payload) {}
91
92 static void decode(Status &status, const uint8_t *payload, const PropertyContext &ctx) {
93 status.room_temperature = Temperature::decode(payload[2], payload[5], 10);
94 }
95 };
96
97 struct Remote {
98 static constexpr auto ID = PropertyId::REMOTE_TEMPERATURE;
99
100 static void encode(uint8_t *payload, uint8_t remote_temperature_half_deg, const PropertyContext &) {
101 if (remote_temperature_half_deg == MitsubishiCN105::REMOTE_TEMPERATURE_DISABLED) {
102 payload[3] = 0x80;
103 } else {
104 payload[1] = 0x01;
105 payload[2] = static_cast<uint8_t>(remote_temperature_half_deg - 16);
106 payload[3] = static_cast<uint8_t>(remote_temperature_half_deg + 128);
107 }
108 }
109 };
110
111 protected:
112 static constexpr float decode(int temp_a, int temp_b, int delta) {
113 return temp_b != 0 ? (temp_b - 128) / 2.0f : delta + temp_a;
114 }
115 };
116
117 template<typename Derived, auto Field> struct Lookup {
118 using Value = std::remove_cvref_t<decltype(std::declval<Status>().*Field)>;
119
120 static void decode_context(PropertyContext &ctx, const uint8_t *payload) {}
121
122 static void decode(Status &status, const uint8_t *payload, const PropertyContext &ctx) {
123 status.*Field = Derived::MAP.lookup(Derived::decode_raw(payload, ctx));
124 }
125
126 static void encode(uint8_t *payload, const Status &status, const PropertyContext &ctx) {
127 uint8_t raw;
128 if (Derived::MAP.reverse_lookup(status.*Field, raw)) {
129 Derived::encode_raw(payload, raw, ctx);
130 }
131 }
132
133 template<typename Mask> static bool validate_and_set(Value value, Status &status, Mask &mask) {
134 uint8_t raw;
135 if (!Derived::MAP.reverse_lookup(value, raw)) {
136 return false;
137 }
138 status.*Field = value;
139 mask.set(Derived::ID);
140 return true;
141 }
142
143 private:
144 friend Derived;
145 constexpr Lookup() = default;
146 };
147
148 struct Mode : Lookup<Mode, &Status::mode> {
149 static constexpr auto ID = PropertyId::MODE;
150 static constexpr auto MAP = make_map<MitsubishiCN105::Mode::UNKNOWN>({
160 });
161
162 static uint8_t decode_raw(const uint8_t *payload, const PropertyContext &ctx) {
163 const bool i_see = payload[3] > 0x08;
164 return payload[3] - (i_see ? 0x08 : 0);
165 }
166
167 static void encode_raw(uint8_t *payload, uint8_t raw, const PropertyContext &) {
168 payload[1] |= 0x02;
169 payload[4] = raw;
170 }
171 };
172
173 struct FanMode : Lookup<FanMode, &Status::fan_mode> {
174 static constexpr auto ID = PropertyId::FAN;
175 static constexpr auto MAP = make_map<MitsubishiCN105::FanMode::UNKNOWN>({
183 });
184
185 static uint8_t decode_raw(const uint8_t *payload, const PropertyContext &ctx) { return payload[5]; }
186
187 static void encode_raw(uint8_t *payload, uint8_t raw, const PropertyContext &) {
188 payload[1] |= 0x08;
189 payload[6] = raw;
190 }
191 };
192
193 struct VaneMode : Lookup<VaneMode, &Status::vane_mode> {
194 static constexpr auto ID = PropertyId::VANE;
195 static constexpr auto MAP = make_map<MitsubishiCN105::VaneMode::UNKNOWN>({
204 });
205
206 static uint8_t decode_raw(const uint8_t *payload, const PropertyContext &ctx) { return payload[6]; }
207
208 static void encode_raw(uint8_t *payload, uint8_t raw, const PropertyContext &) {
209 payload[1] |= 0x10;
210 payload[7] = raw;
211 }
212 };
213
214 struct WideVaneMode : Lookup<WideVaneMode, &Status::wide_vane_mode> {
215 static constexpr auto ID = PropertyId::WIDE_VANE;
216 static constexpr auto MAP = make_map<MitsubishiCN105::WideVaneMode::UNKNOWN>({
225 MitsubishiCN105::WideVaneMode::SPLIT, // 0x08 left and right
230 });
231
232 static void decode_context(PropertyContext &ctx, const uint8_t *payload) {
233 ctx.set_wide_vane_high_bit = (payload[9] & 0xF0) == 0x80;
234 }
235
236 static uint8_t decode_raw(const uint8_t *payload, const PropertyContext &ctx) { return payload[9] & 0x0F; }
237
238 static void encode_raw(uint8_t *payload, uint8_t raw, const PropertyContext &ctx) {
239 payload[2] |= 0x01;
240 payload[13] = ctx.set_wide_vane_high_bit ? raw | 0x80 : raw;
241 }
242 };
243
244 template<typename Mask> struct Decoder {
245 const std::span<const uint8_t> payload;
247 const Mask &pending_writes;
248
249 bool ESPHOME_ALWAYS_INLINE decode_settings(Status &status) {
250 if (this->payload.size() <= 10) {
251 return false;
252 }
254 return true;
255 }
256
257 bool ESPHOME_ALWAYS_INLINE decode_room_temperature(Status &status) {
258 if (this->payload.size() <= 5) {
259 return false;
260 }
261 this->decode_<Temperature::Room>(status);
262 return true;
263 }
264
265 bool ESPHOME_ALWAYS_INLINE decode_operating_status(Status &status) {
266 if (this->payload.size() <= 3) {
267 return false;
268 }
269 this->decode_<Operating>(status);
270 return true;
271 }
272
273 protected:
274 template<typename T, typename Out> ESPHOME_ALWAYS_INLINE void decode_one_(Out &out) {
275 T::decode_context(this->context, this->payload.data());
276 if constexpr (requires { T::ID; }) {
277 if (this->pending_writes.contains(T::ID)) {
278 return;
279 }
280 }
281 T::decode(out, this->payload.data(), this->context);
282 }
283
284 template<typename... T, typename Out> void ESPHOME_ALWAYS_INLINE decode_(Out &out) {
285 (this->decode_one_<T>(out), ...);
286 }
287 };
288
289 template<typename Mask> struct Encoder {
290 uint8_t *payload;
293
294 void ESPHOME_ALWAYS_INLINE encode_settings(const Status &status) {
295 this->payload[0] = 0x01;
297 }
298
299 void ESPHOME_ALWAYS_INLINE encode_remote_temperature(uint8_t remote_temperature_half_deg) {
300 this->payload[0] = 0x07;
301 this->encode_and_clear_<Temperature::Remote>(remote_temperature_half_deg);
302 }
303
304 protected:
305 template<typename... T, typename In> void ESPHOME_ALWAYS_INLINE encode_and_clear_(const In &in) {
306 (this->encode_one_<T>(in), ...);
307 (this->pending_writes.clear(T::ID), ...);
308 }
309
310 template<typename T, typename In> void encode_one_(const In &in) {
311 if (this->pending_writes.contains(T::ID)) {
312 T::encode(this->payload, in, this->context);
313 }
314 }
315 };
316};
317
318} // namespace esphome::mitsubishi_cn105
uint8_t raw[35]
Definition bl0939.h:0
uint8_t status
Definition bl0942.h:8
static constexpr uint8_t REMOTE_TEMPERATURE_DISABLED
std::vector< uint8_t > & out
Definition gif.cpp:49
uint32_t value
Definition hal.cpp:41
constexpr value_type lookup(uint8_t raw) const
constexpr bool reverse_lookup(value_type value, uint8_t &out) const
const std::array< value_type, N > table
bool ESPHOME_ALWAYS_INLINE decode_settings(Status &status)
bool ESPHOME_ALWAYS_INLINE decode_operating_status(Status &status)
bool ESPHOME_ALWAYS_INLINE decode_room_temperature(Status &status)
void ESPHOME_ALWAYS_INLINE encode_remote_temperature(uint8_t remote_temperature_half_deg)
void ESPHOME_ALWAYS_INLINE encode_settings(const Status &status)
void ESPHOME_ALWAYS_INLINE encode_and_clear_(const In &in)
static void encode_raw(uint8_t *payload, uint8_t raw, const PropertyContext &)
static uint8_t decode_raw(const uint8_t *payload, const PropertyContext &ctx)
static bool validate_and_set(Value value, Status &status, Mask &mask)
std::remove_cvref_t< decltype(std::declval< Status >().*Field)> Value
static void decode(Status &status, const uint8_t *payload, const PropertyContext &ctx)
static void encode(uint8_t *payload, const Status &status, const PropertyContext &ctx)
static void decode_context(PropertyContext &ctx, const uint8_t *payload)
static uint8_t decode_raw(const uint8_t *payload, const PropertyContext &ctx)
static void encode_raw(uint8_t *payload, uint8_t raw, const PropertyContext &)
static void decode_context(PropertyContext &ctx, const uint8_t *payload)
static void decode(Status &status, const uint8_t *payload, const PropertyContext &ctx)
static void encode(uint8_t *payload, const Status &status, const PropertyContext &ctx)
static void decode(Status &status, const uint8_t *payload, const PropertyContext &ctx)
static void decode_context(PropertyContext &ctx, const uint8_t *payload)
static void encode(uint8_t *payload, uint8_t remote_temperature_half_deg, const PropertyContext &)
static void decode_context(PropertyContext &ctx, const uint8_t *payload)
static void decode(Status &status, const uint8_t *payload, const PropertyContext &ctx)
static void encode(uint8_t *payload, const Status &status, const PropertyContext &ctx)
static void decode_context(PropertyContext &ctx, const uint8_t *payload)
static void decode(Status &status, const uint8_t *payload, const PropertyContext &ctx)
static constexpr float decode(int temp_a, int temp_b, int delta)
static uint8_t decode_raw(const uint8_t *payload, const PropertyContext &ctx)
static void encode_raw(uint8_t *payload, uint8_t raw, const PropertyContext &)
static void decode_context(PropertyContext &ctx, const uint8_t *payload)
static void encode_raw(uint8_t *payload, uint8_t raw, const PropertyContext &ctx)
static uint8_t decode_raw(const uint8_t *payload, const PropertyContext &ctx)