homework 2 foreground_background code

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

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