東北大学学友会準加盟団体 From The Earth の高高度ロケットFTE-06(通称:海豚)にて使用したソフトウェアです.ご自由にお使いください.このプログラムによって生じた損害について当団体は一切責任を負いません.また,各モジュールのライブラリは当団体が作成したものではないので再配布は禁止します.

Dependencies:   mbed FATFileSystem

Fork of FTE-06 by Tetsushi Amano

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CBuffer.h Source File

CBuffer.h

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 /*
00019  * Modifyed by Suga
00020  */
00021 
00022 #ifndef CIRCBUFFER_H_
00023 #define CIRCBUFFER_H_
00024 
00025 template <class T>
00026 class CircBuffer {
00027 public:
00028     CircBuffer(int length, void *addr = NULL) {
00029         write = 0;
00030         read = 0;
00031         size = length + 1;
00032         if (addr) {
00033             buf = (T *)addr;
00034         } else {
00035             buf = (T *)malloc(size * sizeof(T));
00036         }
00037         if (buf == NULL)
00038             error("Can't allocate memory");
00039     };
00040 
00041     bool isFull() {
00042         return (((write + 1) % size) == read);
00043     };
00044 
00045     bool isEmpty() {
00046         return (read == write);
00047     };
00048 
00049     void queue(T k) {
00050         if (isFull()) {
00051 //            read++;
00052 //            read %= size;
00053             return;
00054         }
00055         buf[write++] = k;
00056         write %= size;
00057     }
00058     
00059     void flush() {
00060         read = 0;
00061         write = 0;
00062     }
00063     
00064 
00065     uint32_t available() {
00066         return (write >= read) ? write - read : size - read + write;
00067     };
00068 
00069     bool dequeue(T * c) {
00070         bool empty = isEmpty();
00071         if (!empty) {
00072             *c = buf[read++];
00073             read %= size;
00074         }
00075         return(!empty);
00076     };
00077 
00078 private:
00079     volatile uint32_t write;
00080     volatile uint32_t read;
00081     uint32_t size;
00082     T * buf;
00083 };
00084 
00085 #endif