mirror of
https://github.com/esphome/esphome.git
synced 2024-11-27 01:08:03 +01:00
commit
c9d93ff685
10 changed files with 110 additions and 45 deletions
|
@ -11,7 +11,30 @@ namespace adc {
|
||||||
static const char *const TAG = "adc";
|
static const char *const TAG = "adc";
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
void ADCSensor::set_attenuation(adc_attenuation_t attenuation) { this->attenuation_ = attenuation; }
|
void ADCSensor::set_attenuation(adc_atten_t attenuation) { this->attenuation_ = attenuation; }
|
||||||
|
|
||||||
|
inline adc1_channel_t gpio_to_adc1(uint8_t pin) {
|
||||||
|
switch (pin) {
|
||||||
|
case 36:
|
||||||
|
return ADC1_CHANNEL_0;
|
||||||
|
case 37:
|
||||||
|
return ADC1_CHANNEL_1;
|
||||||
|
case 38:
|
||||||
|
return ADC1_CHANNEL_2;
|
||||||
|
case 39:
|
||||||
|
return ADC1_CHANNEL_3;
|
||||||
|
case 32:
|
||||||
|
return ADC1_CHANNEL_4;
|
||||||
|
case 33:
|
||||||
|
return ADC1_CHANNEL_5;
|
||||||
|
case 34:
|
||||||
|
return ADC1_CHANNEL_6;
|
||||||
|
case 35:
|
||||||
|
return ADC1_CHANNEL_7;
|
||||||
|
default:
|
||||||
|
return ADC1_CHANNEL_MAX;
|
||||||
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void ADCSensor::setup() {
|
void ADCSensor::setup() {
|
||||||
|
@ -21,7 +44,9 @@ void ADCSensor::setup() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
analogSetPinAttenuation(this->pin_, this->attenuation_);
|
adc1_config_channel_atten(gpio_to_adc1(pin_), attenuation_);
|
||||||
|
adc1_config_width(ADC_WIDTH_BIT_12);
|
||||||
|
adc_gpio_init(ADC_UNIT_1, (adc_channel_t) gpio_to_adc1(pin_));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
void ADCSensor::dump_config() {
|
void ADCSensor::dump_config() {
|
||||||
|
@ -36,18 +61,20 @@ void ADCSensor::dump_config() {
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
ESP_LOGCONFIG(TAG, " Pin: %u", this->pin_);
|
ESP_LOGCONFIG(TAG, " Pin: %u", this->pin_);
|
||||||
switch (this->attenuation_) {
|
switch (this->attenuation_) {
|
||||||
case ADC_0db:
|
case ADC_ATTEN_DB_0:
|
||||||
ESP_LOGCONFIG(TAG, " Attenuation: 0db (max 1.1V)");
|
ESP_LOGCONFIG(TAG, " Attenuation: 0db (max 1.1V)");
|
||||||
break;
|
break;
|
||||||
case ADC_2_5db:
|
case ADC_ATTEN_DB_2_5:
|
||||||
ESP_LOGCONFIG(TAG, " Attenuation: 2.5db (max 1.5V)");
|
ESP_LOGCONFIG(TAG, " Attenuation: 2.5db (max 1.5V)");
|
||||||
break;
|
break;
|
||||||
case ADC_6db:
|
case ADC_ATTEN_DB_6:
|
||||||
ESP_LOGCONFIG(TAG, " Attenuation: 6db (max 2.2V)");
|
ESP_LOGCONFIG(TAG, " Attenuation: 6db (max 2.2V)");
|
||||||
break;
|
break;
|
||||||
case ADC_11db:
|
case ADC_ATTEN_DB_11:
|
||||||
ESP_LOGCONFIG(TAG, " Attenuation: 11db (max 3.9V)");
|
ESP_LOGCONFIG(TAG, " Attenuation: 11db (max 3.9V)");
|
||||||
break;
|
break;
|
||||||
|
default: // This is to satisfy the unused ADC_ATTEN_MAX
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
|
@ -60,20 +87,23 @@ void ADCSensor::update() {
|
||||||
}
|
}
|
||||||
float ADCSensor::sample() {
|
float ADCSensor::sample() {
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
float value_v = analogRead(this->pin_) / 4095.0f; // NOLINT
|
int raw = adc1_get_raw(gpio_to_adc1(pin_));
|
||||||
|
float value_v = raw / 4095.0f;
|
||||||
switch (this->attenuation_) {
|
switch (this->attenuation_) {
|
||||||
case ADC_0db:
|
case ADC_ATTEN_DB_0:
|
||||||
value_v *= 1.1;
|
value_v *= 1.1;
|
||||||
break;
|
break;
|
||||||
case ADC_2_5db:
|
case ADC_ATTEN_DB_2_5:
|
||||||
value_v *= 1.5;
|
value_v *= 1.5;
|
||||||
break;
|
break;
|
||||||
case ADC_6db:
|
case ADC_ATTEN_DB_6:
|
||||||
value_v *= 2.2;
|
value_v *= 2.2;
|
||||||
break;
|
break;
|
||||||
case ADC_11db:
|
case ADC_ATTEN_DB_11:
|
||||||
value_v *= 3.9;
|
value_v *= 3.9;
|
||||||
break;
|
break;
|
||||||
|
default: // This is to satisfy the unused ADC_ATTEN_MAX
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return value_v;
|
return value_v;
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -6,6 +6,10 @@
|
||||||
#include "esphome/components/sensor/sensor.h"
|
#include "esphome/components/sensor/sensor.h"
|
||||||
#include "esphome/components/voltage_sampler/voltage_sampler.h"
|
#include "esphome/components/voltage_sampler/voltage_sampler.h"
|
||||||
|
|
||||||
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
|
#include "driver/adc.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace adc {
|
namespace adc {
|
||||||
|
|
||||||
|
@ -13,7 +17,7 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage
|
||||||
public:
|
public:
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
/// Set the attenuation for this pin. Only available on the ESP32.
|
/// Set the attenuation for this pin. Only available on the ESP32.
|
||||||
void set_attenuation(adc_attenuation_t attenuation);
|
void set_attenuation(adc_atten_t attenuation);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// Update adc values.
|
/// Update adc values.
|
||||||
|
@ -34,7 +38,7 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage
|
||||||
uint8_t pin_;
|
uint8_t pin_;
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
adc_attenuation_t attenuation_{ADC_0db};
|
adc_atten_t attenuation_{ADC_ATTEN_DB_0};
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -16,10 +16,10 @@ from esphome.const import (
|
||||||
AUTO_LOAD = ["voltage_sampler"]
|
AUTO_LOAD = ["voltage_sampler"]
|
||||||
|
|
||||||
ATTENUATION_MODES = {
|
ATTENUATION_MODES = {
|
||||||
"0db": cg.global_ns.ADC_0db,
|
"0db": cg.global_ns.ADC_ATTEN_DB_0,
|
||||||
"2.5db": cg.global_ns.ADC_2_5db,
|
"2.5db": cg.global_ns.ADC_ATTEN_DB_2_5,
|
||||||
"6db": cg.global_ns.ADC_6db,
|
"6db": cg.global_ns.ADC_ATTEN_DB_6,
|
||||||
"11db": cg.global_ns.ADC_11db,
|
"11db": cg.global_ns.ADC_ATTEN_DB_11,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -107,9 +107,13 @@ float ADS1115Component::request_measurement(ADS1115Sensor *sensor) {
|
||||||
}
|
}
|
||||||
this->prev_config_ = config;
|
this->prev_config_ = config;
|
||||||
|
|
||||||
// about 1.6 ms with 860 samples per second
|
// about 1.2 ms with 860 samples per second
|
||||||
delay(2);
|
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();
|
uint32_t start = millis();
|
||||||
while (this->read_byte_16(ADS1115_REGISTER_CONFIG, &config) && (config >> 15) == 0) {
|
while (this->read_byte_16(ADS1115_REGISTER_CONFIG, &config) && (config >> 15) == 0) {
|
||||||
if (millis() - start > 100) {
|
if (millis() - start > 100) {
|
||||||
|
@ -120,6 +124,7 @@ float ADS1115Component::request_measurement(ADS1115Sensor *sensor) {
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t raw_conversion;
|
uint16_t raw_conversion;
|
||||||
if (!this->read_byte_16(ADS1115_REGISTER_CONVERSION, &raw_conversion)) {
|
if (!this->read_byte_16(ADS1115_REGISTER_CONVERSION, &raw_conversion)) {
|
||||||
|
|
|
@ -102,8 +102,11 @@ class PropertiesFrame : public midea_dongle::BaseFrame {
|
||||||
void set_sleep_mode(bool state) { this->set_bytemask_(20, 0x01, state); }
|
void set_sleep_mode(bool state) { this->set_bytemask_(20, 0x01, state); }
|
||||||
|
|
||||||
/* TURBO MODE */
|
/* TURBO MODE */
|
||||||
bool get_turbo_mode() const { return this->pbuf_[18] & 0x20; }
|
bool get_turbo_mode() const { return this->pbuf_[18] & 0x20 || this->pbuf_[20] & 0x02; }
|
||||||
void set_turbo_mode(bool state) { this->set_bytemask_(18, 0x20, state); }
|
void set_turbo_mode(bool state) {
|
||||||
|
this->set_bytemask_(18, 0x20, state);
|
||||||
|
this->set_bytemask_(20, 0x02, state);
|
||||||
|
}
|
||||||
|
|
||||||
/* FREEZE PROTECTION */
|
/* FREEZE PROTECTION */
|
||||||
bool get_freeze_protection_mode() const { return this->pbuf_[31] & 0x80; }
|
bool get_freeze_protection_mode() const { return this->pbuf_[31] & 0x80; }
|
||||||
|
|
|
@ -9,6 +9,7 @@ from esphome.const import (
|
||||||
CONF_MAX_VALUE,
|
CONF_MAX_VALUE,
|
||||||
CONF_MIN_VALUE,
|
CONF_MIN_VALUE,
|
||||||
CONF_OPTIMISTIC,
|
CONF_OPTIMISTIC,
|
||||||
|
CONF_RESTORE_VALUE,
|
||||||
CONF_STEP,
|
CONF_STEP,
|
||||||
)
|
)
|
||||||
from .. import template_ns
|
from .. import template_ns
|
||||||
|
@ -26,6 +27,17 @@ def validate_min_max(config):
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def validate(config):
|
||||||
|
if CONF_LAMBDA in config:
|
||||||
|
if CONF_OPTIMISTIC in config:
|
||||||
|
raise cv.Invalid("optimistic cannot be used with lambda")
|
||||||
|
if CONF_INITIAL_VALUE in config:
|
||||||
|
raise cv.Invalid("initial_value cannot be used with lambda")
|
||||||
|
if CONF_RESTORE_VALUE in config:
|
||||||
|
raise cv.Invalid("restore_value cannot be used with lambda")
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.All(
|
CONFIG_SCHEMA = cv.All(
|
||||||
number.NUMBER_SCHEMA.extend(
|
number.NUMBER_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
|
@ -33,13 +45,15 @@ CONFIG_SCHEMA = cv.All(
|
||||||
cv.Required(CONF_MAX_VALUE): cv.float_,
|
cv.Required(CONF_MAX_VALUE): cv.float_,
|
||||||
cv.Required(CONF_MIN_VALUE): cv.float_,
|
cv.Required(CONF_MIN_VALUE): cv.float_,
|
||||||
cv.Required(CONF_STEP): cv.positive_float,
|
cv.Required(CONF_STEP): cv.positive_float,
|
||||||
cv.Exclusive(CONF_LAMBDA, "lambda-optimistic"): cv.returning_lambda,
|
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||||
cv.Exclusive(CONF_OPTIMISTIC, "lambda-optimistic"): cv.boolean,
|
cv.Optional(CONF_OPTIMISTIC): cv.boolean,
|
||||||
cv.Optional(CONF_SET_ACTION): automation.validate_automation(single=True),
|
cv.Optional(CONF_SET_ACTION): automation.validate_automation(single=True),
|
||||||
cv.Optional(CONF_INITIAL_VALUE): cv.float_,
|
cv.Optional(CONF_INITIAL_VALUE): cv.float_,
|
||||||
|
cv.Optional(CONF_RESTORE_VALUE): cv.boolean,
|
||||||
}
|
}
|
||||||
).extend(cv.polling_component_schema("60s")),
|
).extend(cv.polling_component_schema("60s")),
|
||||||
validate_min_max,
|
validate_min_max,
|
||||||
|
validate,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -60,13 +74,15 @@ async def to_code(config):
|
||||||
)
|
)
|
||||||
cg.add(var.set_template(template_))
|
cg.add(var.set_template(template_))
|
||||||
|
|
||||||
elif CONF_OPTIMISTIC in config:
|
else:
|
||||||
|
if CONF_OPTIMISTIC in config:
|
||||||
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
||||||
|
if CONF_INITIAL_VALUE in config:
|
||||||
|
cg.add(var.set_initial_value(config[CONF_INITIAL_VALUE]))
|
||||||
|
if CONF_RESTORE_VALUE in config:
|
||||||
|
cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE]))
|
||||||
|
|
||||||
if CONF_SET_ACTION in config:
|
if CONF_SET_ACTION in config:
|
||||||
await automation.build_automation(
|
await automation.build_automation(
|
||||||
var.get_set_trigger(), [(float, "x")], config[CONF_SET_ACTION]
|
var.get_set_trigger(), [(float, "x")], config[CONF_SET_ACTION]
|
||||||
)
|
)
|
||||||
|
|
||||||
if CONF_INITIAL_VALUE in config:
|
|
||||||
cg.add(var.set_initial_value(config[CONF_INITIAL_VALUE]))
|
|
||||||
|
|
|
@ -7,17 +7,21 @@ namespace template_ {
|
||||||
static const char *const TAG = "template.number";
|
static const char *const TAG = "template.number";
|
||||||
|
|
||||||
void TemplateNumber::setup() {
|
void TemplateNumber::setup() {
|
||||||
if (this->f_.has_value() || !this->optimistic_)
|
if (this->f_.has_value())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this->pref_ = global_preferences.make_preference<float>(this->get_object_id_hash());
|
|
||||||
float value;
|
float value;
|
||||||
|
if (!this->restore_value_) {
|
||||||
|
value = this->initial_value_;
|
||||||
|
} else {
|
||||||
|
this->pref_ = global_preferences.make_preference<float>(this->get_object_id_hash());
|
||||||
if (!this->pref_.load(&value)) {
|
if (!this->pref_.load(&value)) {
|
||||||
if (!isnan(this->initial_value_))
|
if (!isnan(this->initial_value_))
|
||||||
value = this->initial_value_;
|
value = this->initial_value_;
|
||||||
else
|
else
|
||||||
value = this->traits.get_min_value();
|
value = this->traits.get_min_value();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
this->publish_state(value);
|
this->publish_state(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,10 +39,11 @@ void TemplateNumber::update() {
|
||||||
void TemplateNumber::control(float value) {
|
void TemplateNumber::control(float value) {
|
||||||
this->set_trigger_->trigger(value);
|
this->set_trigger_->trigger(value);
|
||||||
|
|
||||||
if (this->optimistic_) {
|
if (this->optimistic_)
|
||||||
this->publish_state(value);
|
this->publish_state(value);
|
||||||
|
|
||||||
|
if (this->restore_value_)
|
||||||
this->pref_.save(&value);
|
this->pref_.save(&value);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void TemplateNumber::dump_config() {
|
void TemplateNumber::dump_config() {
|
||||||
LOG_NUMBER("", "Template Number", this);
|
LOG_NUMBER("", "Template Number", this);
|
||||||
|
|
|
@ -20,11 +20,13 @@ class TemplateNumber : public number::Number, public PollingComponent {
|
||||||
Trigger<float> *get_set_trigger() const { return set_trigger_; }
|
Trigger<float> *get_set_trigger() const { return set_trigger_; }
|
||||||
void set_optimistic(bool optimistic) { optimistic_ = optimistic; }
|
void set_optimistic(bool optimistic) { optimistic_ = optimistic; }
|
||||||
void set_initial_value(float initial_value) { initial_value_ = initial_value; }
|
void set_initial_value(float initial_value) { initial_value_ = initial_value; }
|
||||||
|
void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void control(float value) override;
|
void control(float value) override;
|
||||||
bool optimistic_{false};
|
bool optimistic_{false};
|
||||||
float initial_value_{NAN};
|
float initial_value_{NAN};
|
||||||
|
bool restore_value_{false};
|
||||||
Trigger<float> *set_trigger_ = new Trigger<float>();
|
Trigger<float> *set_trigger_ = new Trigger<float>();
|
||||||
optional<std::function<optional<float>()>> f_;
|
optional<std::function<optional<float>()>> f_;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
"""Constants used by esphome."""
|
"""Constants used by esphome."""
|
||||||
|
|
||||||
__version__ = "1.20.0b4"
|
__version__ = "1.20.0b5"
|
||||||
|
|
||||||
ESP_PLATFORM_ESP32 = "ESP32"
|
ESP_PLATFORM_ESP32 = "ESP32"
|
||||||
ESP_PLATFORM_ESP8266 = "ESP8266"
|
ESP_PLATFORM_ESP8266 = "ESP8266"
|
||||||
|
|
|
@ -11,4 +11,4 @@ ifaddr==0.1.7
|
||||||
platformio==5.1.1
|
platformio==5.1.1
|
||||||
esptool==2.8
|
esptool==2.8
|
||||||
click==7.1.2
|
click==7.1.2
|
||||||
esphome-dashboard==20210623.0
|
esphome-dashboard==20210719.0
|
||||||
|
|
Loading…
Reference in a new issue