diff --git a/esphome/core/bytebuffer.cpp b/esphome/core/bytebuffer.cpp index 700054ec30..5d5323d875 100644 --- a/esphome/core/bytebuffer.cpp +++ b/esphome/core/bytebuffer.cpp @@ -85,7 +85,7 @@ void ByteBuffer::flip() { uint8_t ByteBuffer::get_uint8() { assert(this->get_remaining() >= 1); this->position_++; - this->update_used_space_(); + this->update_(); return this->data_[this->position_]; } uint64_t ByteBuffer::get_uint(size_t length) { @@ -104,7 +104,7 @@ uint64_t ByteBuffer::get_uint(size_t length) { value |= this->data_[this->position_++]; } } - this->update_used_space_(); + this->update_(); return value; } @@ -128,7 +128,7 @@ std::vector<uint8_t> ByteBuffer::get_vector(size_t length) { assert(this->get_remaining() >= length); auto start = this->data_.begin() + this->position_; this->position_ += length; - this->update_used_space_(); + this->update_(); return {start, start + length}; } void ByteBuffer::get_bytes(uint8_t *value, size_t length) { @@ -137,14 +137,14 @@ void ByteBuffer::get_bytes(uint8_t *value, size_t length) { while (length-- != 0) { *(value + index++) = this->data_[this->position_++]; } - this->update_used_space_(); + this->update_(); } /// Putters void ByteBuffer::put_uint8(uint8_t value) { assert(this->get_remaining() >= 1); this->data_[this->position_++] = value; - this->update_used_space_(); + this->update_(); } void ByteBuffer::put_uint(uint64_t value, size_t length) { @@ -162,7 +162,7 @@ void ByteBuffer::put_uint(uint64_t value, size_t length) { value >>= 8; } } - this->update_used_space_(); + this->update_(); } void ByteBuffer::put_float(float value) { static_assert(sizeof(float) == sizeof(uint32_t), "Float sizes other than 32 bit not supported"); @@ -178,7 +178,7 @@ void ByteBuffer::put_vector(const std::vector<uint8_t> &value) { assert(this->get_remaining() >= value.size()); std::copy(value.begin(), value.end(), this->data_.begin() + this->position_); this->position_ += value.size(); - this->update_used_space_(); + this->update_(); } void ByteBuffer::put_bytes(const uint8_t *value, size_t length) { assert(this->get_remaining() >= length); @@ -186,7 +186,7 @@ void ByteBuffer::put_bytes(const uint8_t *value, size_t length) { while (length-- != 0) { this->data_[this->position_++] = static_cast<uint8_t>(*(value + index++)); } - this->update_used_space_(); + this->update_(); } } // namespace esphome diff --git a/esphome/core/bytebuffer.h b/esphome/core/bytebuffer.h index d566bd3c8c..2ab8ee8db3 100644 --- a/esphome/core/bytebuffer.h +++ b/esphome/core/bytebuffer.h @@ -136,9 +136,16 @@ class ByteBuffer { void reset() { this->position_ = this->mark_; } void resize() { this->used_space_ = this->position_; } + bool is_changed() { + bool changed = this->is_changed_; + this->is_changed_ = false; + return changed; + } + protected: ByteBuffer(std::vector<uint8_t> const &data) : data_(data), limit_(data.size()) {} - void update_used_space_() { + void update_() { + this->is_changed_ = true; if (this->used_space_ < this->position_) { this->resize(); } @@ -149,6 +156,7 @@ class ByteBuffer { size_t mark_{0}; size_t limit_{0}; size_t used_space_{0}; + bool is_changed_{false}; }; } // namespace esphome