Skip to content

Commit 51ce71f

Browse files
committed
make onSent optional and remove underscores for virtual functions
1 parent 439840b commit 51ce71f

File tree

7 files changed

+20
-22
lines changed

7 files changed

+20
-22
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -231,27 +231,27 @@ Set the local master key (LMK) for the peer.
231231
232232
* ``lmk``: Local master key.
233233

234-
_onReceive
234+
onReceive
235235
^^^^^^^^^^
236236

237237
Callback function to handle incoming data from the peer. This is a pure virtual method that must be implemented by the upper class.
238238

239239
.. code-block:: cpp
240240
241-
void _onReceive(const uint8_t *data, int len, bool broadcast);
241+
void onReceive(const uint8_t *data, int len, bool broadcast);
242242
243243
* ``data``: Pointer to the received data.
244244
* ``len``: Length of the received data.
245245
* ``broadcast``: ``true`` if the data is broadcasted, ``false`` otherwise.
246246

247-
_onSent
247+
onSent
248248
^^^^^^^
249249

250-
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+
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.
251251

252252
.. code-block:: cpp
253253
254-
void _onSent(bool success);
254+
void onSent(bool success);
255255
256256
* ``success``: ``true`` if the data is sent successfully, ``false`` otherwise.
257257

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,11 @@ public:
5353
return true;
5454
}
5555

56-
void _onReceive(const uint8_t *data, size_t len, bool broadcast) {
56+
void onReceive(const uint8_t *data, size_t len, bool broadcast) {
5757
// The broadcast peer will never receive any data. Rather, it will only send data.
5858
// Data broadcasted will be received by the actual object of the peer that made the broadcast.
5959
// It is still required to be implemented because it is a pure virtual method.
6060
}
61-
62-
void _onSent(bool success) {
63-
// As broadcast messages does not require any acknowledgment, this method will never be called.
64-
// It is still required to be implemented because it is a pure virtual method.
65-
}
6661
};
6762

6863
/* Global Variables */

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public:
133133
return send(data, len);
134134
}
135135

136-
void _onReceive(const uint8_t *data, size_t len, bool broadcast) {
136+
void onReceive(const uint8_t *data, size_t len, bool broadcast) {
137137
esp_now_data_t *msg = (esp_now_data_t *)data;
138138

139139
if (peer_ready == false && msg->ready == true) {
@@ -159,7 +159,7 @@ public:
159159
}
160160
}
161161

162-
void _onSent(bool success) {
162+
void onSent(bool success) {
163163
bool broadcast = memcmp(addr(), ESP_NOW.BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0;
164164
if (broadcast) {
165165
log_v("Broadcast message reported as sent %s", success ? "successfully" : "unsuccessfully");

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ static void _esp_now_rx_cb(const esp_now_recv_info_t *info, const uint8_t *data,
118118
log_v("Checking peer " MACSTR, MAC2STR(_esp_now_peers[i]->addr()));
119119
}
120120
if(_esp_now_peers[i] != NULL && memcmp(info->src_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0){
121-
log_v("Calling _onReceive");
122-
_esp_now_peers[i]->_onReceive(data, len, broadcast);
121+
log_v("Calling onReceive");
122+
_esp_now_peers[i]->onReceive(data, len, broadcast);
123123
return;
124124
}
125125
}
@@ -130,7 +130,7 @@ static void _esp_now_tx_cb(const uint8_t *mac_addr, esp_now_send_status_t status
130130
//find the peer and call it's callback
131131
for(uint8_t i=0; i<ESP_NOW_MAX_TOTAL_PEER_NUM; i++){
132132
if(_esp_now_peers[i] != NULL && memcmp(mac_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0){
133-
_esp_now_peers[i]->_onSent(status == ESP_NOW_SEND_SUCCESS);
133+
_esp_now_peers[i]->onSent(status == ESP_NOW_SEND_SUCCESS);
134134
return;
135135
}
136136
}

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "esp_wifi_types.h"
44
#include "Print.h"
55
#include "esp_now.h"
6+
#include "esp32-hal-log.h"
67

78
class ESP_NOW_Peer {
89
private:
@@ -37,8 +38,10 @@ class ESP_NOW_Peer {
3738
operator bool() const;
3839

3940
//must be implemented by the upper class
40-
virtual void _onReceive(const uint8_t * data, size_t len, bool broadcast) = 0;
41-
virtual void _onSent(bool success) = 0;
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"); }
4245
};
4346

4447
class ESP_NOW_Class : public Print {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void ESP_NOW_Serial_Class::flush(){
140140
}
141141

142142
//RX callback
143-
void ESP_NOW_Serial_Class::_onReceive(const uint8_t * data, size_t len, bool broadcast){
143+
void ESP_NOW_Serial_Class::onReceive(const uint8_t * data, size_t len, bool broadcast){
144144
if(rx_queue == NULL){
145145
return;
146146
}
@@ -241,7 +241,7 @@ size_t ESP_NOW_Serial_Class::write(const uint8_t *buffer, size_t size, uint32_t
241241
return size;
242242
}
243243
//TX Done Callback
244-
void ESP_NOW_Serial_Class::_onSent(bool success){
244+
void ESP_NOW_Serial_Class::onSent(bool success){
245245
log_v(MACSTR" : %s", MAC2STR(addr()), success?"OK":"FAIL");
246246
if(tx_sem == NULL || tx_ring_buf == NULL || !added){
247247
return;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class ESP_NOW_Serial_Class : public Stream, public ESP_NOW_Peer {
3939
size_t write(const uint8_t *buffer, size_t size){ return write(buffer, size, 1000); }
4040
size_t write(uint8_t data){ return write(&data, 1); }
4141
//ESP_NOW_Peer
42-
void _onReceive(const uint8_t * data, size_t len, bool broadcast);
43-
void _onSent(bool success);
42+
void onReceive(const uint8_t * data, size_t len, bool broadcast);
43+
void onSent(bool success);
4444
};
4545

4646

0 commit comments

Comments
 (0)