Inherit from Serial and use software buffers for TX and RX. This allows the UART peripherals to operate in a IRQ driven mode. Overrides most (but not all) stdio functions as Serial did
Fork of BufferedSerial by
Revision 11:9c34df035d99, committed 2015-05-03
- Comitter:
- steeven
- Date:
- Sun May 03 02:48:55 2015 +0000
- Parent:
- 10:9ee15ae3d1a3
- Commit message:
- Make putc blocking; Add read line function
Changed in this revision
BufferedSerial.cpp | Show annotated file Show diff for this revision Revisions of this file |
BufferedSerial.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 9ee15ae3d1a3 -r 9c34df035d99 BufferedSerial.cpp --- a/BufferedSerial.cpp Wed Jan 07 18:37:11 2015 +0000 +++ b/BufferedSerial.cpp Sun May 03 02:48:55 2015 +0000 @@ -52,9 +52,36 @@ int BufferedSerial::getc(void) { + while (!readable() && !SERIAL_BASE::readable()) + ; return _rxbuf; } + +char* BufferedSerial::readl(char *s, int size) +{ + if (s == NULL || size <= 0) + return NULL; + + char* ptr = s; + + while (1) { + if (readable()) { + if ((size--) > 0) { + *ptr = _rxbuf; + if (*ptr == '\n' || *ptr == 0) { + break; + } + ptr++; + } else + goto end; + } + } + if (size > 0) + *ptr = 0; + end: return s; +} + int BufferedSerial::putc(int c) { _txbuf = (char)c;
diff -r 9ee15ae3d1a3 -r 9c34df035d99 BufferedSerial.h --- a/BufferedSerial.h Wed Jan 07 18:37:11 2015 +0000 +++ b/BufferedSerial.h Sun May 03 02:48:55 2015 +0000 @@ -114,6 +114,14 @@ */ virtual int getc(void); + /** + * Read a line from the BufferedSerial Port. + * @param s The string buffer to read from Serial Port + * @param size the max number to read from Serial Port + * @return the string read from Serial Port + */ + char* readl(char *s, int size); + /** Write a single byte to the BufferedSerial Port. * @param c The byte to write to the Serial Port * @return The byte that was written to the Serial Port Buffer