File tree Expand file tree Collapse file tree 6 files changed +18
-18
lines changed
examples/WireSlaveFunctionalCallback Expand file tree Collapse file tree 6 files changed +18
-18
lines changed Original file line number Diff line number Diff line change @@ -38,6 +38,6 @@ class HardwareI2C : public Stream {
38
38
virtual size_t requestFrom (uint8_t address, size_t len) = 0;
39
39
40
40
// Update base class to use std::function
41
- virtual void onReceive (const std::function<void (int )>&) = 0;
42
- virtual void onRequest (const std::function<void ()>&) = 0;
41
+ virtual void onReceive (const std::function<void (int )> &) = 0;
42
+ virtual void onRequest (const std::function<void ()> &) = 0;
43
43
};
Original file line number Diff line number Diff line change @@ -401,20 +401,20 @@ The callback function must have the signature ``void(int numBytes)`` where ``num
401
401
int deviceAddress;
402
402
public:
403
403
I2CDevice(int addr) : deviceAddress(addr) {}
404
-
404
+
405
405
void handleReceive(int numBytes) {
406
406
Serial.printf("Device 0x%02X received %d bytes\n", deviceAddress, numBytes);
407
407
}
408
-
408
+
409
409
void setup() {
410
410
Wire.onReceive([this](int bytes) {
411
411
this->handleReceive(bytes);
412
412
});
413
413
}
414
414
};
415
415
416
- .. note ::
417
- The ``onReceive `` callback is triggered when the I2C master sends data to this slave device.
416
+ .. note ::
417
+ The ``onReceive `` callback is triggered when the I2C master sends data to this slave device.
418
418
Use ``Wire.available() `` and ``Wire.read() `` inside the callback to retrieve the received data.
419
419
420
420
onRequest
@@ -471,24 +471,24 @@ The callback function must have the signature ``void()`` with no parameters. Thi
471
471
void updateTemperature() {
472
472
temperature = 25.5; // Read from actual sensor
473
473
}
474
-
474
+
475
475
void sendTemperature() {
476
476
// Convert float to bytes and send
477
477
uint8_t* tempBytes = (uint8_t*)&temperature;
478
478
Wire.write(tempBytes, sizeof(float));
479
479
}
480
-
480
+
481
481
void setup() {
482
482
Wire.onRequest([this]() {
483
483
this->sendTemperature();
484
484
});
485
485
}
486
486
};
487
487
488
- .. note ::
489
- The ``onRequest `` callback is triggered when the I2C master requests data from this slave device.
488
+ .. note ::
489
+ The ``onRequest `` callback is triggered when the I2C master requests data from this slave device.
490
490
Use ``Wire.write() `` inside the callback to send response data back to the master.
491
-
491
+
492
492
slaveWrite
493
493
^^^^^^^^^^
494
494
Original file line number Diff line number Diff line change @@ -16,9 +16,9 @@ void setup() {
16
16
Wire.print (" Packets." );
17
17
Serial.println (" onRequest" );
18
18
});
19
-
19
+
20
20
Wire.onReceive ([](int len) {
21
- Serial.printf (" onReceive[%d]: " , len);
21
+ Serial.printf (" onReceive[%d]: " , len);
22
22
while (Wire.available ()) {
23
23
Serial.write (Wire.read ());
24
24
}
Original file line number Diff line number Diff line change 2
2
"requires" : [
3
3
" CONFIG_SOC_I2C_SUPPORT_SLAVE=y"
4
4
]
5
- }
5
+ }
Original file line number Diff line number Diff line change @@ -596,14 +596,14 @@ void TwoWire::flush() {
596
596
// i2cFlush(num); // cleanup
597
597
}
598
598
599
- void TwoWire::onReceive (const std::function<void (int )>& function) {
599
+ void TwoWire::onReceive (const std::function<void (int )> & function) {
600
600
#if SOC_I2C_SUPPORT_SLAVE
601
601
user_onReceive = function;
602
602
#endif
603
603
}
604
604
605
605
// sets function called on slave read
606
- void TwoWire::onRequest (const std::function<void ()>& function) {
606
+ void TwoWire::onRequest (const std::function<void ()> & function) {
607
607
#if SOC_I2C_SUPPORT_SLAVE
608
608
user_onRequest = function;
609
609
#endif
Original file line number Diff line number Diff line change @@ -112,8 +112,8 @@ class TwoWire : public HardwareI2C {
112
112
size_t requestFrom (uint8_t address, size_t len, bool stopBit) override ;
113
113
size_t requestFrom (uint8_t address, size_t len) override ;
114
114
115
- void onReceive (const std::function<void (int )>&) override ;
116
- void onRequest (const std::function<void ()>&) override ;
115
+ void onReceive (const std::function<void (int )> &) override ;
116
+ void onRequest (const std::function<void ()> &) override ;
117
117
118
118
// call setPins() first, so that begin() can be called without arguments from libraries
119
119
bool setPins (int sda, int scl);
You can’t perform that action at this time.
0 commit comments