ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
opentherm.cpp
Go to the documentation of this file.
1/*
2 * OpenTherm protocol implementation. Originally taken from https://github.com/jpraus/arduino-opentherm, but
3 * heavily modified to comply with ESPHome coding standards and provide better logging.
4 * Original code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
5 * Public License, which is compatible with GPLv3 license, which covers C++ part of ESPHome project.
6 */
7
8#include "opentherm.h"
10#include <cinttypes>
11// TODO: Migrate from legacy timer API (driver/timer.h) to GPTimer API (driver/gptimer.h)
12// The legacy timer API is deprecated in ESP-IDF 5.x. See opentherm.h for details.
13#ifdef USE_ESP32
14#include "driver/timer.h"
15#include "esp_err.h"
16#endif
17#ifdef ESP8266
18#include "Arduino.h"
19#endif
20#include <string>
21
22namespace esphome {
23namespace opentherm {
24
25using std::string;
26
27static const char *const TAG = "opentherm";
28
29#ifdef ESP8266
30OpenTherm *OpenTherm::instance = nullptr;
31#endif
32
33OpenTherm::OpenTherm(InternalGPIOPin *in_pin, InternalGPIOPin *out_pin, int32_t device_timeout)
34 : in_pin_(in_pin),
35 out_pin_(out_pin),
36#ifdef USE_ESP32
37 timer_group_(TIMER_GROUP_0),
38 timer_idx_(TIMER_0),
39#endif
40 mode_(OperationMode::IDLE),
41 error_type_(ProtocolErrorType::NO_ERROR),
42 capture_(0),
43 clock_(0),
44 data_(0),
45 bit_pos_(0),
46 timeout_counter_(-1),
47 device_timeout_(device_timeout) {
48 this->isr_in_pin_ = in_pin->to_isr();
49 this->isr_out_pin_ = out_pin->to_isr();
50}
51
53#ifdef ESP8266
54 OpenTherm::instance = this;
55#endif
56 this->in_pin_->pin_mode(gpio::FLAG_INPUT);
57 this->in_pin_->setup();
58 this->out_pin_->pin_mode(gpio::FLAG_OUTPUT);
59 this->out_pin_->setup();
60 this->out_pin_->digital_write(true);
61
62#ifdef USE_ESP32
63 return this->init_esp32_timer_();
64#else
65 return true;
66#endif
67}
68
70 this->stop_timer_();
71 this->timeout_counter_ = this->device_timeout_ * 5; // timer_ ticks at 5 ticks/ms
72
73 this->mode_ = OperationMode::LISTEN;
74 this->data_ = 0;
75 this->bit_pos_ = 0;
76
77 this->start_read_timer_();
78}
79
81 this->stop_timer_();
82 this->data_ = data.type;
83 this->data_ = (this->data_ << 12) | data.id;
84 this->data_ = (this->data_ << 8) | data.valueHB;
85 this->data_ = (this->data_ << 8) | data.valueLB;
86 if (!check_parity_(this->data_)) {
87 this->data_ = this->data_ | 0x80000000;
88 }
89
90 this->clock_ = 1; // clock starts at HIGH
91 this->bit_pos_ = 33; // count down (33 == start bit, 32-1 data, 0 == stop bit)
92 this->mode_ = OperationMode::WRITE;
93
94 this->start_write_timer_();
95}
96
98 if (this->mode_ == OperationMode::RECEIVED) {
99 data.type = (this->data_ >> 28) & 0x7;
100 data.id = (this->data_ >> 16) & 0xFF;
101 data.valueHB = (this->data_ >> 8) & 0xFF;
102 data.valueLB = this->data_ & 0xFF;
103 return true;
104 }
105 return false;
106}
107
109 if (this->mode_ != OperationMode::ERROR_PROTOCOL) {
110 return false;
111 }
112
113 error.error_type = this->error_type_;
114 error.bit_pos = this->bit_pos_;
115 error.capture = this->capture_;
116 error.clock = this->clock_;
117 error.data = this->data_;
118
119 return true;
120}
121
123 this->stop_timer_();
124 this->mode_ = OperationMode::IDLE;
125}
126
127void IRAM_ATTR OpenTherm::read_() {
128 this->data_ = 0;
129 this->bit_pos_ = 0;
130 this->mode_ = OperationMode::READ;
131 this->capture_ = 1; // reset counter and add as if read start bit
132 this->clock_ = 1; // clock is high at the start of comm
133 this->start_read_timer_(); // get us into 1/4 of manchester code. 5 timer ticks constitute 1 ms, which is 1 bit
134 // period in OpenTherm.
135}
136
137bool IRAM_ATTR OpenTherm::timer_isr(OpenTherm *arg) {
138 if (arg->mode_ == OperationMode::LISTEN) {
139 if (arg->timeout_counter_ == 0) {
140 arg->mode_ = OperationMode::ERROR_TIMEOUT;
141 arg->stop_timer_();
142 return false;
143 }
144 bool const value = arg->isr_in_pin_.digital_read();
145 if (value) { // incoming data (rising signal)
146 arg->read_();
147 }
148 if (arg->timeout_counter_ > 0) {
149 arg->timeout_counter_--;
150 }
151 } else if (arg->mode_ == OperationMode::READ) {
152 bool const value = arg->isr_in_pin_.digital_read();
153 uint8_t const last = (arg->capture_ & 1);
154 if (value != last) {
155 // transition of signal from last sampling
156 if (arg->clock_ == 1 && arg->capture_ > 0xF) {
157 // no transition in the middle of the bit
159 arg->error_type_ = ProtocolErrorType::NO_TRANSITION;
160 arg->stop_timer_();
161 return false;
162 } else if (arg->clock_ == 1 || arg->capture_ > 0xF) {
163 // transition in the middle of the bit OR no transition between two bit, both are valid data points
164 if (arg->bit_pos_ == BitPositions::STOP_BIT) {
165 // expecting stop bit
166 auto stop_bit_error = arg->verify_stop_bit_(last);
167 if (stop_bit_error == ProtocolErrorType::NO_ERROR) {
168 arg->mode_ = OperationMode::RECEIVED;
169 arg->stop_timer_();
170 return false;
171 } else {
172 // end of data not verified, invalid data
174 arg->error_type_ = stop_bit_error;
175 arg->stop_timer_();
176 return false;
177 }
178 } else {
179 // normal data point at clock high
180 arg->bit_read_(last);
181 arg->clock_ = 0;
182 }
183 } else {
184 // clock low, not a data point, switch clock
185 arg->clock_ = 1;
186 }
187 arg->capture_ = 1; // reset counter
188 } else if (arg->capture_ > 0xFF) {
189 // no change for too long, invalid manchester encoding
191 arg->error_type_ = ProtocolErrorType::NO_CHANGE_TOO_LONG;
192 arg->stop_timer_();
193 return false;
194 }
195 arg->capture_ = (arg->capture_ << 1) | value;
196 } else if (arg->mode_ == OperationMode::WRITE) {
197 // write data to pin
198 if (arg->bit_pos_ == 33 || arg->bit_pos_ == 0) { // start bit
199 arg->write_bit_(1, arg->clock_);
200 } else { // data bits
201 arg->write_bit_(read_bit(arg->data_, arg->bit_pos_ - 1), arg->clock_);
202 }
203 if (arg->clock_ == 0) {
204 if (arg->bit_pos_ <= 0) { // check termination
205 arg->mode_ = OperationMode::SENT; // all data written
206 arg->stop_timer_();
207 }
208 arg->bit_pos_--;
209 arg->clock_ = 1;
210 } else {
211 arg->clock_ = 0;
212 }
213 }
214
215 return false;
216}
217
218#ifdef ESP8266
219void IRAM_ATTR OpenTherm::esp8266_timer_isr() { OpenTherm::timer_isr(OpenTherm::instance); }
220#endif
221
222void IRAM_ATTR OpenTherm::bit_read_(uint8_t value) {
223 this->data_ = (this->data_ << 1) | value;
224 this->bit_pos_++;
225}
226
227ProtocolErrorType IRAM_ATTR OpenTherm::verify_stop_bit_(uint8_t value) {
228 if (value) { // stop bit detected
229 return check_parity_(this->data_) ? ProtocolErrorType::NO_ERROR : ProtocolErrorType::PARITY_ERROR;
230 } else { // no stop bit detected, error
232 }
233}
234
235void IRAM_ATTR OpenTherm::write_bit_(uint8_t high, uint8_t clock) {
236 if (clock == 1) { // left part of manchester encoding
237 this->isr_out_pin_.digital_write(!high); // low means logical 1 to protocol
238 } else { // right part of manchester encoding
239 this->isr_out_pin_.digital_write(high); // high means logical 0 to protocol
240 }
241}
242
243#ifdef USE_ESP32
244
245bool OpenTherm::init_esp32_timer_() {
246 // Search for a free timer. Maybe unstable, we'll see.
247 int cur_timer = 0;
248 timer_group_t timer_group = TIMER_GROUP_0;
249 timer_idx_t timer_idx = TIMER_0;
250 bool timer_found = false;
251
252 for (; cur_timer < SOC_TIMER_GROUP_TOTAL_TIMERS; cur_timer++) {
253 timer_config_t temp_config;
254 timer_group = cur_timer < 2 ? TIMER_GROUP_0 : TIMER_GROUP_1;
255 timer_idx = cur_timer < 2 ? (timer_idx_t) cur_timer : (timer_idx_t) (cur_timer - 2);
256
257 auto err = timer_get_config(timer_group, timer_idx, &temp_config);
258 if (err == ESP_ERR_INVALID_ARG) {
259 // Error means timer was not initialized (or other things, but we are careful with our args)
260 timer_found = true;
261 break;
262 }
263
264 ESP_LOGD(TAG, "Timer %d:%d seems to be occupied, will try another", timer_group, timer_idx);
265 }
266
267 if (!timer_found) {
268 ESP_LOGE(TAG, "No free timer was found! OpenTherm cannot function without a timer.");
269 return false;
270 }
271
272 ESP_LOGD(TAG, "Found free timer %d:%d", timer_group, timer_idx);
273 this->timer_group_ = timer_group;
274 this->timer_idx_ = timer_idx;
275
276 timer_config_t const config = {
277 .alarm_en = TIMER_ALARM_EN,
278 .counter_en = TIMER_PAUSE,
279 .intr_type = TIMER_INTR_LEVEL,
280 .counter_dir = TIMER_COUNT_UP,
281 .auto_reload = TIMER_AUTORELOAD_EN,
282 .clk_src = TIMER_SRC_CLK_DEFAULT,
283 .divider = 80,
284 };
285
286 esp_err_t result;
287
288 result = timer_init(this->timer_group_, this->timer_idx_, &config);
289 if (result != ESP_OK) {
290 const auto *error = esp_err_to_name(result);
291 ESP_LOGE(TAG, "Failed to init timer. Error: %s", error);
292 return false;
293 }
294
295 result = timer_set_counter_value(this->timer_group_, this->timer_idx_, 0);
296 if (result != ESP_OK) {
297 const auto *error = esp_err_to_name(result);
298 ESP_LOGE(TAG, "Failed to set counter value. Error: %s", error);
299 return false;
300 }
301
302 result = timer_isr_callback_add(this->timer_group_, this->timer_idx_, reinterpret_cast<bool (*)(void *)>(timer_isr),
303 this, 0);
304 if (result != ESP_OK) {
305 const auto *error = esp_err_to_name(result);
306 ESP_LOGE(TAG, "Failed to register timer interrupt. Error: %s", error);
307 return false;
308 }
309
310 return true;
311}
312
313void IRAM_ATTR OpenTherm::start_esp32_timer_(uint64_t alarm_value) {
314 // We will report timer errors outside of interrupt handler
315 this->timer_error_ = ESP_OK;
316 this->timer_error_type_ = TimerErrorType::NO_TIMER_ERROR;
317
318 this->timer_error_ = timer_set_alarm_value(this->timer_group_, this->timer_idx_, alarm_value);
319 if (this->timer_error_ != ESP_OK) {
320 this->timer_error_type_ = TimerErrorType::SET_ALARM_VALUE_ERROR;
321 return;
322 }
323 this->timer_error_ = timer_start(this->timer_group_, this->timer_idx_);
324 if (this->timer_error_ != ESP_OK) {
325 this->timer_error_type_ = TimerErrorType::TIMER_START_ERROR;
326 }
327}
328
330 if (this->timer_error_ == ESP_OK) {
331 return;
332 }
333
334 ESP_LOGE(TAG, "Error occured while manipulating timer (%s): %s", this->timer_error_to_str(this->timer_error_type_),
335 esp_err_to_name(this->timer_error_));
336
337 this->timer_error_ = ESP_OK;
338 this->timer_error_type_ = NO_TIMER_ERROR;
339}
340
341// 5 kHz timer_
342void IRAM_ATTR OpenTherm::start_read_timer_() {
343 InterruptLock const lock;
344 this->start_esp32_timer_(200);
345}
346
347// 2 kHz timer_
348void IRAM_ATTR OpenTherm::start_write_timer_() {
349 InterruptLock const lock;
350 this->start_esp32_timer_(500);
351}
352
353void IRAM_ATTR OpenTherm::stop_timer_() {
354 InterruptLock const lock;
355 // We will report timer errors outside of interrupt handler
356 this->timer_error_ = ESP_OK;
357 this->timer_error_type_ = TimerErrorType::NO_TIMER_ERROR;
358
359 this->timer_error_ = timer_pause(this->timer_group_, this->timer_idx_);
360 if (this->timer_error_ != ESP_OK) {
361 this->timer_error_type_ = TimerErrorType::TIMER_PAUSE_ERROR;
362 return;
363 }
364 this->timer_error_ = timer_set_counter_value(this->timer_group_, this->timer_idx_, 0);
365 if (this->timer_error_ != ESP_OK) {
366 this->timer_error_type_ = TimerErrorType::SET_COUNTER_VALUE_ERROR;
367 }
368}
369
370#endif // USE_ESP32
371
372#ifdef ESP8266
373// 5 kHz timer_
374void IRAM_ATTR OpenTherm::start_read_timer_() {
375 InterruptLock const lock;
376 timer1_attachInterrupt(OpenTherm::esp8266_timer_isr);
377 timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP); // 5MHz (5 ticks/us - 1677721.4 us max)
378 timer1_write(1000); // 5kHz
379}
380
381// 2 kHz timer_
382void IRAM_ATTR OpenTherm::start_write_timer_() {
383 InterruptLock const lock;
384 timer1_attachInterrupt(OpenTherm::esp8266_timer_isr);
385 timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP); // 5MHz (5 ticks/us - 1677721.4 us max)
386 timer1_write(2500); // 2kHz
387}
388
389void IRAM_ATTR OpenTherm::stop_timer_() {
390 InterruptLock const lock;
391 timer1_disable();
392 timer1_detachInterrupt();
393}
394
395// There is nothing to report on ESP8266
397
398#endif // END ESP8266
399
400// https://stackoverflow.com/questions/21617970/how-to-check-if-value-has-even-parity-of-bits-or-odd
401bool IRAM_ATTR OpenTherm::check_parity_(uint32_t val) {
402 val ^= val >> 16;
403 val ^= val >> 8;
404 val ^= val >> 4;
405 val ^= val >> 2;
406 val ^= val >> 1;
407 return (~val) & 1;
408}
409
410#define TO_STRING_MEMBER(name) \
411 case name: \
412 return #name;
413
415 switch (mode) {
416 TO_STRING_MEMBER(IDLE)
417 TO_STRING_MEMBER(LISTEN)
418 TO_STRING_MEMBER(READ)
419 TO_STRING_MEMBER(RECEIVED)
420 TO_STRING_MEMBER(WRITE)
421 TO_STRING_MEMBER(SENT)
422 TO_STRING_MEMBER(ERROR_PROTOCOL)
423 TO_STRING_MEMBER(ERROR_TIMEOUT)
424 TO_STRING_MEMBER(ERROR_TIMER)
425 default:
426 return "<INVALID>";
427 }
428}
430 switch (error_type) {
431 TO_STRING_MEMBER(NO_ERROR)
432 TO_STRING_MEMBER(NO_TRANSITION)
433 TO_STRING_MEMBER(INVALID_STOP_BIT)
434 TO_STRING_MEMBER(PARITY_ERROR)
435 TO_STRING_MEMBER(NO_CHANGE_TOO_LONG)
436 default:
437 return "<INVALID>";
438 }
439}
441 switch (error_type) {
442 TO_STRING_MEMBER(NO_TIMER_ERROR)
443 TO_STRING_MEMBER(SET_ALARM_VALUE_ERROR)
444 TO_STRING_MEMBER(TIMER_START_ERROR)
445 TO_STRING_MEMBER(TIMER_PAUSE_ERROR)
446 TO_STRING_MEMBER(SET_COUNTER_VALUE_ERROR)
447 default:
448 return "<INVALID>";
449 }
450}
452 switch (message_type) {
453 TO_STRING_MEMBER(READ_DATA)
454 TO_STRING_MEMBER(READ_ACK)
455 TO_STRING_MEMBER(WRITE_DATA)
456 TO_STRING_MEMBER(WRITE_ACK)
457 TO_STRING_MEMBER(INVALID_DATA)
458 TO_STRING_MEMBER(DATA_INVALID)
459 TO_STRING_MEMBER(UNKNOWN_DATAID)
460 default:
461 return "<INVALID>";
462 }
463}
464
466 switch (id) {
467 TO_STRING_MEMBER(STATUS)
468 TO_STRING_MEMBER(CH_SETPOINT)
469 TO_STRING_MEMBER(CONTROLLER_CONFIG)
470 TO_STRING_MEMBER(DEVICE_CONFIG)
471 TO_STRING_MEMBER(COMMAND_CODE)
472 TO_STRING_MEMBER(FAULT_FLAGS)
473 TO_STRING_MEMBER(REMOTE)
474 TO_STRING_MEMBER(COOLING_CONTROL)
475 TO_STRING_MEMBER(CH2_SETPOINT)
476 TO_STRING_MEMBER(CH_SETPOINT_OVERRIDE)
477 TO_STRING_MEMBER(TSP_COUNT)
478 TO_STRING_MEMBER(TSP_COMMAND)
479 TO_STRING_MEMBER(FHB_SIZE)
480 TO_STRING_MEMBER(FHB_COMMAND)
481 TO_STRING_MEMBER(MAX_MODULATION_LEVEL)
482 TO_STRING_MEMBER(MAX_BOILER_CAPACITY)
483 TO_STRING_MEMBER(ROOM_SETPOINT)
484 TO_STRING_MEMBER(MODULATION_LEVEL)
485 TO_STRING_MEMBER(CH_WATER_PRESSURE)
486 TO_STRING_MEMBER(DHW_FLOW_RATE)
487 TO_STRING_MEMBER(DAY_TIME)
488 TO_STRING_MEMBER(DATE)
489 TO_STRING_MEMBER(YEAR)
490 TO_STRING_MEMBER(ROOM_SETPOINT_CH2)
491 TO_STRING_MEMBER(ROOM_TEMP)
492 TO_STRING_MEMBER(FEED_TEMP)
493 TO_STRING_MEMBER(DHW_TEMP)
494 TO_STRING_MEMBER(OUTSIDE_TEMP)
495 TO_STRING_MEMBER(RETURN_WATER_TEMP)
496 TO_STRING_MEMBER(SOLAR_STORE_TEMP)
497 TO_STRING_MEMBER(SOLAR_COLLECT_TEMP)
498 TO_STRING_MEMBER(FEED_TEMP_CH2)
499 TO_STRING_MEMBER(DHW2_TEMP)
500 TO_STRING_MEMBER(EXHAUST_TEMP)
501 TO_STRING_MEMBER(FAN_SPEED)
502 TO_STRING_MEMBER(FLAME_CURRENT)
503 TO_STRING_MEMBER(ROOM_TEMP_CH2)
504 TO_STRING_MEMBER(REL_HUMIDITY)
505 TO_STRING_MEMBER(DHW_BOUNDS)
506 TO_STRING_MEMBER(CH_BOUNDS)
507 TO_STRING_MEMBER(OTC_CURVE_BOUNDS)
508 TO_STRING_MEMBER(DHW_SETPOINT)
509 TO_STRING_MEMBER(MAX_CH_SETPOINT)
510 TO_STRING_MEMBER(OTC_CURVE_RATIO)
511 TO_STRING_MEMBER(HVAC_STATUS)
512 TO_STRING_MEMBER(REL_VENT_SETPOINT)
513 TO_STRING_MEMBER(DEVICE_VENT)
514 TO_STRING_MEMBER(HVAC_VER_ID)
515 TO_STRING_MEMBER(REL_VENTILATION)
516 TO_STRING_MEMBER(REL_HUMID_EXHAUST)
517 TO_STRING_MEMBER(EXHAUST_CO2)
518 TO_STRING_MEMBER(SUPPLY_INLET_TEMP)
519 TO_STRING_MEMBER(SUPPLY_OUTLET_TEMP)
520 TO_STRING_MEMBER(EXHAUST_INLET_TEMP)
521 TO_STRING_MEMBER(EXHAUST_OUTLET_TEMP)
522 TO_STRING_MEMBER(EXHAUST_FAN_SPEED)
523 TO_STRING_MEMBER(SUPPLY_FAN_SPEED)
524 TO_STRING_MEMBER(REMOTE_VENTILATION_PARAM)
525 TO_STRING_MEMBER(NOM_REL_VENTILATION)
526 TO_STRING_MEMBER(HVAC_NUM_TSP)
527 TO_STRING_MEMBER(HVAC_IDX_TSP)
528 TO_STRING_MEMBER(HVAC_FHB_SIZE)
529 TO_STRING_MEMBER(HVAC_FHB_IDX)
530 TO_STRING_MEMBER(RF_SIGNAL)
531 TO_STRING_MEMBER(DHW_MODE)
532 TO_STRING_MEMBER(OVERRIDE_FUNC)
533 TO_STRING_MEMBER(SOLAR_MODE_FLAGS)
534 TO_STRING_MEMBER(SOLAR_ASF)
535 TO_STRING_MEMBER(SOLAR_VERSION_ID)
536 TO_STRING_MEMBER(SOLAR_PRODUCT_ID)
537 TO_STRING_MEMBER(SOLAR_NUM_TSP)
538 TO_STRING_MEMBER(SOLAR_IDX_TSP)
539 TO_STRING_MEMBER(SOLAR_FHB_SIZE)
540 TO_STRING_MEMBER(SOLAR_FHB_IDX)
541 TO_STRING_MEMBER(SOLAR_STARTS)
542 TO_STRING_MEMBER(SOLAR_HOURS)
543 TO_STRING_MEMBER(SOLAR_ENERGY)
544 TO_STRING_MEMBER(SOLAR_TOTAL_ENERGY)
545 TO_STRING_MEMBER(FAILED_BURNER_STARTS)
546 TO_STRING_MEMBER(BURNER_FLAME_LOW)
547 TO_STRING_MEMBER(OEM_DIAGNOSTIC)
548 TO_STRING_MEMBER(BURNER_STARTS)
549 TO_STRING_MEMBER(CH_PUMP_STARTS)
550 TO_STRING_MEMBER(DHW_PUMP_STARTS)
551 TO_STRING_MEMBER(DHW_BURNER_STARTS)
552 TO_STRING_MEMBER(BURNER_HOURS)
553 TO_STRING_MEMBER(CH_PUMP_HOURS)
554 TO_STRING_MEMBER(DHW_PUMP_HOURS)
555 TO_STRING_MEMBER(DHW_BURNER_HOURS)
556 TO_STRING_MEMBER(OT_VERSION_CONTROLLER)
557 TO_STRING_MEMBER(OT_VERSION_DEVICE)
558 TO_STRING_MEMBER(VERSION_CONTROLLER)
559 TO_STRING_MEMBER(VERSION_DEVICE)
560 default:
561 return "<INVALID>";
562 }
563}
564
566 char type_buf[9], id_buf[9], hb_buf[9], lb_buf[9];
567 ESP_LOGD(TAG, "%s %s %s %s", format_bin_to(type_buf, data.type), format_bin_to(id_buf, data.id),
568 format_bin_to(hb_buf, data.valueHB), format_bin_to(lb_buf, data.valueLB));
569 ESP_LOGD(TAG, "type: %s; id: %u; HB: %u; LB: %u; uint_16: %u; float: %f",
570 this->message_type_to_str((MessageType) data.type), data.id, data.valueHB, data.valueLB, data.u16(),
571 data.f88());
572}
574 ESP_LOGD(TAG, "data: 0x%08" PRIx32 "; clock: %u; capture: 0x%08" PRIx32 "; bit_pos: %u", error.data, this->clock_,
575 error.capture, error.bit_pos);
576}
577
578float OpenthermData::f88() { return ((float) this->s16()) / 256.0; }
579
580void OpenthermData::f88(float value) { this->s16((int16_t) (value * 256)); }
581
583 uint16_t const value = this->valueHB;
584 return (value << 8) | this->valueLB;
585}
586
587void OpenthermData::u16(uint16_t value) {
588 this->valueLB = value & 0xFF;
589 this->valueHB = (value >> 8) & 0xFF;
590}
591
593 int16_t const value = this->valueHB;
594 return (value << 8) | this->valueLB;
595}
596
597void OpenthermData::s16(int16_t value) {
598 this->valueLB = value & 0xFF;
599 this->valueHB = (value >> 8) & 0xFF;
600}
601
602} // namespace opentherm
603} // namespace esphome
BedjetMode mode
BedJet operating mode.
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
virtual void digital_write(bool value)=0
void digital_write(bool value)
Definition gpio.cpp:148
virtual ISRInternalGPIOPin to_isr() const =0
Helper class to disable interrupts.
Definition helpers.h:1694
Opentherm static class that supports either listening or sending Opentherm data packets in the same t...
Definition opentherm.h:238
void debug_error(OpenThermError &error) const
const char * message_id_to_str(MessageId id)
void listen()
Start listening for Opentherm data packet comming from line connected to given pin.
Definition opentherm.cpp:69
bool get_message(OpenthermData &data)
Use this to retrive data packed captured by listen() function.
Definition opentherm.cpp:97
static bool timer_isr(OpenTherm *arg)
void send(OpenthermData &data)
Immediately send out Opentherm data packet to line connected on given pin.
Definition opentherm.cpp:80
const char * timer_error_to_str(TimerErrorType error_type)
bool initialize()
Setup pins.
Definition opentherm.cpp:52
const char * operation_mode_to_str(OperationMode mode)
OpenTherm(InternalGPIOPin *in_pin, InternalGPIOPin *out_pin, int32_t device_timeout=800)
Definition opentherm.cpp:33
void debug_data(OpenthermData &data)
const char * protocol_error_to_str(ProtocolErrorType error_type)
const char * message_type_to_str(MessageType message_type)
bool get_protocol_error(OpenThermError &error)
Get protocol error details in case a protocol error occured.
void stop()
Stops listening for data packet or sending out data packet and resets internal state of this class.
mopeka_std_values val[4]
@ FLAG_OUTPUT
Definition gpio.h:28
@ FLAG_INPUT
Definition gpio.h:27
constexpr T read_bit(T value, uint8_t bit)
Definition opentherm.h:26
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
char * format_bin_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as binary string to buffer.
Definition helpers.cpp:426
Structure to hold Opentherm data packet content.
Definition opentherm.h:188