# include <dos.h>
# include <conio.h>
# include <stdio.h>
# include <process.h>
# include <spp.h>

# define DATA    0x0378
# define STATUS  DATA+1
# define CONTROL DATA+2


void text_window (char);


//Main----------------------------------------
int main(void)
{

 unsigned char valore;             //ADC conversion
 float tensione;
 char c;
 int e;
 char channel=8;                   //Selected channel, initial Stand - By

 clrscr();
 textcolor(YELLOW);
 textbackground(BLACK);

 //Top of screen channel view
 //gotoxy(1,1);


 //Bottom of screen : command description
 gotoxy(12,24);
 printf("F1 to F8 : Select Adc Channel - F9 : Stand by - Esc : Exit");
 gotoxy(1,25);
 printf("8Channel Adc to parallel port Ver 2.0- Andrea Cipriani email : a71cip@tiscali.it");



 //Svuota buffer di tastiera
 while (kbhit())
 {
  getch();
 }
 //Adc 8 channel board initialize
 adc_ch_init(DATA);

 //Do unitl press Esc
 do
 {
  //ADC Conversion or stand by mode
  do
  {
   //Sleep mode
   if (channel==8)
   {
    gotoxy(19,12);
    printf("Stand by mode active... Select one channel !");
   }
   //ADC Conversion
   else
   {
     //Conversion
     valore = adc_ch_read(DATA,channel);
     //Window of selected channel
     text_window(channel);
     cprintf("  Ch %d \r\n",channel+1);
     tensione = signal_level(5.00,8,valore);
     cprintf("%1.3f V\r\n",tensione);
     textcolor(YELLOW+BLINK);
     cprintf("  ADC  \r");

   }
  }
  while (!kbhit());
  c=getch();
  textbackground(BLACK);
  clreol();
  window(1,1,80,25);
  textcolor(YELLOW);
  textbackground(BLACK);
  gotoxy(1,12);
  clreol();
  //if function keys : first byte == 0
  if (c==0)
  {
   c=getch();
   switch (c)
   {
    //f1 pressed
    case 59:
    channel=0;
    break;

    //f2 pressed
    case 60:
    channel=1;
    break;

    //f3 pressed
    case 61:
    channel=2;
    break;

    //f4 pressed
    case 62:
    channel=3;
    break;

    //f5 pressed
    case 63:
    channel=4;
    break;

    //f6 pressed
    case 64:
    channel=5;
    break;

    //f7 pressed
    case 65:
    channel=6;
    break;

    //f8 pressed
    case 66:
    channel=7;
    break;

    //f9 pressed
    case 67:
    channel=8;
    break;

   }
  }
 }
 while (c!=27);

 return 0;
}


void text_window (char adc_ch)
 {
  textcolor(YELLOW);
  textbackground(BLUE);
  switch (adc_ch)
  {
   case 0:
   window(1,1,9,3);
   break;

   case 1:
   window(10,1,19,3);
   break;

   case 2:
   window(20,1,29,3);
   break;

   case 3:
   window(30,1,39,3);
   break;

   case 4:
   window(40,1,49,3);
   break;

   case 5:
   window(50,1,59,3);
   break;

   case 6:
   window(60,1,69,3);
   break;

   case 7:
   window(70,1,79,3);
   break;
  }


 }