diff --git a/esphome/core/bytebuffer.cpp b/esphome/core/bytebuffer.cpp index 698e6fff17..8d63bbea9a 100644 --- a/esphome/core/bytebuffer.cpp +++ b/esphome/core/bytebuffer.cpp @@ -4,9 +4,11 @@ namespace esphome { -ByteBuffer ByteBuffer::create(size_t capacity) { +ByteBuffer ByteBuffer::create(size_t capacity, Endian endianness) { std::vector data(capacity); - return {data}; + ByteBuffer buffer = {data}; + buffer.endianness_ = endianness; + return buffer; } ByteBuffer ByteBuffer::wrap(const uint8_t *ptr, size_t len) { @@ -23,46 +25,31 @@ ByteBuffer ByteBuffer::wrap(uint8_t value) { } ByteBuffer ByteBuffer::wrap(uint16_t value, Endian endianness) { - ByteBuffer buffer = ByteBuffer::create(2); - if (endianness == BIG) { - buffer.big_endian(); - } + ByteBuffer buffer = ByteBuffer::create(2, endianness); buffer.put_uint16(value); return buffer; } ByteBuffer ByteBuffer::wrap(uint32_t value, Endian endianness) { - ByteBuffer buffer = ByteBuffer::create(4); - if (endianness == BIG) { - buffer.big_endian(); - } + ByteBuffer buffer = ByteBuffer::create(4, endianness); buffer.put_uint32(value); return buffer; } ByteBuffer ByteBuffer::wrap(uint64_t value, Endian endianness) { - ByteBuffer buffer = ByteBuffer::create(8); - if (endianness == BIG) { - buffer.big_endian(); - } + ByteBuffer buffer = ByteBuffer::create(8, endianness); buffer.put_uint64(value); return buffer; } ByteBuffer ByteBuffer::wrap(float value, Endian endianness) { - ByteBuffer buffer = ByteBuffer::create(sizeof(float)); - if (endianness == BIG) { - buffer.big_endian(); - } + ByteBuffer buffer = ByteBuffer::create(sizeof(float), endianness); buffer.put_float(value); return buffer; } ByteBuffer ByteBuffer::wrap(double value, Endian endianness) { - ByteBuffer buffer = ByteBuffer::create(sizeof(double)); - if (endianness == BIG) { - buffer.big_endian(); - } + ByteBuffer buffer = ByteBuffer::create(sizeof(double), endianness); buffer.put_double(value); return buffer; } diff --git a/esphome/core/bytebuffer.h b/esphome/core/bytebuffer.h index 0d5acfd7ab..61f3a01c3b 100644 --- a/esphome/core/bytebuffer.h +++ b/esphome/core/bytebuffer.h @@ -31,11 +31,11 @@ enum Endian { LITTLE, BIG }; class ByteBuffer { public: // Default constructor (compatibility with TEMPLATABLE_VALUE) - ByteBuffer() = default; + ByteBuffer() : ByteBuffer(std::vector()) {} /** * Create a new Bytebuffer with the given capacity */ - static ByteBuffer create(size_t capacity); + static ByteBuffer create(size_t capacity, Endian endianness = LITTLE); /** * Wrap an existing vector in a ByteBufffer */