diff --git a/CHANGELOG.md b/CHANGELOG.md index e8e071b1c..2039ea9bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ## [14.5.0.3] ### Added - Extend command `GPIO` with different display options and allowing updating of module GPIO's in one go +- Berry `bytes.add()` now accepts 3-bytes values ### Breaking Changed diff --git a/lib/libesp32/berry/src/be_byteslib.c b/lib/libesp32/berry/src/be_byteslib.c index 1ddd027d5..d692286d9 100644 --- a/lib/libesp32/berry/src/be_byteslib.c +++ b/lib/libesp32/berry/src/be_byteslib.c @@ -258,6 +258,26 @@ static size_t buf_add2_be(buf_impl* attr, const uint16_t data) // append 16 bits return attr->len; } +static size_t buf_add3_le(buf_impl* attr, const uint32_t data) // append 32 bits value +{ + if (attr->len < attr->size - 2) { // do we have room for 4 bytes + attr->bufptr[attr->len++] = data; + attr->bufptr[attr->len++] = data >> 8; + attr->bufptr[attr->len++] = data >> 16; + } + return attr->len; +} + +size_t buf_add3_be(buf_impl* attr, const uint32_t data) // append 32 bits value +{ + if (attr->len < attr->size - 2) { // do we have room for 4 bytes + attr->bufptr[attr->len++] = data >> 16; + attr->bufptr[attr->len++] = data >> 8; + attr->bufptr[attr->len++] = data; + } + return attr->len; +} + static size_t buf_add4_le(buf_impl* attr, const uint32_t data) // append 32 bits value { if (attr->len < attr->size - 3) { // do we have room for 4 bytes @@ -838,10 +858,12 @@ static int m_add(bvm *vm) case -1: /* fallback below */ case 1: buf_add1(&attr, v); break; case 2: buf_add2_le(&attr, v); break; + case 3: buf_add3_le(&attr, v); break; case 4: buf_add4_le(&attr, v); break; case -2: buf_add2_be(&attr, v); break; + case -3: buf_add3_be(&attr, v); break; case -4: buf_add4_be(&attr, v); break; - default: be_raise(vm, "type_error", "size must be -4, -2, -1, 0, 1, 2 or 4."); + default: be_raise(vm, "type_error", "size must be between -4 and 4."); } be_pop(vm, argc - 1); m_write_attributes(vm, 1, &attr); /* update attributes */ diff --git a/lib/libesp32/berry/tests/bytes.be b/lib/libesp32/berry/tests/bytes.be index 0cdc8334b..3533acc2a 100644 --- a/lib/libesp32/berry/tests/bytes.be +++ b/lib/libesp32/berry/tests/bytes.be @@ -53,6 +53,11 @@ b.add(0x12345678, -2) assert(str(b) == "bytes('2278785678563412785678')") b.add(0x12345678, -4) assert(str(b) == "bytes('227878567856341278567812345678')") +b.add(0xAABBCC, 3) +assert(str(b) == "bytes('227878567856341278567812345678CCBBAA')") +b.add(0x998877, -3) +assert(str(b) == "bytes('227878567856341278567812345678CCBBAA998877')") + #- get -# b=bytes("000102030405")