Codebase from CC1101_Transceiver, ported to LPC1114/LPC824/STM32F103 and other micros, will be merged with panStamp project to replace AVR/MSP.

Dependencies:   mbed

Fork of CC1101_Transceiver_LPC1114 by Kai Liu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RingBuffer.cpp Source File

RingBuffer.cpp

00001 /*
00002  * mbed library for RingBuffer
00003  * Copyright (c) 2010 Hiroshi Suga
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  */
00006 
00007 #include "RingBuffer.h"
00008 
00009 RingBuffer::RingBuffer (int p_size) {
00010     size = p_size + 1;
00011     buf = new char[size];
00012     addr_w = 0;
00013     addr_r = 0;
00014 }
00015 /*
00016 RingBuffer::~RingBuffer () {
00017     delete [] buf;
00018 }
00019 */
00020 int RingBuffer::_putc (int dat) {
00021     int next;
00022 
00023     next = (addr_w + 1) % size;
00024     if (next == addr_r) {
00025         return -1;
00026     }
00027     buf[addr_w] = dat;
00028     addr_w = next;
00029     return dat;
00030 }
00031 
00032 int RingBuffer::put (char *dat, int len) {
00033     int next, i;
00034 
00035 
00036     for (i = 0; i < len; i ++) {
00037         next = (addr_w + 1) % size;
00038         if (next == addr_r) {
00039             break;
00040         }
00041         buf[addr_w] = dat[i];
00042         addr_w = next;
00043     }
00044     return i;
00045 }
00046 
00047 int RingBuffer::_getc (void) 
00048 {
00049     char dat;
00050     if (addr_r == addr_w) {
00051         return 0;
00052     }
00053     dat = buf[addr_r];
00054     addr_r = (addr_r + 1) % size;
00055     return dat;
00056 }
00057 
00058 int RingBuffer::get (char *dat, int len) {
00059     int i;
00060 
00061     for (i = 0; i < len; i ++) {
00062         if (addr_r == addr_w) {
00063             break;
00064         }
00065         dat[i] = buf[addr_r];
00066         addr_r = (addr_r + 1) % size;
00067     }
00068     return i;
00069 }
00070 
00071 int RingBuffer::available () {
00072     if (addr_w < addr_r) {
00073         return addr_r - addr_w - 1;
00074     } else {
00075         return (size - addr_w) + addr_r - 1;
00076     }
00077 }
00078 
00079 int RingBuffer::use () {
00080     return size - available() - 1;
00081 }
00082 
00083 void RingBuffer::clear () {
00084     addr_w = 0;
00085     addr_r = 0;
00086 }