Skip to content

Commit 67c3205

Browse files
committed
feat(wire): adds std::functional to slave callback functions
1 parent 87b718a commit 67c3205

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

libraries/Wire/src/Wire.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ TwoWire::TwoWire(uint8_t bus_num)
4848
#endif
4949
#if SOC_I2C_SUPPORT_SLAVE
5050
,
51-
is_slave(false), user_onRequest(NULL), user_onReceive(NULL)
51+
is_slave(false), user_onRequest(nullptr), user_onReceive(nullptr)
5252
#endif /* SOC_I2C_SUPPORT_SLAVE */
5353
{
5454
}
@@ -596,14 +596,14 @@ void TwoWire::flush() {
596596
//i2cFlush(num); // cleanup
597597
}
598598

599-
void TwoWire::onReceive(void (*function)(int)) {
599+
void TwoWire::onReceive(user_onReceive_t function) {
600600
#if SOC_I2C_SUPPORT_SLAVE
601601
user_onReceive = function;
602602
#endif
603603
}
604604

605605
// sets function called on slave read
606-
void TwoWire::onRequest(void (*function)(void)) {
606+
void TwoWire::onRequest(user_onRequest_t function) {
607607
#if SOC_I2C_SUPPORT_SLAVE
608608
user_onRequest = function;
609609
#endif

libraries/Wire/src/Wire.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@
4848
#ifndef I2C_BUFFER_LENGTH
4949
#define I2C_BUFFER_LENGTH 128 // Default size, if none is set using Wire::setBuffersize(size_t)
5050
#endif
51-
#if SOC_I2C_SUPPORT_SLAVE
52-
typedef void (*user_onRequest)(void);
53-
typedef void (*user_onReceive)(uint8_t *, int);
54-
#endif /* SOC_I2C_SUPPORT_SLAVE */
5551

5652
class TwoWire : public HardwareI2C {
5753
protected:
@@ -77,8 +73,11 @@ class TwoWire : public HardwareI2C {
7773
private:
7874
#if SOC_I2C_SUPPORT_SLAVE
7975
bool is_slave;
80-
void (*user_onRequest)(void);
81-
void (*user_onReceive)(int);
76+
// functional pointers for user callbacks
77+
using user_onRequest_t = void (*)(void);
78+
using user_onReceive_t = void (*)(int);
79+
user_onRequest_t user_onRequest;
80+
user_onReceive_t user_onReceive;
8281
static void onRequestService(uint8_t, void *);
8382
static void onReceiveService(uint8_t, uint8_t *, size_t, bool, void *);
8483
#endif /* SOC_I2C_SUPPORT_SLAVE */
@@ -116,8 +115,8 @@ class TwoWire : public HardwareI2C {
116115
size_t requestFrom(uint8_t address, size_t len, bool stopBit) override;
117116
size_t requestFrom(uint8_t address, size_t len) override;
118117

119-
void onReceive(void (*)(int)) override;
120-
void onRequest(void (*)(void)) override;
118+
void onReceive(user_onReceive_t) override;
119+
void onRequest(user_onRequest_t) override;
121120

122121
//call setPins() first, so that begin() can be called without arguments from libraries
123122
bool setPins(int sda, int scl);

0 commit comments

Comments
 (0)