I would like to bring the following library to your attention.
Summary
The SimpleRPC library provides an easy way to export Arduino functions as remote procedure calls. The exported method definitions are communicated to the host, which is then able to generate an API interface.
For each method, only one additional line of code is needed for exporting. On the host, only one function call is needed to perform a remote procedure call.
The Arduino library is independent of any host implementation, we provide a Python API library as a reference implementation.
Features
- Automatic parameter- and return type inference.
- Support for all native C types and strings.
- Support for arbitrary functions and class methods.
- Optional function and parameter naming and documentation.
- Support for PROGMEM's F() macro to reduce memory footprint.
Example
Simply pass any function as paramter to the interface() function.
#include <simpleRPC.h>
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
interface(digitalRead, "", digitalWrite, "");
}
These functions are now exposed to the host under the names method2() and method3().
>>> from simple_rpc import Interface
>>>
>>> interface = Interface('/dev/ttyACM0')
>>>
>>> interface.method2(8) # Read from pin 8.
0
>>> interface.method3(13, True) # Turn on LED.
Exposed functions and parameters can be named and supplied with documentation.
interface(
digitalRead,
"digital_read: Read digital pin. @pin: Pin number. @return: Pin value.",
digitalWrite,
"digital_write: Write to a digital pin. @pin: Pin number. @value: Pin value.");
This is reflected on the host.
>>> help(interface.digital_read)
Help on method digital_read:
digital_read(pin) method of simple_rpc.simple_rpc.Interface instance
Read digital pin.
:arg int pin: Pin number.
:returns int: Pin value.
>>> interface.digital_read(8)
0
>>> interface.digital_write(13, True)
Host implementations
The protocol is fully documented to facilitate implementation of host API libraries. Merge- and feature requests and are welcome.
With kind regards,
Jeroen.