diff --git a/.clang-format b/.clang-format index 9720cd0..d420259 100644 --- a/.clang-format +++ b/.clang-format @@ -3,23 +3,24 @@ UseTab: Never IndentWidth: 4 TabWidth: 4 BreakBeforeBraces: Custom -AllowShortFunctionsOnASingleLine: InlineOnly -AllowShortIfStatementsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Inline +AllowShortBlocksOnASingleLine: false AllowShortLambdasOnASingleLine: true AllowAllArgumentsOnNextLine: true IndentCaseLabels: true AccessModifierOffset: -4 -NamespaceIndentation: All +NamespaceIndentation: None FixNamespaceComments: false PackConstructorInitializers: Never AlignAfterOpenBracket: AlwaysBreak +BreakConstructorInitializersBeforeComma: true InsertBraces: true BraceWrapping: AfterClass: true # false AfterControlStatement: false AfterEnum: true # false AfterFunction: true # false - AfterNamespace: true # false + AfterNamespace: false AfterObjCDeclaration: true # false AfterStruct: true # false AfterUnion: true # false diff --git a/components/opentherm/opentherm.c b/components/opentherm/opentherm.c index 249a811..8fea658 100644 --- a/components/opentherm/opentherm.c +++ b/components/opentherm/opentherm.c @@ -5,10 +5,10 @@ * @licence MIT */ -#include -#include "esp_log.h" #include "opentherm.h" +#include "esp_log.h" #include "rom/ets_sys.h" +#include static const char *TAG = "ot-example"; @@ -40,15 +40,12 @@ volatile byte esp_ot_response_bit_index; * @return void */ esp_err_t esp_ot_init( - gpio_num_t _pin_in, - gpio_num_t _pin_out, - bool _esp_ot_is_slave, + gpio_num_t _pin_in, gpio_num_t _pin_out, bool _esp_ot_is_slave, void (*esp_ot_process_responseCallback)(unsigned long, open_therm_response_status_t)) { esp_err_t err = gpio_install_isr_service(0); - if (err != ESP_OK) - { + if (err != ESP_OK) { ESP_LOGE("ISR", "Error with state %s", esp_err_to_name(err)); } @@ -99,15 +96,17 @@ esp_err_t esp_ot_init( */ void esp_ot_send_bit(bool high) { - if (high) + if (high) { esp_ot_set_active_state(); - else + } else { esp_ot_set_idle_state(); + } ets_delay_us(500); - if (high) + if (high) { esp_ot_set_idle_state(); - else + } else { esp_ot_set_active_state(); + } ets_delay_us(500); } @@ -116,9 +115,12 @@ void esp_ot_send_bit(bool high) * * @return long */ -unsigned long esp_ot_build_set_boiler_status_request(bool enableCentralHeating, bool enableHotWater, bool enableCooling, bool enableOutsideTemperatureCompensation, bool enableCentralHeating2) +unsigned long esp_ot_build_set_boiler_status_request( + bool enableCentralHeating, bool enableHotWater, bool enableCooling, bool enableOutsideTemperatureCompensation, + bool enableCentralHeating2) { - unsigned int data = enableCentralHeating | (enableHotWater << 1) | (enableCooling << 2) | (enableOutsideTemperatureCompensation << 3) | (enableCentralHeating2 << 4); + unsigned int data = enableCentralHeating | (enableHotWater << 1) | (enableCooling << 2) | (enableOutsideTemperatureCompensation << 3) | + (enableCentralHeating2 << 4); data <<= 8; return esp_ot_build_request(OT_READ_DATA, MSG_ID_STATUS, data); } @@ -203,8 +205,7 @@ void esp_ot_activate_boiler() */ void esp_ot_process_response() { - if (esp_ot_process_response_callback != NULL) - { + if (esp_ot_process_response_callback != NULL) { // ESP_LOGI("PROCESS RESPONSE", "esp_ot_process_response, %ld, %d", response, esp_ot_response_status); esp_ot_process_response_callback(response, esp_ot_response_status); } @@ -245,13 +246,11 @@ open_therm_message_id_t esp_ot_get_data_id(unsigned long frame) unsigned long esp_ot_build_request(open_therm_message_type_t type, open_therm_message_id_t id, unsigned int data) { unsigned long request = data; - if (type == OT_WRITE_DATA) - { + if (type == OT_WRITE_DATA) { request |= 1ul << 28; } request |= ((unsigned long)id) << 16; - if (parity(request)) - { + if (parity(request)) { request |= (1ul << 31); } return request; @@ -269,8 +268,9 @@ unsigned long esp_ot_build_response(open_therm_message_type_t type, open_therm_m unsigned long response = data; response |= ((unsigned long)type) << 28; response |= ((unsigned long)id) << 16; - if (parity(response)) + if (parity(response)) { response |= (1ul << 31); + } return response; } @@ -283,8 +283,9 @@ unsigned long esp_ot_build_response(open_therm_message_type_t type, open_therm_m */ bool esp_ot_is_valid_request(unsigned long request) { - if (parity(request)) + if (parity(request)) { return false; + } byte msgType = (request << 1) >> 29; return msgType == (byte)OT_READ_DATA || msgType == (byte)OT_WRITE_DATA; } @@ -298,8 +299,9 @@ bool esp_ot_is_valid_request(unsigned long request) */ bool esp_ot_is_valid_response(unsigned long response) { - if (parity(response)) + if (parity(response)) { return false; + } byte msgType = (response << 1) >> 29; return msgType == (byte)OT_READ_ACK || msgType == (byte)OT_WRITE_ACK; } @@ -314,10 +316,10 @@ bool esp_ot_is_valid_response(unsigned long response) bool parity(unsigned long frame) // odd parity { byte p = 0; - while (frame > 0) - { - if (frame & 1) + while (frame > 0) { + if (frame & 1) { p++; + } frame = frame >> 1; } return (p & 1); @@ -386,60 +388,41 @@ float esp_ot_get_slave_ot_version() void IRAM_ATTR esp_ot_handle_interrupt() { // ESP_DRAM_LOGI("esp_ot_handle_interrupt", "%ld", status); - if (esp_ot_is_ready()) - { - if (esp_ot_is_slave && esp_ot_read_state() == 1) - { + if (esp_ot_is_ready()) { + if (esp_ot_is_slave && esp_ot_read_state() == 1) { esp_ot_status = OT_RESPONSE_WAITING; - } - else - { + } else { return; } } unsigned long newTs = esp_timer_get_time(); - if (esp_ot_status == OT_RESPONSE_WAITING) - { - if (esp_ot_read_state() == 1) - { + if (esp_ot_status == OT_RESPONSE_WAITING) { + if (esp_ot_read_state() == 1) { // ESP_DRAM_LOGI("BIT", "Set start bit"); esp_ot_status = OT_RESPONSE_START_BIT; esp_ot_response_timestamp = newTs; - } - else - { + } else { // ESP_DRAM_LOGI("BIT", "Set OT_RESPONSE_INVALID"); esp_ot_status = OT_RESPONSE_INVALID; esp_ot_response_timestamp = newTs; } - } - else if (esp_ot_status == OT_RESPONSE_START_BIT) - { - if ((newTs - esp_ot_response_timestamp < 750) && esp_ot_read_state() == 0) - { + } else if (esp_ot_status == OT_RESPONSE_START_BIT) { + if ((newTs - esp_ot_response_timestamp < 750) && esp_ot_read_state() == 0) { esp_ot_status = OT_RESPONSE_RECEIVING; esp_ot_response_timestamp = newTs; esp_ot_response_bit_index = 0; - } - else - { + } else { esp_ot_status = OT_RESPONSE_INVALID; esp_ot_response_timestamp = newTs; } - } - else if (esp_ot_status == OT_RESPONSE_RECEIVING) - { - if ((newTs - esp_ot_response_timestamp) > 750) - { - if (esp_ot_response_bit_index < 32) - { + } else if (esp_ot_status == OT_RESPONSE_RECEIVING) { + if ((newTs - esp_ot_response_timestamp) > 750) { + if (esp_ot_response_bit_index < 32) { response = (response << 1) | !esp_ot_read_state(); esp_ot_response_timestamp = newTs; esp_ot_response_bit_index++; - } - else - { // stop bit + } else { // stop bit esp_ot_status = OT_RESPONSE_READY; esp_ot_response_timestamp = newTs; } @@ -459,35 +442,28 @@ void process() unsigned long ts = esp_ot_response_timestamp; PORT_EXIT_CRITICAL; - if (st == OT_READY) - { + if (st == OT_READY) { return; } unsigned long newTs = esp_timer_get_time(); - if (st != OT_NOT_INITIALIZED && st != OT_DELAY && (newTs - ts) > 1000000) - { + if (st != OT_NOT_INITIALIZED && st != OT_DELAY && (newTs - ts) > 1000000) { esp_ot_status = OT_READY; ESP_LOGI("SET STATUS", "set status to READY"); // here READY esp_ot_response_status = OT_STATUS_TIMEOUT; esp_ot_process_response(); - } - else if (st == OT_RESPONSE_INVALID) - { + } else if (st == OT_RESPONSE_INVALID) { ESP_LOGE("SET STATUS", "set status to OT_RESPONSE_INVALID"); // here OT_RESPONSE_INVALID esp_ot_status = OT_DELAY; esp_ot_response_status = OT_STATUS_INVALID; esp_ot_process_response(); - } - else if (st == OT_RESPONSE_READY) - { + } else if (st == OT_RESPONSE_READY) { esp_ot_status = OT_DELAY; - esp_ot_response_status = (esp_ot_is_slave ? esp_ot_is_valid_request(response) : esp_ot_is_valid_response(response)) ? OT_STATUS_SUCCESS : OT_STATUS_INVALID; + esp_ot_response_status = (esp_ot_is_slave ? esp_ot_is_valid_request(response) : esp_ot_is_valid_response(response)) + ? OT_STATUS_SUCCESS + : OT_STATUS_INVALID; esp_ot_process_response(); - } - else if (st == OT_DELAY) - { - if ((newTs - ts) > (esp_ot_is_slave ? 20000 : 100000)) - { + } else if (st == OT_DELAY) { + if ((newTs - ts) > (esp_ot_is_slave ? 20000 : 100000)) { esp_ot_status = OT_READY; } } @@ -505,8 +481,7 @@ bool esp_ot_send_request_async(unsigned long request) PORT_ENTER_CRITICAL; const bool ready = esp_ot_is_ready(); PORT_EXIT_CRITICAL; - if (!ready) - { + if (!ready) { return false; } PORT_ENTER_CRITICAL; @@ -518,8 +493,7 @@ bool esp_ot_send_request_async(unsigned long request) // vTaskSuspendAll(); esp_ot_send_bit(1); // start bit - for (int i = 31; i >= 0; i--) - { + for (int i = 31; i >= 0; i--) { esp_ot_send_bit(ESP_OT_BIT_READ(request, i)); } esp_ot_send_bit(1); // stop bit @@ -542,13 +516,11 @@ bool esp_ot_send_request_async(unsigned long request) */ unsigned long esp_ot_send_request(unsigned long request) { - if (!esp_ot_send_request_async(request)) - { + if (!esp_ot_send_request_async(request)) { return 0; } // ESP_LOGI("STATUS", "esp_ot_send_request with status %d", status); // here WAITING - while (!esp_ot_is_ready()) - { + while (!esp_ot_is_ready()) { process(); vPortYield(); } @@ -663,13 +635,11 @@ float esp_ot_get_float(const unsigned long response) */ unsigned int esp_ot_temperature_to_data(float temperature) { - if (temperature < 0) - { + if (temperature < 0) { temperature = 0; } - if (temperature > 100) - { + if (temperature > 100) { temperature = 100; } @@ -689,13 +659,11 @@ unsigned int esp_ot_temperature_to_data(float temperature) * @return long boiler status */ unsigned long esp_ot_set_boiler_status( - bool enableCentralHeating, - bool enableHotWater, - bool enableCooling, - bool enableOutsideTemperatureCompensation, + bool enableCentralHeating, bool enableHotWater, bool enableCooling, bool enableOutsideTemperatureCompensation, bool enableCentralHeating2) { - return esp_ot_send_request(esp_ot_build_set_boiler_status_request(enableCentralHeating, enableHotWater, enableCooling, enableOutsideTemperatureCompensation, enableCentralHeating2)); + return esp_ot_send_request(esp_ot_build_set_boiler_status_request( + enableCentralHeating, enableHotWater, enableCooling, enableOutsideTemperatureCompensation, enableCentralHeating2)); } /** @@ -765,9 +733,7 @@ esp_ot_min_max_t esp_ot_get_ch_upper_lower_bounds() uint8_t min = (uint8_t)(result & 0x00FF); uint8_t max = (uint8_t)((result & 0xFF00) >> 8); - esp_ot_min_max_t min_max = { - .min = min, - .max = max}; + esp_ot_min_max_t min_max = {.min = min, .max = max}; return min_max; } @@ -777,9 +743,7 @@ esp_ot_min_max_t esp_ot_get_dhw_upper_lower_bounds() uint16_t result = esp_ot_is_valid_response(response) ? esp_ot_get_uint(response) : 0; uint8_t min = (uint8_t)(result & 0x00FF); uint8_t max = (uint8_t)((result & 0xFF00) >> 8); - esp_ot_min_max_t min_max = { - .min = min, - .max = max}; + esp_ot_min_max_t min_max = {.min = min, .max = max}; return min_max; } @@ -793,9 +757,7 @@ esp_ot_min_max_t esp_ot_get_heat_curve_ul_bounds() uint16_t result = esp_ot_is_valid_response(response) ? esp_ot_get_uint(response) : 0; uint8_t min = (uint8_t)(result & 0x00FF); uint8_t max = (uint8_t)((result & 0xFF00) >> 8); - esp_ot_min_max_t min_max = { - .min = min, - .max = max}; + esp_ot_min_max_t min_max = {.min = min, .max = max}; return min_max; } @@ -809,9 +771,7 @@ esp_ot_cap_mod_t esp_ot_get_max_capacity_min_modulation() uint16_t result = esp_ot_is_valid_response(response) ? esp_ot_get_uint(response) : 0; uint8_t mod = (uint8_t)(result & 0x00FF); uint8_t kw = (uint8_t)((result & 0xFF00) >> 8); - esp_ot_cap_mod_t cap_mod = { - .kw = kw, - .min_modulation = mod}; + esp_ot_cap_mod_t cap_mod = {.kw = kw, .min_modulation = mod}; return cap_mod; } @@ -948,8 +908,7 @@ float esp_ot_get_modulation() bool esp_ot_set_modulation_level(int percent) { unsigned int data = percent; - unsigned long response = esp_ot_send_request( - esp_ot_build_request(OT_WRITE_DATA, MSG_ID_MAX_REL_MOD_LEVEL_SETTING, data)); + unsigned long response = esp_ot_send_request(esp_ot_build_request(OT_WRITE_DATA, MSG_ID_MAX_REL_MOD_LEVEL_SETTING, data)); return esp_ot_is_valid_response(response); } @@ -1001,8 +960,7 @@ bool esp_ot_send_response(unsigned long request) PORT_ENTER_CRITICAL; const bool ready = esp_ot_is_ready(); - if (!ready) - { + if (!ready) { PORT_EXIT_CRITICAL; return false; } @@ -1016,8 +974,7 @@ bool esp_ot_send_response(unsigned long request) PORT_EXIT_CRITICAL; esp_ot_send_bit(1); // start bit - for (int i = 31; i >= 0; i--) - { + for (int i = 31; i >= 0; i--) { esp_ot_send_bit(ESP_OT_BIT_READ(request, i)); } esp_ot_send_bit(1); // stop bit diff --git a/components/opentherm/opentherm.h b/components/opentherm/opentherm.h index e85cb2b..d687efd 100644 --- a/components/opentherm/opentherm.h +++ b/components/opentherm/opentherm.h @@ -5,11 +5,11 @@ * @licence MIT */ -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" #include "driver/gpio.h" #include "esp_timer.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include #include "opentherm_struct.h" @@ -45,6 +45,7 @@ typedef enum OpenThermMessageType // old name OpenThermRequestType; // for backw OT_UNKNOWN_DATA_ID = 0b111 } open_therm_message_type_t; +// clang-format off typedef enum OpenThermMessageID { MSG_ID_STATUS = 0, // flag8/flag8 |R-| Master and Slave Status flags. @@ -169,6 +170,7 @@ typedef enum OpenThermMessageID MSG_ID_MASTER_VERSION = 126, // u8/u8 |-W| Master product version number and type MSG_ID_SLAVE_VERSION = 127, // u8/u8 |R-| Slave product version number and type } open_therm_message_id_t; +// clang-format on typedef enum OpenThermStatus { @@ -185,9 +187,7 @@ typedef enum OpenThermStatus // ENUMS esp_err_t esp_ot_init( - gpio_num_t _pin_in, - gpio_num_t _pin_out, - bool _esp_ot_is_slave, + gpio_num_t _pin_in, gpio_num_t _pin_out, bool _esp_ot_is_slave, void (*esp_ot_process_responseCallback)(unsigned long, open_therm_response_status_t)); bool esp_ot_is_ready(); @@ -232,7 +232,9 @@ void esp_ot_send_bit(bool high); void esp_ot_process_response(); -unsigned long esp_ot_build_set_boiler_status_request(bool enableCentralHeating, bool enableHotWater, bool enableCooling, bool enableOutsideTemperatureCompensation, bool enableCentralHeating2); +unsigned long esp_ot_build_set_boiler_status_request( + bool enableCentralHeating, bool enableHotWater, bool enableCooling, bool enableOutsideTemperatureCompensation, + bool enableCentralHeating2); unsigned long esp_ot_build_set_boiler_temperature_request(float temperature); @@ -256,7 +258,9 @@ float esp_ot_get_float(const unsigned long response); unsigned int esp_ot_temperature_to_data(float temperature); -unsigned long esp_ot_set_boiler_status(bool enableCentralHeating, bool enableHotWater, bool enableCooling, bool enableOutsideTemperatureCompensation, bool enableCentralHeating2); +unsigned long esp_ot_set_boiler_status( + bool enableCentralHeating, bool enableHotWater, bool enableCooling, bool enableOutsideTemperatureCompensation, + bool enableCentralHeating2); bool esp_ot_set_boiler_temperature(float temperature); diff --git a/components/opentherm/opentherm_struct.h b/components/opentherm/opentherm_struct.h index 64283fd..2e55b71 100644 --- a/components/opentherm/opentherm_struct.h +++ b/components/opentherm/opentherm_struct.h @@ -1,6 +1,6 @@ #pragma once -#include #include +#include typedef struct { diff --git a/main/ot_example.c b/main/ot_example.c index 2cd30ef..8cf61c0 100644 --- a/main/ot_example.c +++ b/main/ot_example.c @@ -1,16 +1,16 @@ /** -* @package Opentherm library for ESP-IDF framework - EXAMPLE -* @author: Mikhail Sazanof -* @copyright Copyright (C) 2024 - Sazanof.ru -* @licence MIT -*/ + * @package Opentherm library for ESP-IDF framework - EXAMPLE + * @author: Mikhail Sazanof + * @copyright Copyright (C) 2024 - Sazanof.ru + * @licence MIT + */ -#include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "opentherm.h" -#include #include +#include +#include #define GPIO_OT_IN CONFIG_OT_IN_PIN #define GPIO_OT_OUT CONFIG_OT_OUT_PIN @@ -33,22 +33,19 @@ static void IRAM_ATTR esp_ot_process_response_callback(unsigned long response, o void esp_ot_control_task_handler(void *pvParameter) { - while (1) - { + while (1) { unsigned long status = esp_ot_set_boiler_status(false, true, false, false, false); ESP_LOGI(T, "====== OPENTHERM ====="); ESP_LOGI(T, "Free heap size before: %ld", esp_get_free_heap_size()); open_therm_response_status_t esp_ot_response_status = esp_ot_get_last_response_status(); - if (esp_ot_response_status == OT_STATUS_SUCCESS) - { + if (esp_ot_response_status == OT_STATUS_SUCCESS) { ESP_LOGI(T, "Central Heating: %s", esp_ot_is_central_heating_active(status) ? "ON" : "OFF"); ESP_LOGI(T, "Hot Water: %s", esp_ot_is_hot_water_active(status) ? "ON" : "OFF"); ESP_LOGI(T, "Flame: %s", esp_ot_is_flame_on(status) ? "ON" : "OFF"); fault = esp_ot_is_fault(status); ESP_LOGI(T, "Fault: %s", fault ? "YES" : "NO"); - if (fault) - { + if (fault) { ot_reset(); } esp_ot_set_boiler_temperature(targetCHTemp); @@ -71,22 +68,15 @@ void esp_ot_control_task_handler(void *pvParameter) float slaveOTVersion = esp_ot_get_slave_ot_version(); ESP_LOGI(T, "Slave OT Version: %.1f", slaveOTVersion); - } - else if (esp_ot_response_status == OT_STATUS_TIMEOUT) - { + } else if (esp_ot_response_status == OT_STATUS_TIMEOUT) { ESP_LOGE(T, "OT Communication Timeout"); - } - else if (esp_ot_response_status == OT_STATUS_INVALID) - { + } else if (esp_ot_response_status == OT_STATUS_INVALID) { ESP_LOGE(T, "OT Communication Invalid"); - } - else if (esp_ot_response_status == OT_STATUS_NONE) - { + } else if (esp_ot_response_status == OT_STATUS_NONE) { ESP_LOGE(T, "OpenTherm not initialized"); } - if (fault) - { + if (fault) { ESP_LOGE(T, "Fault Code: %i", esp_ot_get_fault()); } ESP_LOGI(T, "Free heap size after: %ld", esp_get_free_heap_size()); @@ -98,11 +88,7 @@ void esp_ot_control_task_handler(void *pvParameter) void app_main() { - esp_ot_init( - GPIO_OT_IN, - GPIO_OT_OUT, - false, - esp_ot_process_response_callback); + esp_ot_init(GPIO_OT_IN, GPIO_OT_OUT, false, esp_ot_process_response_callback); xTaskCreate(esp_ot_control_task_handler, T, configMINIMAL_STACK_SIZE * 4, NULL, 3, NULL); vTaskSuspend(NULL);