/* ---------------------------------------------------------------------------
 * topoftest.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: 
 * DesignTest MAIN File
 * Contains automatic Test Application
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Mar 06, 2008, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

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

#include "LPC23xx.h"
#include "types.h"

/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */
#include "topoftest.h"
#include "dio.h"
#include "ledfunctions.h"
#include "taskfunctions.h"
#include "menu.h" 
#include "menuargs.h"
#include "serial.h"
#include "SerOut.h"
#include "bus.h"
#include "dac.h"
#include "BusProtocol.h"
#include "protocolfunctions.h"
#include "remote_tests.h"
#include "remote_misc.h"

/*Test includes */
#include "testLED.h"
#include "testdio.h"
#include "testaio.h"
#include "testBUS.h"
#include "testeeprom.h"
#include "testMMC.h"
#include "testpower.h"
#include "testrtc.h"

/* temp includes */
#include "fat_test.h"
#include "mmc_transfer.h"

/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */

/* Time in ms between single Tests                                          */
#define TestPause 			100

/* BUS Communication IDs                                                    */
#define BUS_ID              1           /* 1 for Master, >1 for Slave       */
#define BUS_numbers         2           /* Maximum Numbers of Devices       */

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

/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */
UINT32 UINT32result;
BOOLEAN auto_testResult;
BOOLEAN placedEB = TRUE; /* Indicate whether Extension Board is connected   */

/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */

void mainInit(void)
{
#if ((PINSET_TESTER == 0) || (PINSET_TESTER == 2))
    /* Olimex Board and Tester only have one serial port                    */
    SerOutPort = COM1;                  /* Set Message ComPort to COM2      */
    MenuPort = COM1;
#else
    /* IO-CTRL got 2 serial ports to communicate                            */
    SerOutPort = COM2;                  /* Set Message ComPort to COM2      */
    MenuPort = COM2;
#endif
    
    thisDeviceNumber = 0;                   /* define this device as Master */
    remoteDeviceNumber = 2;                 /* define default remote device */
    
    block_ImportantMessage = FALSE;
    block_HeaderMessage = FALSE;
    block_ResultMessage = FALSE;
    block_NoteMessage = FALSE;
    block_TestMessage = FALSE;
    block_MenuMessage = TRUE;
    block_SpinningWheel = TRUE;
    GotoNewLine = TRUE;

 
    /* Do Test Initialisations                                              */
    taskInit();                         /* Initialize TaskStatus Variables  */
    protocolInit (BUS1, BUS_ID, BUS_numbers); /* Init Bus System            */
    
    
                         /* Indicate Board activity with flashing green LED */
    d_gLED = xTaskCreate( (void *)gLEDToggle,
            ( signed portCHAR * ) "LedToggle", 
            configMINIMAL_STACK_SIZE, (UINT16 *)500, 
            tskIDLE_PRIORITY + 2, &gLED);
    
    /* Create Spinning Wheel Task and block it immediately                  */
    d_spinWheel = xTaskCreate( (void *)showSpinningWheel,
            ( signed portCHAR * ) "SpinningWheel", 
            configMINIMAL_STACK_SIZE, NULL, 
            tskIDLE_PRIORITY + 2, &spinWheel);
    


//    dioClean();
    CallMenu();                         /* Goto Menu                        */

} 


void showSpinningWheel (void)
{
    static UINT32 arraycnt = 0;
    UINT8 SpinningWheelArray[4] = {'-', '\\', '|', '/'};

    
    for (;;)
        {
        /* Make it possible to block the PrintOut of the Wheel              */
        switch (block_SpinningWheel)
            {
            case TRUE: 
                break;
            case FALSE:
                /* Set cursor one Position to left, then Put Character      */
                /* Don't care about the Warning                             */
                serWrite(SerOutPort, strlen("\x1B[1D"), (UINT8 *)"\x1B[1D");
                serPut(SerOutPort, SpinningWheelArray[arraycnt]);
                
                if (arraycnt == 3)
                    {
                    arraycnt = 0;
                    }
                else
                    {
                    arraycnt += 1;
                    }
            break;
            }
        vTaskDelay(100);
        }
}
