Skip to content

Commit

Permalink
Separated macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfjlaros committed Oct 22, 2018
1 parent 06984b3 commit bf6ac6b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 49 deletions.
2 changes: 1 addition & 1 deletion docs/protocol.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Protocol

In this section we describe the serial protocol.

Every exported method defined in ``methods.h`` (see the :doc:`usage_device`
Every exported method defined in ``methods.inc`` (see the :doc:`usage_device`
section) is assigned a number between 0 and 254 in order of appearance. The
number 0 maps to the first method, the number 1 maps to the second method, etc.

Expand Down
12 changes: 7 additions & 5 deletions docs/usage_device.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ function in the ``loop()`` body.
interface();
}
To export a method, we need to modify the file ``methods.h`` which can be found
in the library code.
To export a method, we need to modify the file ``methods.inc`` which can be
found in the library code.

- Arduino IDE: ``libraries/simpleRPC/methods.h`` in the ``sketchbook`` folder.
- Ino: ``lib/simpleRPC/simpleRPC/methods.h`` in the root of the project folder.
- Arduino IDE: ``libraries/simpleRPC/methods.inc`` in the ``sketchbook``
folder.
- Ino: ``lib/simpleRPC/simpleRPC/methods.inc`` in the root of the project
folder.

In this file, we use the ``INTERFACE()`` macro to make a method definition.
This macro takes three or more parameters, depending on the number of
Expand Down Expand Up @@ -77,7 +79,7 @@ like the following one.
return a + b;
}
The method definition in ``methods.h`` looks as follows.
The method definition in ``methods.inc`` looks as follows.

.. code:: cpp
Expand Down
39 changes: 39 additions & 0 deletions simpleRPC/macros.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* https://stackoverflow.com/questions/319328
*/
#define QUOTE(x...) #x

#define EMPTY()
#define DEFER(id) id EMPTY()
#define OBSTRUCT(id) id DEFER(EMPTY)()
#define EXPAND(args...) args
#define EAT(args...)

#define EVAL(args...) EVAL1(EVAL1(EVAL1(args)))
#define EVAL1(args...) EVAL2(EVAL2(EVAL2(args)))
#define EVAL2(args...) EVAL3(EVAL3(EVAL3(args)))
#define EVAL3(args...) EVAL4(EVAL4(EVAL4(args)))
#define EVAL4(args...) EVAL5(EVAL5(EVAL5(args)))
#define EVAL5(args...) args

#define CAT(a, args...) a ## args

#define CHECK_N(x, n, args...) n
#define CHECK(args...) CHECK_N(args, 0)

#define NOT_0 ~, 1,
#define NOT(x) CHECK(CAT(NOT_, x))

#define COMPL_0 1
#define COMPL_1 0
#define COMPL(b) CAT(COMPL_, b)

#define BOOL(x) COMPL(NOT(x))

#define IIF_0(t, args...) args
#define IIF_1(t, args...) t
#define IIF(c) CAT(IIF_, c)

#define IF(c) IIF(BOOL(c))

#define WHEN(c) IF(c)(EXPAND, EAT)
File renamed without changes.
47 changes: 4 additions & 43 deletions simpleRPC/simpleRPC.cpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,5 @@
#include "simpleRPC.h"

/*
* https://stackoverflow.com/questions/319328
*/
#define QUOTE(x...) #x

#define EMPTY()
#define DEFER(id) id EMPTY()
#define OBSTRUCT(id) id DEFER(EMPTY)()
#define EXPAND(args...) args
#define EAT(args...)

#define EVAL(args...) EVAL1(EVAL1(EVAL1(args)))
#define EVAL1(args...) EVAL2(EVAL2(EVAL2(args)))
#define EVAL2(args...) EVAL3(EVAL3(EVAL3(args)))
#define EVAL3(args...) EVAL4(EVAL4(EVAL4(args)))
#define EVAL4(args...) EVAL5(EVAL5(EVAL5(args)))
#define EVAL5(args...) args

#define CAT(a, args...) a ## args

#define CHECK_N(x, n, args...) n
#define CHECK(args...) CHECK_N(args, 0)

#define NOT_0 ~, 1,
#define NOT(x) CHECK(CAT(NOT_, x))

#define COMPL_0 1
#define COMPL_1 0
#define COMPL(b) CAT(COMPL_, b)

#define BOOL(x) COMPL(NOT(x))

#define IIF_0(t, args...) args
#define IIF_1(t, args...) t
#define IIF(c) CAT(IIF_, c)

#define IF(c) IIF(BOOL(c))

#define WHEN(c) IF(c)(EXPAND, EAT)
#include "macros.inl"


/*
Expand All @@ -48,7 +9,7 @@
{(char *)QUOTE(name), (char *)QUOTE(type), (char *)QUOTE(args), (char *)doc},

const Method methods[] = {
#include "methods.h"
#include "methods.inc"
};

#undef INTERFACE
Expand All @@ -62,7 +23,7 @@ const byte numberOfMethods = sizeof(methods) / sizeof(Method);
#define INTERFACE(doc, type, name, args...) \
extern type name(args);

#include "methods.h"
#include "methods.inc"

#undef INTERFACE

Expand Down Expand Up @@ -105,7 +66,7 @@ void interface(void) {

if (Serial.available()) {
switch (Serial.read()) {
#include "methods.h"
#include "methods.inc"
default:
Serial.write(numberOfMethods);
for (i = 0; i < numberOfMethods; i++) {
Expand Down

0 comments on commit bf6ac6b

Please sign in to comment.