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
Add new Wire.sendTo(...) and Wire.requestFrom(...) API's that use external buffer #269
Add new Wire.sendTo(...) and Wire.requestFrom(...) API's that use external buffer #269
Conversation
✅ Build completed. ⬇️ Build URL: ℹ️ To test this build:
|
The idea is great, I think the library should definitely allow the user to provide an existing buffer to be sent or filled, so that that there is no hard-coded limit on its size (#285). Why don't you expand the functionality of the write function, so that it can accept the buffer as argument? Then it would maintain the beginTransmission+write+endTrasnmission approach, but if I call write() passing a pointer+size instead of a char it would set the library to use the buffer I give rather than the internal ring buffer. Then endTransmission would send the data using the ring buffer or, if set, my buffer. beginTrasmission would always default the buffer to be the ring buffer, for compatibility. What do you think? It may be a bit more consistent with the current API, but still giving the user the possibility to provide a buffer. |
Alternatively you can just extend beginTransmission to also take a buffer as input, and that would set the library to use that buffer for all subsequent operations (write+endTransmission) instead of the ring buffer. It is another option but I don't like it as much because if I already have all data sitting in an array somewhere, that would still require to copy it all byte by byte with many write() calls. I personally prefer the approach of my previous comment (#269 (comment)) because if I have some other libraries serializing the data for me, then I can simply provide a pointer to that array to the Wire lib and be done with it, without extra duplications |
@lupalby good points. Could you please share the method signatures along with some sketch snippets you have in mind just to clarify? |
I think I was a little confusing when talking about how to extend the functions. Here a more detail explanation. You could add 2 private global variables beginTransmission would always set externalBuffer = NULL as default, and flush the ring buffer ( just to be safe). Write could be expanded like this: So, for compatibility The implementation of endTransmission can be similar to what you have now in sendTo. You check if externalBuffer==NULL, if yes you use the ring buffer, otherwise you use the buffer that the user provided. In the sketch it would look like: |
@lupalby how about:
|
@sandeepmistry What would be the advantage? Probably that you can still use Wire.write passing few bytes at a time, but it would write on the provided buffer instead of the ring one. Is there any other reason? |
@lupalby Advantages I can think of:
|
@sandeepmistry It makes sense. Are you gonna update the code of this pull request with what we discussed? I'll be happy to review it once you commit |
Closing in favour of #298. |
added optimization menu option for M0
Proposal to add new Wire lib API's that allow the caller to specify an external buffer to use. Use cases:
New APIs: