Skip to content

Commit e2a9e7f

Browse files
committed
add Serial rx callback function
example: //return false drop char, bool rxcap(char ch) { if (ch == '1') { //if get char '1' Serial.println("callback!"); Serial.setRxCallBack(0); //disable CallBack return false; //del char '1' from rx buff } return true; } void setup() { Serial.begin(115200); Serial.setRxCallBack(rxcap); } void loop() { while(Serial.available()) Serial.write(Serial.read()); } ~
1 parent 0d4f175 commit e2a9e7f

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

cores/arduino/HardwareSerial.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class HardwareSerial : public Stream
106106
volatile rx_buffer_index_t _rx_buffer_tail;
107107
volatile tx_buffer_index_t _tx_buffer_head;
108108
volatile tx_buffer_index_t _tx_buffer_tail;
109+
bool (*_rxcallback)(char);
109110

110111
// Don't put any members after these buffers, since only the first
111112
// 32 bytes of this struct can be accessed quickly using the ldd
@@ -137,8 +138,8 @@ class HardwareSerial : public Stream
137138
// Interrupt handlers - Not intended to be called externally
138139
inline void _rx_complete_irq(void);
139140
void _tx_udr_empty_irq(void);
141+
void setRxCallBack( bool (*CallBackFun)(char)) { _rxcallback = CallBackFun; }
140142
};
141-
142143
#if defined(UBRRH) || defined(UBRR0H)
143144
extern HardwareSerial Serial;
144145
#define HAVE_HWSERIAL0

cores/arduino/HardwareSerial_private.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ HardwareSerial::HardwareSerial(
9292
_ucsra(ucsra), _ucsrb(ucsrb), _ucsrc(ucsrc),
9393
_udr(udr),
9494
_rx_buffer_head(0), _rx_buffer_tail(0),
95-
_tx_buffer_head(0), _tx_buffer_tail(0)
95+
_tx_buffer_head(0), _tx_buffer_tail(0),
96+
_rxcallback(0)
9697
{
9798
}
9899

@@ -104,6 +105,8 @@ void HardwareSerial::_rx_complete_irq(void)
104105
// No Parity error, read byte and store it in the buffer if there is
105106
// room
106107
unsigned char c = *_udr;
108+
if(_rxcallback != 0 && !_rxcallback(c))
109+
return;
107110
rx_buffer_index_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE;
108111

109112
// if we should be storing the received character into the location

0 commit comments

Comments
 (0)