mirror of
https://github.com/esphome/esphome.git
synced 2024-11-30 18:54:14 +01:00
rename update_ to update_used_space_ and move code from header to the cpp file
+ fixing byte getter.
This commit is contained in:
parent
2725b33835
commit
55a23691d7
2 changed files with 25 additions and 20 deletions
|
@ -81,12 +81,25 @@ void ByteBuffer::flip() {
|
||||||
this->position_ = 0;
|
this->position_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ByteBuffer::is_changed() {
|
||||||
|
bool changed = this->is_changed_;
|
||||||
|
this->is_changed_ = false;
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ByteBuffer::update_used_space_() {
|
||||||
|
this->is_changed_ = true;
|
||||||
|
if (this->used_space_ < this->position_) {
|
||||||
|
this->resize();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Getters
|
/// Getters
|
||||||
uint8_t ByteBuffer::get_uint8() {
|
uint8_t ByteBuffer::get_uint8() {
|
||||||
assert(this->get_remaining() >= 1);
|
assert(this->get_remaining() >= 1);
|
||||||
this->position_++;
|
this->position_++;
|
||||||
this->update_();
|
this->update_used_space_();
|
||||||
return this->data_[this->position_];
|
return this->data_[this->position_ - 1];
|
||||||
}
|
}
|
||||||
uint64_t ByteBuffer::get_uint(size_t length) {
|
uint64_t ByteBuffer::get_uint(size_t length) {
|
||||||
assert(this->get_remaining() >= length);
|
assert(this->get_remaining() >= length);
|
||||||
|
@ -104,7 +117,7 @@ uint64_t ByteBuffer::get_uint(size_t length) {
|
||||||
value |= this->data_[this->position_++];
|
value |= this->data_[this->position_++];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this->update_();
|
this->update_used_space_();
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,7 +141,7 @@ std::vector<uint8_t> ByteBuffer::get_vector(size_t length) {
|
||||||
assert(this->get_remaining() >= length);
|
assert(this->get_remaining() >= length);
|
||||||
auto start = this->data_.begin() + this->position_;
|
auto start = this->data_.begin() + this->position_;
|
||||||
this->position_ += length;
|
this->position_ += length;
|
||||||
this->update_();
|
this->update_used_space_();
|
||||||
return {start, start + length};
|
return {start, start + length};
|
||||||
}
|
}
|
||||||
void ByteBuffer::get_bytes(uint8_t *value, size_t length) {
|
void ByteBuffer::get_bytes(uint8_t *value, size_t length) {
|
||||||
|
@ -137,14 +150,14 @@ void ByteBuffer::get_bytes(uint8_t *value, size_t length) {
|
||||||
while (length-- != 0) {
|
while (length-- != 0) {
|
||||||
*(value + index++) = this->data_[this->position_++];
|
*(value + index++) = this->data_[this->position_++];
|
||||||
}
|
}
|
||||||
this->update_();
|
this->update_used_space_();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Putters
|
/// Putters
|
||||||
void ByteBuffer::put_uint8(uint8_t value) {
|
void ByteBuffer::put_uint8(uint8_t value) {
|
||||||
assert(this->get_remaining() >= 1);
|
assert(this->get_remaining() >= 1);
|
||||||
this->data_[this->position_++] = value;
|
this->data_[this->position_++] = value;
|
||||||
this->update_();
|
this->update_used_space_();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ByteBuffer::put_uint(uint64_t value, size_t length) {
|
void ByteBuffer::put_uint(uint64_t value, size_t length) {
|
||||||
|
@ -162,7 +175,7 @@ void ByteBuffer::put_uint(uint64_t value, size_t length) {
|
||||||
value >>= 8;
|
value >>= 8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this->update_();
|
this->update_used_space_();
|
||||||
}
|
}
|
||||||
void ByteBuffer::put_float(float value) {
|
void ByteBuffer::put_float(float value) {
|
||||||
static_assert(sizeof(float) == sizeof(uint32_t), "Float sizes other than 32 bit not supported");
|
static_assert(sizeof(float) == sizeof(uint32_t), "Float sizes other than 32 bit not supported");
|
||||||
|
@ -178,7 +191,7 @@ void ByteBuffer::put_vector(const std::vector<uint8_t> &value) {
|
||||||
assert(this->get_remaining() >= value.size());
|
assert(this->get_remaining() >= value.size());
|
||||||
std::copy(value.begin(), value.end(), this->data_.begin() + this->position_);
|
std::copy(value.begin(), value.end(), this->data_.begin() + this->position_);
|
||||||
this->position_ += value.size();
|
this->position_ += value.size();
|
||||||
this->update_();
|
this->update_used_space_();
|
||||||
}
|
}
|
||||||
void ByteBuffer::put_bytes(const uint8_t *value, size_t length) {
|
void ByteBuffer::put_bytes(const uint8_t *value, size_t length) {
|
||||||
assert(this->get_remaining() >= length);
|
assert(this->get_remaining() >= length);
|
||||||
|
@ -186,7 +199,7 @@ void ByteBuffer::put_bytes(const uint8_t *value, size_t length) {
|
||||||
while (length-- != 0) {
|
while (length-- != 0) {
|
||||||
this->data_[this->position_++] = static_cast<uint8_t>(*(value + index++));
|
this->data_[this->position_++] = static_cast<uint8_t>(*(value + index++));
|
||||||
}
|
}
|
||||||
this->update_();
|
this->update_used_space_();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
|
@ -139,11 +139,7 @@ class ByteBuffer {
|
||||||
void reset() { this->position_ = this->mark_; }
|
void reset() { this->position_ = this->mark_; }
|
||||||
void resize() { this->used_space_ = this->position_; }
|
void resize() { this->used_space_ = this->position_; }
|
||||||
|
|
||||||
bool is_changed() {
|
bool is_changed();
|
||||||
bool changed = this->is_changed_;
|
|
||||||
this->is_changed_ = false;
|
|
||||||
return changed;
|
|
||||||
}
|
|
||||||
|
|
||||||
ByteBuffer &operator[](size_t idx) {
|
ByteBuffer &operator[](size_t idx) {
|
||||||
this->set_position(idx);
|
this->set_position(idx);
|
||||||
|
@ -152,12 +148,8 @@ class ByteBuffer {
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ByteBuffer(std::vector<uint8_t> const &data) : data_(data), limit_(data.size()) {}
|
ByteBuffer(std::vector<uint8_t> const &data) : data_(data), limit_(data.size()) {}
|
||||||
void update_() {
|
void update_used_space_();
|
||||||
this->is_changed_ = true;
|
|
||||||
if (this->used_space_ < this->position_) {
|
|
||||||
this->resize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
std::vector<uint8_t> data_;
|
std::vector<uint8_t> data_;
|
||||||
Endian endianness_{LITTLE};
|
Endian endianness_{LITTLE};
|
||||||
size_t position_{0};
|
size_t position_{0};
|
||||||
|
|
Loading…
Reference in a new issue