clang-format
This commit is contained in:
parent
8bc863b648
commit
381c994449
5 changed files with 98 additions and 150 deletions
|
@ -5,10 +5,10 @@
|
|||
* @licence MIT
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "esp_log.h"
|
||||
#include "opentherm.h"
|
||||
#include "esp_log.h"
|
||||
#include "rom/ets_sys.h"
|
||||
#include <stdio.h>
|
||||
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue