|
Fast Research Interface Library
Manual and Documentation
|
00001 // ---------------------- Doxygen info ---------------------- 00047 // ---------------------------------------------------------- 00048 // For a convenient reading of this file's source code, 00049 // please use a tab width of four characters. 00050 // ---------------------------------------------------------- 00051 00052 #include <OSAbstraction.h> 00053 00054 00055 00056 00057 #include <time.h> 00058 #include <string.h> 00059 #include <strings.h> 00060 #include <stdio.h> 00061 #include <stdlib.h> 00062 #include <termios.h> 00063 #include <unistd.h> 00064 #include <sys/time.h> 00065 00066 00067 00068 00069 // **************************************************************** 00070 // Global variables 00071 // 00072 00073 static struct termios termattr 00074 , save_termattr; 00075 00076 static int ttysavefd = -1; 00077 00078 static bool GetSystemTimeInSecondsCalledFirstTime = true; 00079 00080 timeval StoredSystemTimeInSeconds; 00081 00082 00083 00084 00085 // **************************************************************** 00086 // Data structure declarations 00087 // 00088 00089 static enum 00090 { 00091 RESET, RAW 00092 } ttystate = RESET; 00093 00094 int DisableSingleCharacterInput(void); 00095 int EnableSingleCharacterInput(void); 00096 00097 00098 void delay(const int &TimeInMilliseconds) 00099 { 00100 usleep(TimeInMilliseconds * 1000); 00101 return; 00102 } 00103 00104 00105 int stricmp(const char *s1, const char *s2) 00106 { 00107 return(strcasecmp(s1, s2)); 00108 } 00109 00110 00111 // **************************************************************** 00112 // EnableSingleCharacterInput() 00113 // Put the user's TTY in one-character-at-a-time mode. 00114 // returns 0 on success, -1 on failure. 00115 00116 int EnableSingleCharacterInput(void) 00117 { 00118 int i; 00119 00120 i = tcgetattr (STDIN_FILENO, &termattr); 00121 00122 if (i < 0) 00123 { 00124 printf("tcgetattr() returned %d for fildes = %d ", i, STDIN_FILENO); 00125 perror (""); 00126 return -1; 00127 } 00128 00129 save_termattr = termattr; 00130 00131 termattr.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); 00132 termattr.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); 00133 termattr.c_cflag &= ~(CSIZE | PARENB); 00134 termattr.c_cflag |= CS8; 00135 termattr.c_oflag &= ~(OPOST); 00136 00137 termattr.c_cc[VMIN] = 0; 00138 termattr.c_cc[VTIME] = 0; 00139 00140 i = tcsetattr (STDIN_FILENO, TCSANOW, &termattr); 00141 if (i < 0) 00142 { 00143 printf("tcsetattr() returned %d for fildes=%d",i,STDIN_FILENO); 00144 perror(""); 00145 return -1; 00146 } 00147 00148 ttystate = RAW; 00149 ttysavefd = STDIN_FILENO; 00150 return 0; 00151 } 00152 00153 00154 00155 // **************************************************************** 00156 // DisableSingleCharacterInput() 00157 // Restore normal TTY mode. Very important to call 00158 // the function before exiting else the TTY won't be too usable. 00159 // returns 0 on success, -1 on failure. 00160 00161 int DisableSingleCharacterInput() 00162 { 00163 int i; 00164 if (ttystate != RAW) 00165 { 00166 return 0; 00167 } 00168 i = tcsetattr (STDIN_FILENO, TCSAFLUSH, &save_termattr); 00169 if (i < 0) 00170 { 00171 return -1; 00172 } 00173 ttystate = RESET; 00174 return 0; 00175 } 00176 00177 00178 // **************************************************************** 00179 // WaitForKBCharacter() 00180 // 00181 unsigned char WaitForKBCharacter(bool *Abort) 00182 { 00183 unsigned char ch = 0; 00184 size_t size; 00185 00186 fflush(stdout); 00187 fflush(stdin); 00188 00189 if (Abort == NULL) 00190 { 00191 delay(100); 00192 } 00193 00194 if (ttystate != RAW) 00195 { 00196 EnableSingleCharacterInput(); 00197 } 00198 00199 if (Abort == NULL) 00200 { 00201 while (1) 00202 { 00203 usleep(20000); 00204 00205 size = read (STDIN_FILENO, &ch, 1); 00206 if (size > 0) 00207 { 00208 break; 00209 } 00210 } 00211 } 00212 else 00213 { 00214 size = read (STDIN_FILENO, &ch, 1); 00215 if (size == 0) 00216 { 00217 ch = 255; 00218 while (!(*Abort)) 00219 { 00220 usleep(20000); 00221 00222 size = read (STDIN_FILENO, &ch, 1); 00223 if (size > 0) 00224 { 00225 break; 00226 } 00227 } 00228 } 00229 } 00230 DisableSingleCharacterInput(); 00231 fflush(stdout); 00232 fflush(stdin); 00233 if (Abort == NULL) 00234 { 00235 delay(100); 00236 } 00237 return(ch); 00238 } 00239 00240 00241 unsigned char CheckForKBCharacter(void) 00242 { 00243 unsigned char ch = 0; 00244 size_t size; 00245 00246 fflush(stdout); 00247 fflush(stdin); 00248 00249 if (ttystate != RAW) 00250 { 00251 EnableSingleCharacterInput(); 00252 } 00253 00254 size = read (STDIN_FILENO, &ch, 1); 00255 00256 if (size == 0) 00257 { 00258 ch = 255; 00259 } 00260 00261 DisableSingleCharacterInput(); 00262 00263 return(ch); 00264 } 00265 00266 00267 float GetSystemTimeInSeconds(const bool &Reset) 00268 { 00269 timeval CurrentLocalMachineTime; 00270 00271 gettimeofday(&CurrentLocalMachineTime, NULL); 00272 00273 if ( (GetSystemTimeInSecondsCalledFirstTime) || (Reset) ) 00274 { 00275 gettimeofday(&StoredSystemTimeInSeconds, NULL); 00276 GetSystemTimeInSecondsCalledFirstTime = false; 00277 } 00278 00279 return((float)(((double)(CurrentLocalMachineTime.tv_sec 00280 - StoredSystemTimeInSeconds.tv_sec)) 00281 + (double)(CurrentLocalMachineTime.tv_usec 00282 - StoredSystemTimeInSeconds.tv_usec) 00283 * (double)1e-6)); 00284 } 00285 00286 00287 00288