2019-04-17 12:06:00 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "esphome/core/component.h"
|
|
|
|
#include "esphome/components/sensor/sensor.h"
|
|
|
|
#include "esphome/components/i2c/i2c.h"
|
2019-05-08 09:58:03 +02:00
|
|
|
#include "esphome/components/voltage_sampler/voltage_sampler.h"
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2022-11-24 01:12:55 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
namespace esphome {
|
|
|
|
namespace ads1115 {
|
|
|
|
|
|
|
|
enum ADS1115Multiplexer {
|
|
|
|
ADS1115_MULTIPLEXER_P0_N1 = 0b000,
|
|
|
|
ADS1115_MULTIPLEXER_P0_N3 = 0b001,
|
|
|
|
ADS1115_MULTIPLEXER_P1_N3 = 0b010,
|
|
|
|
ADS1115_MULTIPLEXER_P2_N3 = 0b011,
|
|
|
|
ADS1115_MULTIPLEXER_P0_NG = 0b100,
|
|
|
|
ADS1115_MULTIPLEXER_P1_NG = 0b101,
|
|
|
|
ADS1115_MULTIPLEXER_P2_NG = 0b110,
|
|
|
|
ADS1115_MULTIPLEXER_P3_NG = 0b111,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ADS1115Gain {
|
|
|
|
ADS1115_GAIN_6P144 = 0b000,
|
|
|
|
ADS1115_GAIN_4P096 = 0b001,
|
|
|
|
ADS1115_GAIN_2P048 = 0b010,
|
|
|
|
ADS1115_GAIN_1P024 = 0b011,
|
|
|
|
ADS1115_GAIN_0P512 = 0b100,
|
|
|
|
ADS1115_GAIN_0P256 = 0b101,
|
|
|
|
};
|
|
|
|
|
|
|
|
class ADS1115Sensor;
|
|
|
|
|
|
|
|
class ADS1115Component : public Component, public i2c::I2CDevice {
|
|
|
|
public:
|
|
|
|
void register_sensor(ADS1115Sensor *obj) { this->sensors_.push_back(obj); }
|
|
|
|
/// Set up the internal sensor array.
|
|
|
|
void setup() override;
|
|
|
|
void dump_config() override;
|
|
|
|
/// HARDWARE_LATE setup priority
|
2019-07-02 13:02:46 +02:00
|
|
|
float get_setup_priority() const override { return setup_priority::DATA; }
|
|
|
|
void set_continuous_mode(bool continuous_mode) { continuous_mode_ = continuous_mode; }
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
/// Helper method to request a measurement from a sensor.
|
2019-05-08 10:17:08 +02:00
|
|
|
float request_measurement(ADS1115Sensor *sensor);
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2019-05-08 09:58:03 +02:00
|
|
|
protected:
|
2019-04-17 12:06:00 +02:00
|
|
|
std::vector<ADS1115Sensor *> sensors_;
|
2019-07-02 13:02:46 +02:00
|
|
|
uint16_t prev_config_{0};
|
|
|
|
bool continuous_mode_;
|
2019-04-17 12:06:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Internal holder class that is in instance of Sensor so that the hub can create individual sensors.
|
2019-05-08 14:56:54 +02:00
|
|
|
class ADS1115Sensor : public sensor::Sensor, public PollingComponent, public voltage_sampler::VoltageSampler {
|
2019-04-17 12:06:00 +02:00
|
|
|
public:
|
2019-05-08 09:58:03 +02:00
|
|
|
ADS1115Sensor(ADS1115Component *parent) : parent_(parent) {}
|
|
|
|
void update() override;
|
2019-07-02 13:02:46 +02:00
|
|
|
void set_multiplexer(ADS1115Multiplexer multiplexer) { multiplexer_ = multiplexer; }
|
|
|
|
void set_gain(ADS1115Gain gain) { gain_ = gain; }
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2019-05-08 09:58:03 +02:00
|
|
|
float sample() override;
|
2019-07-02 13:02:46 +02:00
|
|
|
uint8_t get_multiplexer() const { return multiplexer_; }
|
|
|
|
uint8_t get_gain() const { return gain_; }
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
protected:
|
2019-05-08 09:58:03 +02:00
|
|
|
ADS1115Component *parent_;
|
2019-04-17 12:06:00 +02:00
|
|
|
ADS1115Multiplexer multiplexer_;
|
|
|
|
ADS1115Gain gain_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ads1115
|
|
|
|
} // namespace esphome
|