diff --git a/esphome/components/ltr390/ltr390.cpp b/esphome/components/ltr390/ltr390.cpp index 48899d932c..d616b716c2 100644 --- a/esphome/components/ltr390/ltr390.cpp +++ b/esphome/components/ltr390/ltr390.cpp @@ -7,11 +7,11 @@ namespace ltr390 { static const char *TAG = "ltr390"; -static const float gain_values_[5] = {1.0, 3.0, 6.0, 9.0, 18.0}; -static const float resolution_values_[6] = {4.0, 2.0, 1.0, 0.5, 0.25, 0.125}; -static const uint32_t mode_addresses_[2] = {0x0D, 0x10}; +static const float GAINVALUES[5] = {1.0, 3.0, 6.0, 9.0, 18.0}; +static const float RESOLUTIONVALUE[6] = {4.0, 2.0, 1.0, 0.5, 0.25, 0.125}; +static const uint32_t MODEADDRESSES[2] = {0x0D, 0x10}; -bool LTR390Component::enabled_(void) { +bool LTR390Component::enabled_() { std::bitset<8> crtl_value(this->ctrl_reg_->get()); return (bool) crtl_value[LTR390_CTRL_EN]; } @@ -22,20 +22,18 @@ void LTR390Component::enable_(bool en) { *this->ctrl_reg_ = crtl_value.to_ulong(); } -bool LTR390Component::reset_(void) { +bool LTR390Component::reset_() { std::bitset<8> crtl_value(this->ctrl_reg_->get()); - crtl_value[LTR390_CTRL_RST] = 1; + crtl_value[LTR390_CTRL_RST] = true; *this->ctrl_reg_ = crtl_value.to_ulong(); delay(10); + // Read after reset crtl_value = std::bitset<8>(this->ctrl_reg_->get()); - if (crtl_value.to_ulong()) { - return false; - } - return true; + return !(bool) crtl_value.to_ulong(); } void LTR390Component::set_mode_(LTR390MODE mode) { @@ -44,14 +42,14 @@ void LTR390Component::set_mode_(LTR390MODE mode) { *this->ctrl_reg_ = crtl_value.to_ulong(); } -LTR390MODE LTR390Component::get_mode_(void) { +LTR390MODE LTR390Component::get_mode_() { std::bitset<8> crtl_value(this->ctrl_reg_->get()); return (LTR390MODE)(int) crtl_value[LTR390_CTRL_MODE]; } void LTR390Component::set_gain_(LTR390GAIN gain) { *this->gain_reg_ = gain; } -LTR390GAIN LTR390Component::get_gain_(void) { +LTR390GAIN LTR390Component::get_gain_() { std::bitset<8> gain_value(this->gain_reg_->get()); return (LTR390GAIN) gain_value.to_ulong(); } @@ -68,7 +66,7 @@ void LTR390Component::set_resolution_(LTR390RESOLUTION res) { *this->res_reg_ = res_value.to_ulong(); } -LTR390RESOLUTION LTR390Component::get_resolution_(void) { +LTR390RESOLUTION LTR390Component::get_resolution_() { std::bitset<8> res_value(this->res_reg_->get()); std::bitset<3> output_value(0); @@ -79,12 +77,12 @@ LTR390RESOLUTION LTR390Component::get_resolution_(void) { return (LTR390RESOLUTION) output_value.to_ulong(); } -bool LTR390Component::new_data_available_(void) { +bool LTR390Component::new_data_available_() { std::bitset<8> status_value(this->status_reg_->get()); return (bool) status_value[3]; } -uint32_t little_endian_bytes_to_int(uint8_t *buffer, uint8_t num_bytes) { +uint32_t little_endian_bytes_to_int(const uint8_t *buffer, uint8_t num_bytes) { uint32_t value = 0; for (int i = 0; i < num_bytes; i++) { @@ -99,21 +97,21 @@ uint32_t LTR390Component::read_sensor_data_(LTR390MODE mode) { const uint8_t num_bytes = 3; uint8_t buffer[num_bytes]; - while (!this->new_data_available()) { + while (!this->new_data_available_()) { ESP_LOGD(TAG, "WAITING FOR DATA"); delay(2); } - this->read_bytes(mode_addresses_[mode], buffer, num_bytes); + this->read_bytes(MODEADDRESSES[mode], buffer, num_bytes); return little_endian_bytes_to_int(buffer, num_bytes); } void LTR390Component::read_als_() { - uint32_t als = this->read_sensor_data(LTR390_MODE_ALS); + uint32_t als = this->read_sensor_data_(LTR390_MODE_ALS); if (this->light_sensor_ != nullptr) { - float lux = (0.6 * als) / (gain_values_[this->gain_] * resolution_values_[this->res_]) * this->wfac_; + float lux = (0.6 * als) / (GAINVALUES[this->gain_] * RESOLUTIONVALUE[this->res_]) * this->wfac_; this->light_sensor_->publish_state(lux); } @@ -123,7 +121,7 @@ void LTR390Component::read_als_() { } void LTR390Component::read_uvs_() { - uint32_t uv = this->read_sensor_data(LTR390_MODE_UVS); + uint32_t uv = this->read_sensor_data_(LTR390_MODE_UVS); if (this->uvi_sensor_ != nullptr) { this->uvi_sensor_->publish_state(uv / LTR390_SENSITIVITY * this->wfac_); @@ -136,19 +134,19 @@ void LTR390Component::read_uvs_() { void LTR390Component::read_mode_(int mode_index) { // Set mode - this->set_mode(std::get<0>(this->mode_funcs_->at(mode_index))); + this->set_mode_(std::get<0>(this->mode_funcs_->at(mode_index))); // After the sensor integration time do the following - this->set_timeout(resolution_values_[this->res_] * 100, [this, mode_index]() { + this->set_timeout_(RESOLUTIONVALUE[this->res_] * 100, [this, mode_index]() { // Read from the sensor std::get<1>(this->mode_funcs_->at(mode_index))(); // If there are more modes to read then begin the next // otherwise stop if (mode_index + 1 < this->mode_funcs_->size()) { - this->read_mode(mode_index + 1); + this->read_mode_(mode_index + 1); } else { - this->reading = false; + this->reading_ = false; } }); } @@ -161,44 +159,44 @@ void LTR390Component::setup() { this->gain_reg_ = new i2c::I2CRegister(this, LTR390_GAIN); this->res_reg_ = new i2c::I2CRegister(this, LTR390_MEAS_RATE); - this->reset(); + this->reset_(); - this->enable(true); + this->enable_(true); ESP_LOGD(TAG, "%s", this->enabled() ? "ENABLED" : "DISABLED"); - if (!this->enabled()) { + if (!this->enabled_()) { this->mark_failed(); return; } // Set gain - this->set_gain(this->gain_); + this->set_gain_(this->gain_); // Set resolution - this->set_resolution(this->res_); + this->set_resolution_(this->res_); // Set sensor read state - this->reading = false; + this->reading_ = false; // Create a list of modes and corresponding read functions this->mode_funcs_ = new std::vector > >(); // If we need the light sensor then add to the list if (this->light_sensor_ != nullptr || this->als_sensor_ != nullptr) { - this->mode_funcs_->push_back(std::make_tuple(LTR390_MODE_ALS, std::bind(<R390Component::read_als, this))); + this->mode_funcs_->push_back(std::make_tuple(LTR390_MODE_ALS, std::bind(<R390Component::read_als_, this))); } // If we need the UV sensor then add to the list if (this->uvi_sensor_ != nullptr || this->uv_sensor_ != nullptr) { - this->mode_funcs_->push_back(std::make_tuple(LTR390_MODE_UVS, std::bind(<R390Component::read_uvs, this))); + this->mode_funcs_->push_back(std::make_tuple(LTR390_MODE_UVS, std::bind(<R390Component::read_uvs_, this))); } } void LTR390Component::dump_config() { LOG_I2C_DEVICE(this); } void LTR390Component::update() { - if (!this->reading) { - this->reading = true; - this->read_mode(0); + if (!this->reading_) { + this->reading_ = true; + this->read_mode_(0); } }