![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
TI CC1101 Transceiver
RingBuffer.cpp@0:9df942ea84f4, 2010-11-21 (annotated)
- Committer:
- tmav123
- Date:
- Sun Nov 21 11:37:56 2010 +0000
- Revision:
- 0:9df942ea84f4
initial revision
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tmav123 | 0:9df942ea84f4 | 1 | /* |
tmav123 | 0:9df942ea84f4 | 2 | * mbed library for RingBuffer |
tmav123 | 0:9df942ea84f4 | 3 | * Copyright (c) 2010 Hiroshi Suga |
tmav123 | 0:9df942ea84f4 | 4 | * Released under the MIT License: http://mbed.org/license/mit |
tmav123 | 0:9df942ea84f4 | 5 | */ |
tmav123 | 0:9df942ea84f4 | 6 | |
tmav123 | 0:9df942ea84f4 | 7 | #include "RingBuffer.h" |
tmav123 | 0:9df942ea84f4 | 8 | |
tmav123 | 0:9df942ea84f4 | 9 | RingBuffer::RingBuffer (int p_size) { |
tmav123 | 0:9df942ea84f4 | 10 | size = p_size + 1; |
tmav123 | 0:9df942ea84f4 | 11 | buf = new char[size]; |
tmav123 | 0:9df942ea84f4 | 12 | addr_w = 0; |
tmav123 | 0:9df942ea84f4 | 13 | addr_r = 0; |
tmav123 | 0:9df942ea84f4 | 14 | } |
tmav123 | 0:9df942ea84f4 | 15 | /* |
tmav123 | 0:9df942ea84f4 | 16 | RingBuffer::~RingBuffer () { |
tmav123 | 0:9df942ea84f4 | 17 | delete [] buf; |
tmav123 | 0:9df942ea84f4 | 18 | } |
tmav123 | 0:9df942ea84f4 | 19 | */ |
tmav123 | 0:9df942ea84f4 | 20 | int RingBuffer::_putc (int dat) { |
tmav123 | 0:9df942ea84f4 | 21 | int next; |
tmav123 | 0:9df942ea84f4 | 22 | |
tmav123 | 0:9df942ea84f4 | 23 | next = (addr_w + 1) % size; |
tmav123 | 0:9df942ea84f4 | 24 | if (next == addr_r) { |
tmav123 | 0:9df942ea84f4 | 25 | return -1; |
tmav123 | 0:9df942ea84f4 | 26 | } |
tmav123 | 0:9df942ea84f4 | 27 | buf[addr_w] = dat; |
tmav123 | 0:9df942ea84f4 | 28 | addr_w = next; |
tmav123 | 0:9df942ea84f4 | 29 | return dat; |
tmav123 | 0:9df942ea84f4 | 30 | } |
tmav123 | 0:9df942ea84f4 | 31 | |
tmav123 | 0:9df942ea84f4 | 32 | int RingBuffer::put (char *dat, int len) { |
tmav123 | 0:9df942ea84f4 | 33 | int next, i; |
tmav123 | 0:9df942ea84f4 | 34 | |
tmav123 | 0:9df942ea84f4 | 35 | |
tmav123 | 0:9df942ea84f4 | 36 | for (i = 0; i < len; i ++) { |
tmav123 | 0:9df942ea84f4 | 37 | next = (addr_w + 1) % size; |
tmav123 | 0:9df942ea84f4 | 38 | if (next == addr_r) { |
tmav123 | 0:9df942ea84f4 | 39 | break; |
tmav123 | 0:9df942ea84f4 | 40 | } |
tmav123 | 0:9df942ea84f4 | 41 | buf[addr_w] = dat[i]; |
tmav123 | 0:9df942ea84f4 | 42 | addr_w = next; |
tmav123 | 0:9df942ea84f4 | 43 | } |
tmav123 | 0:9df942ea84f4 | 44 | return i; |
tmav123 | 0:9df942ea84f4 | 45 | } |
tmav123 | 0:9df942ea84f4 | 46 | |
tmav123 | 0:9df942ea84f4 | 47 | int RingBuffer::_getc (void) |
tmav123 | 0:9df942ea84f4 | 48 | { |
tmav123 | 0:9df942ea84f4 | 49 | char dat; |
tmav123 | 0:9df942ea84f4 | 50 | if (addr_r == addr_w) { |
tmav123 | 0:9df942ea84f4 | 51 | return 0; |
tmav123 | 0:9df942ea84f4 | 52 | } |
tmav123 | 0:9df942ea84f4 | 53 | dat = buf[addr_r]; |
tmav123 | 0:9df942ea84f4 | 54 | addr_r = (addr_r + 1) % size; |
tmav123 | 0:9df942ea84f4 | 55 | return dat; |
tmav123 | 0:9df942ea84f4 | 56 | } |
tmav123 | 0:9df942ea84f4 | 57 | |
tmav123 | 0:9df942ea84f4 | 58 | int RingBuffer::get (char *dat, int len) { |
tmav123 | 0:9df942ea84f4 | 59 | int i; |
tmav123 | 0:9df942ea84f4 | 60 | |
tmav123 | 0:9df942ea84f4 | 61 | for (i = 0; i < len; i ++) { |
tmav123 | 0:9df942ea84f4 | 62 | if (addr_r == addr_w) { |
tmav123 | 0:9df942ea84f4 | 63 | break; |
tmav123 | 0:9df942ea84f4 | 64 | } |
tmav123 | 0:9df942ea84f4 | 65 | dat[i] = buf[addr_r]; |
tmav123 | 0:9df942ea84f4 | 66 | addr_r = (addr_r + 1) % size; |
tmav123 | 0:9df942ea84f4 | 67 | } |
tmav123 | 0:9df942ea84f4 | 68 | return i; |
tmav123 | 0:9df942ea84f4 | 69 | } |
tmav123 | 0:9df942ea84f4 | 70 | |
tmav123 | 0:9df942ea84f4 | 71 | int RingBuffer::available () { |
tmav123 | 0:9df942ea84f4 | 72 | if (addr_w < addr_r) { |
tmav123 | 0:9df942ea84f4 | 73 | return addr_r - addr_w - 1; |
tmav123 | 0:9df942ea84f4 | 74 | } else { |
tmav123 | 0:9df942ea84f4 | 75 | return (size - addr_w) + addr_r - 1; |
tmav123 | 0:9df942ea84f4 | 76 | } |
tmav123 | 0:9df942ea84f4 | 77 | } |
tmav123 | 0:9df942ea84f4 | 78 | |
tmav123 | 0:9df942ea84f4 | 79 | int RingBuffer::use () { |
tmav123 | 0:9df942ea84f4 | 80 | return size - available() - 1; |
tmav123 | 0:9df942ea84f4 | 81 | } |
tmav123 | 0:9df942ea84f4 | 82 | |
tmav123 | 0:9df942ea84f4 | 83 | void RingBuffer::clear () { |
tmav123 | 0:9df942ea84f4 | 84 | addr_w = 0; |
tmav123 | 0:9df942ea84f4 | 85 | addr_r = 0; |
tmav123 | 0:9df942ea84f4 | 86 | } |