Skip to content

Commit

Permalink
feat: Add calculator example with Commander
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Jul 4, 2019
1 parent a894536 commit 13e47c5
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
84 changes: 84 additions & 0 deletions examples/CommanderCalculator/CommanderCalculator.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <serde-commander.h>

/**
* 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<float>;

// 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);
}

0 comments on commit 13e47c5

Please sign in to comment.