Templatize class to minimize code size. #36
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is kind of a large change, but it saves 1100 bytes of program space when only using SPI. which is significant. I assume the savings are similar if you use the other comms protocols.
Because the library used a runtime variable to determine which comm method to use, the compiler could not easily eliminate code that we know will not be used since the constructor sets the comms mode.
If we instead of using a runtime variable keep this information in a template, it is obvious to the compiler at compile time which methods won't be used and it can eliminate that code. The MicroOLED class is now three different classes for the SPI, I2C, and Parallel communication modes, with the functionality in a templatized common base class. This avoids always building in the I2C, SPI, and Parallel comm code even though only one will likely be used.
The drawback is that cases that DO use more than one comms mode will now use more space. If this is a use case that people care about, it could be accommodated most simply by adding another derived class.