@@ -347,20 +347,147 @@ This function will return ``true`` if the peripheral was initialized correctly.
347347onReceive
348348^^^^^^^^^
349349
350- The ``onReceive `` function is used to define the callback for the data received from the master.
350+ The ``onReceive `` function is used to define the callback for data received from the master device .
351351
352352.. code-block :: arduino
353353
354- void onReceive( void (*)(int) );
354+ void onReceive(const std::function<void(int)>& callback);
355+
356+ **Function Signature: **
357+
358+ The callback function must have the signature ``void(int numBytes) `` where ``numBytes `` indicates how many bytes were received from the master.
359+
360+ **Usage Examples: **
361+
362+ .. code-block :: arduino
363+
364+ // Method 1: Regular function
365+ void handleReceive(int numBytes) {
366+ Serial.printf("Received %d bytes: ", numBytes);
367+ while (Wire.available()) {
368+ char c = Wire.read();
369+ Serial.print(c);
370+ }
371+ Serial.println();
372+ }
373+ Wire.onReceive(handleReceive);
374+
375+ // Method 2: Lambda function
376+ Wire.onReceive([](int numBytes) {
377+ Serial.printf("Master sent %d bytes\n", numBytes);
378+ while (Wire.available()) {
379+ uint8_t data = Wire.read();
380+ // Process received data
381+ Serial.printf("Data: 0x%02X\n", data);
382+ }
383+ });
384+
385+ // Method 3: Lambda with capture (for accessing variables)
386+ int deviceId = 42;
387+ Wire.onReceive([deviceId](int numBytes) {
388+ Serial.printf("Device %d received %d bytes\n", deviceId, numBytes);
389+ // Process data...
390+ });
391+
392+ // Method 4: Using std::function variable
393+ std::function<void(int)> receiveHandler = [](int bytes) {
394+ Serial.printf("Handling %d received bytes\n", bytes);
395+ };
396+ Wire.onReceive(receiveHandler);
397+
398+ // Method 5: Class member function (using lambda wrapper)
399+ class I2CDevice {
400+ private:
401+ int deviceAddress;
402+ public:
403+ I2CDevice(int addr) : deviceAddress(addr) {}
404+
405+ void handleReceive(int numBytes) {
406+ Serial.printf("Device 0x%02X received %d bytes\n", deviceAddress, numBytes);
407+ }
408+
409+ void setup() {
410+ Wire.onReceive([this](int bytes) {
411+ this->handleReceive(bytes);
412+ });
413+ }
414+ };
415+
416+ .. note ::
417+ The ``onReceive `` callback is triggered when the I2C master sends data to this slave device.
418+ Use ``Wire.available() `` and ``Wire.read() `` inside the callback to retrieve the received data.
355419
356420onRequest
357421^^^^^^^^^
358422
359- The ``onRequest `` function is used to define the callback for the data to be send to the master.
423+ The ``onRequest `` function is used to define the callback for responding to master read requests.
424+
425+ .. code-block :: arduino
426+
427+ void onRequest(const std::function<void()>& callback);
428+
429+ **Function Signature: **
430+
431+ 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: **
360434
361435.. code-block :: arduino
362436
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)
453+ int deviceStatus = 1;
454+ String deviceName = "Sensor1";
455+ Wire.onRequest([&deviceStatus, &deviceName]() {
456+ Wire.write(deviceStatus);
457+ Wire.write(deviceName.c_str(), deviceName.length());
458+ });
459+
460+ // Method 4: Using std::function variable
461+ std::function<void()> requestHandler = []() {
462+ Wire.write("Hello Master!");
463+ };
464+ Wire.onRequest(requestHandler);
465+
466+ // Method 5: Class member function (using lambda wrapper)
467+ class TemperatureSensor {
468+ private:
469+ float temperature;
470+ public:
471+ void updateTemperature() {
472+ temperature = 25.5; // Read from actual sensor
473+ }
474+
475+ void sendTemperature() {
476+ // Convert float to bytes and send
477+ uint8_t* tempBytes = (uint8_t*)&temperature;
478+ Wire.write(tempBytes, sizeof(float));
479+ }
480+
481+ void setup() {
482+ Wire.onRequest([this]() {
483+ this->sendTemperature();
484+ });
485+ }
486+ };
487+
488+ .. note ::
489+ The ``onRequest `` callback is triggered when the I2C master requests data from this slave device.
490+ Use ``Wire.write() `` inside the callback to send response data back to the master.
364491
365492slaveWrite
366493^^^^^^^^^^
0 commit comments