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