|
8 | 8 | #include <PacketSerial.h> |
9 | 9 |
|
10 | 10 |
|
11 | | -// The PacketSerial object. |
12 | | -// It cleverly wraps one of the Serial objects. |
13 | | -// While it is still possible to use the Serial object |
14 | | -// directly, it is recommended that the user let the |
15 | | -// PacketSerial object manage all serial communication. |
16 | | -// Thus the user should not call Serial.write(), etc. |
17 | | -// Additionally the user should not use the serialEvent() |
18 | | -// callbacks. |
19 | | -SLIPPacketSerial serial; |
| 11 | +// By default, PacketSerial automatically wraps the built-in `Serial` object. |
| 12 | +// While it is still possible to use the Serial object directly, it is |
| 13 | +// recommended that the user let the PacketSerial object manage all serial |
| 14 | +// communication. Thus the user should not call Serial.write(), Serial.print(), |
| 15 | +// etc. Additionally the user should not use the serialEvent() framework. |
| 16 | +// |
| 17 | +// By default, SLIPPacketSerial uses SLIP encoding and has a 256 byte receive |
| 18 | +// buffer. This can be adjusted by the user by replacing `SLIPPacketSerial` |
| 19 | +// with a variation of the `PacketSerial_<SLIP, SLIP::END, BufferSize>` template |
| 20 | +// found in PacketSerial.h. |
| 21 | +SLIPPacketSerial myPacketSerial; |
20 | 22 |
|
21 | 23 |
|
22 | 24 | void setup() |
23 | 25 | { |
24 | | - // We must specify a packet handler method so that |
25 | | - serial.setPacketHandler(&onPacket); |
26 | | - serial.begin(115200); |
| 26 | + // We begin communication with our PacketSerial object by setting the |
| 27 | + // communication speed in bits / second (baud). |
| 28 | + myPacketSerial.begin(115200); |
| 29 | + |
| 30 | + // If we want to receive packets, we must specify a packet handler function. |
| 31 | + // The packet handler is a custom function with a signature like the onPacket |
| 32 | + // function below. |
| 33 | + myPacketSerial.setPacketHandler(&onPacket); |
27 | 34 | } |
28 | 35 |
|
29 | 36 |
|
30 | 37 | void loop() |
31 | 38 | { |
32 | | - // Do other things here. |
| 39 | + // Do your program-specific loop() work here as usual. |
33 | 40 |
|
34 | | - // The update() method attempts to read in |
35 | | - // any incoming serial data and emits packets via |
36 | | - // the user's onPacket(const uint8_t* buffer, size_t size) |
37 | | - // method registered with the setPacketHandler() method. |
| 41 | + // The PacketSerial::update() method attempts to read in any incoming serial |
| 42 | + // data and emits received and decoded packets via the packet handler |
| 43 | + // function specified by the user in the void setup() function. |
38 | 44 | // |
39 | | - // The update() method should be called at the end of the loop(). |
40 | | - serial.update(); |
| 45 | + // The PacketSerial::update() method should be called once per loop(). Failure |
| 46 | + // to call the PacketSerial::update() frequently enough may result in buffer |
| 47 | + // serial overflows. |
| 48 | + myPacketSerial.update(); |
41 | 49 | } |
42 | 50 |
|
43 | | -// This is our packet callback. |
44 | | -// The buffer is delivered already decoded. |
| 51 | +// This is our handler callback function. |
| 52 | +// When an encoded packet is received and decoded, it will be delivered here. |
| 53 | +// The `buffer` is a pointer to the decoded byte array. `size` is the number of |
| 54 | +// bytes in the `buffer`. |
45 | 55 | void onPacket(const uint8_t* buffer, size_t size) |
46 | 56 | { |
| 57 | + // In this example, we will simply reverse the contents of the array and send |
| 58 | + // it back to the sender. |
| 59 | + |
47 | 60 | // Make a temporary buffer. |
48 | | - uint8_t tmp[size]; |
| 61 | + uint8_t tempBuffer[size]; |
49 | 62 |
|
50 | 63 | // Copy the packet into our temporary buffer. |
51 | | - memcpy(tmp, buffer, size); |
| 64 | + memcpy(tempBuffer, buffer, size); |
52 | 65 |
|
53 | 66 | // Reverse our temporaray buffer. |
54 | | - reverse(tmp, size); |
| 67 | + reverse(tempBuffer, size); |
55 | 68 |
|
56 | | - // Send the reversed buffer back. |
57 | | - // The send() method will encode the buffer |
58 | | - // as a packet, set packet markers, etc. |
59 | | - serial.send(tmp, size); |
| 69 | + // Send the reversed buffer back to the sender. The send() method will encode |
| 70 | + // the whole buffer as as single packet, set packet markers, etc. |
| 71 | + // The `tempBuffer` is a pointer to the `tempBuffer` array and `size` is the |
| 72 | + // number of bytes to send in the `tempBuffer`. |
| 73 | + myPacketSerial.send(tempBuffer, size); |
60 | 74 | } |
61 | 75 |
|
62 | | -/// \brief A simple array reversal method. |
| 76 | +// This function takes a byte buffer and reverses it. |
63 | 77 | void reverse(uint8_t* buffer, size_t size) |
64 | 78 | { |
65 | 79 | uint8_t tmp; |
|
0 commit comments