Fix linting errors

This commit is contained in:
Stephen Tierney 2021-02-01 21:35:54 +11:00 committed by steve
parent 17d784bbc5
commit 7d13d6aa50
3 changed files with 89 additions and 97 deletions

View file

@ -2,7 +2,6 @@
#include "esphome/core/log.h"
#include <bitset>
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(&LTR390Component::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)));
@ -204,7 +198,6 @@ void LTR390Component::read_mode(int mode_index) {
this->reading = false;
}
});
}
void LTR390Component::update() {

View file

@ -10,49 +10,49 @@
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_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_17BIT,
LTR390_RESOLUTION_16BIT,
LTR390_RESOLUTION_13BIT,
} ltr390_resolution_t;
};
class LTR390Component : public PollingComponent, public i2c::I2CDevice {
public:
public:
void setup() override;
void dump_config() override;
void update() override;
@ -67,30 +67,30 @@ class LTR390Component : public PollingComponent, public i2c::I2CDevice {
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<bool> reading;
std::atomic<bool> reading_;
std::vector< std::tuple< ltr390_mode_t, std::function<void(void)> > > *mode_funcs_;

View file

@ -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'