homework2 protothread
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 Timeout flipper; 00004 00005 Serial host_pc(USBTX, USBRX); // tx, rx 00006 00007 DigitalOut myled1(LED1); 00008 DigitalOut myled2(LED2); 00009 DigitalOut myled3(LED3); 00010 DigitalOut myled4(LED4); 00011 00012 AnalogIn pin_15(p15); 00013 DigitalOut pin_17(p17); // used as virtual gnd 00014 DigitalIn pin_16(p16); // usded as charger 00015 00016 AnalogIn pin_20(p20); 00017 DigitalOut pin_18(p18); // used as virtual gnd 00018 DigitalIn pin_19(p19); // usded as charger 00019 00020 bool verbose = false; // verbose mode flag 00021 00022 bool if_S; // if the serial port see a S before E 00023 00024 //flags 00025 bool if_timer_expired; 00026 00027 int i; 00028 int pre_state; 00029 int user_input[64]; // 0 for invalid 00030 int* string; // 1 for T0 00031 int string_in[64]; // 2 for T1 00032 00033 int TouchSense(); 00034 void process_serial(char char_readin); 00035 void update_user_input(); // fucntion for user_mode 00036 void reset_host(); 00037 void check_string(); 00038 void timer_toggle(); 00039 void V_PT_WAIT_UNTIL_T_S(); // virtual PT_WAIT_UNTIL(timer_expired||serial_ready) 00040 00041 int main() { 00042 // initial the system 00043 // assume the string length is less than 64 00044 if_S = false; 00045 pre_state = 0; 00046 string = new int [64]; 00047 for (i=0;i<64;i++) 00048 { 00049 user_input[i] = 0; 00050 string_in[i] = 0; 00051 string[i] = 0; 00052 } 00053 00054 00055 // set the thread 00056 while(1) 00057 { 00058 flipper.attach(&timer_toggle, 0.0045); //set timer 00059 if_timer_expired = false; 00060 00061 V_PT_WAIT_UNTIL_T_S(); // PT_WAIT_UNTIL(timer_expired||serial_ready) 00062 00063 while ( !if_timer_expired ) 00064 { 00065 if (verbose) 00066 { 00067 host_pc.printf("detect input, not yet expired"); 00068 host_pc.putc(10); 00069 host_pc.putc(13); 00070 } 00071 00072 process_serial(host_pc.getc()); 00073 00074 V_PT_WAIT_UNTIL_T_S(); // PT_WAIT_UNTIL(timer_expired||serial_ready) 00075 } 00076 00077 update_user_input(); 00078 00079 } 00080 00081 } 00082 00083 void V_PT_WAIT_UNTIL_T_S() 00084 { 00085 while (1) 00086 { 00087 if ( if_timer_expired || host_pc.readable() ) 00088 break; 00089 } 00090 } 00091 00092 00093 void timer_toggle() 00094 { 00095 if_timer_expired = true; 00096 } 00097 00098 void update_user_input() 00099 { 00100 00101 00102 int input = TouchSense(); 00103 if (input == pre_state) //check whether the input has changed 00104 { 00105 } 00106 else 00107 { 00108 pre_state = input; 00109 switch (input) 00110 { 00111 case 0: 00112 break; 00113 case 1: 00114 // detect an input T0, shift all input trace 00115 for (i=63;i>0;i--) 00116 { 00117 user_input[i] = user_input[i-1]; 00118 } 00119 user_input[0] = 1; 00120 check_string(); 00121 break; 00122 case 2: 00123 // detect an input T1, shift all input trace 00124 for (i=63;i>0;i--) 00125 { 00126 user_input[i] = user_input[i-1]; 00127 } 00128 user_input[0] = 2; 00129 check_string(); 00130 break; 00131 case 3: 00132 host_pc.printf("TOUCH ERROR"); 00133 host_pc.putc(10); 00134 host_pc.putc(13); 00135 break; 00136 default: 00137 break; 00138 } 00139 if (verbose) 00140 { 00141 if (input ==0) 00142 { 00143 myled1 = 1; 00144 myled2 = 0; 00145 myled3 = 0; 00146 myled4 = 0; 00147 } 00148 else if (input ==1) 00149 { 00150 myled1 = 0; 00151 myled2 = 1; 00152 myled3 = 0; 00153 myled4 = 0; 00154 host_pc.printf("Input Detected: 0"); 00155 host_pc.putc(10); 00156 host_pc.putc(13); 00157 } 00158 else if (input ==2) 00159 { 00160 myled1 = 0; 00161 myled2 = 0; 00162 myled3 = 1; 00163 myled4 = 0; 00164 host_pc.printf("Input Detected: 1"); 00165 host_pc.putc(10); 00166 host_pc.putc(13); 00167 } 00168 else 00169 { 00170 myled1 = 0; 00171 myled2 = 0; 00172 myled3 = 0; 00173 myled4 = 1; 00174 } 00175 } 00176 } 00177 00178 00179 } 00180 00181 void process_serial(char char_readin) 00182 { 00183 switch (char_readin) 00184 { 00185 case 'S': 00186 if (if_S) 00187 { 00188 host_pc.printf("HOST ERROR"); 00189 host_pc.putc(10); //start new line 00190 host_pc.putc(13); //reset cursor location 00191 reset_host(); 00192 } 00193 else 00194 { 00195 // refresh string_in 00196 for (i=0;i<64;i++) 00197 { 00198 string_in[i] = 0; 00199 } 00200 // set start flag 00201 if_S = true; 00202 } 00203 break; 00204 case 'E': 00205 if (if_S) 00206 { 00207 if ( string_in[0] == 0 ) 00208 { 00209 host_pc.printf("HOST ERROR"); 00210 if (verbose) 00211 { 00212 host_pc.printf(": no string defined"); 00213 } 00214 00215 host_pc.putc(10); //start new line 00216 host_pc.putc(13); //reset cursor location 00217 00218 reset_host(); 00219 break; 00220 } 00221 00222 // detect the length of string 00223 int string_length; 00224 for (i=0;i<64;i++) 00225 { 00226 if (string_in[i] == 0) 00227 { 00228 break; 00229 } 00230 } 00231 string_length = --i; 00232 00233 int* string_buffer = new int [64]; 00234 00235 for (i=0;i<64;i++) 00236 { 00237 if (i<=string_length) 00238 { 00239 string_buffer[i] = string_in[string_length-i]; 00240 } 00241 else 00242 { 00243 string_buffer[i] = 0; 00244 } 00245 } 00246 int * buffer = string; 00247 string = string_buffer; // hope that this operation will not be interrupted, at least the interrupted should not be a problem because either value is valid 00248 delete [] buffer; 00249 00250 00251 // unset start flag 00252 if_S = false; 00253 00254 if (verbose) 00255 { 00256 host_pc.printf("Input finishes, the sequency is\t"); 00257 for (i=0;i<64;i++) 00258 { 00259 if (string[i] == 0) 00260 { 00261 break; 00262 } 00263 else 00264 { 00265 host_pc.printf("%d", string[i]-1); 00266 } 00267 } 00268 host_pc.putc(10); 00269 host_pc.putc(13); 00270 } 00271 } 00272 else 00273 { 00274 host_pc.printf("HOST ERROR"); 00275 host_pc.putc(10); //start new line 00276 host_pc.putc(13); //reset cursor location 00277 reset_host(); 00278 } 00279 break; 00280 case '0': 00281 if (if_S) 00282 { 00283 // check the next string_in write location 00284 for (i=0;i<64;i++) 00285 { 00286 if ( string_in[i] == 0) 00287 { 00288 break; 00289 } 00290 } 00291 if ( i == 64 ) 00292 { 00293 host_pc.printf("ERROR: Input String is Too Long"); 00294 host_pc.putc(10); //start new line 00295 host_pc.putc(13); //reset cursor location 00296 reset_host(); 00297 } 00298 else 00299 { 00300 string_in[i] = 1; 00301 } 00302 00303 } 00304 else 00305 { 00306 host_pc.printf("HOST ERROR"); 00307 host_pc.putc(10); //start new line 00308 host_pc.putc(13); //reset cursor location 00309 reset_host(); 00310 } 00311 break; 00312 case '1': 00313 if (if_S) 00314 { 00315 // check the next string_in write location 00316 for (i=0;i<64;i++) 00317 { 00318 if ( string_in[i] == 0) 00319 { 00320 break; 00321 } 00322 } 00323 if ( i == 64 ) 00324 { 00325 host_pc.printf("ERROR: Input String is Too Long"); 00326 host_pc.putc(10); //start new line 00327 host_pc.putc(13); //reset cursor location 00328 reset_host(); 00329 } 00330 else 00331 { 00332 string_in[i] = 2; 00333 } 00334 00335 } 00336 else 00337 { 00338 host_pc.printf("HOST ERROR"); 00339 host_pc.putc(10); //start new line 00340 host_pc.putc(13); //reset cursor location 00341 reset_host(); 00342 } 00343 break; 00344 case ' ': 00345 break; 00346 default: 00347 host_pc.printf("HOST ERROR"); 00348 host_pc.putc(10); //start new line 00349 host_pc.putc(13); //reset cursor location 00350 reset_host(); 00351 } 00352 } 00353 00354 00355 00356 // return 0:no input; 1:lower iput only; 2:higher input only; 3:both input 00357 int TouchSense(void) 00358 { 00359 00360 float sensor_readin0; 00361 float sensor_readin1; 00362 00363 pin_17 = 0; //set the gnd 00364 pin_18 = 0; //set the gnd 00365 pin_16.mode(PullUp); //set up the charger 00366 pin_19.mode(PullUp); //set up the charger 00367 wait(0.0005); 00368 pin_16.mode(PullNone); //finish the charging 00369 pin_19.mode(PullNone); //finish the charging 00370 00371 sensor_readin1=pin_15.read(); 00372 sensor_readin0=pin_20.read(); 00373 if ( sensor_readin0 < 0.7 ) // 00374 { 00375 if ( sensor_readin1 < 0.7 ) 00376 { 00377 return 3; 00378 } 00379 else 00380 { 00381 return 1; 00382 } 00383 } 00384 else 00385 { 00386 if ( sensor_readin1 < 0.7 ) 00387 { 00388 return 2; 00389 } 00390 else 00391 { 00392 return 0; 00393 } 00394 } 00395 00396 } 00397 void reset_host() 00398 { 00399 if_S = false; 00400 for (i=0;i<64;i++) 00401 { 00402 string_in[i] = 0; 00403 } 00404 if (verbose) 00405 { 00406 host_pc.printf("Reset host, redo string input\n"); 00407 host_pc.putc(10); 00408 host_pc.putc(13); 00409 } 00410 } 00411 00412 void check_string() 00413 { 00414 // check user_input and string 00415 bool if_identical = true; 00416 if (string[0]==0) 00417 return; 00418 for (i=0;i<64;i++) 00419 { 00420 if (string[i]) 00421 { 00422 if (string[i] != user_input[i]) 00423 { 00424 if_identical = false; 00425 continue; 00426 } 00427 } 00428 else 00429 { 00430 break; 00431 } 00432 } 00433 if (if_identical) 00434 { 00435 host_pc.printf("MATCH"); 00436 host_pc.putc(10); 00437 host_pc.putc(13); 00438 } 00439 }
Generated on Sat Jul 23 2022 00:42:54 by
1.7.2