![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
aa
CommonLibraries/RingBuffer/RingBuffer.cpp@60:24ebb611f26e, 2019-12-12 (annotated)
- Committer:
- yabahiro
- Date:
- Thu Dec 12 07:16:20 2019 +0000
- Revision:
- 60:24ebb611f26e
- Parent:
- 0:669ef71cba68
aa
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
t_yamamoto | 0:669ef71cba68 | 1 | /* |
t_yamamoto | 0:669ef71cba68 | 2 | * RingBuffer.cpp |
t_yamamoto | 0:669ef71cba68 | 3 | * |
t_yamamoto | 0:669ef71cba68 | 4 | * Created: 2016/08/10 12:14:47 |
t_yamamoto | 0:669ef71cba68 | 5 | * Author: masuk |
t_yamamoto | 0:669ef71cba68 | 6 | */ |
t_yamamoto | 0:669ef71cba68 | 7 | |
t_yamamoto | 0:669ef71cba68 | 8 | #include "mbed.h" |
t_yamamoto | 0:669ef71cba68 | 9 | #include "RingBuffer.h" |
t_yamamoto | 0:669ef71cba68 | 10 | |
t_yamamoto | 0:669ef71cba68 | 11 | namespace RINGBUFFER |
t_yamamoto | 0:669ef71cba68 | 12 | { |
t_yamamoto | 0:669ef71cba68 | 13 | |
t_yamamoto | 0:669ef71cba68 | 14 | RingBuffer::RingBuffer(char *bufPtr, int size) |
t_yamamoto | 0:669ef71cba68 | 15 | { |
t_yamamoto | 0:669ef71cba68 | 16 | Buffer.data = bufPtr; |
t_yamamoto | 0:669ef71cba68 | 17 | Buffer.size = size; |
t_yamamoto | 0:669ef71cba68 | 18 | Buffer.top = 0; |
t_yamamoto | 0:669ef71cba68 | 19 | Buffer.bottom = 0; |
t_yamamoto | 0:669ef71cba68 | 20 | Buffer.fullup = false; |
t_yamamoto | 0:669ef71cba68 | 21 | } |
t_yamamoto | 0:669ef71cba68 | 22 | |
t_yamamoto | 0:669ef71cba68 | 23 | void RingBuffer::PutData(char data, bool ASCIItoNum /* = false */) |
t_yamamoto | 0:669ef71cba68 | 24 | { |
t_yamamoto | 0:669ef71cba68 | 25 | __disable_irq(); |
t_yamamoto | 0:669ef71cba68 | 26 | if(!Buffer.fullup) |
t_yamamoto | 0:669ef71cba68 | 27 | { |
t_yamamoto | 0:669ef71cba68 | 28 | if(ASCIItoNum) Buffer.data[Buffer.top] = data+48; |
t_yamamoto | 0:669ef71cba68 | 29 | else Buffer.data[Buffer.top] = data; |
t_yamamoto | 0:669ef71cba68 | 30 | Buffer.length++; |
t_yamamoto | 0:669ef71cba68 | 31 | Buffer.top++; |
t_yamamoto | 0:669ef71cba68 | 32 | Buffer.top %= Buffer.size; |
t_yamamoto | 0:669ef71cba68 | 33 | Buffer.fullup = false; |
t_yamamoto | 0:669ef71cba68 | 34 | if(Buffer.length>=Buffer.size) Buffer.fullup = true; |
t_yamamoto | 0:669ef71cba68 | 35 | } |
t_yamamoto | 0:669ef71cba68 | 36 | __enable_irq(); |
t_yamamoto | 0:669ef71cba68 | 37 | } |
t_yamamoto | 0:669ef71cba68 | 38 | |
t_yamamoto | 0:669ef71cba68 | 39 | void RingBuffer::PutData(char *data, int length) |
t_yamamoto | 0:669ef71cba68 | 40 | { |
t_yamamoto | 0:669ef71cba68 | 41 | __disable_irq(); |
t_yamamoto | 0:669ef71cba68 | 42 | for(int i=0; i<length; i++) |
t_yamamoto | 0:669ef71cba68 | 43 | { |
t_yamamoto | 0:669ef71cba68 | 44 | if(!Buffer.fullup) |
t_yamamoto | 0:669ef71cba68 | 45 | { |
t_yamamoto | 0:669ef71cba68 | 46 | Buffer.data[Buffer.top] = data[i]; |
t_yamamoto | 0:669ef71cba68 | 47 | Buffer.length++; |
t_yamamoto | 0:669ef71cba68 | 48 | Buffer.top++; |
t_yamamoto | 0:669ef71cba68 | 49 | Buffer.top %= Buffer.size; |
t_yamamoto | 0:669ef71cba68 | 50 | Buffer.fullup = false; |
t_yamamoto | 0:669ef71cba68 | 51 | if(Buffer.length>=Buffer.size) Buffer.fullup = true; |
t_yamamoto | 0:669ef71cba68 | 52 | } |
t_yamamoto | 0:669ef71cba68 | 53 | } |
t_yamamoto | 0:669ef71cba68 | 54 | __enable_irq(); |
t_yamamoto | 0:669ef71cba68 | 55 | } |
t_yamamoto | 0:669ef71cba68 | 56 | |
t_yamamoto | 0:669ef71cba68 | 57 | void RingBuffer::PutData(const char *str) |
t_yamamoto | 0:669ef71cba68 | 58 | { |
t_yamamoto | 0:669ef71cba68 | 59 | __disable_irq(); |
t_yamamoto | 0:669ef71cba68 | 60 | for( ; *str!='\0'; str++) |
t_yamamoto | 0:669ef71cba68 | 61 | { |
t_yamamoto | 0:669ef71cba68 | 62 | if(!Buffer.fullup) |
t_yamamoto | 0:669ef71cba68 | 63 | { |
t_yamamoto | 0:669ef71cba68 | 64 | Buffer.data[Buffer.top] = *str; |
t_yamamoto | 0:669ef71cba68 | 65 | Buffer.length++; |
t_yamamoto | 0:669ef71cba68 | 66 | Buffer.top++; |
t_yamamoto | 0:669ef71cba68 | 67 | Buffer.top %= Buffer.size; |
t_yamamoto | 0:669ef71cba68 | 68 | if(Buffer.length>=Buffer.size) Buffer.fullup = true; |
t_yamamoto | 0:669ef71cba68 | 69 | else Buffer.fullup = false; |
t_yamamoto | 0:669ef71cba68 | 70 | } |
t_yamamoto | 0:669ef71cba68 | 71 | } |
t_yamamoto | 0:669ef71cba68 | 72 | __enable_irq(); |
t_yamamoto | 0:669ef71cba68 | 73 | } |
t_yamamoto | 0:669ef71cba68 | 74 | |
t_yamamoto | 0:669ef71cba68 | 75 | char RingBuffer::GetData() |
t_yamamoto | 0:669ef71cba68 | 76 | { |
t_yamamoto | 0:669ef71cba68 | 77 | __disable_irq(); |
t_yamamoto | 0:669ef71cba68 | 78 | char data; |
t_yamamoto | 0:669ef71cba68 | 79 | if(Buffer.length > 0) |
t_yamamoto | 0:669ef71cba68 | 80 | { |
t_yamamoto | 0:669ef71cba68 | 81 | data = Buffer.data[Buffer.bottom]; |
t_yamamoto | 0:669ef71cba68 | 82 | Buffer.length--; |
t_yamamoto | 0:669ef71cba68 | 83 | Buffer.bottom++; |
t_yamamoto | 0:669ef71cba68 | 84 | Buffer.bottom %= Buffer.size; |
t_yamamoto | 0:669ef71cba68 | 85 | if(Buffer.length<Buffer.size) Buffer.fullup = false; |
t_yamamoto | 0:669ef71cba68 | 86 | } |
t_yamamoto | 0:669ef71cba68 | 87 | else data = '\0'; |
t_yamamoto | 0:669ef71cba68 | 88 | __enable_irq(); |
t_yamamoto | 0:669ef71cba68 | 89 | return data; |
t_yamamoto | 0:669ef71cba68 | 90 | } |
t_yamamoto | 0:669ef71cba68 | 91 | |
t_yamamoto | 0:669ef71cba68 | 92 | bool RingBuffer::IsFullup() |
t_yamamoto | 0:669ef71cba68 | 93 | { |
t_yamamoto | 0:669ef71cba68 | 94 | return Buffer.fullup; |
t_yamamoto | 0:669ef71cba68 | 95 | } |
t_yamamoto | 0:669ef71cba68 | 96 | |
t_yamamoto | 0:669ef71cba68 | 97 | bool RingBuffer::InAnyData() |
t_yamamoto | 0:669ef71cba68 | 98 | { |
t_yamamoto | 0:669ef71cba68 | 99 | if(Buffer.length > 0) return true; |
t_yamamoto | 0:669ef71cba68 | 100 | else return false; |
t_yamamoto | 0:669ef71cba68 | 101 | } |
t_yamamoto | 0:669ef71cba68 | 102 | |
t_yamamoto | 0:669ef71cba68 | 103 | } |