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