You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR enhances the Wire library to support std::function–based callbacks for I2C slave mode, enabling the use of lambdas and captured contexts.
- Replaces raw function pointers in TwoWire and HardwareI2C with std::function for onRequest and onReceive
- Updates constructors, method signatures, and default initializations to use std::function
- Adds new example sketch, CI config, and documentation updates demonstrating the functional callback API
The callback function must have the signature ``void()`` with no parameters. This callback is triggered when the master requests data from this slave device.
432
+
433
+
**Usage Examples:**
360
434
361
435
.. code-block:: arduino
362
436
363
-
void onRequest( void (*)(void) );
437
+
// Method 1: Regular function
438
+
void handleRequest() {
439
+
static int counter = 0;
440
+
Wire.printf("Response #%d", counter++);
441
+
}
442
+
Wire.onRequest(handleRequest);
443
+
444
+
// Method 2: Lambda function
445
+
Wire.onRequest([]() {
446
+
// Send sensor data to master
447
+
int sensorValue = analogRead(A0);
448
+
Wire.write(sensorValue >> 8); // High byte
449
+
Wire.write(sensorValue & 0xFF); // Low byte
450
+
});
451
+
452
+
// Method 3: Lambda with capture (for accessing variables)
0 commit comments