// This demo-program illustrates continuous reading and display of all four
// axes. All encoders are initialized to resolution = 4 clocks per cycle.

#include <bios.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#include "EncPci.h"

void CommunicationError(void);

void main(void)
{
#define MAX_PCI_CARDS 10
// (MAX_PCI_CARDS is rather arbitrary. The array below has MAX_PCI
// elements, indexed 0 through MAX_PCI_CARDS-1.)
long CardHandleArray[MAX_PCI_CARDS-1] ;
short CardsDetected ;

                char msg [200] ;
                long CardHandle ;
		short CardIndex ;
		short OK ;  // 0 = Success, 1 = Failure.

int ReturnCode ;
int ChipInputsMirror ;
long PositionCounterA, PositionCounterB, PositionCounterC, PositionCounterD ;
long SetPoint ;

        // Locate all the existing EIC-325/PCI cards:
		for (CardIndex = 0 ; (CardIndex < MAX_PCI_CARDS) ; CardIndex++) {
			OK = GetCardHandle (&CardHandle, CardIndex) ;
			if (OK == 0) break ;  // No more cards.

            // (EIC-325/PCI card detected.)

            // Store the result in an array:
            CardHandleArray[CardIndex] = CardHandle ;
		}

        clrscr() ;
        // Report how many cards were detected:
        // Note that current CardIndex = how many cards were detected.
	CardsDetected = CardIndex ;
	switch (CardsDetected)
        {
            case 0:
                                gotoxy (2,12) ;
                                printf ("No 'EIC-325/PCI' cards detected, program aborted.") ;
				exit(0) ;
                                break ;
            case 1:
                                gotoxy (2,12) ;
                                printf ("One 'EIC-325/PCI' card detected.") ;
				break ;
	    default:
				sprintf (msg, "%d 'EIC-325/PCI' cards were detected.\n The readings refer to the first card recognized.", CardsDetected) ;
                                gotoxy (2,12) ;
                                printf (msg) ;
		                break ;
	}

   CardHandle = CardHandleArray[0] ; // We refer to the first card recognized.
   ReturnCode = SetActiveChip(CardHandle, Chip1) ;
   if (ReturnCode == 0)   CommunicationError();

   ReturnCode = SetEncoderResolution (CardHandle, Encoder1, Clock4);
   if (ReturnCode == 0)   CommunicationError();

   ReturnCode = SetEncoderResolution (CardHandle, Encoder2, Clock4);
   if (ReturnCode == 0)   CommunicationError();


   ReturnCode = SetActiveChip(CardHandle, Chip2) ;
   if (ReturnCode == 0)   CommunicationError();

   ReturnCode = SetEncoderResolution (CardHandle, Encoder1, Clock4);
   if (ReturnCode == 0)   CommunicationError();

   ReturnCode = SetEncoderResolution (CardHandle, Encoder2, Clock4);
   if (ReturnCode == 0)   CommunicationError();

   ChipInputsMirror = 0 ;

   gotoxy (2,20) ;
   printf ("Press any key to stop -") ;

   while (bioskey(1) == 0) {  // Pressing any key stops the loop
      ReturnCode = SetActiveChip(CardHandle, Chip1) ;
      if (ReturnCode == 0)   CommunicationError();
      ReturnCode = RequestPositionCounter (CardHandle, Encoder1, ChipInputsMirror) ;
      if (ReturnCode == 0)   CommunicationError();
      ReturnCode = ReadPositionCounter (CardHandle, Encoder1, &PositionCounterA) ;
      if (ReturnCode == 0)   CommunicationError();
      ReturnCode = RequestPositionCounter (CardHandle, Encoder2, ChipInputsMirror) ;
      if (ReturnCode == 0)   CommunicationError();
      ReturnCode = ReadPositionCounter (CardHandle, Encoder2, &PositionCounterB) ;
      if (ReturnCode == 0)   CommunicationError();

      ReturnCode = SetActiveChip(CardHandle, Chip2) ;
      if (ReturnCode == 0)   CommunicationError();
      ReturnCode = RequestPositionCounter (CardHandle, Encoder1, ChipInputsMirror) ;
      if (ReturnCode == 0)   CommunicationError();
      ReturnCode = ReadPositionCounter (CardHandle, Encoder1, &PositionCounterC) ;
      if (ReturnCode == 0)   CommunicationError();
      ReturnCode = RequestPositionCounter (CardHandle, Encoder2, ChipInputsMirror) ;
      if (ReturnCode == 0)   CommunicationError();
      ReturnCode = ReadPositionCounter (CardHandle, Encoder2, &PositionCounterD) ;
      if (ReturnCode == 0)   CommunicationError();

      gotoxy (2,3) ;
      printf ("A = %8lu", PositionCounterA) ;
      clreol();

      gotoxy (2,4) ;
      printf ("B = %8lu", PositionCounterB) ;
      clreol();

      gotoxy (2,5) ;
      printf ("C = %8lu", PositionCounterC) ;
      clreol();

      gotoxy (2,6) ;
      printf ("D = %8lu", PositionCounterD) ;
      clreol();

   }
   clrscr() ;
}

void CommunicationError(void)
{
 gotoxy (2,20) ;
 printf ("Communication Error, Press any key to terminate.") ;
 while (bioskey(1) == 0) ;
}

