Skip to content

Commit 8e1da5c

Browse files
committed
I2C slave: allow function wrapped callbacks
Signed-off-by: hitech95 <[email protected]>
1 parent c6f3f08 commit 8e1da5c

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

libraries/Wire/src/Wire.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,13 @@ void TwoWire::onRequestService(i2c_t *obj)
438438
}
439439

440440
// sets function called on slave write
441-
void TwoWire::onReceive(void (*function)(int))
441+
void TwoWire::onReceive(cb_function_receive_t function)
442442
{
443443
user_onReceive = function;
444444
}
445445

446446
// sets function called on slave read
447-
void TwoWire::onRequest(void (*function)(void))
447+
void TwoWire::onRequest(cb_function_request_t function)
448448
{
449449
user_onRequest = function;
450450
}

libraries/Wire/src/Wire.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef TwoWire_h
2323
#define TwoWire_h
2424

25+
#include <functional>
26+
2527
#include "Stream.h"
2628
#include "Arduino.h"
2729
extern "C" {
@@ -40,6 +42,10 @@ extern "C" {
4042
#define WIRE_HAS_END 1
4143

4244
class TwoWire : public Stream {
45+
public:
46+
typedef std::function<void(int)> cb_function_receive_t;
47+
typedef std::function<void(void)> cb_function_request_t;
48+
4349
private:
4450
uint8_t *rxBuffer;
4551
uint16_t rxBufferAllocated;
@@ -56,8 +62,9 @@ class TwoWire : public Stream {
5662
uint8_t ownAddress;
5763
i2c_t _i2c;
5864

59-
void (*user_onRequest)(void);
60-
void (*user_onReceive)(int);
65+
std::function<void(int)> user_onReceive;
66+
std::function<void(void)> user_onRequest;
67+
6168
static void onRequestService(i2c_t *);
6269
static void onReceiveService(i2c_t *);
6370

@@ -110,8 +117,9 @@ class TwoWire : public Stream {
110117
virtual int read(void);
111118
virtual int peek(void);
112119
virtual void flush(void);
113-
void onReceive(void (*)(int));
114-
void onRequest(void (*)(void));
120+
121+
void onReceive(cb_function_receive_t callback);
122+
void onRequest(cb_function_request_t callback);
115123

116124
inline size_t write(unsigned long n)
117125
{

0 commit comments

Comments
 (0)