buffered_serial.h

00001 /*
00002  * buffered_serial.h
00003  *
00004  * Defines a variable size buffer for received serial data
00005  *
00006  * Original code by: Richard Sewell (11/17/09)
00007  * Changes by: Nick Young (9/26/10)
00008  *
00009  */
00010 
00011 #ifndef BUFFERED_SERIAL_H
00012 #define BUFFERED_SERIAL_H
00013 
00014 /**
00015  * Serial wrapper providing a variable sized RX buffer
00016  *
00017  * Used to store serial data received in a circular buffer in cases
00018  * where the data stream is larger than the hardware buffer (16 bytes)
00019  * and the data is not immediately processed. On RXIRQ interrupt
00020  * (i.e., on receipt of data), new data is retreived from the hardware
00021  * serial buffer and stored in a circular buffer.  If the circular
00022  * buffer becomes full, no additional data will be added until some
00023  * of the current data has been removed.
00024  *
00025  */
00026 class BufferedSerial : public Serial
00027 {
00028 public:
00029   BufferedSerial(PinName tx, PinName rx, uint8_t bufferSize);
00030   virtual ~BufferedSerial();
00031 
00032   bool readable();
00033   uint8_t availableBytes();
00034   uint8_t peek();
00035   uint8_t getc();
00036   long readLong();
00037   void readBytes(uint8_t *bytes, size_t requested);
00038   void flushBuffer();
00039   void writeLong(long data);
00040   
00041   /**
00042   * Set a callback function when buffer fills
00043   *
00044   * Set a member function on a particular object to be
00045   * called once the buffer becomes full.  While this 
00046   * this callback function is running, no additional
00047   * data will be received (even if the callback function
00048   * immediately empties the buffer).  If this becomes an
00049   * issue, we can add a Timeout to call the callback
00050   * 1us later, which would allow us to receive data again.
00051   * 
00052   * @param T* object
00053   *  The object on whom the callback should be made
00054   * @param void (T::*member)(void)
00055   *  The member which should be called back
00056   */ 
00057   template<typename T> void setFullCallback(T * object, void (T::*member)(void))
00058   {
00059     callback_ = FunctionPointer(object, member);
00060     has_callback_ = true;
00061   }
00062 
00063 private:
00064   void handleInterrupt();
00065 
00066   /// Circular buffer of bytes
00067   uint8_t *buffer_;
00068   /// Index of the first byte of data
00069   uint8_t content_start_;
00070   /// Index of the first free byte (index after the last byte of data)
00071   uint8_t content_end_;
00072   /// Number of bytes in the buffer
00073   uint8_t buffer_size_;
00074   /// Number of bytes filled in the data buffer (ready to be read)
00075   uint8_t available_bytes_;
00076   /// Callback method handle
00077   FunctionPointer callback_;
00078   bool has_callback_;
00079 };
00080 
00081 #endif /* BUFFERED_SERIAL_H */
Generated on Sun May 8 08:41:32 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3