#ifdef	ournix
#include "ournix.h"
#endif
char sccsID[] =
 "@(#) flash.c V1.3 Copyright Julian H. Stacey, Munich, 29th July 1988\n" ;

/* Test observable response time of an LCD display. Author: J. Stacey */

/* 2 modes of operation, first time say
	flash t 50000
this tells you how long 50000 takes, next time say
	flash f 5000
this will flash forever at 5000 countdown between flashes


Runs on msdos & UCB.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifdef	MSDOS	/* { */
#include <time.h>		/* needed for ctime */
#endif		/* } */

extern	char	*ctime() ;
extern	long	time() ;
extern	long	atol() ;

char **ARGV ;
char	first[] =	"XXX" ;
char	second[] =	"YYY" ;

main(argc,argv)
	int argc; char **argv ;
	{
	char	mode ;
	long	count_orig , count_dup ;
	int	count_flush ;
	long	first_time, second_time ;
	float	per_count ;

	ARGV = argv ;
#ifdef	VSL	/* { */
#include	"../../include/vsl.h"
#endif		/* } */
	if (argc != 3) syntax() ;
	mode = **++argv ;
	count_orig = atol(*++argv) ;
#ifdef	DEBUG	/* { */
	printf("count requested is %ld\n",count_orig);
#endif		/* } */
	if (count_orig==0) syntax() ;
	switch(mode)
		{
		case 't': case 'T':
			/* time the loop for the particular processor */
			count_dup = count_orig ;
			/* print real time clock */
			(void) time(&first_time);
			while(--count_dup) ; /* use same variable as elsewhere,
						so that if count_dup can be
						accessed at a different speed
						to count_orig, it doesnt affect
						precision */
			/* print real time clock */
			(void) time(&second_time);
#ifdef	DEBUG	/* { */
			printf("Start time: %sStop time: %s",
				ctime(&first_time), ctime(&second_time) );
#endif		/* } */
			second_time = second_time - first_time ;
			printf("Elapsed seconds %ld\n",second_time);
			per_count = (float)second_time/(float)count_orig ;
			printf("1 count takes = %f seconds.\n", per_count) ;
			if ((second_time <= 5L ) || ( per_count == 0.0 ) )
				{
				printf(
"Warning: quantisation error because count was too small, try again.\n");
				exit(0);
				}
			break ;
	
		case 'f': case 'F':
			while(1)
				{
				count_dup = count_orig ;
				printf(first);
#ifdef	unix
				(void)fflush(stdout);
#endif
				while(--count_dup) ;	/* delay */
				/* clean display */
				putchar('\r') ;
				for (count_flush = strlen(first) ; 
					count_flush-- ; putchar('\b') ) ;

				printf(second);
#ifdef	unix
				(void)fflush(stdout);
#endif
				count_dup = count_orig ;
				while(--count_dup) ;	/* delay */
				/* clean display */
				putchar('\r') ;
				for (count_flush = strlen(second) ; 
					count_flush-- ; putchar('\b') ) ;
				}
			break ;
		default:
			syntax() ;
			break ;
		}
	exit(0);
	}

syntax()	
	{
	fprintf(stderr,"%s: Syntax t/f number\n",*ARGV);
	fprintf(stderr,"\t t for timing a loop of count <number>\n");
	fprintf(stderr,"\t f for flashing alternate strings after <number>\n");
	exit(1);
	}
