Richard Sewell
/
ucam
This is the code we showed at Uncraftivism
SerialBuffered.h@2:01115080f6da, 2009-12-14 (annotated)
- Committer:
- jarkman
- Date:
- Mon Dec 14 08:28:21 2009 +0000
- Revision:
- 2:01115080f6da
- Parent:
- 0:57f4fdadc97f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jarkman | 0:57f4fdadc97f | 1 | #pragma once |
jarkman | 0:57f4fdadc97f | 2 | |
jarkman | 0:57f4fdadc97f | 3 | // This is a buffered serial reading class, using the serial interrupt introduced in mbed library version 18 on 17/11/09 |
jarkman | 0:57f4fdadc97f | 4 | |
jarkman | 0:57f4fdadc97f | 5 | // In the simplest case, construct it with a buffer size at least equal to the largest message you |
jarkman | 0:57f4fdadc97f | 6 | // expect your program to receive in one go. |
jarkman | 0:57f4fdadc97f | 7 | |
jarkman | 0:57f4fdadc97f | 8 | class SerialBuffered : public Serial |
jarkman | 0:57f4fdadc97f | 9 | { |
jarkman | 0:57f4fdadc97f | 10 | public: |
jarkman | 0:57f4fdadc97f | 11 | SerialBuffered( size_t bufferSize, PinName tx, PinName rx ); |
jarkman | 0:57f4fdadc97f | 12 | virtual ~SerialBuffered(); |
jarkman | 0:57f4fdadc97f | 13 | |
jarkman | 0:57f4fdadc97f | 14 | int getc(); // will block till the next character turns up, or return -1 if there is a timeout |
jarkman | 0:57f4fdadc97f | 15 | |
jarkman | 0:57f4fdadc97f | 16 | int readable(); // returns 1 if there is a character available to read, 0 otherwise |
jarkman | 0:57f4fdadc97f | 17 | |
jarkman | 0:57f4fdadc97f | 18 | void setTimeout( float seconds ); // maximum time in seconds that getc() should block |
jarkman | 0:57f4fdadc97f | 19 | // while waiting for a character |
jarkman | 0:57f4fdadc97f | 20 | // Pass -1 to disable the timeout. |
jarkman | 0:57f4fdadc97f | 21 | |
jarkman | 0:57f4fdadc97f | 22 | size_t readBytes( uint8_t *bytes, size_t requested ); // read requested bytes into a buffer, |
jarkman | 0:57f4fdadc97f | 23 | // return number actually read, |
jarkman | 0:57f4fdadc97f | 24 | // which may be less than requested if there has been a timeout |
jarkman | 0:57f4fdadc97f | 25 | |
jarkman | 0:57f4fdadc97f | 26 | |
jarkman | 0:57f4fdadc97f | 27 | private: |
jarkman | 0:57f4fdadc97f | 28 | |
jarkman | 0:57f4fdadc97f | 29 | void handleInterrupt(); |
jarkman | 0:57f4fdadc97f | 30 | |
jarkman | 0:57f4fdadc97f | 31 | |
jarkman | 0:57f4fdadc97f | 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:57f4fdadc97f | 33 | uint16_t m_contentStart; // index of first bytes of content |
jarkman | 0:57f4fdadc97f | 34 | uint16_t m_contentEnd; // index of bytes after last byte of content |
jarkman | 0:57f4fdadc97f | 35 | uint16_t m_buffSize; |
jarkman | 0:57f4fdadc97f | 36 | float m_timeout; |
jarkman | 0:57f4fdadc97f | 37 | Timer m_timer; |
jarkman | 0:57f4fdadc97f | 38 | |
jarkman | 0:57f4fdadc97f | 39 | }; |