# include <dos.h>
# include <conio.h>
# include <stdio.h>
# include <process.h>
# include <alloc.h>
# include <spp.h>


# define DATA    0x0378
# define STATUS  DATA+1
# define CONTROL DATA+2

//# define MEMORY_SIZE 128


//Global Var
static char  channel=8;

/*
static float *store = (float *) malloc (MEMORY_SIZE);
static unsigned char ch1_memory;
static unsigned char ch2_memory;
static unsigned char ch3_memory;
static unsigned char ch4_memory;
static unsigned char ch5_memory;
static unsigned char cc6_memory;
static unsigned char ch7_memory;
static unsigned char ch8_memory;
*/

//Function proto
void   text_window (char);
void   beep (unsigned int , unsigned int );
void   blink_arrow_ch (char);
void   delete_blink_arrow (void);
void   voltage_5v (unsigned char);
void   sleep_mode (void);
void   delete_sleep_mode (void);
void   empty_keyboard_buffer (void);
void   print_menu2 (void);
void   channel_selected (char);
//int    memory_test (void);

//Main----------------------------------------
int main(void)
{


 unsigned char valore;             //ADC conversion
 char c;

 /*
 if (memory_test())
 {
  clrscr();
  flushall();
  beep(500,500);
  printf("Memory not available\n");
  printf("Press any key to procede, esc to exit");
  if (getch()==27)
   {
    exit(1);
   }

 }

 */

 print_menu2();

 empty_keyboard_buffer();

 //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)
   {
    sleep_mode();
   }

   //ADC Conversion
   else
   {

    //Conversion
    valore =adc_ch_read(DATA,channel);
    text_window(channel);
    blink_arrow_ch(channel);
    voltage_5v(valore);
   }
  }

  while (!kbhit());
  c=getch();
  beep(900,100);


  delete_blink_arrow();
  delete_sleep_mode();

  //if function keys : first byte == 0
  if (c==0)
  {
   c=getch();
   channel_selected(c);
  }
 }
 while (c!=27);
 //free(store);
 return 0;
}


void text_window (char adc_ch)
 {
  textcolor(YELLOW);
  textbackground(BLUE);
  switch (adc_ch)
  {
   case 0:
   window(1,1,80,1);
   break;

   case 1:
   window(1,3,80,3);
   break;

   case 2:
   window(1,5,80,5);
   break;

   case 3:
   window(1,7,80,7);
   break;

   case 4:
   window(1,9,80,9);
   break;

   case 5:
   window(1,11,80,11);
   break;

   case 6:
   window(1,13,80,13);
   break;

   case 7:
   window(1,15,80,15);
   break;
  }

 }


void beep (unsigned int freq, unsigned int pause)
{
 sound(freq);
 delay(pause);
 nosound();

}


void blink_arrow_ch (char ch)
{
 textcolor(YELLOW+BLINK);
 textbackground(BLUE);
 cprintf("%c",16);
 textcolor(CYAN);
 textbackground(BLUE);
 cprintf("Ch%d ",ch+1);
}


void delete_blink_arrow (void)
{

 char j;
 textbackground(BLACK);
 for (j=0; j<78; j++)
  {
   cprintf("\b");
  }
  cprintf(" ");
}


void voltage_5v (unsigned char cha)
{
 float tensione;

 tensione = signal_level(5.00,8,cha);
 textcolor(YELLOW);
 cprintf(": %1.3f V",tensione);
 textbackground(BLACK);
 textcolor(BLACK);
 cprintf(" ");
 textbackground(BLACK);
 textcolor(BLACK);
 cprintf(" \b");

}


void sleep_mode(void)
{
 gotoxy(19,17);
 printf("Stand by mode active... Select one channel !");

}

void delete_sleep_mode(void)
{

 window(1,1,80,25);
 textcolor(YELLOW);
 textbackground(BLACK);
 gotoxy(1,17);
 clreol();
}

void empty_keyboard_buffer(void)
{
 while (kbhit())
 {
  getch();
 }
}

/*

void print_menu1(void)
{
 clrscr();
 textcolor(YELLOW);
 textbackground(BLACK);
 //Bottom of screen : command description
 gotoxy(12,24);
 printf("Sampling and Storing configuration - Esc : Exit");
 gotoxy(1,25);
 printf("8Channel Adc to parallel port Ver 2.7 -Andrea Cipriani email : a71cip@tiscali.it");

}
*/

void print_menu2(void)
{
 clrscr();
 textcolor(YELLOW);
 textbackground(BLACK);
 //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.7 -Andrea Cipriani email : a71cip@tiscali.it");
}

void channel_selected (char f)
{
 switch (f)
   {
    //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;

   }
}

/*
int    memory_test (void)
{
 if (store==NULL)
  {
   return (1);
  }
 return 0;
}
*/

