Files
esphome-dev/esphome/components/tsl2561/tsl2561.h
T
Otto Winter 6682c43dfa 🏗 Merge C++ into python codebase (#504)
## Description:

Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97

Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍

Progress:
- Core support (file copy etc): 80%
- Base Abstractions (light, switch): ~50%
- Integrations: ~10%
- Working? Yes, (but only with ported components).

Other refactors:
- Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`)
- Rework coroutine syntax
- Move from `component/platform.py` to `domain/component.py` structure as with HA
- Move all defaults out of C++ and into config validation.
- Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration.
- Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit.

Future work:
- Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block
- Enable loading from `custom_components` folder.

**Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97

**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here>

## Checklist:
  - [ ] The code change is tested and works locally.
  - [ ] Tests have been added to verify that the new code works (under `tests/` folder).

If user exposed functionality or configuration variables are added/changed:
  - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
2019-04-17 12:06:00 +02:00

90 lines
2.6 KiB
C++

#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace tsl2561 {
/** Enum listing all conversion/integration time settings for the TSL2561
*
* Higher values mean more accurate results, but will take more energy/more time.
*/
enum TSL2561IntegrationTime {
TSL2561_INTEGRATION_14MS = 0b00,
TSL2561_INTEGRATION_101MS = 0b01,
TSL2561_INTEGRATION_402MS = 0b10,
};
/** Enum listing all gain settings for the TSL2561.
*
* Higher values are better for low light situations, but can increase noise.
*/
enum TSL2561Gain {
TSL2561_GAIN_1X = 0,
TSL2561_GAIN_16X = 1,
};
/// This class includes support for the TSL2561 i2c ambient light sensor.
class TSL2561Sensor : public sensor::PollingSensorComponent, public i2c::I2CDevice {
public:
TSL2561Sensor(const std::string &name, uint32_t update_interval);
/** Set the time that sensor values should be accumulated for.
*
* Longer means more accurate, but also mean more power consumption.
*
* Possible values are:
*
* - `sensor::TSL2561_INTEGRATION_14MS`
* - `sensor::TSL2561_INTEGRATION_101MS`
* - `sensor::TSL2561_INTEGRATION_402MS` (default)
*
* @param integration_time The new integration time.
*/
void set_integration_time(TSL2561IntegrationTime integration_time);
/** Set the internal gain of the sensor. Can be useful for low-light conditions
*
* Possible values are:
*
* - `sensor::TSL2561_GAIN_1X` (default)
* - `sensor::TSL2561_GAIN_16X`
*
* @param gain The new gain.
*/
void set_gain(TSL2561Gain gain);
/** The "CS" package of this sensor has a slightly different formula for
* converting the raw values. Use this setting to indicate that this is a CS
* package. Defaults to false (not a CS package)
*
* @param package_cs Is this a CS package.
*/
void set_is_cs_package(bool package_cs);
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
void setup() override;
void dump_config() override;
void update() override;
float get_setup_priority() const override;
bool tsl2561_read_byte(uint8_t a_register, uint8_t *value);
bool tsl2561_read_uint(uint8_t a_register, uint16_t *value);
bool tsl2561_write_byte(uint8_t a_register, uint8_t value);
protected:
float get_integration_time_ms_();
void read_data_();
float calculate_lx_(uint16_t ch0, uint16_t ch1);
TSL2561IntegrationTime integration_time_{TSL2561_INTEGRATION_402MS};
TSL2561Gain gain_{TSL2561_GAIN_1X};
bool package_cs_{false};
};
} // namespace tsl2561
} // namespace esphome