Skip to content

Commit 3bfadf8

Browse files
committed
Changed example to buffer
1 parent e3298ea commit 3bfadf8

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

cores/esp32/esp32-hal-uart.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
66

7-
// http://www.apache.org/licenses/LICENSE-2.0
7+
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
// Serial, Serial1 and Serial2 are all of the type HardwareSerial.
1+
#define BUFFER_SIZE 8
22

3-
static void IRAM_ATTR onSerialRX(uint8_t c, void* user_arg){
4-
Serial.print(c);
3+
static volatile char inputBuffer[BUFFER_SIZE];
4+
static volatile size_t inputBufferLength = 0;
5+
6+
// Please keep in mind, since the ESP32 is dual core,
7+
// the interrupt will be running on the same core as the setRxInterrupt function was called on.
8+
static void IRAM_ATTR onSerialRX(uint8_t character, void* user_arg){
9+
10+
// Cast the user_arg back to a array
11+
char[] buffer = (char*)user_arg;
512

6-
// Cast the user_arg containing a void* to the Serial device, to HardwareSerial* to be used
7-
HardwareSerial* serial = (HardwareSerial*)user_arg;
8-
serial->print(c);
13+
if(inputBufferLength < BUFFER_SIZE){
14+
buffer[inputBufferLength++] = (char)character;
15+
}
916
}
1017

1118
void setup()
@@ -14,11 +21,22 @@ void setup()
1421
Serial2.begin(115200);
1522

1623
// The user_arg is expected to be a void pointer (void*),
17-
// so take the reference of Serial2, and cast the HardwareSerial* to a void*
18-
Serial2.setRxInterrupt(onSerialRX, (void*)&Serial2);
24+
// so take the reference of the variable, and it to a void*
25+
Serial2.setRxInterrupt(onSerialRX, (void*)&inputBuffer);
1926
}
2027

2128
void loop()
22-
{
23-
delay(1);
29+
{
30+
if(inputBufferLength == (BUFFER_SIZE - 1)){
31+
for(size_t i = 0; i < inputBufferLength; i++){
32+
Serial.write(inputBuffer[i]);
33+
34+
// Clear the buffer
35+
inputBuffer[i] = '\0';
36+
}
37+
// Clear the bufferLength
38+
inputBufferLength = 0;
39+
}
40+
41+
delay(1000); // Wait for one second
2442
}

0 commit comments

Comments
 (0)