mirror of
https://github.com/esphome/esphome.git
synced 2024-12-02 03:34:18 +01:00
ac0d921413
* Socket refactor and SSL * esp-idf temp * Fixes * Echo component and noise * Add noise API transport support * Updates * ESP-IDF * Complete * Fixes * Fixes * Versions update * New i2c APIs * Complete i2c refactor * SPI migration * Revert ESP Preferences migration, too complex for now * OTA support * Remove echo again * Remove ssl again * GPIOFlags updates * Rename esphal and ICACHE_RAM_ATTR * Make ESP32 arduino compilable again * Fix GPIO flags * Complete pin registry refactor and fixes * Fixes to make test1 compile * Remove sdkconfig file * Ignore sdkconfig file * Fixes in reviewing * Make test2 compile * Make test4 compile * Make test5 compile * Run clang-format * Fix lint errors * Use esp-idf APIs instead of btStart * Another round of fixes * Start implementing ESP8266 * Make test3 compile * Guard esp8266 code * Lint * Reformat * Fixes * Fixes v2 * more fixes * ESP-IDF tidy target * Convert ARDUINO_ARCH_ESPxx * Update WiFiSignalSensor * Update time ifdefs * OTA needs millis from hal * RestartSwitch needs delay from hal * ESP-IDF Uart * Fix OTA blank password * Allow setting sdkconfig * Fix idf partitions and allow setting sdkconfig from yaml * Re-add read/write compat APIs and fix esp8266 uart * Fix esp8266 store log strings in flash * Fix ESP32 arduino preferences not initialized * Update ifdefs * Change how sdkconfig change is detected * Add checks to ci-custom and fix them * Run clang-format * Add esp-idf clang-tidy target and fix errors * Fixes from clang-tidy idf round 2 * Fixes from compiling tests with esp-idf * Run clang-format * Switch test5.yaml to esp-idf * Implement ESP8266 Preferences * Lint * Re-do PIO package version selection a bit * Fix arduinoespressif32 package version * Fix unit tests * Lint * Lint fixes * Fix readv/writev not defined * Fix graphing component * Re-add all old options from core/config.py Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
170 lines
4.7 KiB
C++
170 lines
4.7 KiB
C++
#include "ads1115.h"
|
|
#include "esphome/core/log.h"
|
|
#include "esphome/core/hal.h"
|
|
|
|
namespace esphome {
|
|
namespace ads1115 {
|
|
|
|
static const char *const 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;
|
|
|
|
if (this->continuous_mode_) {
|
|
// Set continuous mode
|
|
// 0bxxxxxxx0xxxxxxxx
|
|
config |= 0b0000000000000000;
|
|
} else {
|
|
// 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;
|
|
}
|
|
this->prev_config_ = config;
|
|
}
|
|
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::request_measurement(ADS1115Sensor *sensor) {
|
|
uint16_t config = this->prev_config_;
|
|
// Multiplexer
|
|
// 0bxBBBxxxxxxxxxxxx
|
|
config &= 0b1000111111111111;
|
|
config |= (sensor->get_multiplexer() & 0b111) << 12;
|
|
|
|
// Gain
|
|
// 0bxxxxBBBxxxxxxxxx
|
|
config &= 0b1111000111111111;
|
|
config |= (sensor->get_gain() & 0b111) << 9;
|
|
|
|
if (!this->continuous_mode_) {
|
|
// Start conversion
|
|
config |= 0b1000000000000000;
|
|
}
|
|
|
|
if (!this->continuous_mode_ || this->prev_config_ != config) {
|
|
if (!this->write_byte_16(ADS1115_REGISTER_CONFIG, config)) {
|
|
this->status_set_warning();
|
|
return NAN;
|
|
}
|
|
this->prev_config_ = config;
|
|
|
|
// about 1.2 ms with 860 samples per second
|
|
delay(2);
|
|
|
|
// in continuous mode, conversion will always be running, rely on the delay
|
|
// to ensure conversion is taking place with the correct settings
|
|
// can we use the rdy pin to trigger when a conversion is done?
|
|
if (!this->continuous_mode_) {
|
|
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 NAN;
|
|
}
|
|
yield();
|
|
}
|
|
}
|
|
}
|
|
|
|
uint16_t raw_conversion;
|
|
if (!this->read_byte_16(ADS1115_REGISTER_CONVERSION, &raw_conversion)) {
|
|
this->status_set_warning();
|
|
return NAN;
|
|
}
|
|
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;
|
|
}
|
|
|
|
this->status_clear_warning();
|
|
return millivolts / 1e3f;
|
|
}
|
|
|
|
float ADS1115Sensor::sample() { return this->parent_->request_measurement(this); }
|
|
void ADS1115Sensor::update() {
|
|
float v = this->parent_->request_measurement(this);
|
|
if (!std::isnan(v)) {
|
|
ESP_LOGD(TAG, "'%s': Got Voltage=%fV", this->get_name().c_str(), v);
|
|
this->publish_state(v);
|
|
}
|
|
}
|
|
|
|
} // namespace ads1115
|
|
} // namespace esphome
|