Skip to content

Commit

Permalink
Clang-formatted.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudd-O committed Jul 31, 2023
1 parent dd117f0 commit 9894a32
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 112 deletions.
148 changes: 65 additions & 83 deletions esphome/components/kmeteriso/kmeteriso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,97 +2,79 @@
#include "esphome/core/hal.h"
#include "esphome/core/log.h"

namespace esphome
{
namespace kmeteriso
{
namespace esphome {
namespace kmeteriso {

static const char *const TAG = "kmeteriso.sensor";
static const char *const TAG = "kmeteriso.sensor";

static const uint8_t KMETER_KMETER_ERROR_STATUS_REG = 0x20;
static const uint8_t KMETER_TEMP_VAL_REG = 0x00;
static const uint8_t KMETER_INTERNAL_TEMP_VAL_REG = 0x10;
static const uint8_t KMETER_FIRMWARE_VERSION_REG = 0xFE;
static const uint8_t KMETER_KMETER_ERROR_STATUS_REG = 0x20;
static const uint8_t KMETER_TEMP_VAL_REG = 0x00;
static const uint8_t KMETER_INTERNAL_TEMP_VAL_REG = 0x10;
static const uint8_t KMETER_FIRMWARE_VERSION_REG = 0xFE;

void KMeterISOComponent::setup()
{
ESP_LOGCONFIG(TAG, "Setting up KMeterISO...");
this->error_code_ = NONE;
void KMeterISOComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up KMeterISO...");
this->error_code_ = NONE;

// Mark as not failed before initializing. Some devices will turn off sensors to save on batteries
// and when they come back on, the COMPONENT_STATE_FAILED bit must be unset on the component.
if ((this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_FAILED)
{
this->component_state_ &= ~COMPONENT_STATE_MASK;
this->component_state_ |= COMPONENT_STATE_CONSTRUCTION;
}
// Mark as not failed before initializing. Some devices will turn off sensors to save on batteries
// and when they come back on, the COMPONENT_STATE_FAILED bit must be unset on the component.
if ((this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_FAILED) {
this->component_state_ &= ~COMPONENT_STATE_MASK;
this->component_state_ |= COMPONENT_STATE_CONSTRUCTION;
}

auto err = this->bus_->writev(this->address_, nullptr, 0);
if (err == esphome::i2c::ERROR_OK)
{
ESP_LOGCONFIG(TAG, "Could write to the address %d.", this->address_);
}
else
{
ESP_LOGCONFIG(TAG, "Could not write to the address %d.", this->address_);
this->error_code_ = COMMUNICATION_FAILED;
this->mark_failed();
return;
}
auto err = this->bus_->writev(this->address_, nullptr, 0);
if (err == esphome::i2c::ERROR_OK) {
ESP_LOGCONFIG(TAG, "Could write to the address %d.", this->address_);
} else {
ESP_LOGCONFIG(TAG, "Could not write to the address %d.", this->address_);
this->error_code_ = COMMUNICATION_FAILED;
this->mark_failed();
return;
}

uint8_t read_buf[4] = {1};
if (!this->read_bytes(KMETER_KMETER_ERROR_STATUS_REG, read_buf, 1))
{
ESP_LOGCONFIG(TAG, "Could not read from the device.");
this->error_code_ = COMMUNICATION_FAILED;
this->mark_failed();
return;
}
if (read_buf[0] != 0)
{
ESP_LOGCONFIG(TAG, "The device is not ready.");
this->error_code_ = STATUS_FAILED;
this->mark_failed();
return;
}
ESP_LOGCONFIG(TAG, "The device was successfully setup.");
}
uint8_t read_buf[4] = {1};
if (!this->read_bytes(KMETER_KMETER_ERROR_STATUS_REG, read_buf, 1)) {
ESP_LOGCONFIG(TAG, "Could not read from the device.");
this->error_code_ = COMMUNICATION_FAILED;
this->mark_failed();
return;
}
if (read_buf[0] != 0) {
ESP_LOGCONFIG(TAG, "The device is not ready.");
this->error_code_ = STATUS_FAILED;
this->mark_failed();
return;
}
ESP_LOGCONFIG(TAG, "The device was successfully setup.");
}

float KMeterISOComponent::get_setup_priority() const { return setup_priority::DATA; }
float KMeterISOComponent::get_setup_priority() const { return setup_priority::DATA; }

void KMeterISOComponent::update()
{
uint8_t read_buf[4];
void KMeterISOComponent::update() {
uint8_t read_buf[4];

if (!this->read_bytes(KMETER_TEMP_VAL_REG, read_buf, 4))
{
ESP_LOGW(TAG, "Error reading temperature.");
}
else
{
int32_t temp = (read_buf[3] << 24) | (read_buf[2] << 16) | (read_buf[1] << 8) |
read_buf[0];
float temp_f = temp / 100.0;
ESP_LOGV(TAG, "Got temperature=%.2f °C", temp_f);
if (this->temperature_sensor_ != nullptr)
this->temperature_sensor_->publish_state(temp_f);
}
if (!this->read_bytes(KMETER_TEMP_VAL_REG, read_buf, 4)) {
ESP_LOGW(TAG, "Error reading temperature.");
} else {
int32_t temp = (read_buf[3] << 24) | (read_buf[2] << 16) | (read_buf[1] << 8) | read_buf[0];
float temp_f = temp / 100.0;
ESP_LOGV(TAG, "Got temperature=%.2f °C", temp_f);
if (this->temperature_sensor_ != nullptr)
this->temperature_sensor_->publish_state(temp_f);
}

if (!this->read_bytes(KMETER_INTERNAL_TEMP_VAL_REG, read_buf, 4))
{
ESP_LOGW(TAG, "Error reading internal temperature.");
return;
}
else
{
ESP_LOGV(TAG, "Got internal temperature=%.2f °C", internal_temp_f);
int32_t internal_temp = (read_buf[3] << 24) | (read_buf[2] << 16) | (read_buf[1] << 8) |
read_buf[0];
float internal_temp_f = internal_temp / 100.0;
if (this->internal_temperature_sensor_ != nullptr)
this->internal_temperature_sensor_->publish_state(internal_temp_f);
}
}
if (!this->read_bytes(KMETER_INTERNAL_TEMP_VAL_REG, read_buf, 4)) {
ESP_LOGW(TAG, "Error reading internal temperature.");
return;
} else {
ESP_LOGV(TAG, "Got internal temperature=%.2f °C", internal_temp_f);
int32_t internal_temp = (read_buf[3] << 24) | (read_buf[2] << 16) | (read_buf[1] << 8) | read_buf[0];
float internal_temp_f = internal_temp / 100.0;
if (this->internal_temperature_sensor_ != nullptr)
this->internal_temperature_sensor_->publish_state(internal_temp_f);
}
}

} // namespace bme280
} // namespace esphome
} // namespace kmeteriso
} // namespace esphome
54 changes: 25 additions & 29 deletions esphome/components/kmeteriso/kmeteriso.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,33 @@
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/i2c/i2c_bus.h"

namespace esphome
{
namespace kmeteriso
{
namespace esphome {
namespace kmeteriso {

/// This class implements support for the KMeterISO thermocouple sensor.
class KMeterISOComponent : public PollingComponent, public i2c::I2CDevice
{
public:
void set_temperature_sensor(sensor::Sensor *t) { temperature_sensor_ = t; }
void set_internal_temperature_sensor(sensor::Sensor *t) { internal_temperature_sensor_ = t; }
/// This class implements support for the KMeterISO thermocouple sensor.
class KMeterISOComponent : public PollingComponent, public i2c::I2CDevice {
public:
void set_temperature_sensor(sensor::Sensor *t) { temperature_sensor_ = t; }
void set_internal_temperature_sensor(sensor::Sensor *t) { internal_temperature_sensor_ = t; }

// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
void setup() override;
float get_setup_priority() const override;
void update() override;
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
void setup() override;
float get_setup_priority() const override;
void update() override;

protected:
/// Read the temperature value and store the calculated ambient temperature in t_fine.
float read_temperature_(const uint8_t *data, int32_t *t_fine);
protected:
/// Read the temperature value and store the calculated ambient temperature in t_fine.
float read_temperature_(const uint8_t *data, int32_t *t_fine);

sensor::Sensor *temperature_sensor_{nullptr};
sensor::Sensor *internal_temperature_sensor_{nullptr};
enum ErrorCode
{
NONE = 0,
COMMUNICATION_FAILED,
STATUS_FAILED,
} error_code_{NONE};
};
sensor::Sensor *temperature_sensor_{nullptr};
sensor::Sensor *internal_temperature_sensor_{nullptr};
enum ErrorCode {
NONE = 0,
COMMUNICATION_FAILED,
STATUS_FAILED,
} error_code_{NONE};
};

} // namespace kmeteriso
} // namespace esphome
} // namespace kmeteriso
} // namespace esphome

0 comments on commit 9894a32

Please sign in to comment.