/****************************************************************************

Module: Test2.cpp

Purpose:

   This module calls the Spectrum API dialog routines using Phase 1 
   communication.

****************************************************************************/

/////////////////////////////////////////////////////////////////////////////
// Include files.
/////////////////////////////////////////////////////////////////////////////

#include "afx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "spectrum.h"

/////////////////////////////////////////////////////////////////////////////
// Local definitions.
/////////////////////////////////////////////////////////////////////////////

// Define the API version and the API date.
#define API_VERSION "V3.5-Beta3"
#define API_DATE    "April 10, 2001"

// Define a macro for checking the status code returned from the 
// Spectrum API routines.
#define CHECK_STATUS_CODE(StatusCode)                     \
{                                                         \
   if ((StatusCode) != SPC_SC_Success)                    \
   {                                                      \
      char szStatusText[512];                             \
      SPUTL_FormatStatusCode((StatusCode), szStatusText); \
      printf("ERROR: %s\n", szStatusText);                \
      return(StatusCode);                                 \
   }                                                      \
}

/////////////////////////////////////////////////////////////////////////////
// This routine calls the Spectrum API dialog routines using Phase 1
// communication.
/////////////////////////////////////////////////////////////////////////////

SPT_lStatusCode main(int argc, char* argv[])
{
   SPT_lStatusCode     lStatusCode;
   SPT_lComPortNumber  lComPortNumber;
   SPT_ulLoggerAddress ulLoggerAddress;

   // Ensure the user passed the correct number of parameters.
   if (argc != 2)
   {
      printf("\n");
      printf("PURPOSE:\n");
      printf("\n");
      printf("   This program calls the Spectrum API dialog routines using Phase 1\n");
      printf("   communication.\n");
      printf("\n");
      printf("USAGE:\n");
      printf("\n");
      printf("   Test2 ComPortNumber\n");
      printf("\n");
      printf("EXAMPLE:\n");
      printf("\n");
      printf("   Test2 1\n");
      return(SPC_SC_Success);
   }

   // Check the API version.
   char szApiVersion[256];

   lStatusCode = SPAPI_GetApiVersion(szApiVersion);
   CHECK_STATUS_CODE(lStatusCode);

   if (strcmp(szApiVersion, API_VERSION) != 0)
   {
      printf("ERROR: Invalid API version detected");
      return(SPC_SC_Success);
   }

   // Check the API date.
   char szApiDate[256];

   lStatusCode = SPAPI_GetApiDate(szApiDate);
   CHECK_STATUS_CODE(lStatusCode);

   if (strcmp(szApiDate, API_DATE) != 0)
   {
      printf("ERROR: Invalid API date detected");
      return(SPC_SC_Success);
   }

   // Get the COM port number.
   lComPortNumber = atoi(argv[1]);

   // Set the logger address.
   ulLoggerAddress = 0;

   // Enable Phase 1 communication.
   lStatusCode = SPAPI_SetProtocol(FALSE);
   CHECK_STATUS_CODE(lStatusCode);

   // Display the Clear Logger dialog box.
   lStatusCode = SPDLG_ClearLogger(lComPortNumber, ulLoggerAddress);
   CHECK_STATUS_CODE(lStatusCode);

   // Display the Setup Channels dialog box.
   lStatusCode = SPDLG_SetupChannels(lComPortNumber, ulLoggerAddress);
   CHECK_STATUS_CODE(lStatusCode);

   // Display the Setup Description dialog box.
   lStatusCode = SPDLG_SetupDescription(lComPortNumber, ulLoggerAddress);
   CHECK_STATUS_CODE(lStatusCode);

   // Display the Setup Logger dialog box.
   lStatusCode = SPDLG_SetupLogger(lComPortNumber, ulLoggerAddress);
   CHECK_STATUS_CODE(lStatusCode);

   // Display the Setup Sample Interval dialog box.
   lStatusCode = SPDLG_SetupSampleInterval(lComPortNumber, ulLoggerAddress);
   CHECK_STATUS_CODE(lStatusCode);

   // Display the Transfer Samples dialog box.
   char szLoggerFileName[512];
   lStatusCode = SPDLG_TransferSamples(lComPortNumber, 
                                       ulLoggerAddress,
                                       szLoggerFileName);
   CHECK_STATUS_CODE(lStatusCode);
   if (strlen(szLoggerFileName))
   {
      printf("Created Logger File Name: %s\n", szLoggerFileName);
   }

   return(SPC_SC_Success);
}
