Skip to content

Commit 201862b

Browse files
author
Louis Beaudoin
committed
Add example demonstrating the need for passing an arbitrary sender pointer
1 parent 6014e15 commit 201862b

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//
2+
// Copyright (c) 2012 Christopher Baker <https://christopherbaker.net>
3+
//
4+
// SPDX-License-Identifier: MIT
5+
//
6+
7+
// This example is PacketSerialReverseEcho modified to use PacketSerial from within a class
8+
9+
#include <PacketSerial.h>
10+
11+
// This function takes a byte buffer and reverses it.
12+
void reverse(uint8_t* buffer, size_t size)
13+
{
14+
uint8_t tmp;
15+
16+
for (size_t i = 0; i < size / 2; i++)
17+
{
18+
tmp = buffer[i];
19+
buffer[i] = buffer[size - i - 1];
20+
buffer[size - i - 1] = tmp;
21+
}
22+
}
23+
24+
class EchoClass {
25+
public:
26+
void begin(unsigned long speed) {
27+
// If we want to receive packets, we must specify a packet handler function.
28+
// The packet handler is a custom function with a signature like the
29+
// onPacketReceived function below.
30+
myPacketSerial.setPacketHandler(&onPacketReceived, this);
31+
32+
myPacketSerial.begin(speed);
33+
}
34+
35+
void loop() {
36+
// The PacketSerial::update() method attempts to read in any incoming serial
37+
// data and emits received and decoded packets via the packet handler
38+
// function specified by the user in the void setup() function.
39+
//
40+
// The PacketSerial::update() method should be called once per loop(). Failure
41+
// to call the PacketSerial::update() frequently enough may result in buffer
42+
// serial overflows.
43+
myPacketSerial.update();
44+
45+
// Check for a receive buffer overflow (optional).
46+
if (myPacketSerial.overflow())
47+
{
48+
// Send an alert via a pin (e.g. make an overflow LED) or return a
49+
// user-defined packet to the sender.
50+
//
51+
// Ultimately you may need to just increase your recieve buffer via the
52+
// template parameters (see the README.md).
53+
}
54+
}
55+
56+
private:
57+
// C-style callbacks can't use non-static methods, so we use a static method that receives "this" as the sender argument: https://wiki.c2.com/?VirtualStaticIdiom
58+
static void onPacketReceived(const void* sender, const uint8_t* buffer, size_t size) {
59+
((EchoClass*)sender)->onPacketReceived(buffer, size);
60+
}
61+
62+
// This is our handler callback function.
63+
// When an encoded packet is received and decoded, it will be delivered here.
64+
// The `buffer` is a pointer to the decoded byte array. `size` is the number of
65+
// bytes in the `buffer`.
66+
void onPacketReceived(const uint8_t* buffer, size_t size) {
67+
// In this example, we will simply reverse the contents of the array and send
68+
// it back to the sender.
69+
70+
// Make a temporary buffer.
71+
uint8_t tempBuffer[size];
72+
73+
// Copy the packet into our temporary buffer.
74+
memcpy(tempBuffer, buffer, size);
75+
76+
// Reverse our temporaray buffer.
77+
reverse(tempBuffer, size);
78+
79+
// Send the reversed buffer back to the sender. The send() method will encode
80+
// the whole buffer as as single packet, set packet markers, etc.
81+
// The `tempBuffer` is a pointer to the `tempBuffer` array and `size` is the
82+
// number of bytes to send in the `tempBuffer`.
83+
myPacketSerial.send(tempBuffer, size);
84+
}
85+
86+
PacketSerial myPacketSerial;
87+
};
88+
89+
// By default, PacketSerial automatically wraps the built-in `Serial` object.
90+
// While it is still possible to use the Serial object directly, it is
91+
// recommended that the user let the PacketSerial object manage all serial
92+
// communication. Thus the user should not call Serial.write(), Serial.print(),
93+
// etc. Additionally the user should not use the serialEvent() framework.
94+
//
95+
// By default, PacketSerial uses COBS encoding and has a 256 byte receive
96+
// buffer. This can be adjusted by the user by replacing `PacketSerial` with
97+
// a variation of the `PacketSerial_<COBS, 0, BufferSize>` template found in
98+
// PacketSerial.h.
99+
100+
EchoClass myEchoClass;
101+
102+
void setup()
103+
{
104+
// We begin communication with our PacketSerial object by setting the
105+
// communication speed in bits / second (baud).
106+
myEchoClass.begin(115200);
107+
}
108+
109+
110+
void loop()
111+
{
112+
// Do your program-specific loop() work here as usual.
113+
114+
myEchoClass.loop();
115+
}

0 commit comments

Comments
 (0)