Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 21, 2025

Conversation

Lorandil
Copy link
Contributor

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

Copy link
Owner

@earlephilhower earlephilhower left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor Author

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

@Lorandil
Copy link
Contributor Author

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

@earlephilhower
Copy link
Owner

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

@earlephilhower
Copy link
Owner

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
Copy link
Owner

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

@earlephilhower
Copy link
Owner

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

@earlephilhower earlephilhower merged commit 3df1392 into earlephilhower:master May 21, 2025
26 checks passed
schkovich pushed a commit to schkovich/arduino-pico that referenced this pull request May 24, 2025
'setBufferSize()' allows to modify the receive buffer size (inspired by the ESP32 code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants