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

📄 uimgr.c

📁 The AVRcam source files were built using the WinAVR distribution (version 3.3.1 of GCC). I haven t
💻 C
📖 第 1 页 / 共 2 页
字号:
/***********************************************************
    Module Name: UIMgr.c
    Module Date: 04/10/2004
    Module Auth: John Orlando 
    Copyright (c) 2004 John Orlando  All Rights Reserved 

    Description: This module is responsible for providing
    the processing to manage the user interface of the
    system.  This user interface is provided via the UART.
    This module handles the incoming serial commands, and
    performs the needed functionality.  It is then
    responsible for generating any needed response to
    the external entity.
    ***********************************************************/

/*	Includes */
#include <avr/io.h>
#include <stdlib.h>
#include <string.h>
#include <avr/eeprom.h>
#include "CommonDefs.h"
#include "UIMgr.h"
#include "UartInterface.h"
#include "CamConfig.h"
#include "Utility.h"
#include "Executive.h"
#include "CamInterface.h"

/* 	Local Structures and Typedefs */

typedef enum
{
    getVersionCmd,
    pingCmd,
    setCameraRegsCmd,
    dumpFrameCmd,
    enableTrackingCmd,
    disableTrackingCmd,
    setColorMapCmd,
    resetCameraCmd,
    noCmd,
    invalidCmd
} UIMgr_Cmd_t;

typedef enum
{
    setRed,
    setGreen,
    setBlue
} setColorState_t;


/*  Local Variables */
static unsigned char charCount = 0;
static unsigned char charIndex = 0;
static unsigned char asciiTokenBuffer[MAX_TOKEN_LENGTH+1]; /* +1 to ensure NULL at end */
static unsigned char tokenCount = 0;
static unsigned char tokenBuffer[MAX_TOKEN_COUNT];
static UIMgr_Cmd_t receivedCmd = noCmd;
static unsigned char AVRcamVersion[] = "AVRcam v1.0\r";

/*  Local Function Declaration */
static unsigned char UIMgr_readRxFifo(void);
static unsigned char UIMgr_readTxFifo(void);
static unsigned char UIMgr_readRxFifo(void);
static void UIMgr_sendNck(void);
static void UIMgr_sendAck(void);
static void UIMgr_convertTokenToCmd(void);
static void UIMgr_convertTokenToValue(void);
static void UIMgr_executeCmd(void);

/*  Extern Variables */
unsigned char UIMgr_rxFifo[UI_MGR_RX_FIFO_SIZE];
unsigned char UIMgr_rxFifoHead=0;
unsigned char UIMgr_rxFifoTail=0;

unsigned char UIMgr_txFifo[UI_MGR_TX_FIFO_SIZE];
unsigned char UIMgr_txFifoHead=0;
unsigned char UIMgr_txFifoTail=0;

/*  Definitions */
#define IS_DATA_IN_TX_FIFO() (!(UIMgr_txFifoHead == UIMgr_txFifoTail))
#define IS_DATA_IN_RX_FIFO() (!(UIMgr_rxFifoHead == UIMgr_rxFifoTail))

/***********************************************************
    Function Name: UIMgr_init
    Function Description: This function is responsible for
    initializing the UIMgr module.  It sets up the fifo
    used to hold incoming data, etc.
    Inputs:  none
    Outputs: none
    ***********************************************************/
void UIMgr_init(void)
{
    memset(asciiTokenBuffer,0x00,MAX_TOKEN_LENGTH+1);
    memset(tokenBuffer,0x00,MAX_TOKEN_COUNT);
    memset(UIMgr_txFifo,0x00,UI_MGR_TX_FIFO_SIZE);
    memset(UIMgr_rxFifo,0x00,UI_MGR_RX_FIFO_SIZE);
}

/***********************************************************
    Function Name: UIMgr_dispatchEvent
    Function Description: This function is responsible for
    processing events that pertain to the UIMgr.
    Inputs:  event - the generated event
    Outputs: none
    ***********************************************************/
void UIMgr_dispatchEvent(unsigned char event)
{
    switch(event)
    {
    case EV_ACQUIRE_LINE_COMPLETE:
        UIMgr_transmitPendingData();
        break;

    case EV_SERIAL_DATA_RECEIVED:
        UIMgr_processReceivedData();
        break;

    case EV_SERIAL_DATA_PENDING_TX:
        UIMgr_flushTxBuffer();
        break;
    }
}
/***********************************************************
    Function Name: UIMgr_transmitPendingData
    Function Description: This function is responsible for
    transmitting a single byte of data if data is waiting
    to be sent.  Otherwise, if nothing is waiting, the
    function just returns.
    Inputs:  none
    Outputs: none
    ***********************************************************/
