›Arduino library
Serial codes in, function calls out
Register a function per code. A line like G1 X10.5 calls your function for G with 1 and the one for X with 10.5.
Version 2.0.1
Serial or any Stream
Tested on an Uno
MIT license
In the Arduino IDE: Sketch › Include Library › Add .ZIP Library.
01Quick start
One code, one function
#include <SebisSerialInterpreter.h>
SebisSerialInterpreter ssi(&Serial);
FunctionList fX; // must stay alive while registered: global or static
void onX(String code, double value) { Serial.println(value); }
void setup() {
Serial.begin(115200);
fX = ssi.createFunctionList("X", &onX);
ssi.addFunctionList(&fX);
}
void loop() { ssi.serialRead(false, true); }
Send X10 with a line ending and the sketch prints 10.00.
02Input
What a line may contain
- Line end
- LF, CR or CR LF. CR LF counts once.
- Code
- Letters followed directly by a number:
X10,MGC100. Case-sensitive. Anything else separates codes. - Number
-2.5,1,5(comma = point). Not checked:X1.2.3gives 1.2,X.gives 0.- Ignored
- A code without a number (
X,X 5). Comments like; X9are read as codes. - Length
- At most 128 characters. A longer line, or one that does not fit into the RAM, is dropped.
- Line done
- The final function runs once per line, after its codes, also for empty and dropped lines.
03API
Seven functions
SebisSerialInterpreter(Stream *stream)- Reads from
&Serial,&Serial1, a SoftwareSerial or any other Stream. FunctionList createFunctionList(String code, ListFunction func)- Creates an entry. Keep it alive while registered. Register
"G", not"G0":G0is read asGwith 0. void addFunctionList(FunctionList *entry)- Registers the entry and replaces one with the same code. One interpreter per entry.
void removeFunctionList(String code)- Unregisters a code. The entry is not freed.
void setFinalFunction(FinalFunction func)- Function called once per line.
NULLfor none. void runFunction(String code, double value)- Calls the function of a code directly.
void serialRead(boolean endlessLoop, boolean blockingBehavior)- Reads input and handles every complete line, see below.
Reading
serialRead(true, true)insetup()- Never returns.
serialRead(false, true)inloop()- Reads all available input, then returns.
serialRead(false, false)inloop()- Reads one character per call.
Function types
ListFunctionvoid f(String code, double value)FinalFunctionvoid f()
04Examples
In the Arduino IDE under File › Examples
05Version 2.0.1
What changed
- No crash when removing or re-adding a code, or without a final function.
- CR LF ends one line; too long lines are dropped.
- Any Stream works, including the USB serial of a Leonardo and SoftwareSerial.