This review resulted in 6 separate changes that I have made to the cube library (primarily to the serial command line parser). These changes are as follows:-
Optimise the calls to skipWhiteSpace so that they happen within the parseAxis, parseDirection, parsePosition, parseOffset and parseRGB instead of being called before each function. As each call to those functions had a preceding call to skipWhiteSpace this can be done with no impact to functionality, and saves 244 bytes of program storage space.
Convert the message to lowercase, and then only check against lowercase characters, instead of both upper and lowercase characters. This has the added benefit of now allowing commands as well as items to be in any case (so 'ALL BLUE;' and 'all blue;' both now work). This saves 52 bytes of program storage space.
- Direction Minimisation
Combine two if statements that set the same results into a single statement with or, saving 24 bytes of program storage space.
- Remove middle letters
When testing for the predefined characters, cheat and only look for the first and last character of the colour as these are unique. This saves 182 bytes of program storage space.
- F Macro
Use the F() macro for static strings that are outputted via the serial interface. This does use an 74 bytes of program storage space, but reduces the global variables from using 1,633 bytes (63%) to 729 bytes (28%) of dynamic memory. This makes an extra 904 bytes or 35% of dynamic memory available for local variables.
- No help text
Add an option (disabled by default) to remove the help text from the serial console when the user types 'help;'. If enabled all the user will only see the link to the website, and program storage space will be reduced by 1024 bytes.
- Original Library - 16,108 bytes (Program Storage Space) and 1,633 bytes (Dynamic Memory)
- skipWhiteSpace, Lowercase, Direction Minimisation, Remove middle letters, and F Macro - 15,680 bytes (Program Storage Space) and 729 bytes (Dynamic Memory). This is a saving of 428 bytes (2%) Program Storage Space and a massive 904 bytes (35%) of Dynamic Memory.
- No help text - if enabled, it saves an extra 1,024 bytes (3%) of Program Storage Space.
I've tested the above with the TestPattern, but would love to know if it works for you or if these changes introduce errors that I'm not aware of. I haven't written much Arduino code, so any improvements, comments, bugs, or suggestions are welcomed (including pull requests), but please bear with me as it may take me a while to incorporate any changes.
P.S. I've also posted a combined version of these changes, and the changes I made to add user defined function support to the serial command line parser at https://github.com/neographophobic/Cube ... ary_master. Please see the post Cube Library Code - User Defined Function for more information on this.