Qingtao Li / Mbed 2 deprecated Protothread

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 
00004 #define numsamples 1
00005 int touchSense1(void);
00006 int touchSense2(void);
00007 int comparestr(int x,int y,char a[],char b[]);
00008 void getstring(void);
00009 int new_input(void);
00010 
00011 DigitalOut myled1(LED1);
00012 DigitalOut myled2(LED2);
00013 AnalogIn input1(p20);
00014 AnalogIn input2(p15);
00015 DigitalIn charger1(p19);
00016 DigitalIn charger2(p16);
00017 DigitalOut ground1(p18);
00018 DigitalOut ground2(p17);
00019 Serial pc(USBTX, USBRX); // tx, rx
00020 
00021 static struct pt host_input_pt,human_input_pt,comparestr_pt;
00022 static char str[50],str1[50];
00023 static int j,k,match,newinput,i=0;
00024 
00025 static
00026 PT_THREAD(host_input_thread(struct pt *pt)) {
00027     PT_BEGIN(pt);
00028     while (1) {
00029         PT_WAIT_UNTIL(pt,pc.readable());
00030         if (pc.getc()=='S') {
00031             pc.printf("Please input the trigger string (start with S and end with E):\r\n");
00032             j=0;
00033             getstring();
00034         }
00035     }
00036     PT_END(pt);
00037 }
00038 
00039 static
00040 PT_THREAD(human_input_thread(struct pt *pt)) {
00041     PT_BEGIN(pt);
00042     while (1) {
00043         PT_WAIT_UNTIL(pt,touchSense1() || touchSense2());
00044         if (touchSense1()) {
00045             myled1 = 1;
00046         } else {
00047             myled1 = 0;
00048         }
00049         if (touchSense2()) {
00050             myled2 = 1;
00051         } else {
00052             myled2 = 0;
00053         }
00054         switch (i) {
00055             case 0:
00056                 if (myled1&myled2) {
00057                     pc.printf("TOUCH ERROR\r\n");
00058                     i=1;
00059                 }
00060                 if (myled1==1&myled2==0) {
00061                     pc.printf("1");
00062                     i=1;
00063                     str1[k]='1';
00064                     newinput=1;
00065                     k++;
00066                 }
00067                 if (myled2==1&myled1==0) {
00068                     pc.printf("0");
00069                     i=1;
00070                     str1[k]='0';
00071                     newinput=1;
00072                     k++;
00073                 }
00074             case 1:
00075                 if ((myled1==0)&(myled2==0)) {
00076                     i=0;
00077                 }
00078         }
00079         wait(0.05);
00080     }
00081     PT_END(pt);
00082 }
00083 static
00084 PT_THREAD(comparestr_thread(struct pt *pt)) {
00085     int x,y;
00086     x=k-1,y=j;
00087     PT_BEGIN(pt);
00088     while (1) {
00089         PT_WAIT_UNTIL(pt,new_input());
00090         match=comparestr(x,y,str,str1);
00091         if (match==1) pc.printf("\r\nMATCH\r\n");
00092     }
00093     PT_END(pt);
00094 }
00095 
00096 int main() {
00097     pc.printf("Please input the trigger string (start with S and end with E):\r\n");
00098     j=0;
00099     getstring();
00100     pc.printf("Waiting for human input.\r\n");
00101     PT_INIT(&human_input_pt);
00102     PT_INIT(&comparestr_pt);
00103     PT_INIT(&host_input_pt);
00104     while (PT_SCHEDULE(comparestr_thread(&comparestr_pt))) {
00105         PT_SCHEDULE(human_input_thread(&human_input_pt));
00106         PT_SCHEDULE(host_input_thread(&host_input_pt));
00107     }
00108 }
00109 
00110 void getstring(void) {
00111     char a;
00112 X:
00113     while (1) {
00114         a=pc.getc();
00115         pc.printf("%c",a);
00116         if (a=='S') break;
00117         else {
00118             pc.printf("\r\nHOST ERROR,TRY AGAIN\r\n");
00119             continue;
00120         }
00121     }
00122     while (1) {
00123         a=pc.getc();
00124         pc.printf("%c",a);
00125         if (a=='E') {
00126             pc.printf("\r\n");
00127             break;
00128         } else if (a=='0'|a=='1') {
00129             str[j]=a;
00130             j++;
00131         } else if (a==' ') continue;
00132         else {
00133             pc.printf("\r\nHOST ERROR,TRY AGAIN\r\n");
00134             goto X;
00135         }
00136     }
00137     j=j-1;
00138 }
00139 int touchSense1(void) {
00140     float sample;
00141     ground1 = 0;
00142     charger1.mode(PullUp);
00143     charger1.mode(PullNone);
00144     sample=input1.read();
00145     if (sample < 0.3) {
00146         return 1;
00147     } else {
00148         return 0;
00149     }
00150 }
00151 
00152 int touchSense2(void) {
00153     float sample;
00154     ground2 = 0;
00155     charger2.mode(PullUp);
00156     charger2.mode(PullNone);
00157     sample=input2.read();
00158     if (sample < 0.3) {
00159         return 1;
00160     } else {
00161         return 0;
00162     }
00163 }
00164 
00165 int new_input(void) {
00166     if (newinput==1) {
00167         newinput=0;
00168         return(1);
00169     } else return(0);
00170 }
00171 
00172 int comparestr(int x,int y,char a[],char b[]) {
00173     while (y>=0) {
00174         if (a[y]==b[x]) {
00175             x--;
00176             y--;
00177         } else goto A;
00178     }
00179     return 1;
00180 A:
00181     return 0;
00182 }