Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
CircBuffer.h
00001 #ifndef CIRCBUFFER_H 00002 #define CIRCBUFFER_H 00003 00004 template <class T> 00005 class CircBuffer { 00006 public: 00007 CircBuffer(int length) { 00008 write = 0; 00009 read = 0; 00010 size = length + 1; 00011 buf = (T *)malloc(size * sizeof(T)); 00012 }; 00013 00014 bool isFull() { 00015 return ((write + 1) % size == read); 00016 }; 00017 00018 bool isEmpty() { 00019 return (read == write); 00020 }; 00021 00022 void queue(T k) { 00023 if (isFull()) { 00024 read++; 00025 read %= size; 00026 } 00027 buf[write++] = k; 00028 write %= size; 00029 } 00030 00031 uint16_t available() { 00032 return (write >= read) ? write - read : size - read + write; 00033 }; 00034 00035 bool dequeue(T * c) { 00036 bool empty = isEmpty(); 00037 if (!empty) { 00038 *c = buf[read++]; 00039 read %= size; 00040 } 00041 return(!empty); 00042 }; 00043 00044 private: 00045 volatile uint16_t write; 00046 volatile uint16_t read; 00047 uint16_t size; 00048 T * buf; 00049 }; 00050 00051 #endif
Generated on Wed Jul 13 2022 10:21:28 by
1.7.2