Jingyuan Dong / Mbed 2 deprecated protoThread

Dependencies:   mbed

Revision:
0:9b9c31c57895
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 01 02:46:33 2010 +0000
@@ -0,0 +1,208 @@
+#include "pt.h"
+#include "mbed.h"
+
+#define numsamples 1
+static struct pt project_pt;
+static struct pt project2_pt;
+int twoSensor(void);//get sensor return data;
+void HostConfig(void);//get trigger string from HOST computer
+void sensorInput(void);//store sensor input data in mbed
+
+AnalogIn input1(p20);
+DigitalIn charger(p19);
+DigitalOut ground(p18);
+
+
+AnalogIn input2(p17);
+DigitalIn charger2(p16);
+DigitalOut ground2(p15);
+
+Serial pc(USBTX, USBRX); // tx, rx
+
+int ConfigReady =0;
+int Count = 0;
+int data[5] ={3,3,3,3,3};
+char input[100];
+int i = 0;
+int sensor1EN=0;
+int match;
+int sensor2EN=0;
+int num=0;
+int off=0;
+int errorON=0;
+char config[100];
+char temp;
+int notmat=0;
+
+int startIn=1;
+int output=0;
+
+//two PT_THREAD here
+    PT_THREAD(Match(struct pt *pt))
+    {
+        PT_BEGIN(pt)      
+        while(1){        
+       
+        PT_WAIT_UNTIL(pt,(match==1)&&(num>0)&&(num==Count));
+               pc.printf("MATCH!!\r\n");
+               num=0;      
+        }
+       PT_END(pt);
+    }
+    PT_THREAD(notMatch(struct pt *pt))
+    {
+        PT_BEGIN(pt); 
+        while(1){
+                PT_WAIT_UNTIL(pt,notmat==1);
+                    pc.printf("Your input does NOT MATCH! Please enter your input again!\r\n");   
+                    num=0; 
+                    notmat=0;  
+        }
+       PT_END(pt);
+    }
+
+
+int main(){
+    for (i=0;i<100;i++)
+    {
+        input[i]='4';
+        config[i]='5';
+    }
+    PT_INIT(&project_pt);
+    PT_INIT(&project2_pt);
+        
+         while(1) {        
+        Match(&project_pt);
+        HostConfig();
+        sensorInput();
+        match=1;
+        for(i=0;i<Count;i++){
+           
+            if(input[i]==config[i]){
+                match=match*1;          
+            }
+            else{
+                match=match*0;
+            }     
+        }
+        for(i=0;i<num;i++){
+            if(input[i]!=config[i]){
+                 notmat=1;
+            }   
+        }
+         notMatch(&project2_pt);
+        
+        
+        wait(0.005);        
+    }
+    
+    return 0;
+}
+
+void HostConfig(){
+
+    while(pc.readable()){
+       
+        temp=pc.getc();      
+        if (temp == 'S'){
+            ConfigReady=1;
+            Count=0;           
+        }
+        if (temp=='E'){
+            ConfigReady=0;
+             pc.printf("You have input a string:");
+            for(i=0;i<Count;i++){
+                pc.printf("%c",config[i]);
+            }
+            pc.printf("! Please enter the %dbit sensor input!\r\n", Count);    
+        }
+        
+        if ((ConfigReady==1)&&((temp=='1')||(temp=='0')||(temp==' ')||(temp=='S'))){     
+            if((temp=='1')||(temp=='0')){
+                config[Count]=temp;
+                Count++;
+            }
+        }else if(ConfigReady==1){
+                pc.printf("HOST ERROR!");
+            }     
+    }
+}
+//This function is used to enable the sensor input to be stable. And store them in integer array input.
+void sensorInput(void){
+    
+       for (i=0;i<=3;i++){
+            data[i]=data[i+1];
+        }
+        data[4]=twoSensor();
+        off=1;
+        for (i=0;i<=4;i++){
+            if (data[i]==3){
+                off=off*1;
+            }else{
+                off=off*0;
+                if(data[i]==2){
+                    errorON=1;
+                    sensor1EN=0;
+                    sensor2EN=0;
+                }
+                if(data[i]==0){
+                    if(errorON==0){
+                        sensor1EN=1;
+                    }
+                }
+                if(data[i]==1){
+                    if(errorON==0){
+                        sensor2EN=1;
+                    }
+                }
+            }
+        }
+        
+        if((off==1)&&(sensor1EN==1)){
+            input[num]='0';
+                        pc.printf("You have input a 0!\r\n");
+            sensor1EN=0;
+            num++;     
+        }
+        if((off==1)&&(sensor2EN==1)){
+            input[num]='1';
+                         pc.printf("You have input a 1!\r\n");
+            sensor2EN=0;
+            num++;
+        }
+        if ((off==1)&&(errorON==1)){
+            pc.printf("TOUCH ERROR!");
+            errorON=0;          
+        }
+        if(num==99){
+            num=0;
+        }
+}
+
+int twoSensor (void)
+{
+     float sample1;
+     float sample2;
+     ground=0;
+     charger.mode(PullUp);
+     charger.mode(PullNone);
+     sample1=input1.read();
+     
+     ground2=0;
+     charger2.mode(PullUp);
+     charger2.mode(PullNone);
+     sample2=input2.read();
+    
+     if ((sample1<0.4)&&(sample2>=0.4)){
+        return 0;
+     }
+     else if ((sample2<0.4)&&(sample1>=0.4)){
+        return 1;
+     }
+     else if ((sample2<0.4)&&(sample1<0.4)){
+        return 2;
+     }
+     else if((sample1>=0.4)&&(sample2>=0.4)){
+        return 3;
+     }
+}
\ No newline at end of file