esphome/esphome/components/ltr390/ltr390.h
Stephen Tierney b20760c93c
Add support for LTR390 (#1505)
* Add support for ltr390

* Fix linting errors

* Fix more linting errors

* Linting fixes continued

* Linting forever

* Another one

* Fix regression and linting

* Fix narrowing conversion

* Add test and bugfix

* Add codeowners

* Update CODEOWNERS

* Update sensor defs

* Reformatted with black

* Fixed device class import

* Update CODEOWNERS

* Update CODEOWNERS

* Adding all config options

As requested https://github.com/esphome/esphome/pull/1505#discussion_r597326897

* Moving test to different config file

test1.yml runs out of memory

* Update according to comments

* Add safety clause to reading modes

* Fix clang-tidy complaint

* Revert change to i2c component

* Fix for changes in dev

* Revert "Revert change to i2c component"

This reverts commit 2810df59e9.

Co-authored-by: Otto winter <otto@otto-winter.com>
Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
2021-09-22 13:24:19 +02:00

93 lines
2.4 KiB
C++

#pragma once
#include "esphome/core/component.h"
#include "esphome/core/optional.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
#include <tuple>
namespace esphome {
namespace ltr390 {
enum LTR390CTRL {
LTR390_CTRL_EN = 1,
LTR390_CTRL_MODE = 3,
LTR390_CTRL_RST = 4,
};
// enums from https://github.com/adafruit/Adafruit_LTR390/
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;
static const float LTR390_SENSITIVITY = 2300.0;
// Sensing modes
enum LTR390MODE {
LTR390_MODE_ALS,
LTR390_MODE_UVS,
};
// Sensor gain levels
enum LTR390GAIN {
LTR390_GAIN_1 = 0,
LTR390_GAIN_3, // Default
LTR390_GAIN_6,
LTR390_GAIN_9,
LTR390_GAIN_18,
};
// Sensor resolution
enum LTR390RESOLUTION {
LTR390_RESOLUTION_20BIT,
LTR390_RESOLUTION_19BIT,
LTR390_RESOLUTION_18BIT, // Default
LTR390_RESOLUTION_17BIT,
LTR390_RESOLUTION_16BIT,
LTR390_RESOLUTION_13BIT,
};
class LTR390Component : public PollingComponent, public i2c::I2CDevice {
public:
float get_setup_priority() const override { return setup_priority::DATA; }
void setup() override;
void dump_config() override;
void update() override;
void set_gain_value(LTR390GAIN gain) { this->gain_ = gain; }
void set_res_value(LTR390RESOLUTION 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; }
protected:
optional<uint32_t> read_sensor_data_(LTR390MODE mode);
void read_als_();
void read_uvs_();
void read_mode_(int mode_index);
bool reading_;
// a list of modes and corresponding read functions
std::vector<std::tuple<LTR390MODE, std::function<void()>>> mode_funcs_;
LTR390GAIN gain_;
LTR390RESOLUTION res_;
float wfac_;
sensor::Sensor *light_sensor_{nullptr};
sensor::Sensor *als_sensor_{nullptr};
sensor::Sensor *uvi_sensor_{nullptr};
sensor::Sensor *uv_sensor_{nullptr};
};
} // namespace ltr390
} // namespace esphome