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