/* ---------------------------------------------------------------------------
 * menuargs.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: 
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Nov 13, 2008, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

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

#include "LPC23xx.h"
#include "types.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */ 
#include "menuargs.h"
#include "menufunctions.h"

#include "SerOut.h"

#include "can.h"

#include "remote_tests.h"

/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */
#define ASCII_START             97
/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */

/* Make Sure that at least the first letter of this is in alphabetical order*/
const char *command_table[NUMBER_OF_COMMANDS] = 
{
        "aio",      
        "battery",
        "bus",
        "canread",
        "cantest",
        "canwrite",
        "ccin",
        "ccout",
        "cdel",
        "cload",
        "confin",
        "confout",
        "cslave",
        "curout",
        "curin",
        "cvin",
        "cvout",
        "digiin",
        "digiout",
        "dio",
        "eeprom",
        "fattest",
        "help",
        "initSD",
        "initTest",
        "ledclean",
        "ledtest",
        "listlocal",
//        "listremoteattach",
        "logenable",
        "logmount",
        "logstart",
        "logstop",
        "ls",
        "menudebug",
        "mmc",
        "power",
        "quit",
        "readrtc",
        "remotecall",
        "rtc",
        "run",
        "stat_cal",
        "stat_def",
        "stat_kill",
        "stat_show",
        "stat_showall",
        "setmux",
        "setrtc",
        "tasksdelete",
        "volin",
        "volout"
};

/* Make Sure that at least the first letter of this is in alphabetical order*/
const fpointer command_pointer[NUMBER_OF_COMMANDS] = 
{
        m_aio,
        m_battery,
        m_bus,
        m_canread,
        m_cantest,
        m_canwrite,
        m_ccin,
        m_ccout,
        m_cdel,
        m_cload,
        m_confin,
        m_confout,
        m_cslave,
        m_curout,
        m_curin,
        m_cvin,
        m_cvout,
        m_digiin,
        m_digiout,
        m_dio,
        m_eeprom,
        m_fattest,
        m_help,
        m_initSD,
        r_TestInit,
        m_clean,
        m_ledtest,
        m_listLocalattachments,
//        m_listremoteattachments,
        m_logEnable,
        m_sdMount,
        m_logStart,
        m_logStop,
        m_loglist,
        m_menuDebug,
        m_mmc,
        m_power,
        m_quit,
        m_readRTC,
        m_callRemoteFunction,
        m_rtc,
        m_run,
        m_c_cal,
        m_c_def,
        m_c_kill,
        m_c_stat,
        m_c_show,
        m_switchMux,
        m_setRTC,
        m_tasksdelete,
        m_volin,
        m_volout,        
};

/* Array to store the offsets to the first letters of commands              */
UINT8 cmd_table_offset[ALPHABET_DIGITS];

