Speaker return bytes written and do not wait for queue (#5182)

This commit is contained in:
Jesse Hills 2023-08-02 16:24:52 +12:00 committed by GitHub
parent 581cb642ff
commit ce8091c14e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 9 deletions

View file

@ -185,7 +185,7 @@ void I2SAudioSpeaker::loop() {
}
}
bool I2SAudioSpeaker::play(const uint8_t *data, size_t length) {
size_t I2SAudioSpeaker::play(const uint8_t *data, size_t length) {
if (this->state_ != speaker::STATE_RUNNING && this->state_ != speaker::STATE_STARTING) {
this->start();
}
@ -197,13 +197,13 @@ bool I2SAudioSpeaker::play(const uint8_t *data, size_t length) {
size_t to_send_length = std::min(remaining, BUFFER_SIZE);
event.len = to_send_length;
memcpy(event.data, data + index, to_send_length);
if (xQueueSend(this->buffer_queue_, &event, 100 / portTICK_PERIOD_MS) == pdTRUE) {
remaining -= to_send_length;
index += to_send_length;
if (xQueueSend(this->buffer_queue_, &event, 0) != pdTRUE) {
return index;
}
App.feed_wdt();
remaining -= to_send_length;
index += to_send_length;
}
return true;
return index;
}
} // namespace i2s_audio

View file

@ -54,7 +54,7 @@ class I2SAudioSpeaker : public Component, public speaker::Speaker, public I2SAud
void start();
void stop() override;
bool play(const uint8_t *data, size_t length) override;
size_t play(const uint8_t *data, size_t length) override;
protected:
void start_();

View file

@ -12,8 +12,8 @@ enum State : uint8_t {
class Speaker {
public:
virtual bool play(const uint8_t *data, size_t length) = 0;
virtual bool play(const std::vector<uint8_t> &data) { return this->play(data.data(), data.size()); }
virtual size_t play(const uint8_t *data, size_t length) = 0;
virtual size_t play(const std::vector<uint8_t> &data) { return this->play(data.data(), data.size()); }
virtual void stop() = 0;