![](/media/cache/profiles/68fc61f6fd79afe9607a3ec58f2e6ad1.jpg.50x50_q85.jpg)
IM920地温観測システム CQ 2017ARMセミナー用サンプルプログラム
Dependencies: C027_Support_ForIM920
Fork of C027_SupportTest by
RingBuf.cpp@35:7838543282c2, 2017-07-13 (annotated)
- Committer:
- ntaka206
- Date:
- Thu Jul 13 00:44:49 2017 +0000
- Revision:
- 35:7838543282c2
???????commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ntaka206 | 35:7838543282c2 | 1 | #include "RingBuf.h" |
ntaka206 | 35:7838543282c2 | 2 | |
ntaka206 | 35:7838543282c2 | 3 | RingBuf::RingBuf(int _size) |
ntaka206 | 35:7838543282c2 | 4 | { |
ntaka206 | 35:7838543282c2 | 5 | buf = new RingBufType[_size]; |
ntaka206 | 35:7838543282c2 | 6 | size = _size; |
ntaka206 | 35:7838543282c2 | 7 | wp = 0; |
ntaka206 | 35:7838543282c2 | 8 | rp = 0; |
ntaka206 | 35:7838543282c2 | 9 | latest = -1; |
ntaka206 | 35:7838543282c2 | 10 | } |
ntaka206 | 35:7838543282c2 | 11 | |
ntaka206 | 35:7838543282c2 | 12 | RingBuf::~RingBuf() |
ntaka206 | 35:7838543282c2 | 13 | { |
ntaka206 | 35:7838543282c2 | 14 | delete [] buf; |
ntaka206 | 35:7838543282c2 | 15 | } |
ntaka206 | 35:7838543282c2 | 16 | |
ntaka206 | 35:7838543282c2 | 17 | void RingBuf::push(RingBufType *d) |
ntaka206 | 35:7838543282c2 | 18 | { |
ntaka206 | 35:7838543282c2 | 19 | latest = wp; |
ntaka206 | 35:7838543282c2 | 20 | buf[wp++] = *d; |
ntaka206 | 35:7838543282c2 | 21 | wp %= size; |
ntaka206 | 35:7838543282c2 | 22 | } |
ntaka206 | 35:7838543282c2 | 23 | RingBufType* RingBuf::pop(void) |
ntaka206 | 35:7838543282c2 | 24 | { |
ntaka206 | 35:7838543282c2 | 25 | if (rp == wp) return NULL; |
ntaka206 | 35:7838543282c2 | 26 | RingBufType *d = &buf[rp++]; |
ntaka206 | 35:7838543282c2 | 27 | rp %= size; |
ntaka206 | 35:7838543282c2 | 28 | return d; |
ntaka206 | 35:7838543282c2 | 29 | } |
ntaka206 | 35:7838543282c2 | 30 | int RingBuf::len_get(void) |
ntaka206 | 35:7838543282c2 | 31 | { |
ntaka206 | 35:7838543282c2 | 32 | int i = wp - rp; |
ntaka206 | 35:7838543282c2 | 33 | if (i < 0) i += size; |
ntaka206 | 35:7838543282c2 | 34 | return i; |
ntaka206 | 35:7838543282c2 | 35 | } |
ntaka206 | 35:7838543282c2 | 36 | int RingBuf::size_get(void) |
ntaka206 | 35:7838543282c2 | 37 | { |
ntaka206 | 35:7838543282c2 | 38 | return size; |
ntaka206 | 35:7838543282c2 | 39 | } |
ntaka206 | 35:7838543282c2 | 40 | void RingBuf::clear(void) |
ntaka206 | 35:7838543282c2 | 41 | { |
ntaka206 | 35:7838543282c2 | 42 | rp = 0; |
ntaka206 | 35:7838543282c2 | 43 | wp = 0; |
ntaka206 | 35:7838543282c2 | 44 | } |
ntaka206 | 35:7838543282c2 | 45 | RingBufType* RingBuf::peek(int p) |
ntaka206 | 35:7838543282c2 | 46 | { |
ntaka206 | 35:7838543282c2 | 47 | if (p < 0 || p >= size) return NULL; |
ntaka206 | 35:7838543282c2 | 48 | return &buf[p]; |
ntaka206 | 35:7838543282c2 | 49 | } |
ntaka206 | 35:7838543282c2 | 50 |