Skip to content

Commit 69a3d4c

Browse files
committed
Added hint about receiveEvent interrupt context.
1 parent 3d818b7 commit 69a3d4c

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

libraries/Wire/examples/master_reader_custombuffer/master_reader_custombuffer.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void loop() {
3434
Wire.requestFrom(8, REQUESTED_BYTE_COUNT);
3535

3636
while (Wire.available()) { // slave may send less than requested
37-
char c = Wire.read(); // receive a byte as character
37+
char c = Wire.read(); // receive a byte as character
3838
Serial.print(c); // print the character
3939
}
4040
Serial.println();

libraries/Wire/examples/slave_receiver_custombuffer/slave_receiver_custombuffer.ino

+9-3
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,17 @@ void loop() {
3434

3535
// function that executes whenever data is received from master
3636
// this function is registered as an event, see setup()
37+
//
38+
// Hint: This function is called within an interrupt context.
39+
// That means, that there must be enough space in the Serial output
40+
// buffer for the characters to be printed. Otherwise the
41+
// Serial.print() call will lock up.
3742
void receiveEvent(int howMany) {
3843
while (1 < Wire.available()) { // loop through all but the last
39-
const char c = Wire.read(); // receive byte as a character
40-
Serial.print(c); // print the character
44+
const char c = Wire.read(); // receive byte as a character
45+
Serial.print(c); // print the character
4146
}
42-
const int x = Wire.read(); // receive byte as an integer
47+
const int x = Wire.read(); // receive byte as an integer
4348
Serial.println(x); // print the integer
4449
}
4550

@@ -60,4 +65,5 @@ void printWireBuffersCapacity(Stream& stream) {
6065

6166
stream.print("twi_txBuffer size is ");
6267
stream.println(buffers.twi_txBufferCapacity());
68+
delay(250); // Give time to free up Serial output buffer.
6369
}

0 commit comments

Comments
 (0)