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:9b9c31c57895, 2010-12-01 (annotated)
- Committer:
- guruimage
- Date:
- Wed Dec 01 02:46:33 2010 +0000
- Revision:
- 0:9b9c31c57895
protoThread problem for hw2
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| guruimage | 0:9b9c31c57895 | 1 | #include "pt.h" |
| guruimage | 0:9b9c31c57895 | 2 | #include "mbed.h" |
| guruimage | 0:9b9c31c57895 | 3 | |
| guruimage | 0:9b9c31c57895 | 4 | #define numsamples 1 |
| guruimage | 0:9b9c31c57895 | 5 | static struct pt project_pt; |
| guruimage | 0:9b9c31c57895 | 6 | static struct pt project2_pt; |
| guruimage | 0:9b9c31c57895 | 7 | int twoSensor(void);//get sensor return data; |
| guruimage | 0:9b9c31c57895 | 8 | void HostConfig(void);//get trigger string from HOST computer |
| guruimage | 0:9b9c31c57895 | 9 | void sensorInput(void);//store sensor input data in mbed |
| guruimage | 0:9b9c31c57895 | 10 | |
| guruimage | 0:9b9c31c57895 | 11 | AnalogIn input1(p20); |
| guruimage | 0:9b9c31c57895 | 12 | DigitalIn charger(p19); |
| guruimage | 0:9b9c31c57895 | 13 | DigitalOut ground(p18); |
| guruimage | 0:9b9c31c57895 | 14 | |
| guruimage | 0:9b9c31c57895 | 15 | |
| guruimage | 0:9b9c31c57895 | 16 | AnalogIn input2(p17); |
| guruimage | 0:9b9c31c57895 | 17 | DigitalIn charger2(p16); |
| guruimage | 0:9b9c31c57895 | 18 | DigitalOut ground2(p15); |
| guruimage | 0:9b9c31c57895 | 19 | |
| guruimage | 0:9b9c31c57895 | 20 | Serial pc(USBTX, USBRX); // tx, rx |
| guruimage | 0:9b9c31c57895 | 21 | |
| guruimage | 0:9b9c31c57895 | 22 | int ConfigReady =0; |
| guruimage | 0:9b9c31c57895 | 23 | int Count = 0; |
| guruimage | 0:9b9c31c57895 | 24 | int data[5] ={3,3,3,3,3}; |
| guruimage | 0:9b9c31c57895 | 25 | char input[100]; |
| guruimage | 0:9b9c31c57895 | 26 | int i = 0; |
| guruimage | 0:9b9c31c57895 | 27 | int sensor1EN=0; |
| guruimage | 0:9b9c31c57895 | 28 | int match; |
| guruimage | 0:9b9c31c57895 | 29 | int sensor2EN=0; |
| guruimage | 0:9b9c31c57895 | 30 | int num=0; |
| guruimage | 0:9b9c31c57895 | 31 | int off=0; |
| guruimage | 0:9b9c31c57895 | 32 | int errorON=0; |
| guruimage | 0:9b9c31c57895 | 33 | char config[100]; |
| guruimage | 0:9b9c31c57895 | 34 | char temp; |
| guruimage | 0:9b9c31c57895 | 35 | int notmat=0; |
| guruimage | 0:9b9c31c57895 | 36 | |
| guruimage | 0:9b9c31c57895 | 37 | int startIn=1; |
| guruimage | 0:9b9c31c57895 | 38 | int output=0; |
| guruimage | 0:9b9c31c57895 | 39 | |
| guruimage | 0:9b9c31c57895 | 40 | //two PT_THREAD here |
| guruimage | 0:9b9c31c57895 | 41 | PT_THREAD(Match(struct pt *pt)) |
| guruimage | 0:9b9c31c57895 | 42 | { |
| guruimage | 0:9b9c31c57895 | 43 | PT_BEGIN(pt) |
| guruimage | 0:9b9c31c57895 | 44 | while(1){ |
| guruimage | 0:9b9c31c57895 | 45 | |
| guruimage | 0:9b9c31c57895 | 46 | PT_WAIT_UNTIL(pt,(match==1)&&(num>0)&&(num==Count)); |
| guruimage | 0:9b9c31c57895 | 47 | pc.printf("MATCH!!\r\n"); |
| guruimage | 0:9b9c31c57895 | 48 | num=0; |
| guruimage | 0:9b9c31c57895 | 49 | } |
| guruimage | 0:9b9c31c57895 | 50 | PT_END(pt); |
| guruimage | 0:9b9c31c57895 | 51 | } |
| guruimage | 0:9b9c31c57895 | 52 | PT_THREAD(notMatch(struct pt *pt)) |
| guruimage | 0:9b9c31c57895 | 53 | { |
| guruimage | 0:9b9c31c57895 | 54 | PT_BEGIN(pt); |
| guruimage | 0:9b9c31c57895 | 55 | while(1){ |
| guruimage | 0:9b9c31c57895 | 56 | PT_WAIT_UNTIL(pt,notmat==1); |
| guruimage | 0:9b9c31c57895 | 57 | pc.printf("Your input does NOT MATCH! Please enter your input again!\r\n"); |
| guruimage | 0:9b9c31c57895 | 58 | num=0; |
| guruimage | 0:9b9c31c57895 | 59 | notmat=0; |
| guruimage | 0:9b9c31c57895 | 60 | } |
| guruimage | 0:9b9c31c57895 | 61 | PT_END(pt); |
| guruimage | 0:9b9c31c57895 | 62 | } |
| guruimage | 0:9b9c31c57895 | 63 | |
| guruimage | 0:9b9c31c57895 | 64 | |
| guruimage | 0:9b9c31c57895 | 65 | int main(){ |
| guruimage | 0:9b9c31c57895 | 66 | for (i=0;i<100;i++) |
| guruimage | 0:9b9c31c57895 | 67 | { |
| guruimage | 0:9b9c31c57895 | 68 | input[i]='4'; |
| guruimage | 0:9b9c31c57895 | 69 | config[i]='5'; |
| guruimage | 0:9b9c31c57895 | 70 | } |
| guruimage | 0:9b9c31c57895 | 71 | PT_INIT(&project_pt); |
| guruimage | 0:9b9c31c57895 | 72 | PT_INIT(&project2_pt); |
| guruimage | 0:9b9c31c57895 | 73 | |
| guruimage | 0:9b9c31c57895 | 74 | while(1) { |
| guruimage | 0:9b9c31c57895 | 75 | Match(&project_pt); |
| guruimage | 0:9b9c31c57895 | 76 | HostConfig(); |
| guruimage | 0:9b9c31c57895 | 77 | sensorInput(); |
| guruimage | 0:9b9c31c57895 | 78 | match=1; |
| guruimage | 0:9b9c31c57895 | 79 | for(i=0;i<Count;i++){ |
| guruimage | 0:9b9c31c57895 | 80 | |
| guruimage | 0:9b9c31c57895 | 81 | if(input[i]==config[i]){ |
| guruimage | 0:9b9c31c57895 | 82 | match=match*1; |
| guruimage | 0:9b9c31c57895 | 83 | } |
| guruimage | 0:9b9c31c57895 | 84 | else{ |
| guruimage | 0:9b9c31c57895 | 85 | match=match*0; |
| guruimage | 0:9b9c31c57895 | 86 | } |
| guruimage | 0:9b9c31c57895 | 87 | } |
| guruimage | 0:9b9c31c57895 | 88 | for(i=0;i<num;i++){ |
| guruimage | 0:9b9c31c57895 | 89 | if(input[i]!=config[i]){ |
| guruimage | 0:9b9c31c57895 | 90 | notmat=1; |
| guruimage | 0:9b9c31c57895 | 91 | } |
| guruimage | 0:9b9c31c57895 | 92 | } |
| guruimage | 0:9b9c31c57895 | 93 | notMatch(&project2_pt); |
| guruimage | 0:9b9c31c57895 | 94 | |
| guruimage | 0:9b9c31c57895 | 95 | |
| guruimage | 0:9b9c31c57895 | 96 | wait(0.005); |
| guruimage | 0:9b9c31c57895 | 97 | } |
| guruimage | 0:9b9c31c57895 | 98 | |
| guruimage | 0:9b9c31c57895 | 99 | return 0; |
| guruimage | 0:9b9c31c57895 | 100 | } |
| guruimage | 0:9b9c31c57895 | 101 | |
| guruimage | 0:9b9c31c57895 | 102 | void HostConfig(){ |
| guruimage | 0:9b9c31c57895 | 103 | |
| guruimage | 0:9b9c31c57895 | 104 | while(pc.readable()){ |
| guruimage | 0:9b9c31c57895 | 105 | |
| guruimage | 0:9b9c31c57895 | 106 | temp=pc.getc(); |
| guruimage | 0:9b9c31c57895 | 107 | if (temp == 'S'){ |
| guruimage | 0:9b9c31c57895 | 108 | ConfigReady=1; |
| guruimage | 0:9b9c31c57895 | 109 | Count=0; |
| guruimage | 0:9b9c31c57895 | 110 | } |
| guruimage | 0:9b9c31c57895 | 111 | if (temp=='E'){ |
| guruimage | 0:9b9c31c57895 | 112 | ConfigReady=0; |
| guruimage | 0:9b9c31c57895 | 113 | pc.printf("You have input a string:"); |
| guruimage | 0:9b9c31c57895 | 114 | for(i=0;i<Count;i++){ |
| guruimage | 0:9b9c31c57895 | 115 | pc.printf("%c",config[i]); |
| guruimage | 0:9b9c31c57895 | 116 | } |
| guruimage | 0:9b9c31c57895 | 117 | pc.printf("! Please enter the %dbit sensor input!\r\n", Count); |
| guruimage | 0:9b9c31c57895 | 118 | } |
| guruimage | 0:9b9c31c57895 | 119 | |
| guruimage | 0:9b9c31c57895 | 120 | if ((ConfigReady==1)&&((temp=='1')||(temp=='0')||(temp==' ')||(temp=='S'))){ |
| guruimage | 0:9b9c31c57895 | 121 | if((temp=='1')||(temp=='0')){ |
| guruimage | 0:9b9c31c57895 | 122 | config[Count]=temp; |
| guruimage | 0:9b9c31c57895 | 123 | Count++; |
| guruimage | 0:9b9c31c57895 | 124 | } |
| guruimage | 0:9b9c31c57895 | 125 | }else if(ConfigReady==1){ |
| guruimage | 0:9b9c31c57895 | 126 | pc.printf("HOST ERROR!"); |
| guruimage | 0:9b9c31c57895 | 127 | } |
| guruimage | 0:9b9c31c57895 | 128 | } |
| guruimage | 0:9b9c31c57895 | 129 | } |
| guruimage | 0:9b9c31c57895 | 130 | //This function is used to enable the sensor input to be stable. And store them in integer array input. |
| guruimage | 0:9b9c31c57895 | 131 | void sensorInput(void){ |
| guruimage | 0:9b9c31c57895 | 132 | |
| guruimage | 0:9b9c31c57895 | 133 | for (i=0;i<=3;i++){ |
| guruimage | 0:9b9c31c57895 | 134 | data[i]=data[i+1]; |
| guruimage | 0:9b9c31c57895 | 135 | } |
| guruimage | 0:9b9c31c57895 | 136 | data[4]=twoSensor(); |
| guruimage | 0:9b9c31c57895 | 137 | off=1; |
| guruimage | 0:9b9c31c57895 | 138 | for (i=0;i<=4;i++){ |
| guruimage | 0:9b9c31c57895 | 139 | if (data[i]==3){ |
| guruimage | 0:9b9c31c57895 | 140 | off=off*1; |
| guruimage | 0:9b9c31c57895 | 141 | }else{ |
| guruimage | 0:9b9c31c57895 | 142 | off=off*0; |
| guruimage | 0:9b9c31c57895 | 143 | if(data[i]==2){ |
| guruimage | 0:9b9c31c57895 | 144 | errorON=1; |
| guruimage | 0:9b9c31c57895 | 145 | sensor1EN=0; |
| guruimage | 0:9b9c31c57895 | 146 | sensor2EN=0; |
| guruimage | 0:9b9c31c57895 | 147 | } |
| guruimage | 0:9b9c31c57895 | 148 | if(data[i]==0){ |
| guruimage | 0:9b9c31c57895 | 149 | if(errorON==0){ |
| guruimage | 0:9b9c31c57895 | 150 | sensor1EN=1; |
| guruimage | 0:9b9c31c57895 | 151 | } |
| guruimage | 0:9b9c31c57895 | 152 | } |
| guruimage | 0:9b9c31c57895 | 153 | if(data[i]==1){ |
| guruimage | 0:9b9c31c57895 | 154 | if(errorON==0){ |
| guruimage | 0:9b9c31c57895 | 155 | sensor2EN=1; |
| guruimage | 0:9b9c31c57895 | 156 | } |
| guruimage | 0:9b9c31c57895 | 157 | } |
| guruimage | 0:9b9c31c57895 | 158 | } |
| guruimage | 0:9b9c31c57895 | 159 | } |
| guruimage | 0:9b9c31c57895 | 160 | |
| guruimage | 0:9b9c31c57895 | 161 | if((off==1)&&(sensor1EN==1)){ |
| guruimage | 0:9b9c31c57895 | 162 | input[num]='0'; |
| guruimage | 0:9b9c31c57895 | 163 | pc.printf("You have input a 0!\r\n"); |
| guruimage | 0:9b9c31c57895 | 164 | sensor1EN=0; |
| guruimage | 0:9b9c31c57895 | 165 | num++; |
| guruimage | 0:9b9c31c57895 | 166 | } |
| guruimage | 0:9b9c31c57895 | 167 | if((off==1)&&(sensor2EN==1)){ |
| guruimage | 0:9b9c31c57895 | 168 | input[num]='1'; |
| guruimage | 0:9b9c31c57895 | 169 | pc.printf("You have input a 1!\r\n"); |
| guruimage | 0:9b9c31c57895 | 170 | sensor2EN=0; |
| guruimage | 0:9b9c31c57895 | 171 | num++; |
| guruimage | 0:9b9c31c57895 | 172 | } |
| guruimage | 0:9b9c31c57895 | 173 | if ((off==1)&&(errorON==1)){ |
| guruimage | 0:9b9c31c57895 | 174 | pc.printf("TOUCH ERROR!"); |
| guruimage | 0:9b9c31c57895 | 175 | errorON=0; |
| guruimage | 0:9b9c31c57895 | 176 | } |
| guruimage | 0:9b9c31c57895 | 177 | if(num==99){ |
| guruimage | 0:9b9c31c57895 | 178 | num=0; |
| guruimage | 0:9b9c31c57895 | 179 | } |
| guruimage | 0:9b9c31c57895 | 180 | } |
| guruimage | 0:9b9c31c57895 | 181 | |
| guruimage | 0:9b9c31c57895 | 182 | int twoSensor (void) |
| guruimage | 0:9b9c31c57895 | 183 | { |
| guruimage | 0:9b9c31c57895 | 184 | float sample1; |
| guruimage | 0:9b9c31c57895 | 185 | float sample2; |
| guruimage | 0:9b9c31c57895 | 186 | ground=0; |
| guruimage | 0:9b9c31c57895 | 187 | charger.mode(PullUp); |
| guruimage | 0:9b9c31c57895 | 188 | charger.mode(PullNone); |
| guruimage | 0:9b9c31c57895 | 189 | sample1=input1.read(); |
| guruimage | 0:9b9c31c57895 | 190 | |
| guruimage | 0:9b9c31c57895 | 191 | ground2=0; |
| guruimage | 0:9b9c31c57895 | 192 | charger2.mode(PullUp); |
| guruimage | 0:9b9c31c57895 | 193 | charger2.mode(PullNone); |
| guruimage | 0:9b9c31c57895 | 194 | sample2=input2.read(); |
| guruimage | 0:9b9c31c57895 | 195 | |
| guruimage | 0:9b9c31c57895 | 196 | if ((sample1<0.4)&&(sample2>=0.4)){ |
| guruimage | 0:9b9c31c57895 | 197 | return 0; |
| guruimage | 0:9b9c31c57895 | 198 | } |
| guruimage | 0:9b9c31c57895 | 199 | else if ((sample2<0.4)&&(sample1>=0.4)){ |
| guruimage | 0:9b9c31c57895 | 200 | return 1; |
| guruimage | 0:9b9c31c57895 | 201 | } |
| guruimage | 0:9b9c31c57895 | 202 | else if ((sample2<0.4)&&(sample1<0.4)){ |
| guruimage | 0:9b9c31c57895 | 203 | return 2; |
| guruimage | 0:9b9c31c57895 | 204 | } |
| guruimage | 0:9b9c31c57895 | 205 | else if((sample1>=0.4)&&(sample2>=0.4)){ |
| guruimage | 0:9b9c31c57895 | 206 | return 3; |
| guruimage | 0:9b9c31c57895 | 207 | } |
| guruimage | 0:9b9c31c57895 | 208 | } |