-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommanderCalculator.ino
84 lines (69 loc) · 1.66 KB
/
CommanderCalculator.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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);
}