diff --git a/.travis.yml b/.travis.yml index c150c53..55c8300 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,6 +27,7 @@ env: - PLATFORMIO_CI_SRC=examples/Sending - PLATFORMIO_CI_SRC=examples/NestedData - PLATFORMIO_CI_SRC=examples/Commander + - PLATFORMIO_CI_SRC=examples/CommanderCalculator addons: apt: diff --git a/examples/CommanderCalculator/CommanderCalculator.ino b/examples/CommanderCalculator/CommanderCalculator.ino new file mode 100644 index 0000000..8092a03 --- /dev/null +++ b/examples/CommanderCalculator/CommanderCalculator.ino @@ -0,0 +1,84 @@ +#include + +/** + * This example shows the same use-case as DifferentTypesForTXandRX, + * but using Commander instead of manually checking the operation type. + * + * As the result is a plain float value, we'll use a standard Serde + * interface, no need for a Commander here, as long as the other + * end uses CommanderTX and SerdeRX. + */ + +using SerdeTX = Serde; + +// RX Commands + +struct UnaryOperation +{ + float a; +}; +struct TwoArgumentsOperation +{ + float a; + float b; +}; + +using Add = TwoArgumentsOperation; +using Subtract = TwoArgumentsOperation; +using Multiply = TwoArgumentsOperation; +using Divide = TwoArgumentsOperation; +using Negate = UnaryOperation; +using Abs = UnaryOperation; + +void onAddReceived(const Add& args) +{ + SerdeTX::send(args.a + args.b, SERIAL_PORT_HARDWARE); +} + +void onSubtractReceived(const Subtract& args) +{ + SerdeTX::send(args.a - args.b, SERIAL_PORT_HARDWARE); +} + +void onMultiplyReceived(const Multiply& args) +{ + SerdeTX::send(args.a * args.b, SERIAL_PORT_HARDWARE); +} + +void onDivideReceived(const Divide& args) +{ + SerdeTX::send(args.a / args.b, SERIAL_PORT_HARDWARE); +} + +void onNegateReceived(const Negate& args) +{ + SerdeTX::send(-args.a, SERIAL_PORT_HARDWARE); +} + +void onAbsReceived(const Abs& args) +{ + SerdeTX::send(args.a > 0 ? args.a : -args.a, SERIAL_PORT_HARDWARE); +} + +SERDE_COMMANDER_CREATE_RX(CommanderRX, + Add, + Subtract, + Multiply, + Divide, + Negate, + Abs +); + +// -- + +void setup() +{ + // SERIAL_PORT_HARDWARE aliases to the default + // hardware serial port on your board. + SERIAL_PORT_HARDWARE.begin(115200); +} + +void loop() +{ + CommanderRX::read(SERIAL_PORT_HARDWARE); +}