ProtoThread Implementation

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "pt.h"
00003 #include "time.h"
00004 
00005 DigitalOut myLed1(LED1);
00006 DigitalOut myLed2(LED2);
00007 DigitalOut myLed3(LED3);
00008 AnalogIn input1(p20);
00009 DigitalIn charger1(p19);
00010 DigitalOut ground1(p18);
00011 AnalogIn input2(p17);
00012 DigitalIn charger2(p16);
00013 DigitalOut ground2(p15);
00014 Serial pc(USBTX,USBRX);
00015 
00016 //Global variables for the touchsense1 and touchsense2 functions that are running in the foreground process.
00017 bool isSensor1Pressed = false;
00018 bool isSensor2Pressed = false;
00019 int count = 0;
00020 char triggerString1[1];
00021 char triggerString2[1];
00022 char realTriggerString[20];
00023 char inputChar; char triggerString[20]; int k=0;
00024 
00025 //Global variable to get the total no of bits in the trigger string.
00026 int countBits = 0;
00027 char triggerStringBits[20];
00028 
00029 int globalTouchSenseCount=0;
00030 //Static variables used to transfer the control from one thread to the other.
00031 static int protoThread0 = 0;
00032 static int protoThread1 = 0;
00033 static int protoThread2 = 0;
00034 
00035 //This thread is useful to monitor the Serial port and get the input from the user.
00036 static int Monitor_Serial_Port_Thread(struct pt *pt)
00037 {    
00038     int counter=0,triggerStringlength;
00039     bool isStartEncountered = false; bool bitsEncountered = false;
00040     PT_BEGIN(pt);
00041     pc.printf("Enter the Trigger String: ");
00042     while(1)
00043     {
00044         pc.putc(inputChar = pc.getc());
00045         if (inputChar == 'e' || inputChar == 'E') 
00046         {
00047             triggerString[k] = inputChar;
00048             k++; 
00049             break;                   
00050         } 
00051         else 
00052         {
00053             triggerString[k] = inputChar;
00054             k++;
00055         }
00056     }
00057     
00058     //All the entered characters from the user are strored in the array.
00059     triggerString[k] = '\0';
00060     //Getting the total length of the trigger string.
00061     while (triggerString[counter] != '\0') 
00062     {
00063         counter++;
00064     }
00065     triggerStringlength = counter;
00066     //Checking for the validity of the input validation string.
00067     for (int i = 0 ;i<triggerStringlength; i++) 
00068     {
00069         //If the input string does not start with required characters, then we continue.
00070         if (triggerString[i] != 'S' && triggerString[i] != 's' && triggerString[i] != '0' && triggerString[i] != '1' && triggerString[i] != 'E' && triggerString[i] != 'e' && triggerString[i] != ' ') 
00071         {           
00072             pc.printf("Invalid Trigger String");
00073             PT_RESTART(pt);
00074         } else
00075         {
00076             //If the input string contains spaces, simply continue.
00077             if (triggerString[i] == ' ') 
00078             {
00079                 continue;
00080             }
00081 
00082             if (triggerString[i] == 'S' || triggerString[i] == 's') 
00083             {
00084                 isStartEncountered = true;
00085                 continue;
00086             }
00087         }
00088         if (isStartEncountered && (triggerString[i]!= 'E' || triggerString[i] != 'e')) 
00089         {
00090             if (triggerString[i] == ' ') 
00091             {
00092                 continue;
00093             } 
00094             else 
00095             {
00096                 if (triggerString[i] == '0' || triggerString[i] == '1') 
00097                 {
00098                     triggerStringBits[countBits] = triggerString[i];
00099                     bitsEncountered = true;
00100                     countBits++;
00101                 }
00102             }
00103         } 
00104         else 
00105         {
00106             //If the user enters E character first without enterting the S character then the user has entered and invalid trigger string.
00107             if (isStartEncountered && !bitsEncountered && (triggerString[i] == 'E' || triggerString[i] == 'e')) 
00108             {
00109                 pc.printf("Please enter a valid Trigger String");
00110                 //Restart the protothread if the string entered is not a valied trigger string.
00111                 PT_RESTART(pt);
00112             } 
00113             else 
00114             {
00115                 if (isStartEncountered && (triggerString[i] == 'E' || triggerString[i] == 'e')) 
00116                 {
00117                     break;
00118                 }
00119             }
00120         }
00121     }
00122     //Ending the triggerString Bit array.
00123     triggerStringBits[countBits] = '\0';
00124     //This variable is used to start the other sensor threads.
00125     protoThread0 = 1;
00126     PT_END(pt);
00127 }
00128 //This thread is used to monitor the To capacitor sensor.
00129 static int Touchsense1_Thread(struct pt *pt)
00130 {
00131     PT_BEGIN(pt);  
00132     PT_WAIT_UNTIL(pt, protoThread0 == 1); 
00133     float sample;    
00134     ground1 = 0;
00135     charger1.mode(PullUp); //p19 used for charging the capacitor.
00136     charger1.mode(PullNone);
00137     sample = input1.read();
00138     if (sample < 0.3) 
00139     {        
00140         triggerString1[0] = '0';
00141         myLed1 = 1;
00142         wait(0.005);
00143         isSensor1Pressed = true; 
00144     } 
00145     else 
00146     {    
00147         if (isSensor1Pressed) 
00148         {
00149              realTriggerString[count] = triggerString1[0];          
00150              count++;
00151              if(count < strlen(triggerStringBits))
00152              {
00153                 protoThread1 = 0;
00154              }
00155              else
00156              {
00157                 protoThread1 = 1;
00158              }
00159         }
00160         myLed1 = 0;
00161         for (int i=0;i<strlen(triggerString1);i++) 
00162         {
00163             triggerString1[i] = '\0';
00164         }
00165         isSensor1Pressed = false; 
00166     }   
00167     PT_END(pt);
00168 }
00169 
00170 
00171 //This thread is used to monitor the touch sensor T1.
00172 
00173 static int Touchsense2_Thread(struct pt *pt)
00174 {
00175     PT_BEGIN(pt);
00176     PT_WAIT_UNTIL(pt, protoThread0 == 1);
00177     float sample;   
00178     ground2 = 0;
00179     charger2.mode(PullUp);
00180     charger2.mode(PullNone);
00181     sample = input2.read();   
00182     if (sample < 0.3) 
00183     {        
00184         triggerString2[0] = '1';
00185         myLed2 = 1;
00186         wait(0.005);
00187         isSensor2Pressed = true;       
00188     } 
00189     else 
00190     {
00191         if (isSensor2Pressed) 
00192         {
00193              realTriggerString[count] = triggerString2[0];
00194              count++;
00195              if(count < strlen(triggerStringBits))
00196              {
00197                 protoThread2 = 0;
00198              }
00199              else
00200              {
00201                 protoThread2 = 1;
00202              }            
00203         }
00204         myLed2 = 0;
00205         for (int i=0;i<strlen(triggerString2);i++) 
00206         {
00207            triggerString2[i] = '\0';
00208         }
00209         isSensor2Pressed = false;
00210     }
00211     PT_END(pt);
00212 }
00213 
00214 static int Check_Trigger_String_Thread(struct pt *pt)
00215 {
00216     PT_BEGIN(pt);
00217     pc.printf("Value of proto thread: %d",protoThread1);
00218     PT_WAIT_UNTIL(pt, (protoThread1 == 1 || protoThread2 == 1));
00219     //Marking the end of the trigger string captured through the sensors from the users. This value needs to be matched to the trigger string entered from the user through pc.
00220     realTriggerString[count] = '\0';
00221     pc.printf("\n\nReal Trigger String Len: %d",strlen(realTriggerString));  
00222     pc.printf("\n\nTrigger String Bits: %d",strlen(triggerStringBits));
00223     int matchCount = 0;
00224     //Only if the trigger string length equals the length of the string entered by the user, then only the validation for each character should take place.
00225     if (strlen(realTriggerString) == strlen(triggerStringBits)) {
00226         for (int i=0;i<strlen(realTriggerString);i++) {
00227             if (realTriggerString[i] == triggerStringBits[i]) {
00228                 matchCount++;
00229             }
00230             //if (realTriggerString[i]== '0' || realTriggerString[i] == '1')
00231             //  pc.printf("%c",realTriggerString[i]);
00232         }
00233         if (matchCount == strlen(realTriggerString)) {
00234             pc.printf("HOST MATCH\n");
00235             protoThread0 = 0;
00236             protoThread1 = 0;
00237             protoThread2 = 0;
00238         }
00239     } 
00240     else 
00241     {
00242         pc.printf("HOST ERROR or TOUCH ERROR\n"); 
00243         protoThread0 = 0;
00244         protoThread1 = 0;
00245         protoThread2 = 0;       
00246     }   
00247     PT_END(pt);
00248 }
00249 
00250 
00251 //Protothreads state variables.
00252 struct pt pt0, pt1, pt2,pt3;
00253 
00254 int main() 
00255 {
00256     //Initializing the threads.
00257     PT_INIT(&pt0);
00258     PT_INIT(&pt1);
00259     PT_INIT(&pt2);     
00260     PT_INIT(&pt3);
00261     
00262     //Thread to monitor the serial input.
00263     Monitor_Serial_Port_Thread(&pt0);
00264     
00265     //Only after the trigger string is got from the user, the touch sensors need to be enabled.    
00266     pc.printf("\n\nPlease input on sensor:");
00267     while(1)
00268     {  
00269         Touchsense1_Thread(&pt1);
00270         Touchsense2_Thread(&pt2);        
00271          //Check if the Input from the sensors matches the trigger string.
00272         Check_Trigger_String_Thread(&pt3);
00273     }       
00274 }