#include <stdio.h>
#include "GeoSampler.h"
#include "DmErrno.h"
extern "C" 
{
# include "usrTime.h"
};
  
GeoSampler::GeoSampler(const char *name, GeoApp *app)
  : IbcMicroSampler(name, app)
{
  Int16 board;
  
  _geoApp = app;

  addCallback((float)10.0, 
	      (CallbackMethod) GeoSampler::updateDigitalInputs);
  
  Errno err;
  char errorBuf[errorMsgSize];

  MBool debug = TRUE;

  //Start Provider for Digital Inputs  
  if ((err = _geoApp->dmItems->spareDigitalInput0->startProvide(DM_ASYNC,
      ExclusiveProvider)) != SUCCESS)
  {	
      sprintDmError(err, "spareDigitalInput0->startProvide()", errorBuf);
      fprintf(stderr, "%s\n", errorBuf);
  }

  initDigitalInputs();
}


GeoSampler::~GeoSampler()
{
}



void GeoSampler::updateDigitalInputs()
{ 
  Word portWord;
  MBool spareDigitalInput0;
   
  MBool debug = FALSE;
  
  dprintf("GeoSampler::updateDigitalInputs()\n");
  
  Int16   cmdStatus;		    /* Command Status                       */
  Word    reply[2];               /* Command Result from microcontroller  */

                                    /* Send command to VME IBC micro        */
  if ( sendMicroCommand(_geoApp->device->channel(),
			&reply, sizeof(Word) * 2,
			2, IBC_CPU_APP | ISO_IO_READ_PORT, 
			ISOLATED_IO_BOARD_ADDR) == ERROR )
  {  
    logMsg("GeoSampler::updateDigitalInputs() "
	   "- sendMicroCommand() failed\n");

    return;
  }
    
  /* Check if command result is CMD OK    */
  if (wordFromBuf(&reply, 0) != CMD_OK) 
  {
    logMsg("GeoSampler::updateDigitalInputs() "
	   "- wordFromBuf() failed\n");
    return;
  }

  /* Command OK so update thresholds      */
  wordsFromBuf(reply, 2, &cmdStatus, &portWord);
   
  spareDigitalInput0 = ((portWord & SpareDigitalInput0Bit) ? TRUE : FALSE);    
  dprintf("updateDigitalInputs(): spareDigitalInput0=%d\n", spareDigitalInput0);

  if (spareDigitalInput0 != lastSpareDigitalInput0)
  {
    if(writeDmItem(_geoApp->dmItems->spareDigitalInput0->getHandle(),
                   &spareDigitalInput0, sizeof(spareDigitalInput0))==ERROR)
    {
      logMsg("GeoSampler::updateDigitalInput() Spare Digital Input 0 "
	     "- writeDmItem() failed\n");
      return;
    }
    lastSpareDigitalInput0 = spareDigitalInput0;
  }

  return;
} 


void GeoSampler::initDigitalInputs()
{ 
  Word portWord;
  MBool spareDigitalInput0;
 
  Int16   cmdStatus;		    /* Command Status                       */
  Word    reply[2];               /* Command Result from microcontroller  */

                                    /* Send command to VME IBC micro        */
  if ( sendMicroCommand(_geoApp->device->channel(), 
			&reply, sizeof(Word) * 2,
			2, IBC_CPU_APP | ISO_IO_READ_PORT, 
			ISOLATED_IO_BOARD_ADDR) == ERROR )
  {
    return;//ERROR
  }
    
  /* Check if command result is CMD OK    */
  if (wordFromBuf(&reply, 0) != CMD_OK) 
    return; //ERROR		    /* Command failed so return ERROR       */

  /* Command OK so update thresholds      */
  wordsFromBuf(reply, 2, &cmdStatus, &portWord);
   
  spareDigitalInput0 = ((portWord & SpareDigitalInput0Bit) ? TRUE : FALSE);

  writeDmItem(_geoApp->dmItems->spareDigitalInput0->getHandle(),
	      &spareDigitalInput0, sizeof(spareDigitalInput0));
  lastSpareDigitalInput0 = spareDigitalInput0;

  return;
}






