// This is a basic sample program demonstrating how to access the LCIC-4HS.
// The program displays the following data about each channel of the card:
// 1. Zero A/D
// 2. Current A/D
// 3. Current Weight
// 4. Channel calibrated? (1=yes, 0=no)
// In this sample program the card is mapped in address CD40.
// If this is not the case, please change the "Mem_Seg = 0xCD40 ;" statement.
// Note that INIT4.EXE should be applied before running this program.

#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <process.h>
#include <bios.h>

typedef unsigned char far *Char_Ptr;
typedef unsigned int  far *Int_Ptr;
typedef float         far *Flt_Ptr;

Char_Ptr  info_flag ;
Int_Ptr   zero_a2d[4] ;
Int_Ptr   cur_a2d[4] ;
Flt_Ptr   cur_wgt[4] ;
Char_Ptr  calibrated ;

unsigned int Mem_Seg;

void make_pointers (void)
{
 info_flag      = (char *far) MK_FP  (Mem_Seg, 4);

 zero_a2d[0]   = (unsigned int  *far) MK_FP  (Mem_Seg, 0x1C0);
 zero_a2d[1]   = (unsigned int  *far) MK_FP  (Mem_Seg, 0x1C4);
 zero_a2d[2]   = (unsigned int  *far) MK_FP  (Mem_Seg, 0x1C8);
 zero_a2d[3]   = (unsigned int  *far) MK_FP  (Mem_Seg, 0x1CC);

 cur_a2d[0]    = (unsigned int *far) MK_FP  (Mem_Seg, 0xA0);
 cur_a2d[1]    = (unsigned int *far) MK_FP  (Mem_Seg, 0xA2);
 cur_a2d[2]    = (unsigned int *far) MK_FP  (Mem_Seg, 0xA4);
 cur_a2d[3]    = (unsigned int *far) MK_FP  (Mem_Seg, 0xA6);

 cur_wgt[0]    = (float *far) MK_FP (Mem_Seg, 0x60);
 cur_wgt[1]    = (float *far) MK_FP (Mem_Seg, 0x64);
 cur_wgt[2]    = (float *far) MK_FP (Mem_Seg, 0x68);
 cur_wgt[3]    = (float *far) MK_FP (Mem_Seg, 0x6C);

 calibrated = (char *far) MK_FP  (Mem_Seg,  0xB8) ;
}

void main(void)
{
int i ;
char calib[4] ;
const char mask[] = {1, 2, 4, 8} ;

  Mem_Seg = 0xCD40 ;   // LCIC-4HS segment address. Change if necessary. <<<
  make_pointers () ;
  clrscr();
  while (bioskey(1) == 0) {
     *info_flag = 5 ;
     delay (2) ;
     i = 100 ;  // Time-out: 100 ms.
     while ( (*info_flag != 1) && (i > 0) )  {
        delay (1) ;
        i-- ;
     }

     if (i == 0) {
	printf ("Sorry, no response from card.\nMaybe INIT4 should run?") ;
        exit (1) ;
     }

     for (i = 0 ; i <= 3 ; i++) {
        gotoxy (3, 1) ;
        printf ("Zero A/D:") ;

        gotoxy (3, 2+i) ;
        printf ("%ld", (long) *zero_a2d[i]) ;

        gotoxy (3, 7) ;
        printf ("Current A/D:") ;

        gotoxy (3, 8+i) ;
        printf ("%ld", (long) *cur_a2d[i]) ;

        gotoxy (3, 13) ;
        printf ("Current Weight:") ;

        gotoxy (3, 14+i) ;
        printf ("%f", *cur_wgt[i]) ;

        gotoxy (3, 19) ;
        printf ("Channel calibrated?") ;

        calib[i] = ( (*calibrated & mask[i]) == 0 )? 0 : 1 ;
        gotoxy (3, 20+i) ;
        printf ("%d", calib[i]) ;
     }
  }
  bioskey (0) ; // Clear keyboard buffer
}

