/****************************/ /* riflessi.c */ /* */ /* richiede: */ /* kbhit.c */ /* my_rand.c */ /* */ /* GdA 14/09/09 */ /****************************/ #ifdef __cplusplus extern "C" { #endif #include #include #include #include #ifndef STDIN_FILENO #define STDIN_FILENO 0 #endif static struct termios termattr, save_termattr; static int ttysavefd = -1; void my_initrand(void); float randfloat(void); int main(int argc, char * argv[]) { unsigned char ch; struct timeval tt1; struct timezone ttz; double ts1, ts2, ts3, dt; float r; int rt, i, gotit; printf("Misura tempo di riflessi \n\n"); printf("Dare Enter (Invia) dopo che viene visualizzato 'VAI'\n\n"); printf("oppure Ctrl/C per interrompere il programma\n"); /* inizializza random */ my_initrand(); for (i=0; i<10; i++) { /* intervallo di attesa random (fra 1 e 3 secondi) */ r = randfloat(); rt = (int) ((r * 2. + 1) * 1000000); /* pausa e poi messaggio*/ usleep(rt); printf ("\n VAI \n"); /* tempo iniziale */ gettimeofday(&tt1,&ttz); ts1= (double)(tt1.tv_sec)+(double)(tt1.tv_usec)/1000000; /* attesa */ set_tty_raw(); /* set up character-at-a-time */ while (1) /* wait here for a typed char */ { gotit = 0; /* diventa 1 se arriva Enter */ usleep(1000); /* 1/1000 di secondo */ ch = kb_getc(); /* char typed by user? */ if (0x0d == ch) /* Enter */ { gotit = 1; set_tty_cooked(); /* restore normal TTY mode */ gettimeofday(&tt1,&ttz); ts2= (double)(tt1.tv_sec)+(double)(tt1.tv_usec)/1000000; dt = ts2-ts1; /* printf ("\n \n ts1 %f, ts2 %f dt = %f \n", ts1, ts2, dt); */ printf ("\n %d) %f s\n", i+1, dt); } if (gotit) { gotit = 0; break; } if (0x03 == ch) /* might be control-C */ { set_tty_cooked(); /* control-C, restore normal TTY mode */ return 1; /* and get out */ } echo(ch); /* not control-C, echo it */ } } } #ifdef __cplusplus } #endif