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

Committer:
AkinoriHashimoto
Date:
Mon Nov 02 06:59:10 2015 +0000
Revision:
2:db4675083c8c
Parent:
1:8f2a3144902b
Child:
3:dced590a2d1b
make sample code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AkinoriHashimoto 2:db4675083c8c 1 /**
AkinoriHashimoto 2:db4675083c8c 2 Ring Buffer reconciled with RTOS.
AkinoriHashimoto 2:db4675083c8c 3
AkinoriHashimoto 2:db4675083c8c 4 If with using RTOS, this lib is enabled Mutex.
AkinoriHashimoto 2:db4675083c8c 5 Default RingBuffer size is 256 Bytes, Max size is 1024 Bytes.
AkinoriHashimoto 2:db4675083c8c 6 */
AkinoriHashimoto 2:db4675083c8c 7
AkinoriHashimoto 2:db4675083c8c 8 /**
AkinoriHashimoto 2:db4675083c8c 9 @code
AkinoriHashimoto 2:db4675083c8c 10 class tmpSerial{
AkinoriHashimoto 2:db4675083c8c 11 public:
AkinoriHashimoto 2:db4675083c8c 12 tmpSerial(PinName TX, PinName RX)
AkinoriHashimoto 2:db4675083c8c 13 : serial(TX, RX)
AkinoriHashimoto 2:db4675083c8c 14 {
AkinoriHashimoto 2:db4675083c8c 15 // attach rxIrq. because, rx buf of serial equals to 16 Bytes.
AkinoriHashimoto 2:db4675083c8c 16 this->serial.attach(this, &tmpSerial::_readIrq, Serial::RxIrq);
AkinoriHashimoto 2:db4675083c8c 17 }
AkinoriHashimoto 2:db4675083c8c 18 string read()
AkinoriHashimoto 2:db4675083c8c 19 {
AkinoriHashimoto 2:db4675083c8c 20 if(ringBuf.empty())
AkinoriHashimoto 2:db4675083c8c 21 return "";
AkinoriHashimoto 2:db4675083c8c 22 // rxStr is not empty.
AkinoriHashimoto 2:db4675083c8c 23 return ringBuf.get();
AkinoriHashimoto 2:db4675083c8c 24 }
AkinoriHashimoto 2:db4675083c8c 25 private:
AkinoriHashimoto 2:db4675083c8c 26 Serial serial;
AkinoriHashimoto 2:db4675083c8c 27 RingBuffer ringBuf;
AkinoriHashimoto 2:db4675083c8c 28 void _readIrq(void)
AkinoriHashimoto 2:db4675083c8c 29 {
AkinoriHashimoto 2:db4675083c8c 30 while(serial.readable())
AkinoriHashimoto 2:db4675083c8c 31 ringBuf.set(hc05.getc());
AkinoriHashimoto 2:db4675083c8c 32 return;
AkinoriHashimoto 2:db4675083c8c 33 }
AkinoriHashimoto 2:db4675083c8c 34 };
AkinoriHashimoto 2:db4675083c8c 35 @endcode
AkinoriHashimoto 2:db4675083c8c 36 */
AkinoriHashimoto 2:db4675083c8c 37
AkinoriHashimoto 0:5373472190f5 38 #pragma once
AkinoriHashimoto 0:5373472190f5 39
AkinoriHashimoto 0:5373472190f5 40 #include "mbed.h"
AkinoriHashimoto 0:5373472190f5 41 #include <string>
AkinoriHashimoto 0:5373472190f5 42
AkinoriHashimoto 1:8f2a3144902b 43 #define MaxBufSize 1024
AkinoriHashimoto 0:5373472190f5 44
AkinoriHashimoto 0:5373472190f5 45 #ifdef RTOS_H
AkinoriHashimoto 0:5373472190f5 46 extern Mutex mutex;
AkinoriHashimoto 0:5373472190f5 47 #endif
AkinoriHashimoto 0:5373472190f5 48
AkinoriHashimoto 0:5373472190f5 49 class RingBuffer
AkinoriHashimoto 0:5373472190f5 50 {
AkinoriHashimoto 0:5373472190f5 51 public:
AkinoriHashimoto 1:8f2a3144902b 52 // Reccomend 2^n: pow(2,n)
AkinoriHashimoto 0:5373472190f5 53 RingBuffer(unsigned int size= 256);
AkinoriHashimoto 0:5373472190f5 54 ~RingBuffer();
AkinoriHashimoto 0:5373472190f5 55
AkinoriHashimoto 2:db4675083c8c 56 /** Returned empty status of ringBuffer.
AkinoriHashimoto 2:db4675083c8c 57 */
AkinoriHashimoto 1:8f2a3144902b 58 bool empty();
AkinoriHashimoto 1:8f2a3144902b 59
AkinoriHashimoto 2:db4675083c8c 60 /** Set char or string to ring-buffer.
AkinoriHashimoto 2:db4675083c8c 61 * @param; char or string.
AkinoriHashimoto 2:db4675083c8c 62 * @return; bool(Buffer FULL: true)
AkinoriHashimoto 2:db4675083c8c 63 */
AkinoriHashimoto 0:5373472190f5 64 bool set(char chr);
AkinoriHashimoto 0:5373472190f5 65 bool set(string &str); // Return Full、参照渡し
AkinoriHashimoto 0:5373472190f5 66
AkinoriHashimoto 2:db4675083c8c 67 /** Get RingBuffer string.
AkinoriHashimoto 2:db4675083c8c 68 * if this called, RingBuffer is cleared.
AkinoriHashimoto 2:db4675083c8c 69 * @return; string: buffered string.
AkinoriHashimoto 2:db4675083c8c 70 */
AkinoriHashimoto 0:5373472190f5 71 string get();
AkinoriHashimoto 1:8f2a3144902b 72
AkinoriHashimoto 0:5373472190f5 73 private:
AkinoriHashimoto 1:8f2a3144902b 74 volatile bool _empty;
AkinoriHashimoto 1:8f2a3144902b 75 unsigned short idxF, idxR; // front, rear
AkinoriHashimoto 0:5373472190f5 76 char *buf;
AkinoriHashimoto 0:5373472190f5 77 unsigned int bufSize;
AkinoriHashimoto 1:8f2a3144902b 78 // %
AkinoriHashimoto 1:8f2a3144902b 79 bool isPowers2;
AkinoriHashimoto 1:8f2a3144902b 80 void modulo(unsigned short &idx);
AkinoriHashimoto 0:5373472190f5 81 };
AkinoriHashimoto 0:5373472190f5 82
AkinoriHashimoto 0:5373472190f5 83 // eof