void UIMgr_transmitPendingData(void)
{
    if (IS_DATA_IN_TX_FIFO() == TRUE)
    {
        /* data is waiting...send a single byte */
        UartInt_txByte( UIMgr_readTxFifo() );
    }
}
/***********************************************************
    Function Name: UIMgr_processReceivedData
    Function Description: This function is responsible for
    parsing any serial data waiting in the rx fifo
    Inputs:  none
    Outputs: none
    ***********************************************************/
void UIMgr_processReceivedData(void)
{
    unsigned char tmpData = 0;

    /* still need to add a mechanism to handle token counts
        that are excessive!!! FIX ME!!! */
    while(IS_DATA_IN_RX_FIFO() == TRUE)
    {
        tmpData = UIMgr_readRxFifo();
        if (tmpData == '\r')
        {
            /* we have reached a token separator */
            if (tokenCount == 0)
            {
                /* convert the command */
                UIMgr_convertTokenToCmd();
            }
            else
            {
                /* convert a value */
                UIMgr_convertTokenToValue();
                tokenCount++;
            }
            /* either way, it is time to try to process the received
                token list since we have reached the end of the cmd. */
            Utility_delay(100);
            if (receivedCmd == invalidCmd ||
                receivedCmd == noCmd )
            {
                UIMgr_sendNck();
                PUBLISH_EVENT(EV_SERIAL_DATA_PENDING_TX);
            }
            else
            {
                UIMgr_sendAck();
                /* publish the serial data pending event, so it
                    will push the ACK out before we execute the cmd */
                PUBLISH_EVENT(EV_SERIAL_DATA_PENDING_TX);
                UIMgr_executeCmd();
            }

            /* reset any necessary data */
            tokenCount = 0;
            memset(tokenBuffer,0x00,MAX_TOKEN_COUNT);
        }
        else if (tmpData == ' ')  /* space char */
        {
            /* the end of a token has been reached */
            if (tokenCount == 0)
            {
                UIMgr_convertTokenToCmd();
                tokenCount++;
            }
            else
            {
                /* check to see if this token is going to push
                    us over the limit...if so, abort the transaction */
                if (tokenCount+1 >= MAX_TOKEN_COUNT)
                {
                    /* we received too many tokens, and
                        need to NCK this request, since its too
                        large...reset everything...*/
                    charCount=0;
                    charIndex=0;
                    tokenCount=0;
                    receivedCmd = invalidCmd;
                }
                else
                {
                    /* tokenCount is still in range...*/
                    UIMgr_convertTokenToValue();
                    tokenCount++;
                }
            }
        }
        else if ( (tmpData >= 'A' && tmpData <= 'Z') ||
                 (tmpData >= '0' && tmpData <= '9') )
        {
            /* a valid range of token was received */
            asciiTokenBuffer[charIndex] = tmpData;
            charCount++;
            charIndex++;
            if (charCount > MAX_TOKEN_LENGTH)
            {
                /* we have received a token that cannot be handled...
                    set the received cmd to an invalid cmd, and wait
                    for the \r to process it */
                receivedCmd = invalidCmd;
                charIndex = 0;  /* ...so we won't overwrite memory */
            }
        }
        else
        {
            /* an invalid character was received */
            receivedCmd = invalidCmd;
        }
    }  /* end while */

    asm volatile("clt"::);  /* clear out the T flag in case it wasn't
                                cleared already */
}

/***********************************************************
    Function Name: UIMgr_executeCmd
    Function Description: This function is responsible for
    executing whatever cmd is stored in the receivedCmd
    object.
    Inputs:  none
    Outputs: none
    ***********************************************************/
static void UIMgr_executeCmd(void)
{
    unsigned char i;
    unsigned char *pData;
#if	DEBUG_COLOR_MAP
    unsigned char asciiBuffer[5];
#endif

    if (receivedCmd == pingCmd)
    {
    }
    else if (receivedCmd == getVersionCmd)
    {
        pData = AVRcamVersion;
        while(*pData != 0)
        {
            UIMgr_writeTxFifo(*pData++);
        }
    }
    else if (receivedCmd == resetCameraCmd)
    {
        CamInt_resetCam();
    }
    else if (receivedCmd == dumpFrameCmd)
    {
        /* publish the event that will indicate that
            a request has come to dump a frame...this will
            be received by the FrameMgr, which will begin
            dumping the frame...a short delay is needed
            here to keep the Java demo app happy (sometimes
            it wouldn't be able to receive the serial data
            as quickly as AVRcam can provide it). */
        Utility_delay(100);
        PUBLISH_EVENT(EV_DUMP_FRAME);
    }
    else if (receivedCmd == setCameraRegsCmd)
    {
        /* we need to gather the tokens and
            build config cmds to be sent to the camera */
        for (i=1; i<tokenCount; i+=2)  /* starts at 1 since first token
                                           is the CR cmd */

⌨️ 快捷键说明

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