Modified BufferedSerial - callback when data received - baudrate as ctor param
Fork of BufferedSerial by
Diff: BufferedSerial.cpp
- Revision:
- 14:f198c9f76382
- Parent:
- 13:006e7d23b08a
--- a/BufferedSerial.cpp Sat Jan 28 16:12:14 2017 +0000 +++ b/BufferedSerial.cpp Fri Apr 07 09:11:19 2017 +0000 @@ -23,12 +23,12 @@ #include "BufferedSerial.h" #include <stdarg.h> -BufferedSerial::BufferedSerial(PinName tx, PinName rx, uint32_t buf_size, uint32_t tx_multiple, const char* name) - : RawSerial(tx, rx) , _rxbuf(buf_size), _txbuf((uint32_t)(tx_multiple*buf_size)) +BufferedSerial::BufferedSerial(PinName tx, PinName rx, int baud, uint32_t buf_size, uint32_t tx_multiple, const char* name) + : RawSerial(tx, rx, baud) , _rxbuf(buf_size), _txbuf((uint32_t)(tx_multiple*buf_size)) { RawSerial::attach(callback(this, &BufferedSerial::rxIrq), Serial::RxIrq); this->_buf_size = buf_size; - this->_tx_multiple = tx_multiple; + this->_tx_multiple = tx_multiple; return; } @@ -67,18 +67,18 @@ { if (s != NULL) { const char* ptr = s; - + while(*(ptr) != 0) { _txbuf = *(ptr++); } _txbuf = '\n'; // done per puts definition BufferedSerial::prime(); - + return (ptr - s) + 1; } return 0; } - +/* int BufferedSerial::printf(const char* format, ...) { char buffer[this->_buf_size]; @@ -99,29 +99,40 @@ return r; } +*/ ssize_t BufferedSerial::write(const void *s, size_t length) { if (s != NULL && length > 0) { const char* ptr = (const char*)s; const char* end = ptr + length; - + while (ptr != end) { _txbuf = *(ptr++); } BufferedSerial::prime(); - + return ptr - (const char*)s; } return 0; } +void BufferedSerial::attach(Callback<void()> func) +{ + if(func) { + this->_rx.attach(func); + } +} + void BufferedSerial::rxIrq(void) { // read from the peripheral and make sure something is available if(serial_readable(&_serial)) { _rxbuf = serial_getc(&_serial); // if so load them into a buffer + //call a potential handler + _rx.call(); + } return;