From 4cec5d8351c0bc9f570f925e08f527b58571ed75 Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Wed, 14 Aug 2024 13:37:35 +0200 Subject: [PATCH] Add parameter for endianness to ByteBuffer::wrap --- esphome/core/bytebuffer.cpp | 29 ++++++++++++++++++++++------- esphome/core/bytebuffer.h | 16 ++++++++-------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/esphome/core/bytebuffer.cpp b/esphome/core/bytebuffer.cpp index 9b454cc2ad..93071a75ff 100644 --- a/esphome/core/bytebuffer.cpp +++ b/esphome/core/bytebuffer.cpp @@ -22,32 +22,47 @@ ByteBuffer ByteBuffer::wrap(uint8_t value) { return buffer; } -ByteBuffer ByteBuffer::wrap(uint16_t value) { +ByteBuffer ByteBuffer::wrap(uint16_t value, Endian endianness) { ByteBuffer buffer = ByteBuffer::create(2); + if (endianness == BIG) { + buffer.big_endian(); + } buffer.put_uint16(value); return buffer; } -ByteBuffer ByteBuffer::wrap(uint32_t value) { +ByteBuffer ByteBuffer::wrap(uint32_t value, Endian endianness) { ByteBuffer buffer = ByteBuffer::create(4); + if (endianness == BIG) { + buffer.big_endian(); + } buffer.put_uint32(value); return buffer; } -ByteBuffer ByteBuffer::wrap(uint64_t value) { +ByteBuffer ByteBuffer::wrap(uint64_t value, Endian endianness) { ByteBuffer buffer = ByteBuffer::create(8); + if (endianness == BIG) { + buffer.big_endian(); + } buffer.put_uint64(value); return buffer; } -ByteBuffer ByteBuffer::wrap(float value) { - ByteBuffer buffer = ByteBuffer::create(4); +ByteBuffer ByteBuffer::wrap(float value, Endian endianness) { + ByteBuffer buffer = ByteBuffer::create(sizeof(float)); + if (endianness == BIG) { + buffer.big_endian(); + } buffer.put_float(value); return buffer; } -ByteBuffer ByteBuffer::wrap(double value) { - ByteBuffer buffer = ByteBuffer::create(8); +ByteBuffer ByteBuffer::wrap(double value, Endian endianness) { + ByteBuffer buffer = ByteBuffer::create(sizeof(double)); + if (endianness == BIG) { + buffer.big_endian(); + } buffer.put_double(value); return buffer; } diff --git a/esphome/core/bytebuffer.h b/esphome/core/bytebuffer.h index 549b2431be..961caa6a26 100644 --- a/esphome/core/bytebuffer.h +++ b/esphome/core/bytebuffer.h @@ -46,15 +46,15 @@ class ByteBuffer { static ByteBuffer wrap(const uint8_t *ptr, size_t len); // Convenience functions to create a ByteBuffer from a value static ByteBuffer wrap(const uint8_t value); - static ByteBuffer wrap(const uint16_t value); - static ByteBuffer wrap(const uint32_t value); - static ByteBuffer wrap(const uint64_t value); + static ByteBuffer wrap(const uint16_t value, Endian endianness = LITTLE); + static ByteBuffer wrap(const uint32_t value, Endian endianness = LITTLE); + static ByteBuffer wrap(const uint64_t value, Endian endianness = LITTLE); static ByteBuffer wrap(const int8_t value) { return wrap((uint8_t) value); } - static ByteBuffer wrap(const int16_t value) { return wrap((uint16_t) value); } - static ByteBuffer wrap(const int32_t value) { return wrap((uint32_t) value); } - static ByteBuffer wrap(const int64_t value) { return wrap((uint64_t) value); } - static ByteBuffer wrap(const float value); - static ByteBuffer wrap(const double value); + static ByteBuffer wrap(const int16_t value, Endian endianness = LITTLE) { return wrap((uint16_t) value, endianness); } + static ByteBuffer wrap(const int32_t value, Endian endianness = LITTLE) { return wrap((uint32_t) value, endianness); } + static ByteBuffer wrap(const int64_t value, Endian endianness = LITTLE) { return wrap((uint64_t) value, endianness); } + static ByteBuffer wrap(const float value, Endian endianness = LITTLE); + static ByteBuffer wrap(const double value, Endian endianness = LITTLE); static ByteBuffer wrap(const bool value) { return wrap(value ? (uint8_t) 1 : (uint8_t) 0); } static ByteBuffer wrap(const std::string &value); static ByteBuffer wrap(const std::initializer_list values);