diff --git a/esphome/components/ltr390/ltr390.cpp b/esphome/components/ltr390/ltr390.cpp index fef0d30cc0..b1d0ce2f62 100644 --- a/esphome/components/ltr390/ltr390.cpp +++ b/esphome/components/ltr390/ltr390.cpp @@ -2,7 +2,6 @@ #include "esphome/core/log.h" #include - namespace esphome { namespace ltr390 { @@ -12,19 +11,19 @@ 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}; -bool LTR390Component::enabled(void) { - std::bitset<8> crtl_value (this->ctrl_reg_->get()); - return (bool)crtl_value[LTR390_CTRL_EN]; +bool LTR390Component::enabled_(void) { + std::bitset<8> crtl_value(this->ctrl_reg_->get()); + return (bool) crtl_value[LTR390_CTRL_EN]; } -void LTR390Component::enable(bool en) { - std::bitset<8> crtl_value (this->ctrl_reg_->get()); +void LTR390Component::enable_(bool en) { + std::bitset<8> crtl_value(this->ctrl_reg_->get()); crtl_value[LTR390_CTRL_EN] = en; *this->ctrl_reg_ = crtl_value.to_ulong(); } -bool LTR390Component::reset(void) { - std::bitset<8> crtl_value (this->ctrl_reg_->get()); +bool LTR390Component::reset_(void) { + std::bitset<8> crtl_value(this->ctrl_reg_->get()); crtl_value[LTR390_CTRL_RST] = 1; *this->ctrl_reg_ = crtl_value.to_ulong(); @@ -39,52 +38,52 @@ bool LTR390Component::reset(void) { return true; } -void LTR390Component::set_mode(ltr390_mode_t mode) { - std::bitset<8> crtl_value (this->ctrl_reg_->get()); +void LTR390Component::set_mode_(ltr390_mode_t mode) { + std::bitset<8> crtl_value(this->ctrl_reg_->get()); crtl_value[LTR390_CTRL_MODE] = mode; *this->ctrl_reg_ = crtl_value.to_ulong(); } -ltr390_mode_t LTR390Component::get_mode(void) { - std::bitset<8> crtl_value (this->ctrl_reg_->get()); - return (ltr390_mode_t)(int)crtl_value[LTR390_CTRL_MODE]; +ltr390_mode_t LTR390Component::get_mode_(void) { + std::bitset<8> crtl_value(this->ctrl_reg_->get()); + return (ltr390_mode_t)(int) crtl_value[LTR390_CTRL_MODE]; } -void LTR390Component::set_gain(ltr390_gain_t gain) { +void LTR390Component::set_gain_(ltr390_gain_t gain) { *this->gain_reg_ = gain; } -ltr390_gain_t LTR390Component::get_gain(void) { - std::bitset<8> gain_value (this->gain_reg_->get()); - return (ltr390_gain_t)gain_value.to_ulong(); +ltr390_gain_t LTR390Component::get_gain_(void) { + std::bitset<8> gain_value(this->gain_reg_->get()); + return (ltr390_gain_t) gain_value.to_ulong(); } -void LTR390Component::set_resolution(ltr390_resolution_t res) { - std::bitset<8> res_value (this->res_reg_->get()); +void LTR390Component::set_resolution_(ltr390_resolution_t res) { + std::bitset<8> res_value(this->res_reg_->get()); - std::bitset<3> new_res_value (res); + std::bitset<3> new_res_value(res); for (int i = 0; i < 3; i++) { - res_value[4+i] = new_res_value[i]; + res_value[4 + i] = new_res_value[i]; } *this->res_reg_ = res_value.to_ulong(); } -ltr390_resolution_t LTR390Component::get_resolution(void) { - std::bitset<8> res_value (this->res_reg_->get()); +ltr390_resolution_t LTR390Component::get_resolution_(void) { + std::bitset<8> res_value(this->res_reg_->get()); std::bitset<3> output_value (0); for (int i = 0; i < 3; i++) { - output_value[i] = res_value[4+i]; + output_value[i] = res_value[4 + i]; } - return (ltr390_resolution_t)output_value.to_ulong(); + return (ltr390_resolution_t) output_value.to_ulong(); } -bool LTR390Component::new_data_available(void) { - std::bitset<8> status_value (this->status_reg_->get()); - return (bool)status_value[3]; +bool LTR390Component::new_data_available_(void) { + 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) { @@ -98,7 +97,7 @@ uint32_t little_endian_bytes_to_int(uint8_t *buffer, uint8_t num_bytes) { return value; } -uint32_t LTR390Component::read_sensor_data(ltr390_mode_t mode) { +uint32_t LTR390Component::read_sensor_data_(ltr390_mode_t mode) { const uint8_t num_bytes = 3; uint8_t buffer[num_bytes]; @@ -113,8 +112,6 @@ uint32_t LTR390Component::read_sensor_data(ltr390_mode_t mode) { } void LTR390Component::setup() { - - ESP_LOGCONFIG(TAG, "Setting up ltr390..."); this->ctrl_reg_ = new i2c::I2CRegister(this, LTR390_MAIN_CTRL); @@ -152,14 +149,13 @@ void LTR390Component::setup() { 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))); } - } void LTR390Component::dump_config() { LOG_I2C_DEVICE(this); } -void LTR390Component::read_als() { +void LTR390Component::read_als_() { uint32_t als = this->read_sensor_data(LTR390_MODE_ALS); if (this->light_sensor_ != nullptr) { @@ -170,14 +166,13 @@ void LTR390Component::read_als() { if (this->als_sensor_ != nullptr) { this->als_sensor_->publish_state(als); } - } -void LTR390Component::read_uvs() { +void LTR390Component::read_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_); + this->uvi_sensor_->publish_state(uv / LTR390_SENSITIVITY * this->wfac_); } if (this->uv_sensor_ != nullptr) { @@ -185,8 +180,7 @@ void LTR390Component::read_uvs() { } } - -void LTR390Component::read_mode(int mode_index) { +void LTR390Component::read_mode_(int mode_index) { // Set mode this->set_mode(std::get<0>(this->mode_funcs_->at(mode_index))); @@ -199,12 +193,11 @@ void LTR390Component::read_mode(int 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; } }); - } void LTR390Component::update() { diff --git a/esphome/components/ltr390/ltr390.h b/esphome/components/ltr390/ltr390.h index ac2a3b21c1..f435b68682 100644 --- a/esphome/components/ltr390/ltr390.h +++ b/esphome/components/ltr390/ltr390.h @@ -10,104 +10,104 @@ namespace esphome { namespace ltr390 { -typedef enum { +enum ltr390_ctrl_t { LTR390_CTRL_EN = 1, LTR390_CTRL_MODE = 3, LTR390_CTRL_RST = 4, -} ltr390_ctrl_t; +}; // enums from https://github.com/adafruit/Adafruit_LTR390/ -#define LTR390_MAIN_CTRL 0x00 ///< Main control register -#define LTR390_MEAS_RATE 0x04 ///< Resolution and data rate -#define LTR390_GAIN 0x05 ///< ALS and UVS gain range -#define LTR390_PART_ID 0x06 ///< Part id/revision register -#define LTR390_MAIN_STATUS 0x07 ///< Main status register +static const uint8_t LTR390_MAIN_CTRL = 0x00; +static const uint8_t LTR390_MEAS_RATE = 0x04; +static const uint8_t LTR390_GAIN = 0x05; +static const uint8_t LTR390_PART_ID = 0x06; +static const uint8_t LTR390_MAIN_STATUS = 0x07; #define LTR390_SENSITIVITY 2300.0 // Sensing modes -typedef enum { +enum ltr390_mode_t { LTR390_MODE_ALS, LTR390_MODE_UVS, -} ltr390_mode_t; +}; // Sensor gain levels -typedef enum { +enum ltr390_gain_t { LTR390_GAIN_1 = 0, - LTR390_GAIN_3, // Default + LTR390_GAIN_3, // Default LTR390_GAIN_6, LTR390_GAIN_9, LTR390_GAIN_18, -} ltr390_gain_t; +}; // Sensor resolution -typedef enum { +enum ltr390_resolution_t { LTR390_RESOLUTION_20BIT, LTR390_RESOLUTION_19BIT, - LTR390_RESOLUTION_18BIT, // Default + LTR390_RESOLUTION_18BIT, // Default LTR390_RESOLUTION_17BIT, LTR390_RESOLUTION_16BIT, LTR390_RESOLUTION_13BIT, -} ltr390_resolution_t; +}; class LTR390Component : public PollingComponent, public i2c::I2CDevice { - public: - void setup() override; - void dump_config() override; - void update() override; - float get_setup_priority() const override { return setup_priority::DATA; } +public: + void setup() override; + void dump_config() override; + void update() override; + float get_setup_priority() const override { return setup_priority::DATA; } - void set_gain_value(ltr390_gain_t gain) { this->gain_ = gain; } - void set_res_value(ltr390_resolution_t res) { this->res_ = res; } - void set_wfac_value(float wfac) { this->wfac_ = wfac; } + void set_gain_value(ltr390_gain_t gain) { this->gain_ = gain; } + void set_res_value(ltr390_resolution_t res) { this->res_ = res; } + void set_wfac_value(float wfac) { this->wfac_ = wfac; } - void set_light_sensor(sensor::Sensor *light_sensor) { this->light_sensor_ = light_sensor; } - void set_als_sensor(sensor::Sensor *als_sensor) { this->als_sensor_ = als_sensor; } - void set_uvi_sensor(sensor::Sensor *uvi_sensor) { this->uvi_sensor_ = uvi_sensor; } - void set_uv_sensor(sensor::Sensor *uv_sensor) { this->uv_sensor_ = uv_sensor; } + void set_light_sensor(sensor::Sensor *light_sensor) { this->light_sensor_ = light_sensor; } + void set_als_sensor(sensor::Sensor *als_sensor) { this->als_sensor_ = als_sensor; } + void set_uvi_sensor(sensor::Sensor *uvi_sensor) { this->uvi_sensor_ = uvi_sensor; } + void set_uv_sensor(sensor::Sensor *uv_sensor) { this->uv_sensor_ = uv_sensor; } - protected: - bool enabled(); - void enable(bool en); +protected: + bool enabled_(); + void enable_(bool en); - bool reset(void); + bool reset_(); - void set_mode(ltr390_mode_t mode); - ltr390_mode_t get_mode(void); + void set_mode_(ltr390_mode_t mode); + ltr390_mode_t get_mode_(); - void set_gain(ltr390_gain_t gain); - ltr390_gain_t get_gain(void); + void set_gain_(ltr390_gain_t gain); + ltr390_gain_t get_gain_(); - void set_resolution(ltr390_resolution_t res); - ltr390_resolution_t get_resolution(void); + void set_resolution_(ltr390_resolution_t res); + ltr390_resolution_t get_resolution_(); - bool new_data_available(void); - uint32_t read_sensor_data(ltr390_mode_t mode); + bool new_data_available_(); + uint32_t read_sensor_data_(ltr390_mode_t mode); - void read_als(void); - void read_uvs(void); + void read_als_(); + void read_uvs_(); - void read_mode(int mode_index); + void read_mode_(int mode_index); - std::atomic reading; + std::atomic reading_; - std::vector< std::tuple< ltr390_mode_t, std::function > > *mode_funcs_; + std::vector< std::tuple< ltr390_mode_t, std::function > > *mode_funcs_; - i2c::I2CRegister *ctrl_reg_; - i2c::I2CRegister *status_reg_; - i2c::I2CRegister *gain_reg_; - i2c::I2CRegister *res_reg_; + i2c::I2CRegister *ctrl_reg_; + i2c::I2CRegister *status_reg_; + i2c::I2CRegister *gain_reg_; + i2c::I2CRegister *res_reg_; - ltr390_gain_t gain_; - ltr390_resolution_t res_; - float wfac_; + ltr390_gain_t gain_; + ltr390_resolution_t res_; + float wfac_; - sensor::Sensor *light_sensor_{nullptr}; - sensor::Sensor *als_sensor_{nullptr}; + sensor::Sensor *light_sensor_{nullptr}; + sensor::Sensor *als_sensor_{nullptr}; - sensor::Sensor *uvi_sensor_{nullptr}; - sensor::Sensor *uv_sensor_{nullptr}; + sensor::Sensor *uvi_sensor_{nullptr}; + sensor::Sensor *uv_sensor_{nullptr}; }; diff --git a/esphome/components/ltr390/sensor.py b/esphome/components/ltr390/sensor.py index 97ddc3bf5b..24ce24fa11 100644 --- a/esphome/components/ltr390/sensor.py +++ b/esphome/components/ltr390/sensor.py @@ -1,7 +1,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import i2c, sensor -from esphome.const import CONF_ID, CONF_GAIN, CONF_RESOLUTION, UNIT_LUX, ICON_BRIGHTNESS_5 +from esphome.const import CONF_ID, CONF_GAIN, CONF_LIGHT, CONF_RESOLUTION, UNIT_LUX, ICON_BRIGHTNESS_5 DEPENDENCIES = ['i2c'] @@ -9,7 +9,6 @@ ltr390_ns = cg.esphome_ns.namespace('ltr390') LTR390Component = ltr390_ns.class_('LTR390Component', cg.PollingComponent, i2c.I2CDevice) -CONF_LIGHT = 'light' CONF_ALS = 'als' CONF_UVI = 'uvi' CONF_UV = 'uv'