Skip to content

Commit af6411d

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

File tree

5 files changed

+11
-14
lines changed

5 files changed

+11
-14
lines changed

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.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ class ESP_NOW_Peer {
3737
operator bool() const;
3838

3939
//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;
40+
virtual void onReceive(const uint8_t * data, size_t len, bool broadcast) = 0;
41+
42+
//optional callback
43+
virtual void onSent(bool success) { log_v("Message reported as sent %s", success ? "successfully" : "unsuccessfully"); }
4244
};
4345

4446
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)