added input timeouts
git-svn-id: https://svn.salfter.gotdns.org/svn/a2bfc/trunk@91 1b90f75b-8b96-4784-87c0-14078182fce6
This commit is contained in:
		
							
								
								
									
										95
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										95
									
								
								main.c
									
									
									
									
									
								
							@@ -8,12 +8,14 @@
 | 
			
		||||
#include "ow-time.h"
 | 
			
		||||
#include "main.h"
 | 
			
		||||
 | 
			
		||||
COMP_STATUS compressor_status;
 | 
			
		||||
unsigned char* tempsensor=NULL;
 | 
			
		||||
unsigned char* clockdev=NULL;
 | 
			
		||||
 | 
			
		||||
int main (void)
 | 
			
		||||
{
 | 
			
		||||
  short i, devcount;
 | 
			
		||||
  unsigned char devs[80];
 | 
			
		||||
  unsigned char* tempsensor=NULL;
 | 
			
		||||
  unsigned char* clock=NULL;
 | 
			
		||||
  time_t t;
 | 
			
		||||
  struct tm intime;
 | 
			
		||||
  unsigned char setpoint=70;
 | 
			
		||||
@@ -30,9 +32,9 @@ int main (void)
 | 
			
		||||
    if (ow_temp_idcheck(&devs[i<<3]))
 | 
			
		||||
      tempsensor=&devs[i<<3];
 | 
			
		||||
    if (ow_time_idcheck(&devs[i<<3]))
 | 
			
		||||
      clock=&devs[i<<3];
 | 
			
		||||
      clockdev=&devs[i<<3];
 | 
			
		||||
  }
 | 
			
		||||
  if (tempsensor==NULL || clock==NULL)
 | 
			
		||||
  if (tempsensor==NULL || clockdev==NULL)
 | 
			
		||||
  {
 | 
			
		||||
    printf("Error: temperature sensor and/or clock not found.\n");
 | 
			
		||||
    return -1;
 | 
			
		||||
@@ -44,41 +46,51 @@ int main (void)
 | 
			
		||||
  {
 | 
			
		||||
    bzero(&intime, sizeof(intime));
 | 
			
		||||
    printf("Year? ");
 | 
			
		||||
    intime.tm_year=readnum()-1900;
 | 
			
		||||
    intime.tm_year=readnum(0,0)-1900;
 | 
			
		||||
    printf("Month? ");
 | 
			
		||||
    intime.tm_mon=readnum()-1;
 | 
			
		||||
    intime.tm_mon=readnum(0,0)-1;
 | 
			
		||||
    printf("Day? ");
 | 
			
		||||
    intime.tm_mday=readnum();
 | 
			
		||||
    intime.tm_mday=readnum(0,0);
 | 
			
		||||
    printf("Hour? ");
 | 
			
		||||
    intime.tm_hour=readnum();
 | 
			
		||||
    intime.tm_hour=readnum(0,0);
 | 
			
		||||
    printf("Minute? ");
 | 
			
		||||
    intime.tm_min=readnum();
 | 
			
		||||
    ow_time_set(clock, mktime(&intime));
 | 
			
		||||
    intime.tm_min=readnum(0,0);
 | 
			
		||||
    ow_time_set(clockdev, mktime(&intime));
 | 
			
		||||
    clrscr();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  relayctrl(0);
 | 
			
		||||
  gotoxy(25,0);
 | 
			
		||||
  printf("off ");
 | 
			
		||||
  gotoxy(0,2);
 | 
			
		||||
  do
 | 
			
		||||
  {
 | 
			
		||||
    do
 | 
			
		||||
    {
 | 
			
		||||
//      for (i=0; i<250; i++)
 | 
			
		||||
//        ow_mswait();
 | 
			
		||||
      t=ow_time_read(clock);
 | 
			
		||||
      t=ow_time_read(clockdev);
 | 
			
		||||
      currtemp=ow_temp_read(tempsensor, OW_TEMP_FAHRENHEIT);      
 | 
			
		||||
      gotoxy(0,0);
 | 
			
		||||
      printf("curr=%i set=%i goto=%i \n", 
 | 
			
		||||
        currtemp, 
 | 
			
		||||
        setpoint,
 | 
			
		||||
        gotopoint);
 | 
			
		||||
      printf("%s", asctime(localtime(&t)));
 | 
			
		||||
      memcpy(&intime, localtime(&t), sizeof(intime));
 | 
			
		||||
      printf("%i/%i/%02i %02i:%02i:%02i             \n",
 | 
			
		||||
        intime.tm_mon+1,
 | 
			
		||||
        intime.tm_mday,
 | 
			
		||||
        intime.tm_year%100,
 | 
			
		||||
        intime.tm_hour,
 | 
			
		||||
        intime.tm_min,
 | 
			
		||||
        intime.tm_sec);
 | 
			
		||||
      printf("                     \n");
 | 
			
		||||
      gotoy(wherey()-1);
 | 
			
		||||
      if (currtemp>=setpoint+2)
 | 
			
		||||
      if (currtemp>=setpoint+2 && compressor_status==COMP_OFF)
 | 
			
		||||
      {
 | 
			
		||||
        if (t-shutoff>300)
 | 
			
		||||
        {
 | 
			
		||||
          relayctrl(1);
 | 
			
		||||
          gotoxy(25,0);
 | 
			
		||||
          printf("    ");
 | 
			
		||||
          printf("on  ");
 | 
			
		||||
          gotoxy(0,2);
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
@@ -88,10 +100,13 @@ int main (void)
 | 
			
		||||
          gotoxy(0,2);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      if (currtemp<=setpoint-2)
 | 
			
		||||
      if (currtemp<=setpoint-2 && compressor_status==COMP_ON)
 | 
			
		||||
      {
 | 
			
		||||
        relayctrl(0);
 | 
			
		||||
        shutoff=t;
 | 
			
		||||
        gotoxy(25,0);
 | 
			
		||||
        printf("off ");
 | 
			
		||||
        gotoxy(0,2);
 | 
			
		||||
      }
 | 
			
		||||
      if (t>=gototime && gotopoint!=setpoint)
 | 
			
		||||
      {
 | 
			
		||||
@@ -109,16 +124,17 @@ int main (void)
 | 
			
		||||
    if (cmd=='S')
 | 
			
		||||
    {
 | 
			
		||||
      printf("Set point? ");
 | 
			
		||||
      gotopoint=setpoint=readnum();
 | 
			
		||||
      gotopoint=setpoint=readnum(setpoint,1);
 | 
			
		||||
    }
 | 
			
		||||
    if (cmd=='G')
 | 
			
		||||
    {
 | 
			
		||||
      printf("Go-to point? ");
 | 
			
		||||
      gotopoint=readnum();
 | 
			
		||||
      gotopoint=readnum(gotopoint,1);
 | 
			
		||||
      gototime=t+3600;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  while (cmd!=3);
 | 
			
		||||
  relayctrl(0);
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
@@ -126,15 +142,30 @@ int main (void)
 | 
			
		||||
int readYN()
 | 
			
		||||
{
 | 
			
		||||
  unsigned char c;
 | 
			
		||||
  time_t start;
 | 
			
		||||
 | 
			
		||||
  cputc('_');
 | 
			
		||||
  cputc('_'); // display cursor
 | 
			
		||||
  do
 | 
			
		||||
  {
 | 
			
		||||
    start=ow_time_read(clockdev);
 | 
			
		||||
    do // wait for input, but time out after 15 seconds
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
    while (!kbhit() && ow_time_read(clockdev)-start<15);
 | 
			
		||||
    if (!kbhit()) // time out...return "no"
 | 
			
		||||
    {
 | 
			
		||||
      backup();
 | 
			
		||||
      c='N';
 | 
			
		||||
      cputc(c);
 | 
			
		||||
      cputc(13);
 | 
			
		||||
      cputc(10);
 | 
			
		||||
      return 0;
 | 
			
		||||
    }
 | 
			
		||||
    c=cgetc();
 | 
			
		||||
    if (c>96)
 | 
			
		||||
    if (c>96) // convert lowercase to uppercase
 | 
			
		||||
      c=c-32;
 | 
			
		||||
  }  
 | 
			
		||||
  while (c!='Y' && c!='N');
 | 
			
		||||
  while (c!='Y' && c!='N'); // yes or no only
 | 
			
		||||
  backup();
 | 
			
		||||
  cputc(c);
 | 
			
		||||
  cputc(13);
 | 
			
		||||
@@ -142,17 +173,34 @@ int readYN()
 | 
			
		||||
  return (c=='Y')?1:0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int readnum()
 | 
			
		||||
int readnum(int defval, unsigned char timeout)
 | 
			
		||||
{
 | 
			
		||||
  unsigned char c[8];
 | 
			
		||||
  unsigned char p=0;
 | 
			
		||||
  int r=0;
 | 
			
		||||
  unsigned char i;
 | 
			
		||||
  time_t start;
 | 
			
		||||
  
 | 
			
		||||
  bzero(c, sizeof(c));
 | 
			
		||||
  do
 | 
			
		||||
  {
 | 
			
		||||
    cputc('_');
 | 
			
		||||
    start=ow_time_read(clockdev);
 | 
			
		||||
    do
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
    while (!kbhit() && timeout && ow_time_read(clockdev)-start<15);
 | 
			
		||||
    if (!kbhit() && timeout)
 | 
			
		||||
    {
 | 
			
		||||
      while (p>0)
 | 
			
		||||
      {
 | 
			
		||||
        backup();
 | 
			
		||||
        p--;
 | 
			
		||||
      }
 | 
			
		||||
      backup();
 | 
			
		||||
      printf("%i\n", defval);
 | 
			
		||||
      return defval;
 | 
			
		||||
    }
 | 
			
		||||
    c[p]=cgetc();
 | 
			
		||||
    backup();
 | 
			
		||||
    if (c[p]==8 || c[p]==127)
 | 
			
		||||
@@ -193,5 +241,6 @@ void relayctrl(unsigned char on)
 | 
			
		||||
{
 | 
			
		||||
  signed int addr=-16294+(on>0);
 | 
			
		||||
  unsigned char x=PEEK(addr);
 | 
			
		||||
  compressor_status=(on)?COMP_ON:COMP_OFF;
 | 
			
		||||
  return;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user