I wanted the ability to call a custom function from the serial interface, so have added code to allow this. I've structured the code so that the library does not know about the users code. This means that any user defined functions can be run, and that there is only a one way dependency on the library (as it currently is), and not a two way dependency. This is achieved by using a callback function within the library, that takes a pointer to a function within the sketch.
When you setup the sketch, you call setDelegate from within the sketch with the name of the function that should be called to handle user defined functions. Within parser.cpp, I've added a "user" option, which takes two parameters. The first is a number, and the second which is optional is a colour. Within the function you created within the sketch, you do whatever action you wish based on the number and optional colour that is received (I typically set a global variable that is then referenced during the run loop, and within the run loop I call a function based on what action is set).
To demonstrate this in action, I've added a new example "UserDefinedFunctions", which is available via the examples menu. This example allows you to use all of the existing commands you are familiar with, and a new "user" function. The options available are:-
- user 1 BLUE;
A "ZigZag" style patten that lights up every second LED with the provided colour, and then alternates by lighting up the LED that was off.
- user 2;
Runs the Random Pastels example
- user 3;
Runs the Random Colours example
- user 4;
Runs the Random Primaries example
- Original Library - 16,108 bytes (Program Storage Space) and 1,633 bytes (Dynamic Memory)
- With User Defined Functions - 16,312 bytes (Program Storage Space) and 1,646 bytes (Dynamic Memory). This is an increase of 204 bytes (0%) Program Storage Space and 13 bytes (1%) of Dynamic Memory.
I've tested the above with the new UserDefinedFunctions example, 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 reduce memory usage to the serial command line parser at https://github.com/neographophobic/Cube ... ary_master. Please see the post Cube Library Code - Reduce Memory Usage for more information on this. When combined with those changes, the memory changes are 15,842 bytes (Program Storage Space) and 742 bytes (Dynamic Memory). This is an decrease of 266 bytes (1%) Program Storage Space and 891 bytes (35%) of Dynamic Memory.