mirror of
https://github.com/esphome/esphome.git
synced 2026-01-20 01:49:11 -07:00
Compare commits
2 Commits
status_set
...
copilot/fi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
17eec99bd1 | ||
|
|
726aa67f32 |
@@ -19,7 +19,7 @@ void CST816Touchscreen::continue_setup_() {
|
||||
case CST816T_CHIP_ID:
|
||||
break;
|
||||
default:
|
||||
this->status_set_error(str_sprintf("Unknown chip ID 0x%02X", this->chip_id_));
|
||||
this->status_set_error(str_sprintf("Unknown chip ID 0x%02X", this->chip_id_).c_str());
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ void HttpRequestUpdate::update_task(void *params) {
|
||||
if (container == nullptr || container->status_code != HTTP_STATUS_OK) {
|
||||
std::string msg = str_sprintf("Failed to fetch manifest from %s", this_update->source_url_.c_str());
|
||||
// Defer to main loop to avoid race condition on component_state_ read-modify-write
|
||||
this_update->defer([this_update, msg]() { this_update->status_set_error(msg); });
|
||||
this_update->defer([this_update, msg]() { this_update->status_set_error(msg.c_str()); });
|
||||
UPDATE_RETURN;
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ void HttpRequestUpdate::update_task(void *params) {
|
||||
if (data == nullptr) {
|
||||
std::string msg = str_sprintf("Failed to allocate %zu bytes for manifest", container->content_length);
|
||||
// Defer to main loop to avoid race condition on component_state_ read-modify-write
|
||||
this_update->defer([this_update, msg]() { this_update->status_set_error(msg); });
|
||||
this_update->defer([this_update, msg]() { this_update->status_set_error(msg.c_str()); });
|
||||
container->end();
|
||||
UPDATE_RETURN;
|
||||
}
|
||||
@@ -123,7 +123,7 @@ void HttpRequestUpdate::update_task(void *params) {
|
||||
if (!valid) {
|
||||
std::string msg = str_sprintf("Failed to parse JSON from %s", this_update->source_url_.c_str());
|
||||
// Defer to main loop to avoid race condition on component_state_ read-modify-write
|
||||
this_update->defer([this_update, msg]() { this_update->status_set_error(msg); });
|
||||
this_update->defer([this_update, msg]() { this_update->status_set_error(msg.c_str()); });
|
||||
UPDATE_RETURN;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace jsn_sr04t {
|
||||
static const char *const TAG = "jsn_sr04t.sensor";
|
||||
|
||||
void Jsnsr04tComponent::update() {
|
||||
this->write_byte((this->model_ == AJ_SR04M) ? 0x01 : 0x55);
|
||||
this->write_byte(0x55);
|
||||
ESP_LOGV(TAG, "Request read out from sensor");
|
||||
}
|
||||
|
||||
@@ -31,10 +31,19 @@ void Jsnsr04tComponent::loop() {
|
||||
}
|
||||
|
||||
void Jsnsr04tComponent::check_buffer_() {
|
||||
uint8_t checksum = this->buffer_[0] + this->buffer_[1] + this->buffer_[2];
|
||||
uint8_t checksum = 0;
|
||||
switch (this->model_) {
|
||||
case JSN_SR04T:
|
||||
checksum = this->buffer_[0] + this->buffer_[1] + this->buffer_[2];
|
||||
break;
|
||||
case AJ_SR04M:
|
||||
checksum = this->buffer_[1] + this->buffer_[2];
|
||||
break;
|
||||
}
|
||||
|
||||
if (this->buffer_[3] == checksum) {
|
||||
uint16_t distance = encode_uint16(this->buffer_[1], this->buffer_[2]);
|
||||
if (distance > ((this->model_ == AJ_SR04M) ? 200 : 250)) {
|
||||
if (distance > 250) {
|
||||
float meters = distance / 1000.0f;
|
||||
ESP_LOGV(TAG, "Distance from sensor: %umm, %.3fm", distance, meters);
|
||||
this->publish_state(meters);
|
||||
|
||||
@@ -56,99 +56,112 @@ pulse_counter_t BasicPulseCounterStorage::read_raw_value() {
|
||||
|
||||
#ifdef HAS_PCNT
|
||||
bool HwPulseCounterStorage::pulse_counter_setup(InternalGPIOPin *pin) {
|
||||
static pcnt_unit_t next_pcnt_unit = PCNT_UNIT_0;
|
||||
static pcnt_channel_t next_pcnt_channel = PCNT_CHANNEL_0;
|
||||
this->pin = pin;
|
||||
this->pin->setup();
|
||||
this->pcnt_unit = next_pcnt_unit;
|
||||
this->pcnt_channel = next_pcnt_channel;
|
||||
next_pcnt_unit = pcnt_unit_t(int(next_pcnt_unit) + 1);
|
||||
if (int(next_pcnt_unit) >= PCNT_UNIT_0 + PCNT_UNIT_MAX) {
|
||||
next_pcnt_unit = PCNT_UNIT_0;
|
||||
next_pcnt_channel = pcnt_channel_t(int(next_pcnt_channel) + 1);
|
||||
|
||||
// Create unit configuration
|
||||
pcnt_unit_config_t unit_config = {};
|
||||
unit_config.high_limit = 32767;
|
||||
unit_config.low_limit = -32768;
|
||||
|
||||
esp_err_t error = pcnt_new_unit(&unit_config, &this->pcnt_unit);
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Creating Pulse Counter unit failed: %s", esp_err_to_name(error));
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGCONFIG(TAG, " PCNT Unit Number: %u", this->pcnt_unit);
|
||||
ESP_LOGCONFIG(TAG, " PCNT Channel Number: %u", this->pcnt_channel);
|
||||
// Set up glitch filter if needed
|
||||
if (this->filter_us != 0) {
|
||||
pcnt_glitch_filter_config_t filter_config = {};
|
||||
filter_config.max_glitch_ns = this->filter_us * 1000; // Convert microseconds to nanoseconds
|
||||
|
||||
ESP_LOGCONFIG(TAG, " Filter Value: %" PRIu32 "us", this->filter_us);
|
||||
error = pcnt_unit_set_glitch_filter(this->pcnt_unit, &filter_config);
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Setting glitch filter failed: %s", esp_err_to_name(error));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Determine count modes
|
||||
pcnt_channel_edge_action_t rising_action = PCNT_CHANNEL_EDGE_ACTION_HOLD;
|
||||
pcnt_channel_edge_action_t falling_action = PCNT_CHANNEL_EDGE_ACTION_HOLD;
|
||||
|
||||
pcnt_count_mode_t rising = PCNT_COUNT_DIS, falling = PCNT_COUNT_DIS;
|
||||
switch (this->rising_edge_mode) {
|
||||
case PULSE_COUNTER_DISABLE:
|
||||
rising = PCNT_COUNT_DIS;
|
||||
rising_action = PCNT_CHANNEL_EDGE_ACTION_HOLD;
|
||||
break;
|
||||
case PULSE_COUNTER_INCREMENT:
|
||||
rising = PCNT_COUNT_INC;
|
||||
rising_action = PCNT_CHANNEL_EDGE_ACTION_INCREASE;
|
||||
break;
|
||||
case PULSE_COUNTER_DECREMENT:
|
||||
rising = PCNT_COUNT_DEC;
|
||||
rising_action = PCNT_CHANNEL_EDGE_ACTION_DECREASE;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (this->falling_edge_mode) {
|
||||
case PULSE_COUNTER_DISABLE:
|
||||
falling = PCNT_COUNT_DIS;
|
||||
falling_action = PCNT_CHANNEL_EDGE_ACTION_HOLD;
|
||||
break;
|
||||
case PULSE_COUNTER_INCREMENT:
|
||||
falling = PCNT_COUNT_INC;
|
||||
falling_action = PCNT_CHANNEL_EDGE_ACTION_INCREASE;
|
||||
break;
|
||||
case PULSE_COUNTER_DECREMENT:
|
||||
falling = PCNT_COUNT_DEC;
|
||||
falling_action = PCNT_CHANNEL_EDGE_ACTION_DECREASE;
|
||||
break;
|
||||
}
|
||||
|
||||
pcnt_config_t pcnt_config = {
|
||||
.pulse_gpio_num = this->pin->get_pin(),
|
||||
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
|
||||
.lctrl_mode = PCNT_MODE_KEEP,
|
||||
.hctrl_mode = PCNT_MODE_KEEP,
|
||||
.pos_mode = rising,
|
||||
.neg_mode = falling,
|
||||
.counter_h_lim = 0,
|
||||
.counter_l_lim = 0,
|
||||
.unit = this->pcnt_unit,
|
||||
.channel = this->pcnt_channel,
|
||||
};
|
||||
esp_err_t error = pcnt_unit_config(&pcnt_config);
|
||||
// Create channel configuration
|
||||
pcnt_chan_config_t channel_config = {};
|
||||
channel_config.edge_gpio_num = this->pin->get_pin();
|
||||
channel_config.level_gpio_num = -1; // Not used
|
||||
|
||||
error = pcnt_new_channel(this->pcnt_unit, &channel_config, &this->pcnt_channel);
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Configuring Pulse Counter failed: %s", esp_err_to_name(error));
|
||||
ESP_LOGE(TAG, "Creating Pulse Counter channel failed: %s", esp_err_to_name(error));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this->filter_us != 0) {
|
||||
uint16_t filter_val = std::min(static_cast<unsigned int>(this->filter_us * 80u), 1023u);
|
||||
ESP_LOGCONFIG(TAG, " Filter Value: %" PRIu32 "us (val=%u)", this->filter_us, filter_val);
|
||||
error = pcnt_set_filter_value(this->pcnt_unit, filter_val);
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Setting filter value failed: %s", esp_err_to_name(error));
|
||||
return false;
|
||||
}
|
||||
error = pcnt_filter_enable(this->pcnt_unit);
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Enabling filter failed: %s", esp_err_to_name(error));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
error = pcnt_counter_pause(this->pcnt_unit);
|
||||
// Set edge actions
|
||||
error = pcnt_channel_set_edge_action(this->pcnt_channel, rising_action, falling_action);
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Pausing pulse counter failed: %s", esp_err_to_name(error));
|
||||
ESP_LOGE(TAG, "Setting edge actions failed: %s", esp_err_to_name(error));
|
||||
return false;
|
||||
}
|
||||
error = pcnt_counter_clear(this->pcnt_unit);
|
||||
|
||||
// Set level actions (not used, keep at hold)
|
||||
error =
|
||||
pcnt_channel_set_level_action(this->pcnt_channel, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_KEEP);
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Setting level actions failed: %s", esp_err_to_name(error));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Enable and start the unit
|
||||
error = pcnt_unit_enable(this->pcnt_unit);
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Enabling pulse counter failed: %s", esp_err_to_name(error));
|
||||
return false;
|
||||
}
|
||||
|
||||
error = pcnt_unit_clear_count(this->pcnt_unit);
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Clearing pulse counter failed: %s", esp_err_to_name(error));
|
||||
return false;
|
||||
}
|
||||
error = pcnt_counter_resume(this->pcnt_unit);
|
||||
|
||||
error = pcnt_unit_start(this->pcnt_unit);
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Resuming pulse counter failed: %s", esp_err_to_name(error));
|
||||
ESP_LOGE(TAG, "Starting pulse counter failed: %s", esp_err_to_name(error));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
pulse_counter_t HwPulseCounterStorage::read_raw_value() {
|
||||
pulse_counter_t counter;
|
||||
pcnt_get_counter_value(this->pcnt_unit, &counter);
|
||||
int counter;
|
||||
pcnt_unit_get_count(this->pcnt_unit, &counter);
|
||||
pulse_counter_t ret = counter - this->last_value;
|
||||
this->last_value = counter;
|
||||
return ret;
|
||||
|
||||
@@ -6,10 +6,11 @@
|
||||
|
||||
#include <cinttypes>
|
||||
|
||||
#if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C3)
|
||||
#include <driver/pcnt.h>
|
||||
#if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
|
||||
!defined(USE_ESP32_VARIANT_ESP32C5)
|
||||
#include <driver/pulse_cnt.h>
|
||||
#define HAS_PCNT
|
||||
#endif // defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C3)
|
||||
#endif // HAS_PCNT check
|
||||
|
||||
namespace esphome {
|
||||
namespace pulse_counter {
|
||||
@@ -54,8 +55,8 @@ struct HwPulseCounterStorage : public PulseCounterStorageBase {
|
||||
bool pulse_counter_setup(InternalGPIOPin *pin) override;
|
||||
pulse_counter_t read_raw_value() override;
|
||||
|
||||
pcnt_unit_t pcnt_unit;
|
||||
pcnt_channel_t pcnt_channel;
|
||||
pcnt_unit_handle_t pcnt_unit;
|
||||
pcnt_channel_handle_t pcnt_channel;
|
||||
};
|
||||
#endif // HAS_PCNT
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ static const char *const TAG = "component";
|
||||
namespace {
|
||||
struct ComponentErrorMessage {
|
||||
const Component *component;
|
||||
std::string message;
|
||||
const char *message;
|
||||
};
|
||||
|
||||
struct ComponentPriorityOverride {
|
||||
@@ -146,7 +146,7 @@ void Component::call_dump_config() {
|
||||
if (component_error_messages) {
|
||||
for (const auto &entry : *component_error_messages) {
|
||||
if (entry.component == this) {
|
||||
error_msg = entry.message.c_str();
|
||||
error_msg = entry.message;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -307,33 +307,28 @@ void Component::status_set_warning(const LogString *message) {
|
||||
ESP_LOGW(TAG, "%s set Warning flag: %s", LOG_STR_ARG(this->get_component_log_str()),
|
||||
message ? LOG_STR_ARG(message) : LOG_STR_LITERAL("unspecified"));
|
||||
}
|
||||
void Component::status_set_error(const std::string &message) {
|
||||
void Component::status_set_error(const char *message) {
|
||||
if ((this->component_state_ & STATUS_LED_ERROR) != 0)
|
||||
return;
|
||||
this->component_state_ |= STATUS_LED_ERROR;
|
||||
App.app_state_ |= STATUS_LED_ERROR;
|
||||
ESP_LOGE(TAG, "%s set Error flag: %s", LOG_STR_ARG(this->get_component_log_str()), message.c_str());
|
||||
// Lazy allocate the error messages vector if needed
|
||||
if (!component_error_messages) {
|
||||
component_error_messages = std::make_unique<std::vector<ComponentErrorMessage>>();
|
||||
}
|
||||
// Check if this component already has an error message
|
||||
for (auto &entry : *component_error_messages) {
|
||||
if (entry.component == this) {
|
||||
entry.message = message;
|
||||
return;
|
||||
ESP_LOGE(TAG, "%s set Error flag: %s", LOG_STR_ARG(this->get_component_log_str()),
|
||||
message ? message : LOG_STR_LITERAL("unspecified"));
|
||||
if (message != nullptr) {
|
||||
// Lazy allocate the error messages vector if needed
|
||||
if (!component_error_messages) {
|
||||
component_error_messages = std::make_unique<std::vector<ComponentErrorMessage>>();
|
||||
}
|
||||
// Check if this component already has an error message
|
||||
for (auto &entry : *component_error_messages) {
|
||||
if (entry.component == this) {
|
||||
entry.message = message;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Add new error message
|
||||
component_error_messages->emplace_back(ComponentErrorMessage{this, message});
|
||||
}
|
||||
// Add new error message
|
||||
component_error_messages->emplace_back(ComponentErrorMessage{this, message});
|
||||
}
|
||||
void Component::status_set_error() {
|
||||
// No message version - just set the error flag
|
||||
if ((this->component_state_ & STATUS_LED_ERROR) != 0)
|
||||
return;
|
||||
this->component_state_ |= STATUS_LED_ERROR;
|
||||
App.app_state_ |= STATUS_LED_ERROR;
|
||||
ESP_LOGE(TAG, "%s set Error flag: %s", LOG_STR_ARG(this->get_component_log_str()), LOG_STR_LITERAL("unspecified"));
|
||||
}
|
||||
void Component::status_clear_warning() {
|
||||
if ((this->component_state_ & STATUS_LED_WARNING) == 0)
|
||||
|
||||
@@ -216,8 +216,7 @@ class Component {
|
||||
void status_set_warning(const char *message = nullptr);
|
||||
void status_set_warning(const LogString *message);
|
||||
|
||||
void status_set_error(const std::string &message);
|
||||
void status_set_error();
|
||||
void status_set_error(const char *message = nullptr);
|
||||
|
||||
void status_clear_warning();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user