Programa mbed1 excelencia
Dependencies: mbed
RingBuffer/Bufferinguint.cpp@0:a5908bca4740, 2017-01-18 (annotated)
- Committer:
- JuanManuelAmador
- Date:
- Wed Jan 18 12:58:15 2017 +0000
- Revision:
- 0:a5908bca4740
primer commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JuanManuelAmador | 0:a5908bca4740 | 1 | #include "Bufferinguint.h" |
JuanManuelAmador | 0:a5908bca4740 | 2 | |
JuanManuelAmador | 0:a5908bca4740 | 3 | Bufferinguint::Bufferinguint() |
JuanManuelAmador | 0:a5908bca4740 | 4 | { |
JuanManuelAmador | 0:a5908bca4740 | 5 | for(int i = 0; i < BUFFERSIZE; i++){ |
JuanManuelAmador | 0:a5908bca4740 | 6 | data[i] = 0; |
JuanManuelAmador | 0:a5908bca4740 | 7 | } |
JuanManuelAmador | 0:a5908bca4740 | 8 | windex = 0; |
JuanManuelAmador | 0:a5908bca4740 | 9 | rindex = 0; |
JuanManuelAmador | 0:a5908bca4740 | 10 | full = false; |
JuanManuelAmador | 0:a5908bca4740 | 11 | empty = true; |
JuanManuelAmador | 0:a5908bca4740 | 12 | bufSize = BUFFERSIZE; |
JuanManuelAmador | 0:a5908bca4740 | 13 | } |
JuanManuelAmador | 0:a5908bca4740 | 14 | |
JuanManuelAmador | 0:a5908bca4740 | 15 | void Bufferinguint::put(unsigned int val) |
JuanManuelAmador | 0:a5908bca4740 | 16 | { |
JuanManuelAmador | 0:a5908bca4740 | 17 | if(!full) |
JuanManuelAmador | 0:a5908bca4740 | 18 | { |
JuanManuelAmador | 0:a5908bca4740 | 19 | data[windex] = val; |
JuanManuelAmador | 0:a5908bca4740 | 20 | windex++; |
JuanManuelAmador | 0:a5908bca4740 | 21 | empty = false; |
JuanManuelAmador | 0:a5908bca4740 | 22 | if(windex >= bufSize) |
JuanManuelAmador | 0:a5908bca4740 | 23 | { |
JuanManuelAmador | 0:a5908bca4740 | 24 | windex = 0; |
JuanManuelAmador | 0:a5908bca4740 | 25 | } |
JuanManuelAmador | 0:a5908bca4740 | 26 | if(getDif() >= bufSize - 1){ |
JuanManuelAmador | 0:a5908bca4740 | 27 | full = true; |
JuanManuelAmador | 0:a5908bca4740 | 28 | } |
JuanManuelAmador | 0:a5908bca4740 | 29 | /*if(windex >= rindex) |
JuanManuelAmador | 0:a5908bca4740 | 30 | { |
JuanManuelAmador | 0:a5908bca4740 | 31 | full = true; |
JuanManuelAmador | 0:a5908bca4740 | 32 | }*/ |
JuanManuelAmador | 0:a5908bca4740 | 33 | } |
JuanManuelAmador | 0:a5908bca4740 | 34 | } |
JuanManuelAmador | 0:a5908bca4740 | 35 | |
JuanManuelAmador | 0:a5908bca4740 | 36 | unsigned int Bufferinguint::get() |
JuanManuelAmador | 0:a5908bca4740 | 37 | { |
JuanManuelAmador | 0:a5908bca4740 | 38 | unsigned int temp = 0; |
JuanManuelAmador | 0:a5908bca4740 | 39 | if(!empty) |
JuanManuelAmador | 0:a5908bca4740 | 40 | { |
JuanManuelAmador | 0:a5908bca4740 | 41 | temp = data[rindex]; |
JuanManuelAmador | 0:a5908bca4740 | 42 | data[rindex] = 0; |
JuanManuelAmador | 0:a5908bca4740 | 43 | full = false; |
JuanManuelAmador | 0:a5908bca4740 | 44 | rindex++; |
JuanManuelAmador | 0:a5908bca4740 | 45 | if(rindex >= bufSize) |
JuanManuelAmador | 0:a5908bca4740 | 46 | { |
JuanManuelAmador | 0:a5908bca4740 | 47 | rindex = 0; |
JuanManuelAmador | 0:a5908bca4740 | 48 | } |
JuanManuelAmador | 0:a5908bca4740 | 49 | if(getDif() == 0){ |
JuanManuelAmador | 0:a5908bca4740 | 50 | empty = true; |
JuanManuelAmador | 0:a5908bca4740 | 51 | } |
JuanManuelAmador | 0:a5908bca4740 | 52 | /*if(rindex >= windex) |
JuanManuelAmador | 0:a5908bca4740 | 53 | { |
JuanManuelAmador | 0:a5908bca4740 | 54 | empty = true; |
JuanManuelAmador | 0:a5908bca4740 | 55 | }*/ |
JuanManuelAmador | 0:a5908bca4740 | 56 | } |
JuanManuelAmador | 0:a5908bca4740 | 57 | return temp; |
JuanManuelAmador | 0:a5908bca4740 | 58 | } |
JuanManuelAmador | 0:a5908bca4740 | 59 | |
JuanManuelAmador | 0:a5908bca4740 | 60 | bool Bufferinguint::isFull() |
JuanManuelAmador | 0:a5908bca4740 | 61 | { |
JuanManuelAmador | 0:a5908bca4740 | 62 | return full; |
JuanManuelAmador | 0:a5908bca4740 | 63 | } |
JuanManuelAmador | 0:a5908bca4740 | 64 | |
JuanManuelAmador | 0:a5908bca4740 | 65 | bool Bufferinguint::isEmpty() |
JuanManuelAmador | 0:a5908bca4740 | 66 | { |
JuanManuelAmador | 0:a5908bca4740 | 67 | return empty; |
JuanManuelAmador | 0:a5908bca4740 | 68 | } |
JuanManuelAmador | 0:a5908bca4740 | 69 | |
JuanManuelAmador | 0:a5908bca4740 | 70 | int Bufferinguint::getSize() |
JuanManuelAmador | 0:a5908bca4740 | 71 | { |
JuanManuelAmador | 0:a5908bca4740 | 72 | return bufSize; |
JuanManuelAmador | 0:a5908bca4740 | 73 | } |
JuanManuelAmador | 0:a5908bca4740 | 74 | |
JuanManuelAmador | 0:a5908bca4740 | 75 | unsigned int Bufferinguint::getWritingIndex() |
JuanManuelAmador | 0:a5908bca4740 | 76 | { |
JuanManuelAmador | 0:a5908bca4740 | 77 | return windex; |
JuanManuelAmador | 0:a5908bca4740 | 78 | } |
JuanManuelAmador | 0:a5908bca4740 | 79 | |
JuanManuelAmador | 0:a5908bca4740 | 80 | unsigned int Bufferinguint::getReadingIndex() |
JuanManuelAmador | 0:a5908bca4740 | 81 | { |
JuanManuelAmador | 0:a5908bca4740 | 82 | return rindex; |
JuanManuelAmador | 0:a5908bca4740 | 83 | } |
JuanManuelAmador | 0:a5908bca4740 | 84 | |
JuanManuelAmador | 0:a5908bca4740 | 85 | unsigned int Bufferinguint::getDif() |
JuanManuelAmador | 0:a5908bca4740 | 86 | { |
JuanManuelAmador | 0:a5908bca4740 | 87 | unsigned int dif = 0; |
JuanManuelAmador | 0:a5908bca4740 | 88 | if((int)(windex-rindex)>=0) { dif = windex-rindex; } |
JuanManuelAmador | 0:a5908bca4740 | 89 | else { dif = bufSize+windex-rindex; } |
JuanManuelAmador | 0:a5908bca4740 | 90 | return dif; |
JuanManuelAmador | 0:a5908bca4740 | 91 | } |