ESPHome  2021.11.3
climate_ir_lg.cpp
Go to the documentation of this file.
1 #include "climate_ir_lg.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace climate_ir_lg {
6 
7 static const char *const TAG = "climate.climate_ir_lg";
8 
9 const uint32_t COMMAND_ON = 0x00000;
10 const uint32_t COMMAND_ON_AI = 0x03000;
11 const uint32_t COMMAND_COOL = 0x08000;
12 const uint32_t COMMAND_HEAT = 0x0C000;
13 const uint32_t COMMAND_OFF = 0xC0000;
14 const uint32_t COMMAND_SWING = 0x10000;
15 // On, 25C, Mode: Auto, Fan: Auto, Zone Follow: Off, Sensor Temp: Ignore.
16 const uint32_t COMMAND_AUTO = 0x0B000;
17 const uint32_t COMMAND_DRY_FAN = 0x09000;
18 
19 const uint32_t COMMAND_MASK = 0xFF000;
20 
21 const uint32_t FAN_MASK = 0xF0;
22 const uint32_t FAN_AUTO = 0x50;
23 const uint32_t FAN_MIN = 0x00;
24 const uint32_t FAN_MED = 0x20;
25 const uint32_t FAN_MAX = 0x40;
26 
27 // Temperature
28 const uint8_t TEMP_RANGE = TEMP_MAX - TEMP_MIN + 1;
29 const uint32_t TEMP_MASK = 0XF00;
30 const uint32_t TEMP_SHIFT = 8;
31 
32 const uint16_t BITS = 28;
33 
35  uint32_t remote_state = 0x8800000;
36 
37  // ESP_LOGD(TAG, "climate_lg_ir mode_before_ code: 0x%02X", modeBefore_);
38  if (send_swing_cmd_) {
39  send_swing_cmd_ = false;
40  remote_state |= COMMAND_SWING;
41  } else {
43  remote_state |= COMMAND_ON_AI;
45  remote_state |= COMMAND_ON;
47  } else {
48  switch (this->mode) {
50  remote_state |= COMMAND_COOL;
51  break;
53  remote_state |= COMMAND_HEAT;
54  break;
56  remote_state |= COMMAND_AUTO;
57  break;
59  remote_state |= COMMAND_DRY_FAN;
60  break;
62  default:
63  remote_state |= COMMAND_OFF;
64  break;
65  }
66  }
67  mode_before_ = this->mode;
68 
69  ESP_LOGD(TAG, "climate_lg_ir mode code: 0x%02X", this->mode);
70 
71  if (this->mode == climate::CLIMATE_MODE_OFF) {
72  remote_state |= FAN_AUTO;
73  } else if (this->mode == climate::CLIMATE_MODE_COOL || this->mode == climate::CLIMATE_MODE_DRY ||
75  switch (this->fan_mode.value()) {
77  remote_state |= FAN_MAX;
78  break;
80  remote_state |= FAN_MED;
81  break;
83  remote_state |= FAN_MIN;
84  break;
86  default:
87  remote_state |= FAN_AUTO;
88  break;
89  }
90  }
91 
94  // remote_state |= FAN_MODE_AUTO_DRY;
95  }
97  auto temp = (uint8_t) roundf(clamp<float>(this->target_temperature, TEMP_MIN, TEMP_MAX));
98  remote_state |= ((temp - 15) << TEMP_SHIFT);
99  }
100  }
101  transmit_(remote_state);
102  this->publish_state();
103 }
104 
106  uint8_t nbits = 0;
107  uint32_t remote_state = 0;
108 
109  if (!data.expect_item(this->header_high_, this->header_low_))
110  return false;
111 
112  for (nbits = 0; nbits < 32; nbits++) {
113  if (data.expect_item(this->bit_high_, this->bit_one_low_)) {
114  remote_state = (remote_state << 1) | 1;
115  } else if (data.expect_item(this->bit_high_, this->bit_zero_low_)) {
116  remote_state = (remote_state << 1) | 0;
117  } else if (nbits == BITS) {
118  break;
119  } else {
120  return false;
121  }
122  }
123 
124  ESP_LOGD(TAG, "Decoded 0x%02X", remote_state);
125  if ((remote_state & 0xFF00000) != 0x8800000)
126  return false;
127 
128  if ((remote_state & COMMAND_MASK) == COMMAND_ON) {
130  } else if ((remote_state & COMMAND_MASK) == COMMAND_ON_AI) {
132  }
133 
134  if ((remote_state & COMMAND_MASK) == COMMAND_OFF) {
136  } else if ((remote_state & COMMAND_MASK) == COMMAND_SWING) {
137  this->swing_mode =
139  } else {
140  if ((remote_state & COMMAND_MASK) == COMMAND_AUTO)
142  else if ((remote_state & COMMAND_MASK) == COMMAND_DRY_FAN)
144  else if ((remote_state & COMMAND_MASK) == COMMAND_HEAT) {
146  } else {
148  }
149 
150  // Temperature
152  this->target_temperature = ((remote_state & TEMP_MASK) >> TEMP_SHIFT) + 15;
153 
154  // Fan Speed
155  if (this->mode == climate::CLIMATE_MODE_HEAT_COOL) {
157  } else if (this->mode == climate::CLIMATE_MODE_COOL || this->mode == climate::CLIMATE_MODE_HEAT ||
158  this->mode == climate::CLIMATE_MODE_DRY) {
159  if ((remote_state & FAN_MASK) == FAN_AUTO)
161  else if ((remote_state & FAN_MASK) == FAN_MIN)
163  else if ((remote_state & FAN_MASK) == FAN_MED)
165  else if ((remote_state & FAN_MASK) == FAN_MAX)
167  }
168  }
169  this->publish_state();
170 
171  return true;
172 }
173 void LgIrClimate::transmit_(uint32_t value) {
174  calc_checksum_(value);
175  ESP_LOGD(TAG, "Sending climate_lg_ir code: 0x%02X", value);
176 
177  auto transmit = this->transmitter_->transmit();
178  auto data = transmit.get_data();
179 
180  data->set_carrier_frequency(38000);
181  data->reserve(2 + BITS * 2u);
182 
183  data->item(this->header_high_, this->header_low_);
184 
185  for (uint32_t mask = 1UL << (BITS - 1); mask != 0; mask >>= 1) {
186  if (value & mask) {
187  data->item(this->bit_high_, this->bit_one_low_);
188  } else {
189  data->item(this->bit_high_, this->bit_zero_low_);
190  }
191  }
192  data->mark(this->bit_high_);
193  transmit.perform();
194 }
195 void LgIrClimate::calc_checksum_(uint32_t &value) {
196  uint32_t mask = 0xF;
197  uint32_t sum = 0;
198  for (uint8_t i = 1; i < 8; i++) {
199  sum += (value & (mask << (i * 4))) >> (i * 4);
200  }
201 
202  value |= (sum & mask);
203 }
204 
205 } // namespace climate_ir_lg
206 } // namespace esphome
The fan mode is set to Low.
Definition: climate_mode.h:54
value_type const & value() const
Definition: optional.h:89
const uint32_t COMMAND_MASK
climate::ClimateMode mode_before_
Definition: climate_ir_lg.h:51
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition: climate.h:204
const uint8_t TEMP_MIN
Definition: climate_ir_lg.h:9
float target_temperature
The target temperature of the climate device.
Definition: climate.h:183
const uint32_t FAN_MED
void set_carrier_frequency(uint32_t carrier_frequency)
Definition: remote_base.h:30
const uint32_t TEMP_MASK
The climate device is set to heat to reach the target temperature.
Definition: climate_mode.h:18
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:175
bool on_receive(remote_base::RemoteReceiveData data) override
Handle received IR Buffer.
The climate device is set to dry/humidity mode.
Definition: climate_mode.h:22
const uint32_t COMMAND_OFF
The climate device is set to cool to reach the target temperature.
Definition: climate_mode.h:16
The fan mode is set to Auto.
Definition: climate_mode.h:52
const uint32_t COMMAND_AUTO
const uint32_t COMMAND_COOL
const uint32_t COMMAND_ON_AI
void calc_checksum_(uint32_t &value)
const uint32_t FAN_AUTO
const uint32_t FAN_MASK
The climate device is set to heat/cool to reach the target temperature.
Definition: climate_mode.h:14
The fan mode is set to Vertical.
Definition: climate_mode.h:74
const uint32_t COMMAND_SWING
const uint32_t COMMAND_ON
void transmit_state() override
Transmit via IR the state of this climate controller.
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition: climate.cpp:380
The fan mode is set to High.
Definition: climate_mode.h:58
The swing mode is set to Off.
Definition: climate_mode.h:70
The climate device is off.
Definition: climate_mode.h:12
const uint32_t COMMAND_DRY_FAN
const uint32_t TEMP_SHIFT
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition: climate.h:198
const uint32_t COMMAND_HEAT
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
const uint8_t TEMP_MAX
Definition: climate_ir_lg.h:10
The fan mode is set to Medium.
Definition: climate_mode.h:56
bool expect_item(uint32_t mark, uint32_t space)
Definition: remote_base.h:111
const uint32_t FAN_MIN
remote_transmitter::RemoteTransmitterComponent * transmitter_
Definition: climate_ir.h:65
const uint8_t TEMP_RANGE
const uint32_t FAN_MAX