Monday, August 5, 2013

IE_tclib.c

/*
 *****************************************************************
 *name:         IE_tclib.c
 *author:       Samuel Igwe
 *date:         08/04/2013
 *description:  my thumb library. pretty generic except for the 
 *              output through uart. i am only going to support a
 *              small number of functions.
 *****************************************************************
 */
#include "IE_tclib.h"
#include "IE_stm32.h"


/*
 *****************************************************************
 *description:  checks and returns the uart status
 *output:       int     =status
 *                    0 =no data !0 =data available     
 *****************************************************************
 */
int
tclib_getch(void)
{
volatile int wdTemp;
wdTemp  = *PTR_STM32_UART1_SR;
wdTemp &= STM32_UART_SR_RXNE;
return wdTemp;
}





/*
 *****************************************************************
 *description:  reads a character out of the serial port
 *output:       int     =value
 *****************************************************************
 */
int
tclib_getc(void)
{
volatile int wdTemp;
while ((tclib_getch()) == 0)
        ;

wdTemp = *PTR_STM32_UART1_DR;
return (wdTemp);
}





/*
 *****************************************************************
 *description:  reads a string out of the serial port
 *input:        char *ptrString =pointer to string
 *              int  wdSize     =maximum length of string
 *output:       int             =number of characters read
 *****************************************************************
 */
int
tclib_gets(unsigned char *ptrString, int wdSize)
{
volatile int wdCount, wdTemp;

/* first empty the input buffer*/
while ((tclib_getch() != 0)) 
        tclib_getc();

wdSize--;                       /*subtract the NULL*/
wdCount=0;                      /*start from the beginning*/
while(wdSize >0)
        {
        wdTemp=tclib_getc();
        switch((char)wdTemp)
                {
                case    ASCII_ESC:
                        return 0;
                case    ASCII_CR:
                        {
                        ptrString[wdCount] = 0;
                        return (wdCount+1);
                        }
                case    ASCII_BS:
                        {
                        if(wdCount > 0)
                                {
                                wdCount--;
                                wdSize++;

                                wdTemp = ' ';
                                wdTemp |= (ASCII_BS << 8);
                                wdTemp |= (ASCII_BS << 16);
                                stm32_uart_puts((unsigned char *)&wdTemp, 3);
                                }
                        break;
                        }
                default:
                        {
                        if ((wdTemp >= '0' && wdTemp <= '9') ||\
                            (wdTemp >= 'A' && wdTemp <= 'Z') ||\
                            (wdTemp >= 'a' && wdTemp <= 'z') ||\
                            (wdTemp == '_')                  ||\
                            (wdTemp == '.')                  ||\
                            (wdTemp == ' '))
                                {
                                ptrString[wdCount++]=(char)wdTemp;
                                wdSize--;
                                stm32_uart_puts((unsigned char *)&wdTemp, 1);
                                }
                        break;
                        }
                }
        }

return (wdCount+1);
}





/*
 *****************************************************************
 *description:  print characters out to uart
 *input:        char *ptrString = pointer to string
 *              int     wdValue     =value
 *note:         scan through string for format char %x %c \t \n \r
 *****************************************************************
 */
void
tclib_printf(char *ptrString, int wdValue)
{
unsigned char   sbString[9];
int             wdTemp;

while ((*ptrString) != NULL)
        {
        wdTemp =*ptrString;
        switch((char)wdTemp)
                {
                case    '%':
                        {
                        wdTemp = *(++ptrString);
                        switch(wdTemp)
                                {
                                case    'c':
                                        {
                                        stm32_uart_puts((unsigned char *)&wdTemp, 1);
                                        break;
                                        }
                                case    's':
                                        {
                                        wdTemp = tclib_strlen((unsigned char *)wdValue);
                                        stm32_uart_puts((unsigned char *)wdValue, wdTemp);
                                        break;
                                        }
                                case    'd':
                                case    'x':
                                        {
                                        tclib_itoa(wdValue,sbString);
                                        wdTemp = tclib_strlen(sbString);
                                        stm32_uart_puts(sbString, wdTemp);
                                        break;
                                        }
                                default:
                                        {
                                        stm32_uart_puts((unsigned char *)&wdTemp, 1);
                                        break;
                                        }
                                }

                        ptrString++;
                        break;
                        }
                case    '\\':
                        {
                        if (*(++ptrString) == NULL)
                                {
                                stm32_uart_puts((unsigned char *)&wdTemp, 1);
                                break;
                                }
                        else
                                {
                                wdTemp = *(ptrString++);
                                switch(wdTemp)
                                        {
                                        case    'n':
                                                {
                                                wdTemp = ASCII_CR;
                                                stm32_uart_puts((unsigned char *)&wdTemp, 1);
                                                }
                                        case    'r':
                                                {
                                                wdTemp = ASCII_LF;
                                                stm32_uart_puts((unsigned char *)&wdTemp, 1);
                                                break;
                                                }
                                        case    't':
                                                {
                                                wdTemp = ASCII_TAB;
                                                stm32_uart_puts((unsigned char *)&wdTemp, 1);
                                                break;
                                                }
                                        default:
                                                {
                                                stm32_uart_puts((unsigned char *)&wdTemp, 0);
                                                break;
                                                }
                                        }
                                }
                        break;
                        }
                default:
                        {
                        switch(wdTemp)
                                {
                                case    ASCII_LF:
                                        {
                                        wdTemp = ASCII_LF;
                                        stm32_uart_puts((unsigned char *)&wdTemp, 1);
                                        }
                                case    ASCII_CR:
                                        {
                                        wdTemp = ASCII_CR;
                                        stm32_uart_puts((unsigned char *)&wdTemp, 1);
                                        break;
                                        }
                                case    ASCII_TAB:
                                        {
                                        wdTemp = ASCII_TAB;
                                        stm32_uart_puts((unsigned char *)&wdTemp, 1);
                                        break;
                                        }
                                default:
                                        {
                                        stm32_uart_puts((unsigned char *)&wdTemp, 1);
                                        break;
                                        }
                                }

                        ptrString++;
                        break;
                        }
                }

        }
}






No comments:

Post a Comment