-
Notifications
You must be signed in to change notification settings - Fork 0
/
DifferentTypesForTXandRX.ino
75 lines (68 loc) · 1.72 KB
/
DifferentTypesForTXandRX.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
#include <serde.h>
/**
* This example shows how to use the same serial port
* to send and receive different types of data.
*
* The use case of this example is to build a co-processor
* using an Arduino, where calculations are sent via
* the serial port and the result is sent back also
* via the same serial port.
*/
// First we define the data models to exchange
// These definition should be shared with the sending device.
enum class Operations
{
Add = 0,
Subtract,
Multiply,
Divide,
};
struct Command
{
Operations operation;
float operandA;
float operandB;
};
using SerdeRX = Serde<Command>; // Receive Command objects
using SerdeTX = Serde<float>; // Send a single float back
// --
void setup()
{
// SERIAL_PORT_HARDWARE aliases to the default
// hardware serial port on your board.
SERIAL_PORT_HARDWARE.begin(115200);
}
void loop()
{
Command command;
if (SerdeRX::receive(SERIAL_PORT_HARDWARE, command))
{
switch (command.operation)
{
case Operations::Add:
SerdeTX::send(
command.operandA + command.operandB,
SERIAL_PORT_HARDWARE
);
break;
case Operations::Subtract:
SerdeTX::send(
command.operandA - command.operandB,
SERIAL_PORT_HARDWARE
);
break;
case Operations::Multiply:
SerdeTX::send(
command.operandA * command.operandB,
SERIAL_PORT_HARDWARE
);
break;
case Operations::Divide:
SerdeTX::send(
command.operandA / command.operandB,
SERIAL_PORT_HARDWARE
);
break;
}
}
}