/****************************************************************************

Module: Test10.cpp

Purpose:

   This module periodically reads and displays the channel values 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 periodically reads and displays the channel values using 
// Phase 1 communication
/////////////////////////////////////////////////////////////////////////////

SPT_lStatusCode main(int argc, char* argv[])
{
   SPT_lStatusCode     lStatusCode;
   SPT_lComPortNumber  lComPortNumber;
   SPT_ulLoggerAddress ulLoggerAddress;
   char                szBuffer[512];

   // Ensure the user passed the correct number of parameters.
   if (argc != 2)
   {
      printf("\n");
      printf("PURPOSE:\n");
      printf("\n");
      printf("   This program periodically reads and displays the channel values using\n");
      printf("   Phase 1 communication.\n");
      printf("\n");
      printf("USAGE:\n");
      printf("\n");
      printf("   Test10 ComPortNumber\n");
      printf("\n");
      printf("EXAMPLE:\n");
      printf("\n");
      printf("   Test10 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);

   // Create a logger object.
   SPT_ulLoggerHandle ulLoggerHandle;    
   lStatusCode = SPLGR_CreateLoggerObject(&ulLoggerHandle);
   CHECK_STATUS_CODE(lStatusCode);

   // Loop forever.
   while (TRUE)
   {
      // Read the channel values from the logger.
      lStatusCode = SPLGR_ReadChannelValues(ulLoggerHandle, 
                                            lComPortNumber,
                                            ulLoggerAddress);
      if (lStatusCode != SPC_SC_Success)
      {
         SPUTL_FormatStatusCode(lStatusCode, szBuffer);
      }
      else
      {
         // Clear the print buffer.
         szBuffer[0] = 0;

         // Loop for each channel.
         for (SPT_lChannelIndex lChannelIndex = 0; lChannelIndex < 8; lChannelIndex++)
         {
            // Get the channel enabled flag.
            BOOL bChannelEnabled;
            lStatusCode = SPLGR_GetChannelEnabled(ulLoggerHandle, 
                                                  lChannelIndex, 
                                                  &bChannelEnabled);
            CHECK_STATUS_CODE(lStatusCode);
      
            // See if the channel is enabled.
            if (bChannelEnabled)
            {
               // Get the channel value.
               double dChannelValue;
               lStatusCode = SPLGR_GetChannelValue(ulLoggerHandle,
                                                   lChannelIndex,
                                                   &dChannelValue);
               CHECK_STATUS_CODE(lStatusCode);

               // Get the channel units.
               char szUnits[512];
               lStatusCode = SPLGR_GetChannelUnits(ulLoggerHandle, 
                                                   lChannelIndex, 
                                                   szUnits);
               CHECK_STATUS_CODE(lStatusCode);

               // Display the channel value and the channel units.
               sprintf(&szBuffer[strlen(szBuffer)],
                       "%12.2lf %-4.4s", 
                       dChannelValue, 
                       szUnits);
            }
         }
      }

      // Output the print buffer.
      printf("%s\n", szBuffer);

      // Sleep until it is time for the next update. Note that the 
      // more you communicate with a data logger the quicker the 
      // batteries will go dead.
      Sleep(10000);
   }

   // Delete the logger object.
   lStatusCode = SPLGR_DeleteLoggerObject(ulLoggerHandle);
   CHECK_STATUS_CODE(lStatusCode);

   return(SPC_SC_Success);
}
