# include <dos.h>
# include <conio.h>
# include <stdio.h>
# include <process.h>
# include <alloc.h>
# include <time.h>
# include <spp.h>


# define DATA    0x0378
# define STATUS  DATA+1
# define CONTROL DATA+2

//qui va messo il percorso dei logfile
# define ch1 "c:\\tcpp\\work\\8chadc\\chlog\\ch1.log"
# define ch2 "c:\\tcpp\\work\\8chadc\\chlog\\ch2.log"
# define ch3 "c:\\tcpp\\work\\8chadc\\chlog\\ch3.log"
# define ch4 "c:\\tcpp\\work\\8chadc\\chlog\\ch4.log"
# define ch5 "c:\\tcpp\\work\\8chadc\\chlog\\ch5.log"
# define ch6 "c:\\tcpp\\work\\8chadc\\chlog\\ch6.log"
# define ch7 "c:\\tcpp\\work\\8chadc\\chlog\\ch7.log"
# define ch8 "c:\\tcpp\\work\\8chadc\\chlog\\ch8.log"


//variabili globali
static char  channel=8;
static char  storing=0;
static FILE *ch1log;
static FILE *ch2log;
static FILE *ch3log;
static FILE *ch4log;
static FILE *ch5log;
static FILE *ch6log;
static FILE *ch7log;
static FILE *ch8log;


//prototipi
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   sample_print(void);
void   sample (char,unsigned char);
void   delete_store(void);
void   sleep_mode (void);
void   delete_sleep_mode (void);
void   empty_keyboard_buffer (void);
void   print_menu2 (void);
void   channel_selected (char);

//Main----------------------------------------
int main(void)
{

 char c,u;
 unsigned char valore;             //contiene conversione adc

 print_menu2();
 empty_keyboard_buffer();

 //apre i file, se esistono gia' fa un append
 ch1log=fopen(ch1,"a+");
 ch2log=fopen(ch2,"a+");
 ch3log=fopen(ch3,"a+");
 ch4log=fopen(ch4,"a+");
 ch5log=fopen(ch5,"a+");
 ch6log=fopen(ch6,"a+");
 ch7log=fopen(ch7,"a+");
 ch8log=fopen(ch8,"a+");



 //inizializza la 8adcch
 adc_ch_init(DATA);


 //Do finche' non si preme esc
 do
 {

  //conversione adc o stand by
  do
  {

   //Sleep mode
   if (channel==8)
   {
    sleep_mode();
   }

   //conversione
   else
   {

    //Conversione
    valore =adc_ch_read(DATA,channel);
    text_window(channel);
    blink_arrow_ch(channel);
    voltage_5v(valore);
    sample_print();
    if (storing)
    {
     beep(1000,100);
     sample(channel,valore);
    }
    storing=0;
   }

  }

  while (!kbhit());
  c=getch();
  beep(900,100);

  delete_store();
  delete_blink_arrow();
  delete_store();
  delete_sleep_mode();

  //if function keys : first byte == 0
  if (c==0)
  {
   c=getch();
   channel_selected(c);
  }
 }
 while (c!=27);
 fclose(ch1log);
 fclose(ch2log);
 fclose(ch3log);
 fclose(ch4log);
 fclose(ch5log);
 fclose(ch6log);
 fclose(ch7log);
 fclose(ch8log);
 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("      ");

}


void sample_print(void)
{
 textcolor(CYAN);
 textbackground(BLUE);
 cprintf("F10 : Store");
 textbackground(BLACK);
 textcolor(BLACK);
 cprintf(" \b");
}

void sample (char cch,unsigned char vv)
{
 char chlog=cch+1;
 time_t tempo;

 time(&tempo);

 switch (chlog)
 {
  case 1:
  fprintf(ch1log,"Stored %1.3f V @ %s",signal_level(5.00,8,vv),ctime(&tempo));
  break;

  case 2:
  fprintf(ch2log,"Stored %1.3f V @ %s",signal_level(5.00,8,vv),ctime(&tempo));
  break;

  case 3:
  fprintf(ch3log,"Stored %1.3f V @ %s",signal_level(5.00,8,vv),ctime(&tempo));
  break;

  case 4:
  fprintf(ch4log,"Stored %1.3f V @ %s",signal_level(5.00,8,vv),ctime(&tempo));
  break;

  case 5:
  fprintf(ch5log,"Stored %1.3f V @ %s",signal_level(5.00,8,vv),ctime(&tempo));
  break;

  case 6:
  fprintf(ch6log,"Stored %1.3f V @ %s",signal_level(5.00,8,vv),ctime(&tempo));
  break;

  case 7:
  fprintf(ch7log,"Stored %1.3f V @ %s",signal_level(5.00,8,vv),ctime(&tempo));
  break;

  case 8:
  fprintf(ch8log,"Stored %1.3f V @ %s",signal_level(5.00,8,vv),ctime(&tempo));
  break;
 }

}

void delete_store (void)
{
 gotoxy(15,1);
 clreol();
}


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_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 3.0 -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;

    //f10 pressed
    case 68:
    if (channel<=7 && channel>=0)
    {
     storing=1;
    }
    else
    {
     storing=0;
    }
    break;

   }
}

