-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Converting library to C #328
Comments
I already did this, take a look at: I tested with PIC18F4620 and Arduino. The compiled code is small with avrgcc, but I don´t tested the speed. Only some arduino examples are converted to compatibility with new c library. |
@lcgamboa Oh, that's pretty great. Though, have you thought about pushing your changes upstream? (Without renaming the library or making it specific to a single MCU) |
@Avamander I think that c++ is more suitable for arduino, and c is more portable. Without class/objects the c version API is not directly compatible with existing c++ code and examples. Merge c++ version whith the c branch will result in no more compatibility with previous version, I don´t think so it´s a good idea, but I don´t no against it. I renamed the library because the API incompatibility, for people don´t confuse with the c++ original version. I have tested the c version with PIC and arduino, but it will work with any c compiller for microcontroller. If the project maintainers are in favor of total library conversion to c version, I agree. |
@lcgamboa A major version number bump, indicating an API change can be done. I doubt that will be much of an issue. The suitability won't be much different, though the syntax will change a bit. I think the work could be made in a new branch before merging to the master. (See https://github.com/TMRh20/RF24/tree/c-port, you can make the needed pushes/pull requests to that branch) BTW it seems MySensors has converted the library to C too. |
Yes, we've slowly moved away from c++ classes in MySensors (but still have a couple left for convenience). Like you said, it has been easier to port between archs and more importantly the footprint smaller. |
Hello maintainers, |
Maintain two version in parallel require work in double. |
The current idea is that the c-port branch is for the c port. The current pull request needs conflict resolving before I can merge it though (missing a few functions). In addition to that:
About two radios, just like with RF24Network, multi-head functionality is a preprocessor flag, it can be implemented the same for RF24(c) too. Though with the C++ wrapper it should not be an issue. |
My take is that one way or another C++ support should be maintained for the Arduino users if nothing else. I think arduino is likely a large consumer base for this library. I am not an expert in C or C++, but I prefer C++ for the benefit of using objects. I don't know if it is possible, but if converted to C, can a C++ wrapper supporting the current interface be wrapped around it? As far as footprint size goes, my take is that if I am that constrained on size, I would not use the library at all. For a size constrained system I may use the library for a first pass implementation to figure out what settings I needed, then when I needed the footprint to shrink, replace with my own streamlined code that talks directly to the device (keeping header files for the definitions). |
With every added abstraction library the method you described becomes harder and harder. I think keeping it on a separate branch for now and working on it there is sufficient.
|
As I'm a simple lib consumer, I'm not comfortable with asking again question but what I understand so far about the c++ to c migration is:
cons:
question:
Feel free to complete pro/cons/question sections ... |
I renamed the library back to RF24 and I have made a c++ wrapper to c library (only for one radio). The c++ user don't need to change the code to use the c library with the c++ wrapper.
I don't have much time to work and test all examples for now, I appreciate any help. |
Throwing my two cents in:
I get what you're talking about (SemVer), but a major version bumps are meant to indicate breaking changes that require come effort to correct, not a complete rewrite of the API. If you're going to go down this route, please fork the project. |
There's really no reason why any equivalent C++ code would be bigger and slower than the C version. In fact in some instances you can generate smaller or faster code because certain C++ features allow for better inclining and ROM-ing (eg constexpr). |
There is reason, it depends on the compiler and the optimizations. A c ++ compiler is always much more complex in its construction than a c compiler, so its optimizations are more complex as well. In the case of avr-gcc and avr-g++, the code in c looks smaller than c ++. |
If you look at the code sizes you provided, you see that the C++ wrapper ends up with smaller program and memory footprint than pure C, so essentially the abstractions cost nothing and may even allow for more optimization. There's a great example of how much of modern C++ abstractions optimize to nothing. https://m.youtube.com/watch?v=zBkNBP00wJE |
But I certainly understand that it would be nice for the library to be portable to platforms with limited or no c++ support, like 8051. |
I have converted to C the libraries RF24, RF24Network, RF24Mesh, RF24Ethernet and RF24Gateway for portability. Now this work for arduino and for any microcontroller with a C compiler. C++ compiler are not common between microcontrollers. For example using sdcc compiler the library now supports MCS51, DS80C390, HC08, Z80, STM8, PIC16 and PIC18 targets. |
What do you think about converting this library to C?
I'm 100% sure this would both reduce the size of the end result and make the speed rely less on the compiler's optimizations (that fail way too often), in addition to that it would allow easier porting to some platforms that do not have C++ support.
The text was updated successfully, but these errors were encountered: