Skip to content

add Serial rx callback function #467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cores/arduino/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class HardwareSerial : public Stream
volatile rx_buffer_index_t _rx_buffer_tail;
volatile tx_buffer_index_t _tx_buffer_head;
volatile tx_buffer_index_t _tx_buffer_tail;
bool (*_rxcallback)(char);

// Don't put any members after these buffers, since only the first
// 32 bytes of this struct can be accessed quickly using the ldd
Expand Down Expand Up @@ -137,8 +138,8 @@ class HardwareSerial : public Stream
// Interrupt handlers - Not intended to be called externally
inline void _rx_complete_irq(void);
void _tx_udr_empty_irq(void);
void setRxCallBack( bool (*CallBackFun)(char)) { _rxcallback = CallBackFun; }
};

#if defined(UBRRH) || defined(UBRR0H)
extern HardwareSerial Serial;
#define HAVE_HWSERIAL0
Expand Down
5 changes: 4 additions & 1 deletion cores/arduino/HardwareSerial_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ HardwareSerial::HardwareSerial(
_ucsra(ucsra), _ucsrb(ucsrb), _ucsrc(ucsrc),
_udr(udr),
_rx_buffer_head(0), _rx_buffer_tail(0),
_tx_buffer_head(0), _tx_buffer_tail(0)
_tx_buffer_head(0), _tx_buffer_tail(0),
_rxcallback(0)
{
}

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

// if we should be storing the received character into the location
Expand Down