Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:ee91220d7bea, 2010-11-30 (annotated)
- Committer:
- taoxh
- Date:
- Tue Nov 30 23:53:06 2010 +0000
- Revision:
- 0:ee91220d7bea
HW2 PROTOTHREAD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
taoxh | 0:ee91220d7bea | 1 | /** |
taoxh | 0:ee91220d7bea | 2 | * This is hw2_3 that shows the implementation of |
taoxh | 0:ee91220d7bea | 3 | * protothreads. The program consists of three protothreads that wait |
taoxh | 0:ee91220d7bea | 4 | * for each other to toggle a variable. |
taoxh | 0:ee91220d7bea | 5 | */ |
taoxh | 0:ee91220d7bea | 6 | |
taoxh | 0:ee91220d7bea | 7 | /* We must always include pt.h in our protothreads code. */ |
taoxh | 0:ee91220d7bea | 8 | #include "pt.h" |
taoxh | 0:ee91220d7bea | 9 | #include "mbed.h" |
taoxh | 0:ee91220d7bea | 10 | #include "pre_work.h" |
taoxh | 0:ee91220d7bea | 11 | |
taoxh | 0:ee91220d7bea | 12 | /* Three flags that the three protothread functions use. */ |
taoxh | 0:ee91220d7bea | 13 | static int protothread1_flag, protothread2_flag, protothread3_flag; |
taoxh | 0:ee91220d7bea | 14 | |
taoxh | 0:ee91220d7bea | 15 | int count=0; |
taoxh | 0:ee91220d7bea | 16 | int pre_cir_pro=0; // in case of initial trigger fault |
taoxh | 0:ee91220d7bea | 17 | int judge=1; |
taoxh | 0:ee91220d7bea | 18 | int shown=0; |
taoxh | 0:ee91220d7bea | 19 | /** |
taoxh | 0:ee91220d7bea | 20 | * The first protothread function. It achieves the pc input part. |
taoxh | 0:ee91220d7bea | 21 | * |
taoxh | 0:ee91220d7bea | 22 | * The protothread function is driven by the main loop further down in |
taoxh | 0:ee91220d7bea | 23 | * the code. |
taoxh | 0:ee91220d7bea | 24 | */ |
taoxh | 0:ee91220d7bea | 25 | static int |
taoxh | 0:ee91220d7bea | 26 | protothread1(struct pt *pt) |
taoxh | 0:ee91220d7bea | 27 | { |
taoxh | 0:ee91220d7bea | 28 | PT_BEGIN(pt); |
taoxh | 0:ee91220d7bea | 29 | /* We loop forever here. */ |
taoxh | 0:ee91220d7bea | 30 | while(1) { |
taoxh | 0:ee91220d7bea | 31 | /* Wait until the other protothread has set its flag. */ |
taoxh | 0:ee91220d7bea | 32 | PT_WAIT_UNTIL(pt, protothread2_flag != 0 || pc.readable());//wait until pc inputs |
taoxh | 0:ee91220d7bea | 33 | // printf("Protothread 1 running\n"); |
taoxh | 0:ee91220d7bea | 34 | /* We then reset the other protothread's flag, and set our own |
taoxh | 0:ee91220d7bea | 35 | flag so that the other protothread can run. */ |
taoxh | 0:ee91220d7bea | 36 | protothread2_flag = 0; |
taoxh | 0:ee91220d7bea | 37 | protothread1_flag = 1; |
taoxh | 0:ee91220d7bea | 38 | char temp; |
taoxh | 0:ee91220d7bea | 39 | if (pc.readable()) { |
taoxh | 0:ee91220d7bea | 40 | temp=pc.getc(); |
taoxh | 0:ee91220d7bea | 41 | if (temp=='E'&& length!=0){pc.printf("Input ends");} |
taoxh | 0:ee91220d7bea | 42 | else if (temp=='E'&& length==0){pc.printf("Error. No 'S' before 'E'");} |
taoxh | 0:ee91220d7bea | 43 | else if (temp=='S'){pc.printf("It is a new start.");length=0;count=0;} |
taoxh | 0:ee91220d7bea | 44 | else if (temp=='1'){ |
taoxh | 0:ee91220d7bea | 45 | str[length]='1';length++;} |
taoxh | 0:ee91220d7bea | 46 | else if (temp=='0'){ |
taoxh | 0:ee91220d7bea | 47 | str[length]='0';length++;} |
taoxh | 0:ee91220d7bea | 48 | else if (temp==' '){} |
taoxh | 0:ee91220d7bea | 49 | else senderror2(); |
taoxh | 0:ee91220d7bea | 50 | if (length>40) {pc.printf("The allocated space is too small.");} |
taoxh | 0:ee91220d7bea | 51 | for (int i=0;i<length; i++) |
taoxh | 0:ee91220d7bea | 52 | { pc.putc(str[i]);} |
taoxh | 0:ee91220d7bea | 53 | pc.printf("\n"); |
taoxh | 0:ee91220d7bea | 54 | |
taoxh | 0:ee91220d7bea | 55 | } |
taoxh | 0:ee91220d7bea | 56 | // PT_WAIT_UNTIL(pt, pc.readable()); |
taoxh | 0:ee91220d7bea | 57 | // pc.printf("I am here. Start inputing."); |
taoxh | 0:ee91220d7bea | 58 | // pc.getc(); |
taoxh | 0:ee91220d7bea | 59 | // length=gtchar(); |
taoxh | 0:ee91220d7bea | 60 | /* And we loop. */ |
taoxh | 0:ee91220d7bea | 61 | protothread3_flag=1; |
taoxh | 0:ee91220d7bea | 62 | } |
taoxh | 0:ee91220d7bea | 63 | |
taoxh | 0:ee91220d7bea | 64 | /* All protothread functions must end with PT_END() which takes a |
taoxh | 0:ee91220d7bea | 65 | pointer to a struct pt. */ |
taoxh | 0:ee91220d7bea | 66 | PT_END(pt); |
taoxh | 0:ee91220d7bea | 67 | } |
taoxh | 0:ee91220d7bea | 68 | |
taoxh | 0:ee91220d7bea | 69 | /** |
taoxh | 0:ee91220d7bea | 70 | * The second protothread function. This is almost the same as the |
taoxh | 0:ee91220d7bea | 71 | * first one. This part helps with the sensor input. |
taoxh | 0:ee91220d7bea | 72 | */ |
taoxh | 0:ee91220d7bea | 73 | static int |
taoxh | 0:ee91220d7bea | 74 | protothread2(struct pt *pt) |
taoxh | 0:ee91220d7bea | 75 | { |
taoxh | 0:ee91220d7bea | 76 | PT_BEGIN(pt); |
taoxh | 0:ee91220d7bea | 77 | while(1) { |
taoxh | 0:ee91220d7bea | 78 | /* Let the other protothread run. */ |
taoxh | 0:ee91220d7bea | 79 | protothread2_flag = 1; |
taoxh | 0:ee91220d7bea | 80 | /* Wait until the other protothread has set its flag. */ |
taoxh | 0:ee91220d7bea | 81 | PT_WAIT_UNTIL(pt, protothread1_flag != 0); //This part always usually has not trigger info |
taoxh | 0:ee91220d7bea | 82 | // printf("Protothread 2 running\n"); |
taoxh | 0:ee91220d7bea | 83 | /* We then reset the other protothread's flag. */ |
taoxh | 0:ee91220d7bea | 84 | protothread1_flag = 0; |
taoxh | 0:ee91220d7bea | 85 | if (touchSense1()&& !touchSense2()) { //T0 happens |
taoxh | 0:ee91220d7bea | 86 | myled1 = 1; |
taoxh | 0:ee91220d7bea | 87 | myled2=0; |
taoxh | 0:ee91220d7bea | 88 | pc.putc('a'); |
taoxh | 0:ee91220d7bea | 89 | shown=0; |
taoxh | 0:ee91220d7bea | 90 | while(1) {if (!test1()){break;} if (touchSense2()&& touchSense1()) {break;}} |
taoxh | 0:ee91220d7bea | 91 | // if (str[count]=='0') {judge=0;} |
taoxh | 0:ee91220d7bea | 92 | // judge=1; |
taoxh | 0:ee91220d7bea | 93 | // pc.putc(str[count]); |
taoxh | 0:ee91220d7bea | 94 | if (count>=length){ |
taoxh | 0:ee91220d7bea | 95 | for (int i=1;i<count;i++) |
taoxh | 0:ee91220d7bea | 96 | { str2[i-1]=str2[i]; |
taoxh | 0:ee91220d7bea | 97 | pc.putc(str2[i-1]); |
taoxh | 0:ee91220d7bea | 98 | } |
taoxh | 0:ee91220d7bea | 99 | str2[count-1]='1'; |
taoxh | 0:ee91220d7bea | 100 | pc.putc(str2[count-1]);} |
taoxh | 0:ee91220d7bea | 101 | else { str2[count]='1';count++;} |
taoxh | 0:ee91220d7bea | 102 | //process_char(); |
taoxh | 0:ee91220d7bea | 103 | } else if (touchSense2()&& !touchSense1()) { //T1 happens |
taoxh | 0:ee91220d7bea | 104 | if (pre_cir_pro>30){ |
taoxh | 0:ee91220d7bea | 105 | myled1 = 0; |
taoxh | 0:ee91220d7bea | 106 | myled2=1; |
taoxh | 0:ee91220d7bea | 107 | pc.putc('b'); |
taoxh | 0:ee91220d7bea | 108 | shown=0; |
taoxh | 0:ee91220d7bea | 109 | while(1) {if (!test2()){break;} if (touchSense2()&& touchSense1()) {break;} } |
taoxh | 0:ee91220d7bea | 110 | // if (str[count]=='1') {judge=0;} |
taoxh | 0:ee91220d7bea | 111 | // judge=0; |
taoxh | 0:ee91220d7bea | 112 | // pc.putc(str[count]); |
taoxh | 0:ee91220d7bea | 113 | if (count>=length){ |
taoxh | 0:ee91220d7bea | 114 | for (int i=1;i<count;i++) |
taoxh | 0:ee91220d7bea | 115 | { str2[i-1]=str2[i]; |
taoxh | 0:ee91220d7bea | 116 | pc.putc(str2[i-1]); |
taoxh | 0:ee91220d7bea | 117 | } |
taoxh | 0:ee91220d7bea | 118 | str2[count-1]='0'; |
taoxh | 0:ee91220d7bea | 119 | pc.putc(str2[count-1]);} |
taoxh | 0:ee91220d7bea | 120 | else { str2[count]='0';count++;} |
taoxh | 0:ee91220d7bea | 121 | } |
taoxh | 0:ee91220d7bea | 122 | } else if (touchSense2()&& touchSense1()) { //Touch error |
taoxh | 0:ee91220d7bea | 123 | myled1 = 1; |
taoxh | 0:ee91220d7bea | 124 | myled2=1; |
taoxh | 0:ee91220d7bea | 125 | senderror(); |
taoxh | 0:ee91220d7bea | 126 | } else { //anything else |
taoxh | 0:ee91220d7bea | 127 | myled1=0; |
taoxh | 0:ee91220d7bea | 128 | myled2=0; |
taoxh | 0:ee91220d7bea | 129 | } |
taoxh | 0:ee91220d7bea | 130 | pre_cir_pro++; |
taoxh | 0:ee91220d7bea | 131 | /* And we loop. */ |
taoxh | 0:ee91220d7bea | 132 | protothread3_flag=1; |
taoxh | 0:ee91220d7bea | 133 | } |
taoxh | 0:ee91220d7bea | 134 | PT_END(pt); |
taoxh | 0:ee91220d7bea | 135 | } |
taoxh | 0:ee91220d7bea | 136 | |
taoxh | 0:ee91220d7bea | 137 | static int |
taoxh | 0:ee91220d7bea | 138 | protothread3(struct pt *pt) |
taoxh | 0:ee91220d7bea | 139 | { |
taoxh | 0:ee91220d7bea | 140 | /* A protothread function must begin with PT_BEGIN() which takes a |
taoxh | 0:ee91220d7bea | 141 | pointer to a struct pt. Match judging and output */ |
taoxh | 0:ee91220d7bea | 142 | PT_BEGIN(pt); |
taoxh | 0:ee91220d7bea | 143 | /* We loop forever here. */ |
taoxh | 0:ee91220d7bea | 144 | while(1) { |
taoxh | 0:ee91220d7bea | 145 | PT_WAIT_UNTIL(pt,count>=length &&count!=0 && protothread3_flag==1 ); |
taoxh | 0:ee91220d7bea | 146 | // pc.printf("here"); |
taoxh | 0:ee91220d7bea | 147 | for (int i=0;i<length;i++) |
taoxh | 0:ee91220d7bea | 148 | { if (str[i]!=str2[i]) {judge=0;} } |
taoxh | 0:ee91220d7bea | 149 | if (judge==1 && shown==0) {pc.printf("\nMATCH");shown=1;} |
taoxh | 0:ee91220d7bea | 150 | judge=1; |
taoxh | 0:ee91220d7bea | 151 | protothread3_flag=0; |
taoxh | 0:ee91220d7bea | 152 | // PT_WAIT_UNTIL(pt, pc.readable()); |
taoxh | 0:ee91220d7bea | 153 | // pc.printf("I am here. Start inputing."); |
taoxh | 0:ee91220d7bea | 154 | // pc.getc(); |
taoxh | 0:ee91220d7bea | 155 | // length=gtchar(); |
taoxh | 0:ee91220d7bea | 156 | /* And we loop. */ |
taoxh | 0:ee91220d7bea | 157 | } |
taoxh | 0:ee91220d7bea | 158 | |
taoxh | 0:ee91220d7bea | 159 | /* All protothread functions must end with PT_END() which takes a |
taoxh | 0:ee91220d7bea | 160 | pointer to a struct pt. */ |
taoxh | 0:ee91220d7bea | 161 | PT_END(pt); |
taoxh | 0:ee91220d7bea | 162 | } |
taoxh | 0:ee91220d7bea | 163 | |
taoxh | 0:ee91220d7bea | 164 | |
taoxh | 0:ee91220d7bea | 165 | /** |
taoxh | 0:ee91220d7bea | 166 | * Finally, we have the main loop. Here is where the protothreads are |
taoxh | 0:ee91220d7bea | 167 | * initialized and scheduled. Almost nothing in it. |
taoxh | 0:ee91220d7bea | 168 | */ |
taoxh | 0:ee91220d7bea | 169 | static struct pt pt1,pt2,pt3; |
taoxh | 0:ee91220d7bea | 170 | int |
taoxh | 0:ee91220d7bea | 171 | main(void) |
taoxh | 0:ee91220d7bea | 172 | { |
taoxh | 0:ee91220d7bea | 173 | /* Initialize the protothread state variables with PT_INIT(). */ |
taoxh | 0:ee91220d7bea | 174 | PT_INIT(&pt1); |
taoxh | 0:ee91220d7bea | 175 | PT_INIT(&pt2); |
taoxh | 0:ee91220d7bea | 176 | PT_INIT(&pt3); |
taoxh | 0:ee91220d7bea | 177 | /* |
taoxh | 0:ee91220d7bea | 178 | * Then we schedule the three protothreads by repeatedly calling their |
taoxh | 0:ee91220d7bea | 179 | * protothread functions and passing a pointer to the protothread |
taoxh | 0:ee91220d7bea | 180 | * state variables as arguments. |
taoxh | 0:ee91220d7bea | 181 | */while(1){ protothread2(&pt2); |
taoxh | 0:ee91220d7bea | 182 | protothread1(&pt1); |
taoxh | 0:ee91220d7bea | 183 | protothread3(&pt3); |
taoxh | 0:ee91220d7bea | 184 | } |
taoxh | 0:ee91220d7bea | 185 | } |