Merge branch 'dev' into refactor_esp32camera

This commit is contained in:
DT-art1 2024-11-13 07:07:07 +01:00 committed by GitHub
commit 3839ca3599
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 387 additions and 176 deletions

View file

@ -204,11 +204,11 @@ void BME68xBSEC2Component::update_subscription_() {
}
void BME68xBSEC2Component::run_() {
this->op_mode_ = this->bsec_settings_.op_mode;
int64_t curr_time_ns = this->get_time_ns_();
if (curr_time_ns < this->next_call_ns_) {
if (curr_time_ns < this->bsec_settings_.next_call) {
return;
}
this->op_mode_ = this->bsec_settings_.op_mode;
uint8_t status;
ESP_LOGV(TAG, "Performing sensor run");
@ -219,57 +219,60 @@ void BME68xBSEC2Component::run_() {
ESP_LOGW(TAG, "Failed to fetch sensor control settings (BSEC2 error code %d)", this->bsec_status_);
return;
}
this->next_call_ns_ = this->bsec_settings_.next_call;
if (this->bsec_settings_.trigger_measurement) {
bme68x_get_conf(&bme68x_conf, &this->bme68x_);
switch (this->bsec_settings_.op_mode) {
case BME68X_FORCED_MODE:
bme68x_get_conf(&bme68x_conf, &this->bme68x_);
bme68x_conf.os_hum = this->bsec_settings_.humidity_oversampling;
bme68x_conf.os_temp = this->bsec_settings_.temperature_oversampling;
bme68x_conf.os_pres = this->bsec_settings_.pressure_oversampling;
bme68x_set_conf(&bme68x_conf, &this->bme68x_);
bme68x_conf.os_hum = this->bsec_settings_.humidity_oversampling;
bme68x_conf.os_temp = this->bsec_settings_.temperature_oversampling;
bme68x_conf.os_pres = this->bsec_settings_.pressure_oversampling;
bme68x_set_conf(&bme68x_conf, &this->bme68x_);
this->bme68x_heatr_conf_.enable = BME68X_ENABLE;
this->bme68x_heatr_conf_.heatr_temp = this->bsec_settings_.heater_temperature;
this->bme68x_heatr_conf_.heatr_dur = this->bsec_settings_.heater_duration;
// status = bme68x_set_op_mode(this->bsec_settings_.op_mode, &this->bme68x_);
status = bme68x_set_heatr_conf(BME68X_FORCED_MODE, &this->bme68x_heatr_conf_, &this->bme68x_);
status = bme68x_set_op_mode(BME68X_FORCED_MODE, &this->bme68x_);
this->op_mode_ = BME68X_FORCED_MODE;
ESP_LOGV(TAG, "Using forced mode");
break;
case BME68X_PARALLEL_MODE:
if (this->op_mode_ != this->bsec_settings_.op_mode) {
bme68x_get_conf(&bme68x_conf, &this->bme68x_);
bme68x_conf.os_hum = this->bsec_settings_.humidity_oversampling;
bme68x_conf.os_temp = this->bsec_settings_.temperature_oversampling;
bme68x_conf.os_pres = this->bsec_settings_.pressure_oversampling;
bme68x_set_conf(&bme68x_conf, &this->bme68x_);
switch (this->bsec_settings_.op_mode) {
case BME68X_FORCED_MODE:
this->bme68x_heatr_conf_.enable = BME68X_ENABLE;
this->bme68x_heatr_conf_.heatr_temp = this->bsec_settings_.heater_temperature;
this->bme68x_heatr_conf_.heatr_dur = this->bsec_settings_.heater_duration;
this->bme68x_heatr_conf_.heatr_temp_prof = this->bsec_settings_.heater_temperature_profile;
this->bme68x_heatr_conf_.heatr_dur_prof = this->bsec_settings_.heater_duration_profile;
this->bme68x_heatr_conf_.profile_len = this->bsec_settings_.heater_profile_len;
this->bme68x_heatr_conf_.shared_heatr_dur =
BSEC_TOTAL_HEAT_DUR -
(bme68x_get_meas_dur(BME68X_PARALLEL_MODE, &bme68x_conf, &this->bme68x_) / INT64_C(1000));
status = bme68x_set_op_mode(this->bsec_settings_.op_mode, &this->bme68x_);
status = bme68x_set_heatr_conf(BME68X_FORCED_MODE, &this->bme68x_heatr_conf_, &this->bme68x_);
status = bme68x_set_op_mode(BME68X_FORCED_MODE, &this->bme68x_);
this->op_mode_ = BME68X_FORCED_MODE;
this->sleep_mode_ = false;
ESP_LOGV(TAG, "Using forced mode");
status = bme68x_set_heatr_conf(BME68X_PARALLEL_MODE, &this->bme68x_heatr_conf_, &this->bme68x_);
break;
case BME68X_PARALLEL_MODE:
if (this->op_mode_ != this->bsec_settings_.op_mode) {
this->bme68x_heatr_conf_.enable = BME68X_ENABLE;
this->bme68x_heatr_conf_.heatr_temp_prof = this->bsec_settings_.heater_temperature_profile;
this->bme68x_heatr_conf_.heatr_dur_prof = this->bsec_settings_.heater_duration_profile;
this->bme68x_heatr_conf_.profile_len = this->bsec_settings_.heater_profile_len;
this->bme68x_heatr_conf_.shared_heatr_dur =
BSEC_TOTAL_HEAT_DUR -
(bme68x_get_meas_dur(BME68X_PARALLEL_MODE, &bme68x_conf, &this->bme68x_) / INT64_C(1000));
status = bme68x_set_heatr_conf(BME68X_PARALLEL_MODE, &this->bme68x_heatr_conf_, &this->bme68x_);
status = bme68x_set_op_mode(BME68X_PARALLEL_MODE, &this->bme68x_);
this->op_mode_ = BME68X_PARALLEL_MODE;
this->sleep_mode_ = false;
ESP_LOGV(TAG, "Using parallel mode");
}
break;
case BME68X_SLEEP_MODE:
if (!this->sleep_mode_) {
bme68x_set_op_mode(BME68X_SLEEP_MODE, &this->bme68x_);
this->sleep_mode_ = true;
ESP_LOGV(TAG, "Using sleep mode");
}
break;
}
status = bme68x_set_op_mode(BME68X_PARALLEL_MODE, &this->bme68x_);
this->op_mode_ = BME68X_PARALLEL_MODE;
ESP_LOGV(TAG, "Using parallel mode");
}
break;
case BME68X_SLEEP_MODE:
if (this->op_mode_ != this->bsec_settings_.op_mode) {
bme68x_set_op_mode(BME68X_SLEEP_MODE, &this->bme68x_);
this->op_mode_ = BME68X_SLEEP_MODE;
ESP_LOGV(TAG, "Using sleep mode");
}
break;
}
if (this->bsec_settings_.trigger_measurement && this->bsec_settings_.op_mode != BME68X_SLEEP_MODE) {
uint32_t meas_dur = 0;
meas_dur = bme68x_get_meas_dur(this->op_mode_, &bme68x_conf, &this->bme68x_);
ESP_LOGV(TAG, "Queueing read in %uus", meas_dur);

View file

@ -113,13 +113,11 @@ class BME68xBSEC2Component : public Component {
struct bme68x_heatr_conf bme68x_heatr_conf_;
uint8_t op_mode_; // operating mode of sensor
bool sleep_mode_;
bsec_library_return_t bsec_status_{BSEC_OK};
int8_t bme68x_status_{BME68X_OK};
int64_t last_time_ms_{0};
uint32_t millis_overflow_counter_{0};
int64_t next_call_ns_{0};
std::queue<std::function<void()>> queue_;

View file

@ -6,7 +6,104 @@ namespace dfplayer {
static const char *const TAG = "dfplayer";
void DFPlayer::next() {
this->ack_set_is_playing_ = true;
ESP_LOGD(TAG, "Playing next track");
this->send_cmd_(0x01);
}
void DFPlayer::previous() {
this->ack_set_is_playing_ = true;
ESP_LOGD(TAG, "Playing previous track");
this->send_cmd_(0x02);
}
void DFPlayer::play_mp3(uint16_t file) {
this->ack_set_is_playing_ = true;
ESP_LOGD(TAG, "Playing file %d in mp3 folder", file);
this->send_cmd_(0x12, file);
}
void DFPlayer::play_file(uint16_t file) {
this->ack_set_is_playing_ = true;
ESP_LOGD(TAG, "Playing file %d", file);
this->send_cmd_(0x03, file);
}
void DFPlayer::play_file_loop(uint16_t file) {
this->ack_set_is_playing_ = true;
ESP_LOGD(TAG, "Playing file %d in loop", file);
this->send_cmd_(0x08, file);
}
void DFPlayer::play_folder_loop(uint16_t folder) {
this->ack_set_is_playing_ = true;
ESP_LOGD(TAG, "Playing folder %d in loop", folder);
this->send_cmd_(0x17, folder);
}
void DFPlayer::volume_up() {
ESP_LOGD(TAG, "Increasing volume");
this->send_cmd_(0x04);
}
void DFPlayer::volume_down() {
ESP_LOGD(TAG, "Decreasing volume");
this->send_cmd_(0x05);
}
void DFPlayer::set_device(Device device) {
ESP_LOGD(TAG, "Setting device to %d", device);
this->send_cmd_(0x09, device);
}
void DFPlayer::set_volume(uint8_t volume) {
ESP_LOGD(TAG, "Setting volume to %d", volume);
this->send_cmd_(0x06, volume);
}
void DFPlayer::set_eq(EqPreset preset) {
ESP_LOGD(TAG, "Setting EQ to %d", preset);
this->send_cmd_(0x07, preset);
}
void DFPlayer::sleep() {
this->ack_reset_is_playing_ = true;
ESP_LOGD(TAG, "Putting DFPlayer to sleep");
this->send_cmd_(0x0A);
}
void DFPlayer::reset() {
this->ack_reset_is_playing_ = true;
ESP_LOGD(TAG, "Resetting DFPlayer");
this->send_cmd_(0x0C);
}
void DFPlayer::start() {
this->ack_set_is_playing_ = true;
ESP_LOGD(TAG, "Starting playback");
this->send_cmd_(0x0D);
}
void DFPlayer::pause() {
this->ack_reset_is_playing_ = true;
ESP_LOGD(TAG, "Pausing playback");
this->send_cmd_(0x0E);
}
void DFPlayer::stop() {
this->ack_reset_is_playing_ = true;
ESP_LOGD(TAG, "Stopping playback");
this->send_cmd_(0x16);
}
void DFPlayer::random() {
this->ack_set_is_playing_ = true;
ESP_LOGD(TAG, "Playing random file");
this->send_cmd_(0x18);
}
void DFPlayer::play_folder(uint16_t folder, uint16_t file) {
ESP_LOGD(TAG, "Playing file %d in folder %d", file, folder);
if (folder < 100 && file < 256) {
this->ack_set_is_playing_ = true;
this->send_cmd_(0x0F, (uint8_t) folder, (uint8_t) file);
@ -29,7 +126,7 @@ void DFPlayer::send_cmd_(uint8_t cmd, uint16_t argument) {
this->sent_cmd_ = cmd;
ESP_LOGD(TAG, "Send Command %#02x arg %#04x", cmd, argument);
ESP_LOGV(TAG, "Send Command %#02x arg %#04x", cmd, argument);
this->write_array(buffer, 10);
}
@ -101,9 +198,37 @@ void DFPlayer::loop() {
ESP_LOGV(TAG, "Nack");
this->ack_set_is_playing_ = false;
this->ack_reset_is_playing_ = false;
if (argument == 6) {
ESP_LOGV(TAG, "File not found");
this->is_playing_ = false;
switch (argument) {
case 0x01:
ESP_LOGE(TAG, "Module is busy or uninitialized");
break;
case 0x02:
ESP_LOGE(TAG, "Module is in sleep mode");
break;
case 0x03:
ESP_LOGE(TAG, "Serial receive error");
break;
case 0x04:
ESP_LOGE(TAG, "Checksum incorrect");
break;
case 0x05:
ESP_LOGE(TAG, "Specified track is out of current track scope");
this->is_playing_ = false;
break;
case 0x06:
ESP_LOGE(TAG, "Specified track is not found");
this->is_playing_ = false;
break;
case 0x07:
ESP_LOGE(TAG, "Insertion error (an inserting operation only can be done when a track is being played)");
break;
case 0x08:
ESP_LOGE(TAG, "SD card reading failed (SD card pulled out or damaged)");
break;
case 0x09:
ESP_LOGE(TAG, "Entered into sleep mode");
this->is_playing_ = false;
break;
}
break;
case 0x41:
@ -113,12 +238,13 @@ void DFPlayer::loop() {
this->ack_set_is_playing_ = false;
this->ack_reset_is_playing_ = false;
break;
case 0x3D: // Playback finished
case 0x3D:
ESP_LOGV(TAG, "Playback finished");
this->is_playing_ = false;
this->on_finished_playback_callback_.call();
break;
default:
ESP_LOGD(TAG, "Command %#02x arg %#04x", cmd, argument);
ESP_LOGV(TAG, "Received unknown cmd %#02x arg %#04x", cmd, argument);
}
this->sent_cmd_ = 0;
this->read_pos_ = 0;

View file

@ -23,64 +23,30 @@ enum Device {
TF_CARD = 2,
};
// See the datasheet here:
// https://github.com/DFRobot/DFRobotDFPlayerMini/blob/master/doc/FN-M16P%2BEmbedded%2BMP3%2BAudio%2BModule%2BDatasheet.pdf
class DFPlayer : public uart::UARTDevice, public Component {
public:
void loop() override;
void next() {
this->ack_set_is_playing_ = true;
this->send_cmd_(0x01);
}
void previous() {
this->ack_set_is_playing_ = true;
this->send_cmd_(0x02);
}
void play_mp3(uint16_t file) {
this->ack_set_is_playing_ = true;
this->send_cmd_(0x12, file);
}
void play_file(uint16_t file) {
this->ack_set_is_playing_ = true;
this->send_cmd_(0x03, file);
}
void play_file_loop(uint16_t file) {
this->ack_set_is_playing_ = true;
this->send_cmd_(0x08, file);
}
void next();
void previous();
void play_mp3(uint16_t file);
void play_file(uint16_t file);
void play_file_loop(uint16_t file);
void play_folder(uint16_t folder, uint16_t file);
void play_folder_loop(uint16_t folder) {
this->ack_set_is_playing_ = true;
this->send_cmd_(0x17, folder);
}
void volume_up() { this->send_cmd_(0x04); }
void volume_down() { this->send_cmd_(0x05); }
void set_device(Device device) { this->send_cmd_(0x09, device); }
void set_volume(uint8_t volume) { this->send_cmd_(0x06, volume); }
void set_eq(EqPreset preset) { this->send_cmd_(0x07, preset); }
void sleep() {
this->ack_reset_is_playing_ = true;
this->send_cmd_(0x0A);
}
void reset() {
this->ack_reset_is_playing_ = true;
this->send_cmd_(0x0C);
}
void start() {
this->ack_set_is_playing_ = true;
this->send_cmd_(0x0D);
}
void pause() {
this->ack_reset_is_playing_ = true;
this->send_cmd_(0x0E);
}
void stop() {
this->ack_reset_is_playing_ = true;
this->send_cmd_(0x16);
}
void random() {
this->ack_set_is_playing_ = true;
this->send_cmd_(0x18);
}
void play_folder_loop(uint16_t folder);
void volume_up();
void volume_down();
void set_device(Device device);
void set_volume(uint8_t volume);
void set_eq(EqPreset preset);
void sleep();
void reset();
void start();
void pause();
void stop();
void random();
bool is_playing() { return is_playing_; }
void dump_config() override;

View file

@ -24,9 +24,10 @@ I2SAudioSpeaker = i2s_audio_ns.class_(
"I2SAudioSpeaker", cg.Component, speaker.Speaker, I2SAudioOut
)
CONF_BUFFER_DURATION = "buffer_duration"
CONF_DAC_TYPE = "dac_type"
CONF_I2S_COMM_FMT = "i2s_comm_fmt"
CONF_NEVER = "never"
i2s_dac_mode_t = cg.global_ns.enum("i2s_dac_mode_t")
INTERNAL_DAC_OPTIONS = {
@ -73,8 +74,12 @@ BASE_SCHEMA = (
.extend(
{
cv.Optional(
CONF_TIMEOUT, default="500ms"
CONF_BUFFER_DURATION, default="500ms"
): cv.positive_time_period_milliseconds,
cv.Optional(CONF_TIMEOUT, default="500ms"): cv.Any(
cv.positive_time_period_milliseconds,
cv.one_of(CONF_NEVER, lower=True),
),
}
)
.extend(cv.COMPONENT_SCHEMA)
@ -116,4 +121,6 @@ async def to_code(config):
else:
cg.add(var.set_dout_pin(config[CONF_I2S_DOUT_PIN]))
cg.add(var.set_i2s_comm_fmt(config[CONF_I2S_COMM_FMT]))
cg.add(var.set_timeout(config[CONF_TIMEOUT]))
if config[CONF_TIMEOUT] != CONF_NEVER:
cg.add(var.set_timeout(config[CONF_TIMEOUT]))
cg.add(var.set_buffer_duration(config[CONF_BUFFER_DURATION]))

View file

@ -13,21 +13,22 @@
namespace esphome {
namespace i2s_audio {
static const size_t DMA_BUFFER_SIZE = 512;
static const uint8_t DMA_BUFFER_DURATION_MS = 15;
static const size_t DMA_BUFFERS_COUNT = 4;
static const size_t FRAMES_IN_ALL_DMA_BUFFERS = DMA_BUFFER_SIZE * DMA_BUFFERS_COUNT;
static const size_t RING_BUFFER_SAMPLES = 8192;
static const size_t TASK_DELAY_MS = 10;
static const size_t TASK_DELAY_MS = DMA_BUFFER_DURATION_MS * DMA_BUFFERS_COUNT / 2;
static const size_t TASK_STACK_SIZE = 4096;
static const ssize_t TASK_PRIORITY = 23;
static const size_t I2S_EVENT_QUEUE_COUNT = DMA_BUFFERS_COUNT + 1;
static const char *const TAG = "i2s_audio.speaker";
enum SpeakerEventGroupBits : uint32_t {
COMMAND_START = (1 << 0), // Starts the main task purpose
COMMAND_STOP = (1 << 1), // stops the main task
COMMAND_STOP_GRACEFULLY = (1 << 2), // Stops the task once all data has been written
MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE = (1 << 5), // Locks the ring buffer when not set
COMMAND_START = (1 << 0), // starts the speaker task
COMMAND_STOP = (1 << 1), // stops the speaker task
COMMAND_STOP_GRACEFULLY = (1 << 2), // Stops the speaker task once all data has been written
STATE_STARTING = (1 << 10),
STATE_RUNNING = (1 << 11),
STATE_STOPPING = (1 << 12),
@ -91,15 +92,21 @@ static const std::vector<int16_t> Q15_VOLUME_SCALING_FACTORS = {
void I2SAudioSpeaker::setup() {
ESP_LOGCONFIG(TAG, "Setting up I2S Audio Speaker...");
if (this->event_group_ == nullptr) {
this->event_group_ = xEventGroupCreate();
}
this->event_group_ = xEventGroupCreate();
if (this->event_group_ == nullptr) {
ESP_LOGE(TAG, "Failed to create event group");
this->mark_failed();
return;
}
this->i2s_event_queue_ = xQueueCreate(I2S_EVENT_QUEUE_COUNT, sizeof(i2s_event_t));
if (this->i2s_event_queue_ == nullptr) {
ESP_LOGE(TAG, "Failed to create I2S event queue");
this->mark_failed();
return;
}
}
void I2SAudioSpeaker::loop() {
@ -199,23 +206,17 @@ size_t I2SAudioSpeaker::play(const uint8_t *data, size_t length, TickType_t tick
this->start();
}
// Wait for the ring buffer to be available
uint32_t event_bits =
xEventGroupWaitBits(this->event_group_, SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE, pdFALSE,
pdFALSE, pdMS_TO_TICKS(TASK_DELAY_MS));
size_t bytes_written = 0;
if ((this->state_ == speaker::STATE_RUNNING) && (this->audio_ring_buffer_.use_count() == 1)) {
// Only one owner of the ring buffer (the speaker task), so the ring buffer is allocated and no other components are
// attempting to write to it.
if (event_bits & SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE) {
// Ring buffer is available to write
// Lock the ring buffer, write to it, then unlock it
xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE);
size_t bytes_written = this->audio_ring_buffer_->write_without_replacement((void *) data, length, ticks_to_wait);
xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE);
return bytes_written;
// Temporarily share ownership of the ring buffer so it won't be deallocated while writing
std::shared_ptr<RingBuffer> temp_ring_buffer = this->audio_ring_buffer_;
bytes_written = temp_ring_buffer->write_without_replacement((void *) data, length, ticks_to_wait);
}
return 0;
return bytes_written;
}
bool I2SAudioSpeaker::has_buffered_data() const {
@ -246,10 +247,12 @@ void I2SAudioSpeaker::speaker_task(void *params) {
const ssize_t bytes_per_sample = audio_stream_info.get_bytes_per_sample();
const uint8_t number_of_channels = audio_stream_info.channels;
const size_t dma_buffers_size = FRAMES_IN_ALL_DMA_BUFFERS * bytes_per_sample * number_of_channels;
const size_t dma_buffers_size = DMA_BUFFERS_COUNT * DMA_BUFFER_DURATION_MS * this_speaker->sample_rate_ / 1000 *
bytes_per_sample * number_of_channels;
const size_t ring_buffer_size =
this_speaker->buffer_duration_ms_ * this_speaker->sample_rate_ / 1000 * bytes_per_sample * number_of_channels;
if (this_speaker->send_esp_err_to_event_group_(
this_speaker->allocate_buffers_(dma_buffers_size, RING_BUFFER_SAMPLES * bytes_per_sample))) {
if (this_speaker->send_esp_err_to_event_group_(this_speaker->allocate_buffers_(dma_buffers_size, ring_buffer_size))) {
// Failed to allocate buffers
xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::ERR_ESP_NO_MEM);
this_speaker->delete_task_(dma_buffers_size);
@ -258,9 +261,6 @@ void I2SAudioSpeaker::speaker_task(void *params) {
if (this_speaker->send_esp_err_to_event_group_(this_speaker->start_i2s_driver_())) {
// Failed to start I2S driver
this_speaker->delete_task_(dma_buffers_size);
} else {
// Ring buffer is allocated, so indicate its can be written to
xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE);
}
if (!this_speaker->send_esp_err_to_event_group_(this_speaker->reconfigure_i2s_stream_info_(audio_stream_info))) {
@ -270,8 +270,10 @@ void I2SAudioSpeaker::speaker_task(void *params) {
bool stop_gracefully = false;
uint32_t last_data_received_time = millis();
bool tx_dma_underflow = false;
while ((millis() - last_data_received_time) <= this_speaker->timeout_) {
while (!this_speaker->timeout_.has_value() ||
(millis() - last_data_received_time) <= this_speaker->timeout_.value()) {
event_group_bits = xEventGroupGetBits(this_speaker->event_group_);
if (event_group_bits & SpeakerEventGroupBits::COMMAND_STOP) {
@ -281,12 +283,18 @@ void I2SAudioSpeaker::speaker_task(void *params) {
stop_gracefully = true;
}
i2s_event_t i2s_event;
while (xQueueReceive(this_speaker->i2s_event_queue_, &i2s_event, 0)) {
if (i2s_event.type == I2S_EVENT_TX_Q_OVF) {
tx_dma_underflow = true;
}
}
size_t bytes_to_read = dma_buffers_size;
size_t bytes_read = this_speaker->audio_ring_buffer_->read((void *) this_speaker->data_buffer_, bytes_to_read,
pdMS_TO_TICKS(TASK_DELAY_MS));
if (bytes_read > 0) {
last_data_received_time = millis();
size_t bytes_written = 0;
if ((audio_stream_info.bits_per_sample == 16) && (this_speaker->q15_volume_factor_ < INT16_MAX)) {
@ -307,15 +315,13 @@ void I2SAudioSpeaker::speaker_task(void *params) {
if (bytes_written != bytes_read) {
xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::ERR_ESP_INVALID_SIZE);
}
tx_dma_underflow = false;
last_data_received_time = millis();
} else {
// No data received
if (stop_gracefully) {
if (stop_gracefully && tx_dma_underflow) {
break;
}
i2s_zero_dma_buffer(this_speaker->parent_->get_port());
}
}
} else {
@ -326,7 +332,6 @@ void I2SAudioSpeaker::speaker_task(void *params) {
xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::STATE_STOPPING);
i2s_stop(this_speaker->parent_->get_port());
i2s_driver_uninstall(this_speaker->parent_->get_port());
this_speaker->parent_->unlock();
@ -402,8 +407,8 @@ esp_err_t I2SAudioSpeaker::allocate_buffers_(size_t data_buffer_size, size_t rin
return ESP_ERR_NO_MEM;
}
if (this->audio_ring_buffer_ == nullptr) {
// Allocate ring buffer
if (this->audio_ring_buffer_.use_count() == 0) {
// Allocate ring buffer. Uses a shared_ptr to ensure it isn't improperly deallocated.
this->audio_ring_buffer_ = RingBuffer::create(ring_buffer_size);
}
@ -419,6 +424,8 @@ esp_err_t I2SAudioSpeaker::start_i2s_driver_() {
return ESP_ERR_INVALID_STATE;
}
int dma_buffer_length = DMA_BUFFER_DURATION_MS * this->sample_rate_ / 1000;
i2s_driver_config_t config = {
.mode = (i2s_mode_t) (this->i2s_mode_ | I2S_MODE_TX),
.sample_rate = this->sample_rate_,
@ -427,7 +434,7 @@ esp_err_t I2SAudioSpeaker::start_i2s_driver_() {
.communication_format = this->i2s_comm_fmt_,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = DMA_BUFFERS_COUNT,
.dma_buf_len = DMA_BUFFER_SIZE,
.dma_buf_len = dma_buffer_length,
.use_apll = this->use_apll_,
.tx_desc_auto_clear = true,
.fixed_mclk = I2S_PIN_NO_CHANGE,
@ -448,7 +455,8 @@ esp_err_t I2SAudioSpeaker::start_i2s_driver_() {
}
#endif
esp_err_t err = i2s_driver_install(this->parent_->get_port(), &config, 0, nullptr);
esp_err_t err =
i2s_driver_install(this->parent_->get_port(), &config, I2S_EVENT_QUEUE_COUNT, &this->i2s_event_queue_);
if (err != ESP_OK) {
// Failed to install the driver, so unlock the I2S port
this->parent_->unlock();
@ -502,16 +510,7 @@ esp_err_t I2SAudioSpeaker::reconfigure_i2s_stream_info_(audio::AudioStreamInfo &
}
void I2SAudioSpeaker::delete_task_(size_t buffer_size) {
if (this->audio_ring_buffer_ != nullptr) {
xEventGroupWaitBits(this->event_group_,
MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE, // Bit message to read
pdFALSE, // Don't clear the bits on exit
pdTRUE, // Don't wait for all the bits,
portMAX_DELAY); // Block indefinitely until a command bit is set
this->audio_ring_buffer_.reset(); // Deallocates the ring buffer stored in the unique_ptr
this->audio_ring_buffer_ = nullptr;
}
this->audio_ring_buffer_.reset(); // Releases onwership of the shared_ptr
if (this->data_buffer_ != nullptr) {
ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
@ -520,6 +519,7 @@ void I2SAudioSpeaker::delete_task_(size_t buffer_size) {
}
xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::STATE_STOPPED);
xQueueReset(this->i2s_event_queue_);
this->task_created_ = false;
vTaskDelete(nullptr);

View file

@ -7,6 +7,7 @@
#include <driver/i2s.h>
#include <freertos/event_groups.h>
#include <freertos/queue.h>
#include <freertos/FreeRTOS.h>
#include "esphome/components/audio/audio.h"
@ -27,6 +28,7 @@ class I2SAudioSpeaker : public I2SAudioOut, public speaker::Speaker, public Comp
void setup() override;
void loop() override;
void set_buffer_duration(uint32_t buffer_duration_ms) { this->buffer_duration_ms_ = buffer_duration_ms; }
void set_timeout(uint32_t ms) { this->timeout_ = ms; }
void set_dout_pin(uint8_t pin) { this->dout_pin_ = pin; }
#if SOC_I2S_SUPPORTS_DAC
@ -117,10 +119,14 @@ class I2SAudioSpeaker : public I2SAudioOut, public speaker::Speaker, public Comp
TaskHandle_t speaker_task_handle_{nullptr};
EventGroupHandle_t event_group_{nullptr};
uint8_t *data_buffer_;
std::unique_ptr<RingBuffer> audio_ring_buffer_;
QueueHandle_t i2s_event_queue_;
uint32_t timeout_;
uint8_t *data_buffer_;
std::shared_ptr<RingBuffer> audio_ring_buffer_;
uint32_t buffer_duration_ms_;
optional<uint32_t> timeout_;
uint8_t dout_pin_;
bool task_created_{false};

View file

@ -25,6 +25,8 @@ from .const import (
CONF_MODBUS_CONTROLLER_ID,
CONF_OFFLINE_SKIP_UPDATES,
CONF_ON_COMMAND_SENT,
CONF_ON_ONLINE,
CONF_ON_OFFLINE,
CONF_REGISTER_COUNT,
CONF_REGISTER_TYPE,
CONF_RESPONSE_SIZE,
@ -114,6 +116,14 @@ ModbusCommandSentTrigger = modbus_controller_ns.class_(
"ModbusCommandSentTrigger", automation.Trigger.template(cg.int_, cg.int_)
)
ModbusOnlineTrigger = modbus_controller_ns.class_(
"ModbusOnlineTrigger", automation.Trigger.template(cg.int_, cg.int_)
)
ModbusOfflineTrigger = modbus_controller_ns.class_(
"ModbusOfflineTrigger", automation.Trigger.template(cg.int_, cg.int_)
)
_LOGGER = logging.getLogger(__name__)
ModbusServerRegisterSchema = cv.Schema(
@ -146,6 +156,16 @@ CONFIG_SCHEMA = cv.All(
),
}
),
cv.Optional(CONF_ON_ONLINE): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ModbusOnlineTrigger),
}
),
cv.Optional(CONF_ON_OFFLINE): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ModbusOnlineTrigger),
}
),
}
)
.extend(cv.polling_component_schema("60s"))
@ -284,7 +304,17 @@ async def to_code(config):
for conf in config.get(CONF_ON_COMMAND_SENT, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(
trigger, [(int, "function_code"), (int, "address")], conf
trigger, [(cg.int_, "function_code"), (cg.int_, "address")], conf
)
for conf in config.get(CONF_ON_ONLINE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(
trigger, [(cg.int_, "function_code"), (cg.int_, "address")], conf
)
for conf in config.get(CONF_ON_OFFLINE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(
trigger, [(cg.int_, "function_code"), (cg.int_, "address")], conf
)

View file

@ -15,5 +15,21 @@ class ModbusCommandSentTrigger : public Trigger<int, int> {
}
};
class ModbusOnlineTrigger : public Trigger<int, int> {
public:
ModbusOnlineTrigger(ModbusController *a_modbuscontroller) {
a_modbuscontroller->add_on_online_callback(
[this](int function_code, int address) { this->trigger(function_code, address); });
}
};
class ModbusOfflineTrigger : public Trigger<int, int> {
public:
ModbusOfflineTrigger(ModbusController *a_modbuscontroller) {
a_modbuscontroller->add_on_offline_callback(
[this](int function_code, int address) { this->trigger(function_code, address); });
}
};
} // namespace modbus_controller
} // namespace esphome

View file

@ -9,6 +9,8 @@ CONF_MAX_CMD_RETRIES = "max_cmd_retries"
CONF_MODBUS_CONTROLLER_ID = "modbus_controller_id"
CONF_MODBUS_FUNCTIONCODE = "modbus_functioncode"
CONF_ON_COMMAND_SENT = "on_command_sent"
CONF_ON_ONLINE = "on_online"
CONF_ON_OFFLINE = "on_offline"
CONF_RAW_ENCODE = "raw_encode"
CONF_REGISTER_COUNT = "register_count"
CONF_REGISTER_TYPE = "register_type"

View file

@ -32,8 +32,10 @@ bool ModbusController::send_next_command_() {
r.skip_updates_counter = this->offline_skip_updates_;
}
}
this->module_offline_ = true;
this->offline_callback_.call((int) command->function_code, command->register_address);
}
this->module_offline_ = true;
ESP_LOGD(TAG, "Modbus command to device=%d register=0x%02X no response received - removed from send queue",
this->address_, command->register_address);
this->command_queue_.pop_front();
@ -68,8 +70,10 @@ void ModbusController::on_modbus_data(const std::vector<uint8_t> &data) {
r.skip_updates_counter = 0;
}
}
// Restore module online state
this->module_offline_ = false;
this->online_callback_.call((int) current_command->function_code, current_command->register_address);
}
this->module_offline_ = false;
// Move the commandItem to the response queue
current_command->payload = data;
@ -670,5 +674,13 @@ void ModbusController::add_on_command_sent_callback(std::function<void(int, int)
this->command_sent_callback_.add(std::move(callback));
}
void ModbusController::add_on_online_callback(std::function<void(int, int)> &&callback) {
this->online_callback_.add(std::move(callback));
}
void ModbusController::add_on_offline_callback(std::function<void(int, int)> &&callback) {
this->offline_callback_.add(std::move(callback));
}
} // namespace modbus_controller
} // namespace esphome

View file

@ -468,6 +468,10 @@ class ModbusController : public PollingComponent, public modbus::ModbusDevice {
bool get_module_offline() { return module_offline_; }
/// Set callback for commands
void add_on_command_sent_callback(std::function<void(int, int)> &&callback);
/// Set callback for online changes
void add_on_online_callback(std::function<void(int, int)> &&callback);
/// Set callback for offline changes
void add_on_offline_callback(std::function<void(int, int)> &&callback);
/// called by esphome generated code to set the max_cmd_retries.
void set_max_cmd_retries(uint8_t max_cmd_retries) { this->max_cmd_retries_ = max_cmd_retries; }
/// get how many times a command will be (re)sent if no response is received
@ -508,7 +512,12 @@ class ModbusController : public PollingComponent, public modbus::ModbusDevice {
uint16_t offline_skip_updates_;
/// How many times we will retry a command if we get no response
uint8_t max_cmd_retries_{4};
/// Command sent callback
CallbackManager<void(int, int)> command_sent_callback_{};
/// Server online callback
CallbackManager<void(int, int)> online_callback_{};
/// Server offline callback
CallbackManager<void(int, int)> offline_callback_{};
};
/** Convert vector<uint8_t> response payload to float.

View file

@ -371,11 +371,11 @@ void OpenthermHub::dump_config() {
ESP_LOGCONFIG(TAG, " Numbers: %s", SHOW(OPENTHERM_NUMBER_LIST(ID, )));
ESP_LOGCONFIG(TAG, " Initial requests:");
for (auto type : this->initial_messages_) {
ESP_LOGCONFIG(TAG, " - %d", type);
ESP_LOGCONFIG(TAG, " - %d (%s)", type, this->opentherm_->message_id_to_str((type)));
}
ESP_LOGCONFIG(TAG, " Repeating requests:");
for (auto type : this->repeating_messages_) {
ESP_LOGCONFIG(TAG, " - %d", type);
ESP_LOGCONFIG(TAG, " - %d (%s)", type, this->opentherm_->message_id_to_str((type)));
}
}

View file

@ -483,6 +483,8 @@ const char *OpenTherm::message_id_to_str(MessageId id) {
TO_STRING_MEMBER(EXHAUST_TEMP)
TO_STRING_MEMBER(FAN_SPEED)
TO_STRING_MEMBER(FLAME_CURRENT)
TO_STRING_MEMBER(ROOM_TEMP_CH2)
TO_STRING_MEMBER(REL_HUMIDITY)
TO_STRING_MEMBER(DHW_BOUNDS)
TO_STRING_MEMBER(CH_BOUNDS)
TO_STRING_MEMBER(OTC_CURVE_BOUNDS)
@ -492,14 +494,39 @@ const char *OpenTherm::message_id_to_str(MessageId id) {
TO_STRING_MEMBER(HVAC_STATUS)
TO_STRING_MEMBER(REL_VENT_SETPOINT)
TO_STRING_MEMBER(DEVICE_VENT)
TO_STRING_MEMBER(HVAC_VER_ID)
TO_STRING_MEMBER(REL_VENTILATION)
TO_STRING_MEMBER(REL_HUMID_EXHAUST)
TO_STRING_MEMBER(EXHAUST_CO2)
TO_STRING_MEMBER(SUPPLY_INLET_TEMP)
TO_STRING_MEMBER(SUPPLY_OUTLET_TEMP)
TO_STRING_MEMBER(EXHAUST_INLET_TEMP)
TO_STRING_MEMBER(EXHAUST_OUTLET_TEMP)
TO_STRING_MEMBER(EXHAUST_FAN_SPEED)
TO_STRING_MEMBER(SUPPLY_FAN_SPEED)
TO_STRING_MEMBER(REMOTE_VENTILATION_PARAM)
TO_STRING_MEMBER(NOM_REL_VENTILATION)
TO_STRING_MEMBER(HVAC_NUM_TSP)
TO_STRING_MEMBER(HVAC_IDX_TSP)
TO_STRING_MEMBER(HVAC_FHB_SIZE)
TO_STRING_MEMBER(HVAC_FHB_IDX)
TO_STRING_MEMBER(RF_SIGNAL)
TO_STRING_MEMBER(DHW_MODE)
TO_STRING_MEMBER(OVERRIDE_FUNC)
TO_STRING_MEMBER(SOLAR_MODE_FLAGS)
TO_STRING_MEMBER(SOLAR_ASF)
TO_STRING_MEMBER(SOLAR_VERSION_ID)
TO_STRING_MEMBER(SOLAR_PRODUCT_ID)
TO_STRING_MEMBER(SOLAR_NUM_TSP)
TO_STRING_MEMBER(SOLAR_IDX_TSP)
TO_STRING_MEMBER(SOLAR_FHB_SIZE)
TO_STRING_MEMBER(SOLAR_FHB_IDX)
TO_STRING_MEMBER(SOLAR_STARTS)
TO_STRING_MEMBER(SOLAR_HOURS)
TO_STRING_MEMBER(SOLAR_ENERGY)
TO_STRING_MEMBER(SOLAR_TOTAL_ENERGY)
TO_STRING_MEMBER(FAILED_BURNER_STARTS)
TO_STRING_MEMBER(BURNER_FLAME_LOW)
TO_STRING_MEMBER(OEM_DIAGNOSTIC)
TO_STRING_MEMBER(BURNER_STARTS)
TO_STRING_MEMBER(CH_PUMP_STARTS)

View file

@ -59,6 +59,9 @@ class Sun {
void set_latitude(double latitude) { location_.latitude = latitude; }
void set_longitude(double longitude) { location_.longitude = longitude; }
// Check if the sun is above the horizon, with a default elevation angle of -0.83333 (standard for sunrise/set).
bool is_above_horizon(double elevation = -0.83333) { return this->elevation() > elevation; }
optional<ESPTime> sunrise(double elevation);
optional<ESPTime> sunset(double elevation);
optional<ESPTime> sunrise(ESPTime date, double elevation);

View file

@ -1,6 +1,6 @@
"""Constants used by esphome."""
__version__ = "2024.11.0-dev"
__version__ = "2024.12.0-dev"
ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_"
VALID_SUBSTITUTIONS_CHARACTERS = (

View file

@ -46,7 +46,7 @@ size_t RingBuffer::read(void *data, size_t len, TickType_t ticks_to_wait) {
return bytes_read;
}
size_t RingBuffer::write(void *data, size_t len) {
size_t RingBuffer::write(const void *data, size_t len) {
size_t free = this->free();
if (free < len) {
size_t needed = len - free;
@ -56,7 +56,7 @@ size_t RingBuffer::write(void *data, size_t len) {
return xStreamBufferSend(this->handle_, data, len, 0);
}
size_t RingBuffer::write_without_replacement(void *data, size_t len, TickType_t ticks_to_wait) {
size_t RingBuffer::write_without_replacement(const void *data, size_t len, TickType_t ticks_to_wait) {
return xStreamBufferSend(this->handle_, data, len, ticks_to_wait);
}

View file

@ -37,7 +37,7 @@ class RingBuffer {
* @param len Number of bytes to write
* @return Number of bytes written
*/
size_t write(void *data, size_t len);
size_t write(const void *data, size_t len);
/**
* @brief Writes to the ring buffer without overwriting oldest data.
@ -50,7 +50,7 @@ class RingBuffer {
* @param ticks_to_wait Maximum number of FreeRTOS ticks to wait (default: 0)
* @return Number of bytes written
*/
size_t write_without_replacement(void *data, size_t len, TickType_t ticks_to_wait = 0);
size_t write_without_replacement(const void *data, size_t len, TickType_t ticks_to_wait = 0);
/**
* @brief Returns the number of available bytes in the ring buffer.

View file

@ -21,6 +21,9 @@ modbus_controller:
address: 0x2
modbus_id: mod_bus1
allow_duplicate_commands: false
on_online:
then:
logger.log: "Module Online"
- id: modbus_controller2
address: 0x2
modbus_id: mod_bus2

View file

@ -13,4 +13,7 @@ modbus_controller:
address: 0x2
modbus_id: mod_bus1
allow_duplicate_commands: true
on_offline:
then:
logger.log: "Module Offline"
max_cmd_retries: 10