⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buffer.c

📁 在AVR 平台下的控制代码,通过CAN控制
💻 C
字号:
#include "std.h"
#include "buffer.h"
/**
 * Initializes the FIFO buffer.
 *
 * @param apFIFOBuffer - The address of the FIFO buffer.
 * @param cLength - The length of array of the FIFO buffer, this is not the actual FIFO buffer size.
 *
 * @return None.
 *
 * @see tdGetFIFO
 * @see tdSetFIFO
 * @see tdClearFIFO
 */
void tdInitFIFO2(Byte * gpFIFOBuffer, Byte cLength)  
{
    FifoHandle fh = (FifoHandle)gpFIFOBuffer;
    
    if (cLength <= 3) return;

    fh->ucSize = (Byte)(cLength - 3);
    
    tdClearFIFO(fh);
    return;
}

/**
 * Gets one byte from the FIFO buffer.
 *
 * @param fh - The handle for the FIFO buffer.
 * @param apData - The address of the data to be put.
 *
 * @return _TRUE_ - Succeeded.
 *         _FALSE_ - Failed.
 *
 * @see tdSetFIFO
 * @see tdInitFIFO
 * @see tdClearFIFO
 */
Bool  tdGetFIFO(FifoHandle fh, Byte * apData) 
{
    if (fh->ucHead != fh->ucTail)
    {
        *apData = fh->ucData[fh->ucHead];
        fh->ucHead++;
        if (fh->ucHead >= fh->ucSize) fh->ucHead = 0;
        return _TRUE_;
    }
    return _FALSE_;
}

/**
 * Puts one byte to the FIFO buffer.
 *
 * @param fh - The handle for the FIFO buffer.
 * @param ucData - The data to be put.
 *
 * @return _TRUE_ - Succeeded.
 *         _FALSE_ - Failed.
 *
 * @see tdGetFIFO
 * @see tdInitFIFO
 * @see tdClearFIFO
 */
Bool  tdSetFIFO(FifoHandle fh, Byte ucData) 
{
    if ((fh->ucTail + 1 == fh->ucHead) ||
        (fh->ucTail + 1 - fh->ucSize == fh->ucHead))
        return _FALSE_;          /* Full */
    fh->ucData[fh->ucTail] = ucData;
    fh->ucTail++;
    if (fh->ucTail >= fh->ucSize) fh->ucTail = 0;
    return _TRUE_;
}

/**
 * Clears the FIFO buffer.
 *
 * @param fh - The handle for the FIFO buffer.
 *
 * @return None.
 *
 * @see tdInitFIFO
 * @see tdGetFIFO
 * @see tdSetFIFO
 */
void tdClearFIFO(FifoHandle fh) 
{
    fh->ucHead = 0;
    fh->ucTail = 0;
}

Bool tdFIFOIsEmpty(FifoHandle fh)
{
    return  fh->ucHead != fh->ucTail;
}

Bool tdFIFOIsFull(FifoHandle fh)
{
    return (fh->ucTail + 1 == fh->ucHead) || (fh->ucTail + 1 - fh->ucSize == fh->ucHead);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -