I often need some way to get a bit of data and a few commands into an Arduino-type sketch from the serial port. Trivial but tedious stuff if you need to write the code for it, each time again.
Trouble is, the ATmega has very limited RAM space and string processing capabilities, so writing a super duper fancy string input parser is too wasteful of resources to be practical, in most cases.
In the RF12demo sketch, I used a very simple postfix notation, which lets you enter some bytes of data and a one-letter command. The syntax is:
<arg1>,<arg2>,... <cmd-char>
It works fairly well, but arguments are limited to single bytes and have to be entered as decimal values.
Here’s another attempt to simplify this sort of task. I’ve added an “InputParser” class to the Ports library, which takes care of basic parsing, while remaining flexible enough to allow use in different sketches. It supports input of bytes, ints, and longs in decimal or hex format, as well as short strings. It’s not JeeNode specific, and should work with any Arduino.
Here is the InpurtParser class definition (minor details omitted):
Note that many choices were made to keep the code overhead low, while allowing enough flexibility to input data in different ways. As a consquence, the syntax is a bit strict and limited:
- args are separated by spaces or commas, with the command code typed last
- plain numbers are interpreted as bytes: 123
- negative values can be entered using a trailing minus sign: 123-
- append a decimal point to enter 2-byte ints: 12345.
- append a colon to enter as 4-byte longs: 123456789:
- prefix with a dollar sign to use hex mode: $1A / $1A2B. / $123456:
- enter strings between double quotes: “this is a string”
I’ve added a “parser_demo.pde” sketch to illustrate its use:
When initialized, the parser must be given a buffer it can use to collect all argument data and input strings in. In this simple example, a 50-byte buffer is allocated dynamically.
Each command is a separate function, which pulls its variables from the parser object when called. The parser is initialized with a table of these commands, listing the command code and minimum number of expected bytes, along with the function to call.
Here are some examples of input:
h
?
v
1234. v
"Hello, there!" s
123456789: "abc" $100. "defgh" 12345. c
Here is the output, corresponding to each of the above commands:
[parser_demo]
Hello!
Known commands: h v l s c
Not enough data, need 2 bytes
value = 1234
string = <Hello, there!>
complex = 123456789 <abc> 256 <defgh> 12345
Note that you get a list of known commands when entering a bad command (or “?”).
That last example illustrates how to pass a variety of input argument types to a command. The code for this is:
This demo requires about 5 Kb, including some 850 bytes for the command functions. 600 bytes for malloc (this is optional), and 600 bytes for the Serial class. The parser itself uses some 1.7 Kb.
By default, the parser object is attached to the “Serial” stream, but it can be hooked up to something else if necessary, such as an LCD or Ethernet.
Enjoy!