Skip to content

Commit 11d12b4

Browse files
committed
Clean up example documentation.
1 parent c18c126 commit 11d12b4

File tree

3 files changed

+87
-58
lines changed

3 files changed

+87
-58
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1717

1818
### Changed
1919
- Updated README.md, fixed errors, spelling, byte counts, etc.
20+
- Updated documentation / comments in documentation for clarity.
2021

2122
### Removed
2223
- Deprecated the `void begin(unsigned long speed, size_t port)` method because it could be confused with the standard `Serial.begin(unsigned long speed, uint8_t config)` method.

examples/PacketSerialReverseEcho/PacketSerialReverseEcho.ino

+43-29
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,72 @@
88
#include <PacketSerial.h>
99

1010

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-
PacketSerial 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, PacketSerial uses COBS encoding and has a 256 byte receive
18+
// buffer. This can be adjusted by the user by replacing `PacketSerial` with
19+
// a variation of the `PacketSerial_<COBS, 0, BufferSize>` template found in
20+
// PacketSerial.h.
21+
PacketSerial myPacketSerial;
2022

2123

2224
void setup()
2325
{
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);
2734
}
2835

2936

3037
void loop()
3138
{
32-
// Do other things here.
39+
// Do your program-specific loop() work here as usual.
3340

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.
3844
//
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();
4149
}
4250

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`.
4555
void onPacket(const uint8_t* buffer, size_t size)
4656
{
57+
// In this example, we will simply reverse the contents of the array and send
58+
// it back to the sender.
59+
4760
// Make a temporary buffer.
48-
uint8_t tmp[size];
61+
uint8_t tempBuffer[size];
4962

5063
// Copy the packet into our temporary buffer.
51-
memcpy(tmp, buffer, size);
64+
memcpy(tempBuffer, buffer, size);
5265

5366
// Reverse our temporaray buffer.
54-
reverse(tmp, size);
67+
reverse(tempBuffer, size);
5568

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);
6074
}
6175

62-
/// \brief A simple array reversal method.
76+
// This function takes a byte buffer and reverses it.
6377
void reverse(uint8_t* buffer, size_t size)
6478
{
6579
uint8_t tmp;

examples/PacketSerialReverseEchoSLIP/PacketSerialReverseEchoSLIP.ino

+43-29
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,72 @@
88
#include <PacketSerial.h>
99

1010

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;
2022

2123

2224
void setup()
2325
{
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);
2734
}
2835

2936

3037
void loop()
3138
{
32-
// Do other things here.
39+
// Do your program-specific loop() work here as usual.
3340

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.
3844
//
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();
4149
}
4250

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`.
4555
void onPacket(const uint8_t* buffer, size_t size)
4656
{
57+
// In this example, we will simply reverse the contents of the array and send
58+
// it back to the sender.
59+
4760
// Make a temporary buffer.
48-
uint8_t tmp[size];
61+
uint8_t tempBuffer[size];
4962

5063
// Copy the packet into our temporary buffer.
51-
memcpy(tmp, buffer, size);
64+
memcpy(tempBuffer, buffer, size);
5265

5366
// Reverse our temporaray buffer.
54-
reverse(tmp, size);
67+
reverse(tempBuffer, size);
5568

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);
6074
}
6175

62-
/// \brief A simple array reversal method.
76+
// This function takes a byte buffer and reverses it.
6377
void reverse(uint8_t* buffer, size_t size)
6478
{
6579
uint8_t tmp;

0 commit comments

Comments
 (0)