Skip to content

Commit d5e8571

Browse files
committed
feat(wire): adds a new example using lambda function callback
1 parent 12bae35 commit d5e8571

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This example demonstrates the use of functional callbacks with the Wire library
2+
// for I2C slave communication. It shows how to handle requests and data reception
3+
4+
#include "Wire.h"
5+
6+
#define I2C_DEV_ADDR 0x55
7+
8+
uint32_t i = 0;
9+
10+
void setup() {
11+
Serial.begin(115200);
12+
Serial.setDebugOutput(true);
13+
14+
Wire.onRequest([]() {
15+
Wire.print(i++);
16+
Wire.print(" Packets.");
17+
Serial.println("onRequest");
18+
});
19+
20+
Wire.onReceive([](int len) {
21+
Serial.printf("onReceive[%d]: ", len);
22+
while (Wire.available()) {
23+
Serial.write(Wire.read());
24+
}
25+
Serial.println();
26+
});
27+
28+
Wire.begin((uint8_t)I2C_DEV_ADDR);
29+
30+
#if CONFIG_IDF_TARGET_ESP32
31+
char message[64];
32+
snprintf(message, 64, "%lu Packets.", i++);
33+
Wire.slaveWrite((uint8_t *)message, strlen(message));
34+
#endif
35+
}
36+
37+
void loop() {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"requires": [
3+
"CONFIG_SOC_I2C_SUPPORT_SLAVE=y"
4+
]
5+
}

0 commit comments

Comments
 (0)