Qingtao Li / Mbed 2 deprecated Protothread

Dependencies:   mbed

Revision:
0:4985e86d0bfa
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 01 01:02:23 2010 +0000
@@ -0,0 +1,182 @@
+#include "mbed.h"
+#include "pt.h"
+
+#define numsamples 1
+int touchSense1(void);
+int touchSense2(void);
+int comparestr(int x,int y,char a[],char b[]);
+void getstring(void);
+int new_input(void);
+
+DigitalOut myled1(LED1);
+DigitalOut myled2(LED2);
+AnalogIn input1(p20);
+AnalogIn input2(p15);
+DigitalIn charger1(p19);
+DigitalIn charger2(p16);
+DigitalOut ground1(p18);
+DigitalOut ground2(p17);
+Serial pc(USBTX, USBRX); // tx, rx
+
+static struct pt host_input_pt,human_input_pt,comparestr_pt;
+static char str[50],str1[50];
+static int j,k,match,newinput,i=0;
+
+static
+PT_THREAD(host_input_thread(struct pt *pt)) {
+    PT_BEGIN(pt);
+    while (1) {
+        PT_WAIT_UNTIL(pt,pc.readable());
+        if (pc.getc()=='S') {
+            pc.printf("Please input the trigger string (start with S and end with E):\r\n");
+            j=0;
+            getstring();
+        }
+    }
+    PT_END(pt);
+}
+
+static
+PT_THREAD(human_input_thread(struct pt *pt)) {
+    PT_BEGIN(pt);
+    while (1) {
+        PT_WAIT_UNTIL(pt,touchSense1() || touchSense2());
+        if (touchSense1()) {
+            myled1 = 1;
+        } else {
+            myled1 = 0;
+        }
+        if (touchSense2()) {
+            myled2 = 1;
+        } else {
+            myled2 = 0;
+        }
+        switch (i) {
+            case 0:
+                if (myled1&myled2) {
+                    pc.printf("TOUCH ERROR\r\n");
+                    i=1;
+                }
+                if (myled1==1&myled2==0) {
+                    pc.printf("1");
+                    i=1;
+                    str1[k]='1';
+                    newinput=1;
+                    k++;
+                }
+                if (myled2==1&myled1==0) {
+                    pc.printf("0");
+                    i=1;
+                    str1[k]='0';
+                    newinput=1;
+                    k++;
+                }
+            case 1:
+                if ((myled1==0)&(myled2==0)) {
+                    i=0;
+                }
+        }
+        wait(0.05);
+    }
+    PT_END(pt);
+}
+static
+PT_THREAD(comparestr_thread(struct pt *pt)) {
+    int x,y;
+    x=k-1,y=j;
+    PT_BEGIN(pt);
+    while (1) {
+        PT_WAIT_UNTIL(pt,new_input());
+        match=comparestr(x,y,str,str1);
+        if (match==1) pc.printf("\r\nMATCH\r\n");
+    }
+    PT_END(pt);
+}
+
+int main() {
+    pc.printf("Please input the trigger string (start with S and end with E):\r\n");
+    j=0;
+    getstring();
+    pc.printf("Waiting for human input.\r\n");
+    PT_INIT(&human_input_pt);
+    PT_INIT(&comparestr_pt);
+    PT_INIT(&host_input_pt);
+    while (PT_SCHEDULE(comparestr_thread(&comparestr_pt))) {
+        PT_SCHEDULE(human_input_thread(&human_input_pt));
+        PT_SCHEDULE(host_input_thread(&host_input_pt));
+    }
+}
+
+void getstring(void) {
+    char a;
+X:
+    while (1) {
+        a=pc.getc();
+        pc.printf("%c",a);
+        if (a=='S') break;
+        else {
+            pc.printf("\r\nHOST ERROR,TRY AGAIN\r\n");
+            continue;
+        }
+    }
+    while (1) {
+        a=pc.getc();
+        pc.printf("%c",a);
+        if (a=='E') {
+            pc.printf("\r\n");
+            break;
+        } else if (a=='0'|a=='1') {
+            str[j]=a;
+            j++;
+        } else if (a==' ') continue;
+        else {
+            pc.printf("\r\nHOST ERROR,TRY AGAIN\r\n");
+            goto X;
+        }
+    }
+    j=j-1;
+}
+int touchSense1(void) {
+    float sample;
+    ground1 = 0;
+    charger1.mode(PullUp);
+    charger1.mode(PullNone);
+    sample=input1.read();
+    if (sample < 0.3) {
+        return 1;
+    } else {
+        return 0;
+    }
+}
+
+int touchSense2(void) {
+    float sample;
+    ground2 = 0;
+    charger2.mode(PullUp);
+    charger2.mode(PullNone);
+    sample=input2.read();
+    if (sample < 0.3) {
+        return 1;
+    } else {
+        return 0;
+    }
+}
+
+int new_input(void) {
+    if (newinput==1) {
+        newinput=0;
+        return(1);
+    } else return(0);
+}
+
+int comparestr(int x,int y,char a[],char b[]) {
+    while (y>=0) {
+        if (a[y]==b[x]) {
+            x--;
+            y--;
+        } else goto A;
+    }
+    return 1;
+A:
+    return 0;
+}
\ No newline at end of file