Added 'TwoWire::setBufferSize()' to Wire library #2962

Merged
Lorandil merged 12 commits from master into master 2025-05-21 23:57:35 +03:00
Lorandil commented 2025-05-18 17:47:35 +03:00 (Migrated from github.com)

Hi,

As you suggested I added the missing 'setBufferSize()' method to the Wire library,
The method name and #define are taken from the ESP32 code.

I had to make some design decisions:

  • because most people won't use this feature, I chose to allocate the default buffer size already in the c'tor to prevent unncessary heap fragmenation
  • I implemented a lower limit to the buffer size (32 bytes) - what do you think?
  • the memory is only freed if a new buffer size is required, this again should keep the heap clean.

The code worked fine in my test environment ;)

Cheers,
Sven

Hi, As you suggested I added the missing **'setBufferSize()'** method to the Wire library, The method name and #define are taken from the ESP32 code. I had to make some design decisions: - because most people won't use this feature, I chose to allocate the default buffer size already in the c'tor to prevent unncessary heap fragmenation - I implemented a lower limit to the buffer size (32 bytes) - what do you think? - the memory is only freed if a new buffer size is required, this again should keep the heap clean. The code worked fine in my test environment ;) Cheers, Sven
earlephilhower (Migrated from github.com) requested changes 2025-05-18 22:01:42 +03:00
earlephilhower (Migrated from github.com) left a comment

Looks good, thanks for implementing the option.

You'll need to clean up some minor formatting issues shown in CI (use tools/restyle.sh under Linux to automatically fix those) and some warnings about signedness you need to explicitly cast away.

Looks good, thanks for implementing the option. You'll need to clean up some minor formatting issues shown in CI (use tools/restyle.sh under Linux to automatically fix those) and some warnings about signedness you need to explicitly cast away.
Lorandil commented 2025-05-18 23:24:25 +03:00 (Migrated from github.com)

I fixed an oversight of mine (buffer is also used on write) and added the casts.
Hoping for the best 😉

I fixed an oversight of mine (buffer is also used on write) and added the casts. Hoping for the best :wink:
Lorandil commented 2025-05-19 22:52:55 +03:00 (Migrated from github.com)

You'll need to clean up some minor formatting issues shown in CI (use tools/restyle.sh under Linux to automatically fix those) and some warnings about signedness you need to explicitly cast away.

I would gladly resolve the issue, but I really don't get what the problem is.
The check's output doesn't give me any clues what's wrong (at least none I discovered) and there is no 'restyle.sh' in tools (or maybe I'm just blind 😉).

I would really appreciate, if you could point me in the right direction here...

Regards,
Sven

> You'll need to clean up some minor formatting issues shown in CI (use tools/restyle.sh under Linux to automatically fix those) and some warnings about signedness you need to explicitly cast away. I would gladly resolve the issue, but I really don't get what the problem is. The check's output doesn't give me any clues what's wrong (at least none I discovered) and there is no 'restyle.sh' in tools (or maybe I'm just blind :wink:). I would really appreciate, if you could point me in the right direction here... Regards, Sven
earlephilhower commented 2025-05-20 01:29:33 +03:00 (Migrated from github.com)

You need to remove the spaces on blank lines that the CI identified, please.

You need to remove the spaces on blank lines that the CI identified, please.
earlephilhower commented 2025-05-21 23:10:16 +03:00 (Migrated from github.com)

Per the CI report, there are 3 tab/space/format issues

dex c02175a..4ee6781 100644
--- a/libraries/Wire/src/Wire.cpp
+++ b/libraries/Wire/src/Wire.cpp
@@ -167,7 +167,7 @@ void TwoWire::begin(uint8_t addr) {
 
     // allocate buffer if necessary
     if (!_buff) {
-        _buff=(uint8_t *)malloc(_buffSize);
+        _buff = (uint8_t *)malloc(_buffSize);
         if (!_buff)	{
             // ERROR
             return;
@@ -762,7 +762,7 @@ void TwoWire::clearTimeoutFlag() {
 }
 
 size_t TwoWire::setBufferSize(size_t bSize) {
-    if(_running) {
+    if (_running) {
         // ERROR - transmission already running. Report back current buffer size
         return _buffSize;
     }
@@ -771,7 +771,7 @@ size_t TwoWire::setBufferSize(size_t bSize) {
         free(_buff);
         _buff = nullptr;
     }
-    _buffSize = max(WIRE_BUFFER_SIZE_MIN,int(bSize)); // enforce minimum buffer size
+    _buffSize = max(WIRE_BUFFER_SIZE_MIN, int(bSize)); // enforce minimum buffer size
     return _buffSize;
 }
 
Error: Process completed with exit co
Per the CI report, there are 3 tab/space/format issues ```` dex c02175a..4ee6781 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -167,7 +167,7 @@ void TwoWire::begin(uint8_t addr) { // allocate buffer if necessary if (!_buff) { - _buff=(uint8_t *)malloc(_buffSize); + _buff = (uint8_t *)malloc(_buffSize); if (!_buff) { // ERROR return; @@ -762,7 +762,7 @@ void TwoWire::clearTimeoutFlag() { } size_t TwoWire::setBufferSize(size_t bSize) { - if(_running) { + if (_running) { // ERROR - transmission already running. Report back current buffer size return _buffSize; } @@ -771,7 +771,7 @@ size_t TwoWire::setBufferSize(size_t bSize) { free(_buff); _buff = nullptr; } - _buffSize = max(WIRE_BUFFER_SIZE_MIN,int(bSize)); // enforce minimum buffer size + _buffSize = max(WIRE_BUFFER_SIZE_MIN, int(bSize)); // enforce minimum buffer size return _buffSize; } Error: Process completed with exit co ````
earlephilhower commented 2025-05-21 23:17:47 +03:00 (Migrated from github.com)

And if you have astyle and this repo, you can just run ./tests/restyle.sh to clean them up automatically.

And if you have astyle and this repo, you can just run `./tests/restyle.sh` to clean them up automatically.
earlephilhower (Migrated from github.com) approved these changes 2025-05-21 23:56:53 +03:00
earlephilhower commented 2025-05-21 23:57:11 +03:00 (Migrated from github.com)

Took a little while to get there, but it got there in the end. 😆

Took a little while to get there, but it got there in the end. :laughing:
Sign in to join this conversation.