Skip to content

Commit 55a6193

Browse files
committed
Make onRecieve also optional and make constructor protected
1 parent 51ce71f commit 55a6193

File tree

6 files changed

+13
-23
lines changed

6 files changed

+13
-23
lines changed

Diff for: docs/en/api/espnow.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ Set the local master key (LMK) for the peer.
234234
onReceive
235235
^^^^^^^^^^
236236

237-
Callback function to handle incoming data from the peer. This is a pure virtual method that must be implemented by the upper class.
237+
Callback function to handle incoming data from the peer. This is a virtual method can be implemented by the upper class for custom handling.
238238

239239
.. code-block:: cpp
240240
@@ -247,7 +247,7 @@ Callback function to handle incoming data from the peer. This is a pure virtual
247247
onSent
248248
^^^^^^^
249249

250-
Optional callback function to handle the completion of sending data to the peer. This is a pure virtual method that must be implemented by the upper class.
250+
Callback function to handle the completion of sending data to the peer. This is a virtual method can be implemented by the upper class for custom handling.
251251

252252
.. code-block:: cpp
253253

Diff for: libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Master/ESP_NOW_Broadcast_Master.ino

+1-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
/* Classes */
2424

25-
// Create a new class that inherits from the ESP_NOW_Peer class is required to implement the _onReceive and _onSent methods.
25+
// Creating a new class that inherits from the ESP_NOW_Peer class is required.
2626

2727
class ESP_NOW_Broadcast_Peer : public ESP_NOW_Peer {
2828
public:
@@ -52,12 +52,6 @@ public:
5252
}
5353
return true;
5454
}
55-
56-
void onReceive(const uint8_t *data, size_t len, bool broadcast) {
57-
// The broadcast peer will never receive any data. Rather, it will only send data.
58-
// Data broadcasted will be received by the actual object of the peer that made the broadcast.
59-
// It is still required to be implemented because it is a pure virtual method.
60-
}
6155
};
6256

6357
/* Global Variables */

Diff for: libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Slave/ESP_NOW_Broadcast_Slave.ino

+1-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/* Classes */
2525

26-
// Create a new class that inherits from the ESP_NOW_Peer class is required to implement the _onReceive and _onSent methods.
26+
// Creating a new class that inherits from the ESP_NOW_Peer class is required.
2727

2828
class ESP_NOW_Peer_Class : public ESP_NOW_Peer {
2929
public:
@@ -50,11 +50,6 @@ public:
5050
Serial.printf("Received a message from master " MACSTR " (%s)\n", MAC2STR(addr()), broadcast ? "broadcast" : "unicast");
5151
Serial.printf(" Message: %s\n", (char *)data);
5252
}
53-
54-
void _onSent(bool success) {
55-
// In this example the slave will never send any data, so this method will never be called.
56-
// It is still required to be implemented because it is a pure virtual method.
57-
}
5853
};
5954

6055
/* Global Variables */

Diff for: libraries/ESP_NOW/examples/ESP_NOW_Network/ESP_NOW_Network.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ std::vector<uint32_t> last_data(5); // Vector that will store the last 5 data r
9898

9999
/* Classes */
100100

101-
// We need to create a class that inherits from ESP_NOW_Peer to implement the _onReceive and _onSent methods.
101+
// We need to create a class that inherits from ESP_NOW_Peer and implement the _onReceive and _onSent methods.
102102
// This class will be used to store the priority of the device and to send messages to the peers.
103103
// For more information about the ESP_NOW_Peer class, see the ESP_NOW_Peer class in the ESP32_NOW.h file.
104104

Diff for: libraries/ESP_NOW/src/ESP32_NOW.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <string.h>
33
#include "esp_system.h"
44
#include "esp32-hal.h"
5-
#include "esp_mac.h"
65
#include "esp_wifi.h"
76

87
static void (*new_cb)(const esp_now_recv_info_t *info, const uint8_t * data, int len, void * arg) = NULL;

Diff for: libraries/ESP_NOW/src/ESP32_NOW.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Print.h"
55
#include "esp_now.h"
66
#include "esp32-hal-log.h"
7+
#include "esp_mac.h"
78

89
class ESP_NOW_Peer {
910
private:
@@ -19,8 +20,9 @@ class ESP_NOW_Peer {
1920
bool remove();
2021
size_t send(const uint8_t * data, int len);
2122

22-
public:
2323
ESP_NOW_Peer(const uint8_t *mac_addr, uint8_t channel=0, wifi_interface_t iface=WIFI_IF_AP, const uint8_t *lmk=NULL);
24+
25+
public:
2426
virtual ~ESP_NOW_Peer() {}
2527

2628
const uint8_t * addr() const;
@@ -37,11 +39,11 @@ class ESP_NOW_Peer {
3739

3840
operator bool() const;
3941

40-
//must be implemented by the upper class
41-
virtual void onReceive(const uint8_t * data, size_t len, bool broadcast) = 0;
42-
43-
//optional callback
44-
virtual void onSent(bool success) { log_v("Message reported as sent %s", success ? "successfully" : "unsuccessfully"); }
42+
//optional callbacks to be implemented by the upper class
43+
virtual void onReceive(const uint8_t * data, size_t len, bool broadcast) {
44+
log_i("Received %d bytes from " MACSTR " %s", len, MAC2STR(mac), broadcast ? "broadcast" : "");
45+
}
46+
virtual void onSent(bool success) { log_i("Message reported as sent %s", success ? "successfully" : "unsuccessfully"); }
4547
};
4648

4749
class ESP_NOW_Class : public Print {

0 commit comments

Comments
 (0)