Move new Class Driver powered demos to a new ClassDriver subdirectory, re-add old...
[pub/lufa.git] / Demos / Device / ClassDriver / USBtoSerial / Lib / RingBuff.h
diff --git a/Demos/Device/ClassDriver/USBtoSerial/Lib/RingBuff.h b/Demos/Device/ClassDriver/USBtoSerial/Lib/RingBuff.h
new file mode 100644 (file)
index 0000000..23288d8
--- /dev/null
@@ -0,0 +1,116 @@
+/*\r
+             LUFA Library\r
+     Copyright (C) Dean Camera, 2009.\r
+              \r
+  dean [at] fourwalledcubicle [dot] com\r
+      www.fourwalledcubicle.com\r
+*/\r
+\r
+/*\r
+  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, and distribute this software\r
+  and its documentation for any purpose and without fee is hereby\r
+  granted, provided that the above copyright notice appear in all\r
+  copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting\r
+  documentation, and that the name of the author not be used in\r
+  advertising or publicity pertaining to distribution of the\r
+  software without specific, written prior permission.\r
+\r
+  The author disclaim all warranties with regard to this\r
+  software, including all implied warranties of merchantability\r
+  and fitness.  In no event shall the author be liable for any\r
+  special, indirect or consequential damages or any damages\r
+  whatsoever resulting from loss of use, data or profits, whether\r
+  in an action of contract, negligence or other tortious action,\r
+  arising out of or in connection with the use or performance of\r
+  this software.\r
+*/\r
+\r
+/* Buffer Configuration: */\r
+   /* Buffer length - select static size of created ring buffers: */\r
+        #define BUFF_STATICSIZE 128      // Set to the static ring buffer size for all ring buffers (place size after define)\r
+\r
+   /* Volatile mode - uncomment to make buffers volatile, for use in ISRs, etc: */\r
+        #define BUFF_VOLATILE            // Uncomment to cause all ring buffers to become volatile (and atomic if multi-byte) in access\r
+\r
+   /* Drop mode - select behaviour when Buffer_StoreElement called on a full buffer: */\r
+     #define BUFF_DROPOLD             // Uncomment to cause full ring buffers to drop the oldest character to make space when full\r
+     // #define BUFF_DROPNEW          // Uncomment to cause full ring buffers to drop the new character when full\r
+     // #define BUFF_NODROPCHECK      // Uncomment to ignore full ring buffer checks - checking left to user!\r
+\r
+   /* Underflow behaviour - select behaviour when Buffer_GetElement is called with an empty ring buffer: */\r
+     //#define BUFF_EMPTYRETURNSZERO  // Uncomment to return 0 when an empty ring buffer is read\r
+     #define BUFF_NOEMPTYCHECK        // Uncomment to disable checking of empty ring buffers - checking left to user!\r
+       \r
+   /* Buffer storage type - set the datatype for the stored data */\r
+     #define BUFF_DATATYPE uint8_t    // Change to the data type that is going to be stored into the buffer\r
+       \r
+   /* Peek routine - uncomment to include the peek routine (fetches next byte without removing it from the buffer */\r
+     //#define BUFF_USEPEEK\r
+               \r
+#ifndef _RINGBUFF_H_\r
+#define _RINGBUFF_H_\r
+\r
+       /* Includes: */\r
+       #include <avr/io.h>\r
+       #include <avr/interrupt.h>\r
+       #include <util/atomic.h>\r
+       #include <limits.h>\r
+\r
+       #include <LUFA/Common/Common.h>\r
+\r
+       /* Defines and checks: */\r
+       #if defined(BUFF_STATICSIZE)\r
+               #define BUFF_LENGTH BUFF_STATICSIZE\r
+       #else\r
+               #error No buffer length specified!\r
+       #endif\r
+\r
+       #if !(defined(BUFF_DROPOLD) || defined(BUFF_DROPNEW) || defined(BUFF_NODROPCHECK))\r
+               #error No buffer drop mode specified.\r
+       #endif\r
+\r
+       #if !defined(BUFF_DATATYPE)\r
+               #error Ringbuffer storage data type not specified.\r
+       #endif\r
+\r
+       #if defined(BUFF_VOLATILE)\r
+               #define BUFF_MODE            volatile\r
+               #define BUFF_ATOMIC_BLOCK    ATOMIC_BLOCK(ATOMIC_RESTORESTATE)\r
+       #else\r
+               #define BUFF_MODE\r
+               #define BUFF_ATOMIC_BLOCK\r
+       #endif\r
+\r
+       #if (BUFF_STATICSIZE   > LONG_MAX)\r
+               #define RingBuff_Elements_t uint64_t\r
+       #elif (BUFF_STATICSIZE > INT_MAX)\r
+               #define RingBuff_Elements_t uint32_t\r
+       #elif (BUFF_STATICSIZE > CHAR_MAX)\r
+               #define RingBuff_Elements_t uint16_t\r
+       #else\r
+               #define RingBuff_Elements_t uint8_t\r
+       #endif\r
+       \r
+       /* Type Defines: */\r
+       typedef BUFF_DATATYPE RingBuff_Data_t;\r
+\r
+       typedef BUFF_MODE struct\r
+       {\r
+               RingBuff_Data_t      Buffer[BUFF_LENGTH];\r
+               RingBuff_Data_t*     InPtr;\r
+               RingBuff_Data_t*     OutPtr;\r
+               RingBuff_Elements_t  Elements;\r
+       } RingBuff_t;\r
+               \r
+       /* Function Prototypes: */\r
+       void            Buffer_Initialize(RingBuff_t* Buff);\r
+       void            Buffer_StoreElement(RingBuff_t* Buffer, RingBuff_Data_t Data);\r
+       RingBuff_Data_t Buffer_GetElement(RingBuff_t* Buffer);\r
+       #if defined(BUFF_USEPEEK)\r
+               RingBuff_Data_t Buffer_PeekElement(const RingBuff_t* Buffer);\r
+       #endif\r
+       \r
+#endif\r