ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ballu.cpp
Go to the documentation of this file.
1#include "ballu.h"
2#include "esphome/core/log.h"
3
4namespace esphome::ballu {
5
6static const char *const TAG = "ballu.climate";
7
8const uint16_t BALLU_HEADER_MARK = 9000;
9const uint16_t BALLU_HEADER_SPACE = 4500;
10const uint16_t BALLU_BIT_MARK = 575;
11const uint16_t BALLU_ONE_SPACE = 1675;
12const uint16_t BALLU_ZERO_SPACE = 550;
13
15
16const uint8_t BALLU_STATE_LENGTH = 13;
17
18const uint8_t BALLU_AUTO = 0;
19const uint8_t BALLU_COOL = 0x20;
20const uint8_t BALLU_DRY = 0x40;
21const uint8_t BALLU_HEAT = 0x80;
22const uint8_t BALLU_FAN = 0xc0;
23
24const uint8_t BALLU_FAN_AUTO = 0xa0;
25const uint8_t BALLU_FAN_HIGH = 0x20;
26const uint8_t BALLU_FAN_MED = 0x40;
27const uint8_t BALLU_FAN_LOW = 0x60;
28
29const uint8_t BALLU_SWING_VER = 0x07;
30const uint8_t BALLU_SWING_HOR = 0xe0;
31const uint8_t BALLU_POWER = 0x20;
32
34 uint8_t remote_state[BALLU_STATE_LENGTH] = {0};
35
36 auto temp = (uint8_t) roundf(clamp(this->target_temperature, YKR_K_002E_TEMP_MIN, YKR_K_002E_TEMP_MAX));
37 auto swing_ver =
39 auto swing_hor =
41
42 remote_state[0] = 0xc3;
43 remote_state[1] = ((temp - 8) << 3) | (swing_ver ? 0 : BALLU_SWING_VER);
44 remote_state[2] = swing_hor ? 0 : BALLU_SWING_HOR;
45 remote_state[9] = (this->mode == climate::CLIMATE_MODE_OFF) ? 0 : BALLU_POWER;
46 remote_state[11] = 0x1e;
47
48 // Fan speed
49 switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
51 remote_state[4] |= BALLU_FAN_HIGH;
52 break;
54 remote_state[4] |= BALLU_FAN_MED;
55 break;
57 remote_state[4] |= BALLU_FAN_LOW;
58 break;
60 remote_state[4] |= BALLU_FAN_AUTO;
61 break;
62 default:
63 break;
64 }
65
66 // Mode
67 switch (this->mode) {
69 remote_state[6] |= BALLU_AUTO;
70 break;
72 remote_state[6] |= BALLU_HEAT;
73 break;
75 remote_state[6] |= BALLU_COOL;
76 break;
78 remote_state[6] |= BALLU_DRY;
79 break;
81 remote_state[6] |= BALLU_FAN;
82 break;
84 remote_state[6] |= BALLU_AUTO;
85 default:
86 break;
87 }
88
89 // Checksum
90 for (uint8_t i = 0; i < BALLU_STATE_LENGTH - 1; i++)
91 remote_state[12] += remote_state[i];
92
93 ESP_LOGV(TAG, "Sending: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X", remote_state[0],
94 remote_state[1], remote_state[2], remote_state[3], remote_state[4], remote_state[5], remote_state[6],
95 remote_state[7], remote_state[8], remote_state[9], remote_state[10], remote_state[11], remote_state[12]);
96
97 // Send code
98 auto transmit = this->transmitter_->transmit();
99 auto *data = transmit.get_data();
100
101 data->set_carrier_frequency(38000);
102
103 // Header
104 data->mark(BALLU_HEADER_MARK);
105 data->space(BALLU_HEADER_SPACE);
106 // Data
107 for (uint8_t i : remote_state) {
108 for (uint8_t j = 0; j < 8; j++) {
109 data->mark(BALLU_BIT_MARK);
110 bool bit = i & (1 << j);
111 data->space(bit ? BALLU_ONE_SPACE : BALLU_ZERO_SPACE);
112 }
113 }
114 // Footer
115 data->mark(BALLU_BIT_MARK);
116
117 transmit.perform();
118}
119
121 // Validate header
122 if (!data.expect_item(BALLU_HEADER_MARK, BALLU_HEADER_SPACE)) {
123 ESP_LOGV(TAG, "Header fail");
124 return false;
125 }
126
127 uint8_t remote_state[BALLU_STATE_LENGTH] = {0};
128 // Read all bytes.
129 for (int i = 0; i < BALLU_STATE_LENGTH; i++) {
130 // Read bit
131 for (int j = 0; j < 8; j++) {
132 if (data.expect_item(BALLU_BIT_MARK, BALLU_ONE_SPACE)) {
133 remote_state[i] |= 1 << j;
134
135 } else if (!data.expect_item(BALLU_BIT_MARK, BALLU_ZERO_SPACE)) {
136 ESP_LOGV(TAG, "Byte %d bit %d fail", i, j);
137 return false;
138 }
139 }
140
141 ESP_LOGVV(TAG, "Byte %d %02X", i, remote_state[i]);
142 }
143 // Validate footer
144 if (!data.expect_mark(BALLU_BIT_MARK)) {
145 ESP_LOGV(TAG, "Footer fail");
146 return false;
147 }
148
149 uint8_t checksum = 0;
150 // Calculate checksum and compare with signal value.
151 for (uint8_t i = 0; i < BALLU_STATE_LENGTH - 1; i++)
152 checksum += remote_state[i];
153
154 if (checksum != remote_state[BALLU_STATE_LENGTH - 1]) {
155 ESP_LOGVV(TAG, "Checksum fail");
156 return false;
157 }
158
159 ESP_LOGV(TAG, "Received: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X", remote_state[0],
160 remote_state[1], remote_state[2], remote_state[3], remote_state[4], remote_state[5], remote_state[6],
161 remote_state[7], remote_state[8], remote_state[9], remote_state[10], remote_state[11], remote_state[12]);
162
163 // verify header remote code
164 if (remote_state[0] != 0xc3)
165 return false;
166
167 // powr on/off button
168 ESP_LOGV(TAG, "Power: %02X", (remote_state[9] & BALLU_POWER));
169
170 if ((remote_state[9] & BALLU_POWER) != BALLU_POWER) {
172 } else {
173 auto mode = remote_state[6] & 0xe0;
174 ESP_LOGV(TAG, "Mode: %02X", mode);
175 switch (mode) {
176 case BALLU_HEAT:
178 break;
179 case BALLU_COOL:
181 break;
182 case BALLU_DRY:
184 break;
185 case BALLU_FAN:
187 break;
188 case BALLU_AUTO:
190 break;
191 }
192 }
193
194 // Set received temp
195 int temp = remote_state[1] & 0xf8;
196 ESP_LOGVV(TAG, "Temperature Raw: %02X", temp);
197 temp = ((uint8_t) temp >> 3) + 8;
198 ESP_LOGVV(TAG, "Temperature Climate: %u", temp);
199 this->target_temperature = temp;
200
201 // Set received fan speed
202 auto fan = remote_state[4] & 0xe0;
203 ESP_LOGVV(TAG, "Fan: %02X", fan);
204 switch (fan) {
205 case BALLU_FAN_HIGH:
207 break;
208 case BALLU_FAN_MED:
210 break;
211 case BALLU_FAN_LOW:
213 break;
214 case BALLU_FAN_AUTO:
215 default:
217 break;
218 }
219
220 // Set received swing status
221 ESP_LOGVV(TAG, "Swing status: %02X %02X", remote_state[1] & BALLU_SWING_VER, remote_state[2] & BALLU_SWING_HOR);
222 if (((remote_state[1] & BALLU_SWING_VER) != BALLU_SWING_VER) &&
223 ((remote_state[2] & BALLU_SWING_HOR) != BALLU_SWING_HOR)) {
225 } else if ((remote_state[1] & BALLU_SWING_VER) != BALLU_SWING_VER) {
227 } else if ((remote_state[2] & BALLU_SWING_HOR) != BALLU_SWING_HOR) {
229 } else {
231 }
232
233 this->publish_state();
234 return true;
235}
236
237} // namespace esphome::ballu
uint8_t checksum
Definition bl0906.h:3
void transmit_state() override
Transmit via IR the state of this climate controller.
Definition ballu.cpp:33
bool on_receive(remote_base::RemoteReceiveData data) override
Handle received IR Buffer.
Definition ballu.cpp:120
ClimateMode mode
The active mode of the climate device.
Definition climate.h:293
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:287
float target_temperature
The target temperature of the climate device.
Definition climate.h:274
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:299
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:437
const uint16_t BALLU_HEADER_SPACE
Definition ballu.cpp:9
const uint8_t BALLU_COOL
Definition ballu.cpp:19
const uint8_t BALLU_AUTO
Definition ballu.cpp:18
const uint16_t BALLU_BIT_MARK
Definition ballu.cpp:10
const float YKR_K_002E_TEMP_MIN
Definition ballu.h:10
const uint8_t BALLU_DRY
Definition ballu.cpp:20
const uint8_t BALLU_FAN
Definition ballu.cpp:22
const uint8_t BALLU_STATE_LENGTH
Definition ballu.cpp:16
const uint32_t BALLU_CARRIER_FREQUENCY
Definition ballu.cpp:14
const float YKR_K_002E_TEMP_MAX
Definition ballu.h:11
const uint8_t BALLU_SWING_VER
Definition ballu.cpp:29
const uint8_t BALLU_FAN_LOW
Definition ballu.cpp:27
const uint8_t BALLU_HEAT
Definition ballu.cpp:21
const uint8_t BALLU_FAN_AUTO
Definition ballu.cpp:24
const uint16_t BALLU_HEADER_MARK
Definition ballu.cpp:8
const uint8_t BALLU_FAN_HIGH
Definition ballu.cpp:25
const uint16_t BALLU_ZERO_SPACE
Definition ballu.cpp:12
const uint8_t BALLU_POWER
Definition ballu.cpp:31
const uint16_t BALLU_ONE_SPACE
Definition ballu.cpp:11
const uint8_t BALLU_SWING_HOR
Definition ballu.cpp:30
const uint8_t BALLU_FAN_MED
Definition ballu.cpp:26
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ CLIMATE_SWING_HORIZONTAL
The fan mode is set to Horizontal.
@ CLIMATE_SWING_VERTICAL
The fan mode is set to Vertical.
@ CLIMATE_SWING_BOTH
The fan mode is set to Both.
@ CLIMATE_MODE_DRY
The climate device is set to dry/humidity mode.
@ CLIMATE_MODE_FAN_ONLY
The climate device only has the fan enabled, no heating or cooling is taking place.
@ CLIMATE_MODE_HEAT
The climate device is set to heat to reach the target temperature.
@ CLIMATE_MODE_COOL
The climate device is set to cool to reach the target temperature.
@ CLIMATE_MODE_OFF
The climate device is off.
@ CLIMATE_MODE_AUTO
The climate device is adjusting the temperature dynamically.
@ CLIMATE_FAN_MEDIUM
The fan mode is set to Medium.
@ CLIMATE_FAN_ON
The fan mode is set to On.
@ CLIMATE_FAN_AUTO
The fan mode is set to Auto.
@ CLIMATE_FAN_LOW
The fan mode is set to Low.
@ CLIMATE_FAN_HIGH
The fan mode is set to High.
static void uint32_t