This is the SerialBuffered class, which wraps Serial and adds interrupt-powered buffered reading (so it will catch inbound data even when your app is busy doing something else) and simple read timeouts. There's a tiny demo app in there too.
SerialBuffered.h@0:b55ca3bb8017, 2009-11-18 (annotated)
- Committer:
- jarkman
- Date:
- Wed Nov 18 21:27:15 2009 +0000
- Revision:
- 0:b55ca3bb8017
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jarkman | 0:b55ca3bb8017 | 1 | #pragma once |
jarkman | 0:b55ca3bb8017 | 2 | |
jarkman | 0:b55ca3bb8017 | 3 | // This is a buffered serial reading class, using the serial interrupt introduced in mbed library version 18 on 17/11/09 |
jarkman | 0:b55ca3bb8017 | 4 | |
jarkman | 0:b55ca3bb8017 | 5 | // In the simplest case, construct it with a buffer size at least equal to the largest message you |
jarkman | 0:b55ca3bb8017 | 6 | // expect your program to receive in one go. |
jarkman | 0:b55ca3bb8017 | 7 | |
jarkman | 0:b55ca3bb8017 | 8 | class SerialBuffered : public Serial |
jarkman | 0:b55ca3bb8017 | 9 | { |
jarkman | 0:b55ca3bb8017 | 10 | public: |
jarkman | 0:b55ca3bb8017 | 11 | SerialBuffered( size_t bufferSize, PinName tx, PinName rx ); |
jarkman | 0:b55ca3bb8017 | 12 | virtual ~SerialBuffered(); |
jarkman | 0:b55ca3bb8017 | 13 | |
jarkman | 0:b55ca3bb8017 | 14 | int getc(); // will block till the next character turns up, or return -1 if there is a timeout |
jarkman | 0:b55ca3bb8017 | 15 | |
jarkman | 0:b55ca3bb8017 | 16 | int readable(); // returns 1 if there is a character available to read, 0 otherwise |
jarkman | 0:b55ca3bb8017 | 17 | |
jarkman | 0:b55ca3bb8017 | 18 | void setTimeout( float seconds ); // maximum time in seconds that getc() should block |
jarkman | 0:b55ca3bb8017 | 19 | // while waiting for a character |
jarkman | 0:b55ca3bb8017 | 20 | // Pass -1 to disable the timeout. |
jarkman | 0:b55ca3bb8017 | 21 | |
jarkman | 0:b55ca3bb8017 | 22 | size_t readBytes( uint8_t *bytes, size_t requested ); // read requested bytes into a buffer, |
jarkman | 0:b55ca3bb8017 | 23 | // return number actually read, |
jarkman | 0:b55ca3bb8017 | 24 | // which may be less than requested if there has been a timeout |
jarkman | 0:b55ca3bb8017 | 25 | |
jarkman | 0:b55ca3bb8017 | 26 | |
jarkman | 0:b55ca3bb8017 | 27 | private: |
jarkman | 0:b55ca3bb8017 | 28 | |
jarkman | 0:b55ca3bb8017 | 29 | void handleInterrupt(); |
jarkman | 0:b55ca3bb8017 | 30 | |
jarkman | 0:b55ca3bb8017 | 31 | |
jarkman | 0:b55ca3bb8017 | 32 | uint8_t *m_buff; // points at a circular buffer, containing data from m_contentStart, for m_contentSize bytes, wrapping when you get to the end |
jarkman | 0:b55ca3bb8017 | 33 | uint16_t m_contentStart; // index of first bytes of content |
jarkman | 0:b55ca3bb8017 | 34 | uint16_t m_contentEnd; // index of bytes after last byte of content |
jarkman | 0:b55ca3bb8017 | 35 | uint16_t m_buffSize; |
jarkman | 0:b55ca3bb8017 | 36 | float m_timeout; |
jarkman | 0:b55ca3bb8017 | 37 | Timer m_timer; |
jarkman | 0:b55ca3bb8017 | 38 | |
jarkman | 0:b55ca3bb8017 | 39 | }; |