Ring Buffer reconciled with RTOS. If with using RTOS, this lib is enabled Mutex. Default RingBuffer size is 256 Bytes, Max size is 1024 Bytes.

Dependents:   RN41 HC05 HC05 mySerial ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RingBuffer.h Source File

RingBuffer.h

00001 /**
00002 Ring Buffer reconciled with RTOS.
00003 
00004 Default RingBuffer size is 256 Bytes, Max size is 1024 Bytes.
00005 */
00006 
00007 /**
00008 @code
00009 class tmpSerial{
00010 public:
00011     tmpSerial(PinName TX, PinName RX)
00012         : serial(TX, RX)
00013     {
00014         // attach rxIrq. because, rx buf of serial equals to 16 Bytes.
00015         this->serial.attach(this, &tmpSerial::_readIrq, Serial::RxIrq);
00016     }
00017     string read()
00018     {
00019         if(ringBuf.empty())
00020             return "";
00021         // rxStr is not empty.
00022         return ringBuf.get();
00023     }
00024 private:
00025     Serial serial;
00026     RingBuffer ringBuf;
00027     void _readIrq(void)
00028     {
00029         while(serial.readable())
00030             ringBuf.set(hc05.getc());
00031         return;
00032     }
00033 };
00034 @endcode
00035 */
00036 
00037 #pragma once
00038 
00039 #include "mbed.h"
00040 #include <string>
00041 
00042 #define MaxBufSize     1024
00043 /*
00044 #ifdef RTOS_H
00045 extern Mutex mutex;
00046 #endif
00047 */
00048 class RingBuffer
00049 {
00050 public:
00051     // Reccomend 2^n: pow(2,n)
00052     RingBuffer(unsigned int size= 256);
00053     ~RingBuffer();
00054 
00055     /** Returned empty status of ringBuffer.
00056      */
00057     bool empty();
00058     
00059     /** Returned boolean of find CR(\r)
00060      */
00061     bool chkCR();
00062 
00063     /** Set char or string to ring-buffer.
00064      *  @param;     char or string.
00065      *  @return;    bool(Buffer FULL: true)
00066      */
00067     bool set(char chr);
00068     bool set(string &str);   // Return Full、参照渡し
00069 
00070     /** Get RingBuffer string.
00071      *  if this called, RingBuffer is cleared.
00072      *  @return;    string: buffered string.
00073      */
00074     string get();
00075     char getc();
00076     string getLine();
00077     
00078 private:
00079     volatile bool _empty;
00080     unsigned short idxF, idxR;    // front, rear
00081     char *buf;
00082     unsigned int bufSize;
00083     // %
00084     bool isPowers2;
00085     void modulo(unsigned short &idx);
00086 };
00087 
00088 // eof