//#DOC Main, program entry and main loop
#include "main.h"

otUSB   usb;        // Declare USB library
otDAQ   daq;        // Debug

U16     * mat;

void setup()
{
    // Init connection with the console via UART0
    UartEnableSignals(UART0);
    UartSetBaud(UART0, 115200);
    // Init stout used by printf
    setStdout(4096, UART0);   // printf output is now directed on UART0
    
    // Init the timer for time measure
    TimerSetAsCounter(TIMER0);
    TimerEnable(TIMER0, true);
    
    DelayMs(1000);
    
    // Init USB channel, the command list can accept 64 different commands
    usb.init(64);
    
    // Debug system
    daq.init(&usb);
    
    // Create an array for test ....
    mat = (U16 *) _malloc(sizeof(U16) * 64);
    // Init the array
    for(U16 i = 0; i < 64; i++)
        mat[i] = i;
    
    printf("Start DAQ and press key S to show data ...");
}

void loop()
{
    if(UartCheckChar(UART0))
    {
        U8  ch = UartChar(UART0);
        switch(ch)
        {
            case 's':
            case 'S': // Show the array contents, previous dialog must be closed
                    if(daq.isDialogClose())
                        daq.showArray(mat, otDAQ::DT_U16, 8, 8);
                    break;
            case '*': // Change array contents
                    for(U16 i = 0; i < 64; i++)
                        mat[i] *= 2;
                    break;
            case '+': // Change array contents
                    for(U16 i = 0; i < 64; i++)
                        mat[i]++;
                    break;
        }
    }
    // Handle for USB commands
    usb.process();
}