Skip to content

feat(wire): std::functional Wire slave callback functions #11582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 12, 2025
Merged
6 changes: 4 additions & 2 deletions cores/esp32/HardwareI2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <inttypes.h>
#include "Stream.h"
#include <functional>

class HardwareI2C : public Stream {
public:
Expand All @@ -36,6 +37,7 @@ class HardwareI2C : public Stream {
virtual size_t requestFrom(uint8_t address, size_t len, bool stopBit) = 0;
virtual size_t requestFrom(uint8_t address, size_t len) = 0;

virtual void onReceive(void (*)(int)) = 0;
virtual void onRequest(void (*)(void)) = 0;
// Update base class to use std::function
virtual void onReceive(std::function<void(int)>) = 0;
virtual void onRequest(std::function<void()>) = 0;
};
6 changes: 3 additions & 3 deletions libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TwoWire::TwoWire(uint8_t bus_num)
#endif
#if SOC_I2C_SUPPORT_SLAVE
,
is_slave(false), user_onRequest(NULL), user_onReceive(NULL)
is_slave(false), user_onRequest(nullptr), user_onReceive(nullptr)
#endif /* SOC_I2C_SUPPORT_SLAVE */
{
}
Expand Down Expand Up @@ -596,14 +596,14 @@ void TwoWire::flush() {
//i2cFlush(num); // cleanup
}

void TwoWire::onReceive(void (*function)(int)) {
void TwoWire::onReceive(std::function<void(int)> function) {
#if SOC_I2C_SUPPORT_SLAVE
user_onReceive = function;
#endif
}

// sets function called on slave read
void TwoWire::onRequest(void (*function)(void)) {
void TwoWire::onRequest(std::function<void()> function) {
#if SOC_I2C_SUPPORT_SLAVE
user_onRequest = function;
#endif
Expand Down
13 changes: 5 additions & 8 deletions libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@
#ifndef I2C_BUFFER_LENGTH
#define I2C_BUFFER_LENGTH 128 // Default size, if none is set using Wire::setBuffersize(size_t)
#endif
#if SOC_I2C_SUPPORT_SLAVE
typedef void (*user_onRequest)(void);
typedef void (*user_onReceive)(uint8_t *, int);
#endif /* SOC_I2C_SUPPORT_SLAVE */

class TwoWire : public HardwareI2C {
protected:
Expand All @@ -77,8 +73,9 @@ class TwoWire : public HardwareI2C {
private:
#if SOC_I2C_SUPPORT_SLAVE
bool is_slave;
void (*user_onRequest)(void);
void (*user_onReceive)(int);
// functional pointers for user callbacks
std::function<void()> user_onRequest;
std::function<void(int)> user_onReceive;
static void onRequestService(uint8_t, void *);
static void onReceiveService(uint8_t, uint8_t *, size_t, bool, void *);
#endif /* SOC_I2C_SUPPORT_SLAVE */
Expand Down Expand Up @@ -116,8 +113,8 @@ class TwoWire : public HardwareI2C {
size_t requestFrom(uint8_t address, size_t len, bool stopBit) override;
size_t requestFrom(uint8_t address, size_t len) override;

void onReceive(void (*)(int)) override;
void onRequest(void (*)(void)) override;
void onReceive(std::function<void(int)>) override;
void onRequest(std::function<void()>) override;

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