//////////////////////////////////////////////////////////////////////
//
// PmcaTest.cpp: PMCA COM test Module.
//
//////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <stdio.h>
#include "PmcaCom.h"
#include "PmcaErr.h"

static int g_port;
static int g_baudRate;
static int g_gain;
static int g_scale;
static PmcaDeviceType g_device;
static PmcaFlagsType g_flags;
static PmcaStatusType g_status;
static char chBuff[32];
static unsigned long buff[16384];
#define MAX_MEMORY 32768
#define printf0(format) printf(format); fflush(stdout)
#define printf1(format, item) printf(format, item); fflush(stdout)
 
// command line: test <com port> <baud rate> <device type> <start gain>
// device type 0 - AUTO DETECT, 1 - PMCA 8000, 2 - PMCA 8000A 
int main(int argc, char *argv[])
{
    int stat = 0;
    int result;
    int maxGroup;
    int group;
    int i;
    if (argc > 1)
        stat = atoi(argv[1]);
    while (stat > 4 || stat < 1)
    {
        printf("Enter Com Port Number:");
        scanf("%d", &stat);
    }
    g_port = stat;
    
    g_baudRate = 0;
    if (argc > 2)
        g_baudRate = atoi(argv[2]);
    while (g_baudRate < 1200 || g_baudRate > 115200)
    {
    
        printf("Enter baud rate:");
        scanf("%d", &g_baudRate);
    }
    stat = PMCA_AUTO_DETECT;
    if (argc > 3)
    {
        stat = atoi(argv[3]);
        while (stat < 0 || stat > 2)
        {
        
            printf("\nEnter device type (0 - AUTO DETECT, "
                    "1 - PMCA8000, 2 - PMCA800A) :\n");
            scanf("%d", &stat);
        }
    }
    g_device = (PmcaDeviceType) stat;
    g_gain = 256;
    if (argc > 4)
    {
        g_gain = atoi(argv[4]);
        while (g_gain < 256 || g_gain > 16384)
        {
        
            printf("Enter initial gain:");
            scanf("%d", &g_gain);
        }
    }
    stat = g_gain >> 9;
    g_scale = 6;
    while (stat)
    {
        stat >>= 1;
        g_scale--;
    }
    printf("\nStarting test. Comport : %d, baud rate : %d, gain : %d\n",
            g_port, g_baudRate, g_gain);

    printf("\nPower Up - ");
    stat = PmcaPowerUp(g_port);
    if (!stat)
        printf("passed\n");
    else
    {
        printf("failed, stat -  %x\n", stat);
        goto L_Terminate;
    }

    printf("\nConnecting - ");
    stat = PmcaConnect(g_port, g_baudRate, g_device);
    if (!stat)
        printf("passed\n");
    else
    {
        printf("failed, stat -  %x\n", stat);
        goto L_Terminate;
    }

    g_device = PmcaGetDeviceType();

    if (g_device == PMCA_8000A)
    {
        printf("\nReceiving serial number - ");
        stat = PmcaGetSerialNumber();
        if (stat >= 0)
            printf("passed\n");
        else
        {
            printf("failed, stat -  %x\n", stat);
            goto L_Terminate;
        }
        printf("Device - PMCA-8000A, serial number - %d\n", stat);
    }

    printf("\nSet priority to above normal - ");
    stat = PmcaSetPriority(4);
    if (stat >= 0)
        printf("passed\n");
    else
    {
        printf("failed, stat -  %x\n", stat);
        goto L_Terminate;
    }

    while (g_scale >= 0)
    {
        maxGroup = MAX_MEMORY / g_gain;

        printf1("\nSetting gain to %d - ", g_gain);
        stat = PmcaSetGain(g_gain);
        if (!stat)
            printf("passed\n");
        else
        {
            printf("failed, stat -  %x\n", stat);
            goto L_Terminate;
        }

        printf0("\nChecking gain - ");
        stat = PmcaGetGain();
        if (stat > 0)
            printf("passed\n");
        else
        {
            printf("failed, stat -  %x\n", stat);
            goto L_Terminate;
        }
        printf1("Gain set to %d\n", stat);

        Sleep(1000);
        for (group = -1; ++group < maxGroup;)
        {
            printf1("\nSetting group to %d - ", group);
            stat = PmcaSetGroup(group);
            if (!stat)
                printf("passed\n");
            else
            {
                printf("failed, stat -  %x\n", stat);
                goto L_Terminate;
            }

            if (g_device == PMCA_8000A)
            {
                stat = PmcaGetGroup();
                printf("Group set to %d\n", stat);
            }

            printf0("\nDeleting old data and time - ");
            stat = PmcaAsyncClearDataAndTime(&result);
            if (stat >= 0)
                printf("passed\n");
            else
            {
                printf("failed, stat -  %x\n", stat);
                goto L_Terminate;
            }

            for (i = 1000; --i;)
            {
                int j = i;
            }
            Sleep(10);

            i = stat;
            printf0("\nWaiting for delete data completion - ");
            stat = PmcaWait(stat);
            if (!stat)
                printf("passed\n");
            else if (stat == PMCA_ERROR_REQUEST_NOT_FOUND)
                printf("request has been already completed\n");
            else
            {
                printf("failed, stat -  %x\n", stat);
                goto L_Terminate;
            }

            printf0("\nSelecting live timer - ");

            for (i = 5; i--;)
            {
                stat = PmcaSelectLiveTimer();
                if (!stat)
                    break;
            }
            if (!stat)
                printf("passed\n");
            else
            {
                printf("failed, stat -  %x\n", stat);
                goto L_Terminate;
            }

            printf0("\nSetting threshold to 50 - ");
            stat = PmcaSetThreshold(50);
            if (!stat)
                printf("passed\n");
            else
            {
                printf("failed, stat -  %x\n", stat);
                goto L_Terminate;
            }

            printf0("\nSetting acquisition time to 100 - ");
            stat = PmcaSetAcquisitionTime(100);
            if (!stat)
                printf("passed\n");
            else
            {
                printf("failed, stat -  %x\n", stat);
                goto L_Terminate;
            }

            printf0("\nReceiving status - ");
            stat = PmcaGetStatus(&g_status);
            if (!stat)
                printf("passed\n");
            else
            {
                printf("failed, stat -  %x\n", stat);
                goto L_Terminate;
            }
            printf("Threshold set to %d, "
                    "acquisition time set to %d\n",
                    g_status.control.threshold,
                    g_status.presetTime);
            printf("Real time = %d\n", g_status.liveTime);

            printf0("\nStarting acquisition - ");
            stat = PmcaStartAcquisition(TRUE);
            if (!stat)
                printf("passed\n");
            else
            {
                printf("failed, stat -  %x\n", stat);
                goto L_Terminate;
            }

            Sleep(2000);

            if (g_device == PMCA_8000A)
            {
                printf0("\nAcquisition start time - ");
                stat = PmcaGetDateAndTime(chBuff);
                if (!stat)
                {
                    printf("passed\n");
                    printf("Acquisition started at %s\n", chBuff);
                }
                else
                {
                    printf("failed, stat -  %x\n", stat);
                    goto L_Terminate;
                }
            }

            Sleep(1);

            printf0("\nReceiving flags - ");
            stat = PmcaGetFlags(&g_flags);
            if (!stat)
                printf("passed\n");
            else
            {
                printf("failed, stat -  %x\n", stat);
                goto L_Terminate;
            }
            printf("Flags: 0x%2x\n", g_flags.byte);
            
            Sleep(2000);
            printf0("\nStop acquisition - ");
            for (i = 10; i--;)
            {			
                stat = PmcaStopAcquisition();
                if (!stat)
                    break;
                Sleep(500);
            }
            if (!stat)
                printf("passed\n");
            else
            {
                printf("failed, stat -  %x\n", stat);
                goto L_Terminate;
            }

            Sleep(1000);
            printf0("\nReceiving real time - ");
            stat = PmcaGetRealTime();
            if (stat >= 0)
            {
                printf("passed\n");
                printf("Real time (milliseconds) = %d\n", stat);
            }
            else
            {
                printf("failed, stat -  %x\n", stat);
                goto L_Terminate;
            }

            printf0("\nReceiving live time - ");
            stat = PmcaGetLiveTime();
            if (stat >= 0)
            {
                printf("passed\n");
                printf("Live time (milliseconds) = %d\n", stat);
            }
            else
            {
                printf("failed, stat -  %x\n", stat);
                goto L_Terminate;
            }

            Sleep(1000);
            printf0("\nReceiving data - ");
            stat = PmcaGetData(buff, 0, g_gain);
            if (!stat)
                printf("passed\n");
            else
            {
                printf("failed, stat -  %x\n", stat);
                goto L_Terminate;
            }
        }
        g_gain <<= 1;
        g_scale--;
    }
    printf0("Test completed successfully\n");
L_Leave:
    PmcaTerminateCom();
    return stat;
L_Terminate:
    printf0("Test terminated\n");
    goto L_Leave;
}
