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