I've been picking apart the WireSlave example from Espressif ESP32, I seem to mostly understand how the communication between the Slave and Master works.
Original example code:
https://github.com/espressif/arduino-esp32/blob/master/libraries/Wire/examples/WireMaster/WireMaster.ino#L23
However, the pre-processor code (see below) within setup confuses me. As it seems to work like a macro when WireMaster makes the call to Wire.requestFrom(...)
.
My question is, how is the piece of code being called within the WireMaster loop()
within the Serial Monitor (the "// 473 Packets....." part)
What I assume is pre-processor code within WireSlave
void setup() {
#if CONFIG_IDF_TARGET_ESP32
char message[64];
snprintf(message, 64, "%u Data.", i++); //Formats i++ into %u Data., stored in message
Wire.slaveWrite((uint8_t *)message, strlen(message));
#endif
}
The WireMaster code that calls it
void loop() {
...
error = Wire.requestFrom(I2C_DEV_ADDR, 16); //Make request of length 16, returning number of bytes returned
Serial.printf("requestFrom: %u\n", error);
if (!error) { //If more than 0 bytes returned (there's a message)
Serial.print("Requested data: ");
uint8_t temp[error];
Wire.readBytes(temp, error); //Read number of bytes into temp array
log_print_buf(temp, error); //Print onRequest packets to Serial
}
...
}
WireMaster Serial Monitor
requestFrom: 16
Requested data: 0x34, 0x37, 0x33, 0x20, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x73, 0x2e, 0xff, 0xff, 0xff, 0xff, // 473 Packets.....