For those of us who write a lot of firmware for instruments that is controlled via USBSerial, I've found very few good patterns or libraries for doing this in a way that does things like serial parsing, argument type conversion, and registering of functions. The result is often messy and buggy code that's really hard to maintain or even follow the flow of, so I wanted to write a library to do these common tasks. Particularly when you need to do parsing, the lack of regex means really messy nested if loops that I found to be very error prone in writing them properly.
As such, I've created this:
https://github.com/neltnerb/Teensy-Reactor-Controller
which has an example for controlling a valve, includes an example of using an interrupt, and also demonstrates a best practice for using elapsedMillis to do non-blocking work.
The checkSerial() function automatically looks for new characters on the Serial device and puts them into a buffer to provide expandable buffer length. It properly handles backspaces for interactive control, and accepts both carriage returns and newlines as a termination character so that the mess that is often LabView code is more likely to work on the first try.
checkSerial() prints errors to the serial port for things like attempting to access an argument that doesn't exist, invalid conversion (say from "0.0" to a type int), or out of range errors, and provides a method for retrieving the number of arguments received to do manual checking for exactly the desired number of arguments (or allows for variable argument numbers). Upon an error it returns immediately from the handler and prints a message rather than potentially doing something unintended, which is my preference for things that control toxic chemical flow paths.
It also provides the special serial command "ListCommands" which simply provides a list of all of the registered commands and their descriptions.
I tested command parsing with a Teensy 3.1, and it has no issues I've found yet by trying to do all of the myriad horrible things often done on a Serial interface to the poor firmware. It would be lovely to have other people testing it too if anyone thinks this would be helpful to them. It also compiled for Arduino ARM targets, but the presence of vectors made it not compile for regular AVR. I thought about trying to replace that with a linked list or fixed length char* but since I'm personally just using the Teensy at this point I found this easier. There seem to be AVR implementations of the vector library, but I didn't want to deal with that myself since I'm not using the chips.
I didn't set it up yet so that it's possible to select different serial interfaces for the input character stream. I think something like checkSerial(Serial device) might work, but I haven't thought about the implications fully so for now it just assumes you're using the main USB Serial port.
Happy to accept revisions, bug fixes, or hate mail depending ;-) I'm definitely not a professional programmer, I just ended up having to write piles of firmware for devices I've built for custom systems and run into the same set of problems every time. I think this kind of thing will make it a lot better, even moreso if others can add to it for a common well-tested library.
As such, I've created this:
https://github.com/neltnerb/Teensy-Reactor-Controller
which has an example for controlling a valve, includes an example of using an interrupt, and also demonstrates a best practice for using elapsedMillis to do non-blocking work.
The checkSerial() function automatically looks for new characters on the Serial device and puts them into a buffer to provide expandable buffer length. It properly handles backspaces for interactive control, and accepts both carriage returns and newlines as a termination character so that the mess that is often LabView code is more likely to work on the first try.
checkSerial() prints errors to the serial port for things like attempting to access an argument that doesn't exist, invalid conversion (say from "0.0" to a type int), or out of range errors, and provides a method for retrieving the number of arguments received to do manual checking for exactly the desired number of arguments (or allows for variable argument numbers). Upon an error it returns immediately from the handler and prints a message rather than potentially doing something unintended, which is my preference for things that control toxic chemical flow paths.
It also provides the special serial command "ListCommands" which simply provides a list of all of the registered commands and their descriptions.
I tested command parsing with a Teensy 3.1, and it has no issues I've found yet by trying to do all of the myriad horrible things often done on a Serial interface to the poor firmware. It would be lovely to have other people testing it too if anyone thinks this would be helpful to them. It also compiled for Arduino ARM targets, but the presence of vectors made it not compile for regular AVR. I thought about trying to replace that with a linked list or fixed length char* but since I'm personally just using the Teensy at this point I found this easier. There seem to be AVR implementations of the vector library, but I didn't want to deal with that myself since I'm not using the chips.
I didn't set it up yet so that it's possible to select different serial interfaces for the input character stream. I think something like checkSerial(Serial device) might work, but I haven't thought about the implications fully so for now it just assumes you're using the main USB Serial port.
Happy to accept revisions, bug fixes, or hate mail depending ;-) I'm definitely not a professional programmer, I just ended up having to write piles of firmware for devices I've built for custom systems and run into the same set of problems every time. I think this kind of thing will make it a lot better, even moreso if others can add to it for a common well-tested library.