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.
Fork of Smoothie by
CircBuffer.h
00001 /* Copyright (c) 2010-2011 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 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is 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 #ifndef CIRCBUFFER_H 00020 #define CIRCBUFFER_H 00021 00022 #include <stdlib.h> 00023 #include "sLPC17xx.h" 00024 #include "platform_memory.h" 00025 00026 template <class T> 00027 class CircBuffer { 00028 public: 00029 CircBuffer(int length) { 00030 write = 0; 00031 read = 0; 00032 size = length; 00033 buf = (uint8_t*) AHB0.alloc(size * sizeof(T)); 00034 }; 00035 00036 bool isFull() { 00037 __disable_irq(); 00038 bool b= ((write + 1) % size == read); 00039 __enable_irq(); 00040 return b; 00041 }; 00042 00043 bool isEmpty() { 00044 return (read == write); 00045 }; 00046 00047 void queue(T k) { 00048 __disable_irq(); 00049 if (isFull()) { 00050 read++; 00051 read %= size; 00052 } 00053 buf[write++] = k; 00054 write %= size; 00055 __enable_irq(); 00056 } 00057 00058 uint16_t available() { 00059 __disable_irq(); 00060 uint16_t i= (write >= read) ? write - read : (size - read) + write; 00061 __enable_irq(); 00062 return i; 00063 }; 00064 uint16_t free() { 00065 return size - available() - 1; 00066 }; 00067 00068 void dump() { 00069 iprintf("[RingBuffer Sz:%2d Rd:%2d Wr:%2d Av:%2d Fr:%2d]\n", size, read, write, available(), free()); 00070 } 00071 00072 bool dequeue(T * c) { 00073 bool empty = isEmpty(); 00074 if (!empty) { 00075 *c = buf[read++]; 00076 read %= size; 00077 } 00078 return(!empty); 00079 }; 00080 00081 void peek(T * c, int offset) { 00082 int h = (read + offset) % size; 00083 *c = buf[h]; 00084 }; 00085 00086 void flush() { 00087 read = write; 00088 } 00089 00090 private: 00091 volatile uint16_t write; 00092 volatile uint16_t read; 00093 uint16_t size; 00094 T * buf; 00095 }; 00096 00097 #endif
Generated on Tue Jul 12 2022 20:09:00 by
1.7.2
