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