const char * menuHelptextArray [NUMBER_OF_COMMANDS] = 
{
        "\n\r\n\r\taio arg1 arg2\n\r\ttests the analogue in- and outputs"
            "\n\r\n\r\t\targ1: device number. 0 sets up a self test"
            "\n\r\t\targ2: test type (0: MB, 1: EB) (Only on remote test)",
        
        "\n\r\n\r\tbattery arg1\n\r\ttests the battery supply of a remote device"
            "\n\r\n\r\t\targ1: device number. 0 is self device",
            
        "\n\r\n\r\tbus\n\r\ttests the local bus connections with transmit and receive strings",
        
        "\n\r\n\r\tcanread\n\r\tPrints out the actual frame received on CAN",
        
        "\n\r\n\r\tcantest\n\r\tTests the CAN interface with a Self Test",
        
        "\n\r\n\r\tcanwrite arg1 arg2 arg3\n\r\twrites a message in a CAN frame"
            "\n\r\n\r\t\targ1 - Bits 0-10: Identifier, 13-15: Interface Number, 16-19: Data Length"
            "\n\r\t\targ2 - first 4 Bytes of Message"
            "\n\r\t\targ3 - last 4 Bytes of Message",
            
        "\n\r\n\r\tccin\n\r\tcalibrate the local analogue current inputs"
            "\n\r\tPut calibration Value (e.g.20 mA) on each line separately when asked for",
            
        "\n\r\n\r\tccout\n\r\tcalibrate the local analogue current outputs"
            "\n\r\tOnly useful with already calibrated analogue current inputs"
            "\n\r\tConnect outputs 0-5 to inputs 0-5. Software works automated",
            
        "\n\r\n\r\tcdel arg1 arg2\n\r\tDeletes and resets the calibration values in eeprom"
            "\n\r\n\r\t\targ1: number of target device"
            "\n\r\t\targ2=0-3: kind of values to reset - 0=VI, 1=VO, 2=CI, 3=CO",
            
        "\n\r\n\r\tcload arg1 arg2\n\r\treloads calibration values from EEPROM to system"
            "\n\r\n\r\t\targ1: number of target device"
            "\n\r\t\targ2=0-3: kind of values to reset - 0=VI, 1=VO, 2=CI, 3=CO",
            
        "\n\r\n\r\tconfin arg1 arg2\n\r\tConfigures an analogue input on local device"
            "\n\r\n\r\t\targ1=0-7, 9: chose channel 0-7 or chose all channels with 9"
            "\n\r\t\targ2=0-1: 0: Sets mode to Voltage, 1: Sets mode to Current",
            
        "\n\r\n\r\tconfout arg1 arg2\n\r\tConfigures an analogue input on local device"
            "\n\r\n\r\t\targ1=0-5, 9: chose channel 0-7 or chose all channels with 9"
            "\n\r\t\targ2=0-1: 0: Sets mode to Voltage, 1: Sets mode to Current",
                
        "\n\r\n\r\tcslave\n\r\tcalibrates the slave with number 2",
            
        "\n\r\n\r\tcurout arg1 arg2 arg3\n\r\tDrives an analogue Output with current value"
            "\n\r\n\r\t\targ1: device number"
            "\n\r\t\targ2=0-5: channel number"
            "\n\r\t\targ3=0-20000: value",
            
        "\n\r\n\r\tcurin arg1 arg2\n\r\t Reads the actual current value from a analogue input"
            "\n\r\n\r\t\targ1: device number"
            "\n\r\t\targ2: channel number",
            
        "\n\r\n\r\tcvin\n\r\tCalibrates the analogue voltage inputs"
            "\n\r\tPut calibration value (e.g.10 V) on all inputs at the same time."
            "\n\r\tSoftware works automated",
            
        "\n\r\n\r\tcvout\n\r\tCalibrate the analogue voltage outputs"
            "\n\r\tOnly useful with already calibrated analogue voltage inputs"
            "\n\r\tConnect outputs 0-5 to inputs 0-5. Software works automated",
            
        "\n\r\n\r\tdigiin arg1 arg2\n\r\tReads the actual value of a digital input"
            "\n\r\n\r\t\targ1: device number"
            "\n\r\t\targ2: channel number",
            
        "\n\r\n\r\tdigiout arg1 arg2 arg3\n\r\tWrites to a digital output"
            "\n\r\n\r\t\targ1: device number"
            "\n\r\t\targ2: channel number"
            "\n\r\t\targ3=0-1: 0: FALSE/LOW, 1: TRUE/HIGH",
            
        "\n\r\n\r\tdio arg1 arg2\n\r\tTests the digital in- and outputs"
            "\n\r\n\r\t\targ1: device number. 0 sets up a self test"
            "\n\r\t\targ2: test type (0: MB, 1: EB) (Only on remote test)",
            
        "\n\r\n\r\teeprom arg1\n\r\tTests the connection and algorithms of the EEPROM"
            "\n\r\n\r\t\targ1: device number. 0 sets up a self test",
            
        "\n\r\n\r\tfattest\n\r\tTest sequence for FAT Filesystem on SD/MMC",
            
        "\n\r\n\r\thelp\n\r\tCALLS HELP, WHAT ELSE???",
        
        "\n\r\n\r\tinitSD\n\r\tInitialises the SD Card",
        
        "\n\r\n\r\tinitTest\n\r\tInitialises the remote test applications",
        
        "\n\r\n\r\tledclean arg1\n\r\tSets all digital output LEDs to OFF"
            "\n\r\n\r\t\targ1: device number",
        
        "\n\r\n\r\tledtest arg1\n\r\tTests all LEDs available"
            "\n\r\n\r\t\targ1: device number. 0 sets up a self test",
        
        "\n\r\n\r\tlistlocalattach\n\r\tLists all local bus-attached functions",
        
//        "\n\r\n\r\tlistremoteattach\n\r\tLists all remote bus-attached functions",
        
        "\n\r\n\r\tlogenable arg1\n\r\tEnables/Disables Output logging"
            "\n\r\n\r\targ1=0-1: 0: disable, 1: enable",
            
        "\n\r\n\r\tlogmount\n\r\tmounts the SD Card to file system",
        
        "\n\r\n\r\tlogstart arg1\n\r\tStarts logging on file"
            "\n\r\n\r\targ1: serial number of device and filename of log-file",
            
        "\n\r\n\r\tlogstop\n\r\tstops logging on file",
        
        "\n\r\n\r\tls\n\rlists all existing logfiles",
            
        "\n\r\n\r\tmenudebug arg1\n\r\tEnables detailed messages from the menu"
            "\n\r\n\r\t\targ1=0-1: 1: enable, 0: disable",
            
        "\n\r\n\r\tmmc arg1\n\r\tTests the memory card interface"
            "\n\r\n\r\t\targ1: device number. 0 sets up a self test",
            
        "\n\r\n\r\tpower arg1\n\r\tTests the devices power connections"
            "\n\r\n\r\t\targ1: device number. 0 sets up a self test",
            
        "\n\r\n\r\tquit\n\r\tEXITS MENU",
        
        "\n\r\n\r\treadrtc\n\r\tReads the RTC",
        
        "\n\r\n\r\tremotecall arg1 arg2 arg3\n\r\tCalls remote functions on Salve"
            "\n\r\n\r\t\targ1: Remote function number"
            "\n\r\t\targ2: Number of function parameters"
            "\n\r\t\targ3: Function paramters",
        
        "\n\r\n\r\trtc arg1\n\r\tTests the RealTime Clock"
            "\n\r\n\r\t\targ1: device number. 0 sets up a self test",
        
        "\n\r\n\r\trun\n\r\tRuns a complete test sequence",
        
        "\n\r\n\r\tstat_cal arg1\n\r\tSets the status of the analogue in- and outputs to CALIBRATED"
            "\n\r\n\r\t\targ1: device number. 0 is self device",
            
        "\n\r\n\r\tstat_def arg1\n\r\tSets the status of the analogue in- and outputs to DEFAULT"
            "\n\r\n\r\t\targ1: device number. 0 is self device",
                
        "\n\r\n\r\tstat_kill arg1\n\r\tDeletes the status of the analogue in- and outputs"
            "\n\r\n\r\targ1: device number. 0 is self device", 
                
        "\n\r\n\r\tstat_show arg1 arg2\n\r\tShows the status of the analogue in- and outputs"
            "\n\r\n\r\t\targ1: device number"
            "\n\r\t\targ2: Status type - 0=VI, 1=VO, 2=CI, 3=CO", 
                
        "\n\r\n\r\tstat_showall arg1\n\r\tShows the status of ALL analogue in- and outputs"
            "\n\r\n\r\t\targ1: device number",
        
        "\n\r\n\r\tsetMux arg1 arg2\n\r\tSets the in- and ouput mux devices"
            "\n\r\n\r\t\targ1: Mux type (0: DI; 1: DO; 2: AI; 3: AO)"
            "\n\r\t\targ2: Mode (0: Main board; 1: Extension board)",
        
        "\n\r\n\r\tsetRTC arg1 arg2 arg3\n\r\tSets Date and Time in the RTC"
            "\n\r\n\r\targ1:|day|hour|minutes|seconds| (each 8 bit)"
            "\n\r\targ2: |0|0|0|0|month|dayOfWeek (each 8 bit)"
            "\n\r\targ3: |dayOfYear|year| (each 16 bit)",
            
        "\n\r\n\r\ttasksdelete\n\r\tDeletes all testLED tasks",
        
        "\n\r\n\r\tvolin arg1 arg2\n\r\tReads actual value on analogue voltage input"
            "\n\r\n\r\t\targ1: device number. 0 is self device"
            "\n\r\t\targ2=0-7: channel number",
            
        "\n\r\n\r\tvolout arg1 arg2 arg3\n\r\tDrives analogue voltage output"
            "\n\r\n\r\t\targ1: device number. 0 is self device"
            "\n\r\t\targ2=0-5: channel number"
            "\n\r\t\targ3=0-10000: Value"
        
        
};
/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */

void menuInit (void)
{
    UINT8 loopcnt = 0;
    UINT8 asciicnt;
    BOOLEAN breakout;

    /* Calculate Array-Offsets for each possible first letter
     * The actual Letter is mentioned in asciicnt, which starts with dezimal
     * code of the first, small letter (a = 97).
     */
    for (asciicnt = ASCII_START; asciicnt < (ASCII_START + ALPHABET_DIGITS); asciicnt++)
        {
        loopcnt = 0;
        breakout = FALSE;
        /* Perform search as long as no accordance was found or array ends  */
        while (breakout == FALSE)
            {
            if (loopcnt < NUMBER_OF_COMMANDS)
                {
                if (command_table[loopcnt][0] == asciicnt)
                    {
                    /* Accordance found! Save Offset in offset-array        */
                    cmd_table_offset[(asciicnt - ASCII_START)] = loopcnt;
                    breakout = TRUE;        /* Enable breakout of loop      */
                    }
                }
            else
                {
                /* No Accordance found. Save "NO OFFSET" to offset-array    */
                cmd_table_offset[(asciicnt - ASCII_START)] = 0xFF;
                breakout = TRUE;            /* Enable breakout of loop      */
                }
            loopcnt++;
            }
        }
}
