📄 buffer.h
字号:
/******************************************************************************/
/* */
/* Name: BUFFER.H - Module to manage a circular buffer in the microcontroller */
/* */
/* Author: Sven Rebeschiess */
/* Date: 10.8.1999 */
/* */
/* Modifications: F. Wornle (FW-mn-yr) */
/* Latest change: FW-12-02 */
/* */
/******************************************************************************/
#ifndef _MC_BUFFER_H_
#define _MC_BUFFER_H_
#include <stdlib.h> /* free, malloc, NULL */
#ifdef DEBUGSCI0
#include "mc_signal.h" /* global debug variable mallocTotalBytes */
#endif
#ifndef _MY_TYPES_H_
#define _MY_TYPES_H
#define BOOLEAN char
#define BYTE unsigned char
#define WORD unsigned int
#define DWORD unsigned long
#ifndef TRUE
#define TRUE (1)
#endif
#ifndef FALSE
#define FALSE (0)
#endif
#endif
// ring buffer administration structure ('BufferTyp')
struct BufferTyp
{
WORD First; /* Ringpuffer-Zeiger auf den Anfang der Elemente */
WORD Last; /* Ringpuffer-Zeiger auf das Ende der Elemente */
WORD Count; /* Anzahl der im Ringpuffer gespeicherten Elem. */
//WORD RecordCount; /* Anzahl der im Ringpuffer gespeicherten Records (FW-06-01) */
WORD BufLength; /* L鋘ge des Ringpuffers */
BYTE ElemLength; /* L鋘ge jedes Puffereintrages in Bytes */
BYTE j; /* Hilfs-Schleifenvariable */
char *bp; /* Hilfszeiger */
char *Buffer; /* Zeiger auf den eigentlichen Ringpuffer */
};
// 'RecordAvailable()' returns TRUE if the buffer stores at least one whole record; otherwise it returns FALSE
// FW-06-01
//#define RecordAvailable(BPtr) (BPtr->RecordCount > 0)
// 'IncRecordCount()' increments 'RecordCount' by '1'
//#define IncRecordCount(BPtr) (BPtr->RecordCount += 1)
// 'DecRecordCount()' decrements 'RecordCount' by '1'
//#define DecRecordCount(BPtr) (BPtr->RecordCount -= 1)
// 'IncCount()' increments 'count' by '1' taking into account the maximum buffer length
// (count => ..., MaxLength-1, MaxLength, 0, 1, 2, ...)
#define IncCount(Count,MaxLength) ((++Count)%MaxLength)
// 'NotEmptyBuffer()' returns TRUE if the buffer is empty; otherwise it returns FALSE
#define NotEmptyBuffer(BPtr) (BPtr->Count > 0)
// 'NotFullBuffer()' returns FALSE if the buffer is full; otherwise it returns TRUE
#define NotFullBuffer(BPtr) (BPtr->Count < BPtr->BufLength)
// 'Buffercounter()' returns the current counter value of the buffer
#define Buffercounter(BPtr) (BPtr->Count)
// 'InitBuffer()' initialises a ring buffer of a specified type (e. g. 'char')
// the required memory is allocated dynamically on the HEAP (malloc)
#define InitBuffer(BPtr,BufferMax,Typ) \
if((BPtr = (struct BufferTyp *)malloc(sizeof(struct BufferTyp))) == NULL)\
abort_LED(80);\
else\
{\
BPtr->ElemLength = (BYTE)(sizeof(Typ));\
if((BPtr->Buffer = (char *)malloc(BPtr->ElemLength * BufferMax)) == NULL)\
abort_LED(81);\
else\
{\
ClearBuffer(BPtr);\
BPtr->BufLength = BufferMax;\
}\
}
// 'RemoveBuffer()' destroys a given ring buffer and returns all allocated memory to the HEAP
#define RemoveBuffer(BPtr) \
free((void *)BPtr->Buffer);\
free((void *)BPtr)
// 'ClearBuffer()' deletes all elements in a buffer; the buffer is empty again
#define ClearBuffer(BPtr) \
BPtr->First = 0;\
BPtr->Last = 0;\
BPtr->Count = 0;//\
//BPtr->RecordCount = 0
// 'InBuffer()' stores a value in the (ring) buffer
// (this macro fails with an error code '82' if the buffer is full)
#define InBuffer(Wert,BPtr) \
if(NotFullBuffer(BPtr))\
{\
BPtr->bp = (char*)&Wert;\
for (BPtr->j = 0; BPtr->j < BPtr->ElemLength; BPtr->j++)\
BPtr->Buffer[BPtr->ElemLength * BPtr->First + BPtr->j] = *(BPtr->bp + BPtr->j);\
BPtr->First = IncCount(BPtr->First,BPtr->BufLength);\
BPtr->Count++ ;\
}\
else\
{\
abort_LED(82);\
}
// 'OutBuffer()' fetches a value from the (ring) buffer
// (this macro fails with an error code of '83' in case the buffer is empty)
#define OutBuffer(BPtr,Wert,Typ) \
if(NotEmptyBuffer(BPtr))\
{\
Wert = *(Typ*)&(BPtr->Buffer[BPtr->ElemLength * BPtr->Last]);\
BPtr->Last = IncCount(BPtr->Last,BPtr->BufLength);\
BPtr->Count-- ;\
}\
else\
{\
Wert = 0;\
abort_LED(83);\
}
#endif /* _MC_BUFFER_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -