/* ---------------------------------------------------------------------------
 * FILENAME.c                                       (c) 2008 Micro-key bv
 * ---------------------------------------------------------------------------
 * Micro-key bv
 * Industrieweg 28, 9804 TG Noordhorn
 * Postbus 92, 9800 AB Zuidhorn
 * The Netherlands
 * Tel: +31 594 503020
 * Fax: +31 594 505825
 * Email: support@microkey.nl  
 * Web: www.microkey.nl
 * ---------------------------------------------------------------------------
 * Description: 
 * Contains a Function so send Messages on COM1/COM2 to a Terminal.
 * Contains some handy Functions to convert different DataTypes to Strings
 *  
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Feb 11, 2008, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * System include files
 * ---------------------------------------------------------------------------
 */
#include <string.h>
#include <stdlib.h>

#include "lpc23xx.h"
#include "types.h"

/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "Task.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */
#include "SerOut.h"
#include "serial.h"
#include "logging.h"
/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */

extern UINT32 interruptcounter;
extern UINT32 switchcounter;

/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */
char CustomMessage[40];
char BoolRestoStrMessage[40];
char D_array[40];
char F_array[40];
char H_array[40];
/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */

void sendString(t_serial_devices ComPort, BOOLEAN newline,
        Messagetype_t urgency, char * DefMessage, char * Devider,
        char * CstmMessage)
{
    BOOLEAN allowPrintout = TRUE;

    switch (urgency)
        {
        case importantMessage:
            if (block_ImportantMessage == TRUE)
                {
                allowPrintout = FALSE;
                }
            break;
        case headerMessage:
            if (block_HeaderMessage == TRUE)
                {
                allowPrintout = FALSE;
                }
            break;
        case resultMessage:
            if (block_ResultMessage == TRUE)
                {
                allowPrintout = FALSE;
                }
            break;
        case noteMessage:
            if (block_NoteMessage == TRUE)
                {
                allowPrintout = FALSE;
                }
            break;
        case testMessage:
            if (block_TestMessage == TRUE)
                {
                allowPrintout = FALSE;
                }
            break;
        case menuMessage:
            if (block_MenuMessage == TRUE)
                {
                allowPrintout = FALSE;
                }
            break;
        default:
            ;
        }

    if ((allowPrintout == TRUE) || (LogFlag == TRUE))
        {

        if ((allowPrintout == TRUE) && (newline == TRUE))
            {
            serWrite(ComPort, strlen("\n\r"), (UINT8 *)"\n\r");
            }

        strcpy(CustomMessage, DefMessage);  /* Copy first Message to String */
        strcat(CustomMessage, Devider);     /* Copy second Message to String*/
        strcat(CustomMessage, CstmMessage); /* Copy third Message to String */

        if ((urgency == menuMessage)&& (block_MenuMessage == TRUE))
            {
            /* Do nothing                                               */
            } 
        else
            {
            writeLog(LogOutput, ComPort, urgency, CustomMessage);
            }

        if (allowPrintout == TRUE)
            {
            /* Send built String to defined COM                             */
            serWrite(ComPort, strlen(CustomMessage), (UINT8 *) CustomMessage);
            }

        }
}

void debugPrint(char * Message)
{
    serWrite(SerOutPort, strlen(Message), (UINT8 *)Message);
}


char * BooltoStr (BOOLEAN var)
{
    if (var == TRUE)                     /* If BOOLEAN Variable is TRUE  */
        {
        strcpy(BoolRestoStrMessage, "HIGH/TRUE"); 
        } 
    else                                    /* If BOOLEAN Variable is FALSE */
        {
        strcpy(BoolRestoStrMessage, " LOW/FALSE "); 
        }
    return BoolRestoStrMessage;             /* Return built String          */
}


char * BoolRestoStr(BOOLEAN Result)
{
    if (Result == TRUE)                     /* If BOOLEAN Variable is TRUE  */
        {
        strcpy(BoolRestoStrMessage, "PASSED "); /* Copy "PASSED" to String  */
        } 
    else                                    /* If BOOLEAN Variable is FALSE */
        {
        strcpy(BoolRestoStrMessage, " FAILED "); /* Copy "FAILED"to String  */
        }
    return BoolRestoStrMessage;             /* Return built String          */
}

char * ItoDStr(UINT32 IntValue)
{
    sprintf(D_array, "%i", IntValue); /* Convert INT-Value to DEZ-String    */
    return D_array;                         /* Return DEZ-String            */
}

char * FtoDStr(float IntValue)
{
    sprintf(F_array, "%f", IntValue); /* Convert INT-Value to DEZ-String    */
    return F_array;                         /* Return FLOAT-String          */
}

char * ItoHStr(UINT32 IntValue)
{
    sprintf(H_array, "%X", IntValue); /* Convert INT-Value to HEX-String    */
    return H_array;                         /* Return HEX-String            */
}

