mirror of
https://github.com/esphome/esphome.git
synced 2024-12-01 19:24:14 +01:00
6682c43dfa
## 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).
158 lines
4.5 KiB
C++
158 lines
4.5 KiB
C++
#include "ads1115.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
namespace esphome {
|
|
namespace ads1115 {
|
|
|
|
static const char *TAG = "ads1115";
|
|
static const uint8_t ADS1115_REGISTER_CONVERSION = 0x00;
|
|
static const uint8_t ADS1115_REGISTER_CONFIG = 0x01;
|
|
|
|
static const uint8_t ADS1115_DATA_RATE_860_SPS = 0b111;
|
|
|
|
void ADS1115Component::setup() {
|
|
ESP_LOGCONFIG(TAG, "Setting up ADS1115...");
|
|
uint16_t value;
|
|
if (!this->read_byte_16(ADS1115_REGISTER_CONVERSION, &value)) {
|
|
this->mark_failed();
|
|
return;
|
|
}
|
|
uint16_t config = 0;
|
|
// Clear single-shot bit
|
|
// 0b0xxxxxxxxxxxxxxx
|
|
config |= 0b0000000000000000;
|
|
// Setup multiplexer
|
|
// 0bx000xxxxxxxxxxxx
|
|
config |= ADS1115_MULTIPLEXER_P0_N1 << 12;
|
|
|
|
// Setup Gain
|
|
// 0bxxxx000xxxxxxxxx
|
|
config |= ADS1115_GAIN_6P144 << 9;
|
|
|
|
// Set singleshot mode
|
|
// 0bxxxxxxx1xxxxxxxx
|
|
config |= 0b0000000100000000;
|
|
|
|
// Set data rate - 860 samples per second (we're in singleshot mode)
|
|
// 0bxxxxxxxx100xxxxx
|
|
config |= ADS1115_DATA_RATE_860_SPS << 5;
|
|
|
|
// Set comparator mode - hysteresis
|
|
// 0bxxxxxxxxxxx0xxxx
|
|
config |= 0b0000000000000000;
|
|
|
|
// Set comparator polarity - active low
|
|
// 0bxxxxxxxxxxxx0xxx
|
|
config |= 0b0000000000000000;
|
|
|
|
// Set comparator latch enabled - false
|
|
// 0bxxxxxxxxxxxxx0xx
|
|
config |= 0b0000000000000000;
|
|
|
|
// Set comparator que mode - disabled
|
|
// 0bxxxxxxxxxxxxxx11
|
|
config |= 0b0000000000000011;
|
|
|
|
if (!this->write_byte_16(ADS1115_REGISTER_CONFIG, config)) {
|
|
this->mark_failed();
|
|
return;
|
|
}
|
|
for (auto *sensor : this->sensors_) {
|
|
this->set_interval(sensor->get_name(), sensor->update_interval(),
|
|
[this, sensor] { this->request_measurement_(sensor); });
|
|
}
|
|
}
|
|
void ADS1115Component::dump_config() {
|
|
ESP_LOGCONFIG(TAG, "Setting up ADS1115...");
|
|
LOG_I2C_DEVICE(this);
|
|
if (this->is_failed()) {
|
|
ESP_LOGE(TAG, "Communication with ADS1115 failed!");
|
|
}
|
|
|
|
for (auto *sensor : this->sensors_) {
|
|
LOG_SENSOR(" ", "Sensor", sensor);
|
|
ESP_LOGCONFIG(TAG, " Multiplexer: %u", sensor->get_multiplexer());
|
|
ESP_LOGCONFIG(TAG, " Gain: %u", sensor->get_gain());
|
|
}
|
|
}
|
|
float ADS1115Component::get_setup_priority() const { return setup_priority::DATA; }
|
|
void ADS1115Component::request_measurement_(ADS1115Sensor *sensor) {
|
|
uint16_t config;
|
|
if (!this->read_byte_16(ADS1115_REGISTER_CONFIG, &config)) {
|
|
this->status_set_warning();
|
|
return;
|
|
}
|
|
// Multiplexer
|
|
// 0bxBBBxxxxxxxxxxxx
|
|
config &= 0b1000111111111111;
|
|
config |= (sensor->get_multiplexer() & 0b111) << 12;
|
|
|
|
// Gain
|
|
// 0bxxxxBBBxxxxxxxxx
|
|
config &= 0b1111000111111111;
|
|
config |= (sensor->get_gain() & 0b111) << 9;
|
|
// Start conversion
|
|
config |= 0b1000000000000000;
|
|
|
|
if (!this->write_byte_16(ADS1115_REGISTER_CONFIG, config)) {
|
|
this->status_set_warning();
|
|
return;
|
|
}
|
|
|
|
// about 1.6 ms with 860 samples per second
|
|
delay(2);
|
|
|
|
uint32_t start = millis();
|
|
while (this->read_byte_16(ADS1115_REGISTER_CONFIG, &config) && (config >> 15) == 0) {
|
|
if (millis() - start > 100) {
|
|
ESP_LOGW(TAG, "Reading ADS1115 timed out");
|
|
this->status_set_warning();
|
|
return;
|
|
}
|
|
yield();
|
|
}
|
|
|
|
uint16_t raw_conversion;
|
|
if (!this->read_byte_16(ADS1115_REGISTER_CONVERSION, &raw_conversion)) {
|
|
this->status_set_warning();
|
|
return;
|
|
}
|
|
auto signed_conversion = static_cast<int16_t>(raw_conversion);
|
|
|
|
float millivolts;
|
|
switch (sensor->get_gain()) {
|
|
case ADS1115_GAIN_6P144:
|
|
millivolts = signed_conversion * 0.187500f;
|
|
break;
|
|
case ADS1115_GAIN_4P096:
|
|
millivolts = signed_conversion * 0.125000f;
|
|
break;
|
|
case ADS1115_GAIN_2P048:
|
|
millivolts = signed_conversion * 0.062500f;
|
|
break;
|
|
case ADS1115_GAIN_1P024:
|
|
millivolts = signed_conversion * 0.031250f;
|
|
break;
|
|
case ADS1115_GAIN_0P512:
|
|
millivolts = signed_conversion * 0.015625f;
|
|
break;
|
|
case ADS1115_GAIN_0P256:
|
|
millivolts = signed_conversion * 0.007813f;
|
|
break;
|
|
default:
|
|
millivolts = NAN;
|
|
}
|
|
|
|
float v = millivolts / 1000.0f;
|
|
ESP_LOGD(TAG, "'%s': Got Voltage=%fV", sensor->get_name().c_str(), v);
|
|
sensor->publish_state(v);
|
|
this->status_clear_warning();
|
|
}
|
|
|
|
uint8_t ADS1115Sensor::get_multiplexer() const { return this->multiplexer_; }
|
|
void ADS1115Sensor::set_multiplexer(ADS1115Multiplexer multiplexer) { this->multiplexer_ = multiplexer; }
|
|
uint8_t ADS1115Sensor::get_gain() const { return this->gain_; }
|
|
void ADS1115Sensor::set_gain(ADS1115Gain gain) { this->gain_ = gain; }
|
|
|
|
} // namespace ads1115
|
|
} // namespace esphome
|