XIAOHUI TAO / Mbed 2 deprecated hw2_forebackground
Committer:
taoxh
Date:
Tue Nov 30 23:52:02 2010 +0000
Revision:
0:43bbdf0aa88a
HW2
FOREBACKGOUND

Who changed what in which revision?

UserRevisionLine numberNew contents of line
taoxh 0:43bbdf0aa88a 1 #include "mbed.h"
taoxh 0:43bbdf0aa88a 2 //foregound/background
taoxh 0:43bbdf0aa88a 3 #define numsamples 1
taoxh 0:43bbdf0aa88a 4 int touchSense1(void);
taoxh 0:43bbdf0aa88a 5 int touchSense2(void);
taoxh 0:43bbdf0aa88a 6 int test1(); // for touch trigger fault only
taoxh 0:43bbdf0aa88a 7 int test2();
taoxh 0:43bbdf0aa88a 8 void gtchar ();
taoxh 0:43bbdf0aa88a 9 DigitalOut myled1(LED1);
taoxh 0:43bbdf0aa88a 10 AnalogIn input1(p20);
taoxh 0:43bbdf0aa88a 11 DigitalIn charger1(p19);
taoxh 0:43bbdf0aa88a 12 DigitalOut ground1(p18);
taoxh 0:43bbdf0aa88a 13
taoxh 0:43bbdf0aa88a 14 DigitalOut myled2(LED2);
taoxh 0:43bbdf0aa88a 15 AnalogIn input2(p15);
taoxh 0:43bbdf0aa88a 16 DigitalIn charger2(p16);
taoxh 0:43bbdf0aa88a 17 DigitalOut ground2(p17);
taoxh 0:43bbdf0aa88a 18 Serial pc(USBTX, USBRX); // tx, rx
taoxh 0:43bbdf0aa88a 19 char str[40];
taoxh 0:43bbdf0aa88a 20 char str2[40];
taoxh 0:43bbdf0aa88a 21 int interrupt=0;
taoxh 0:43bbdf0aa88a 22 int shown=0;
taoxh 0:43bbdf0aa88a 23 int count=0;
taoxh 0:43bbdf0aa88a 24 int length;
taoxh 0:43bbdf0aa88a 25 int senderror () { //personal assigned interrupts
taoxh 0:43bbdf0aa88a 26 pc.printf("Touch Error");
taoxh 0:43bbdf0aa88a 27 interrupt=0;
taoxh 0:43bbdf0aa88a 28 return 0;
taoxh 0:43bbdf0aa88a 29 }
taoxh 0:43bbdf0aa88a 30
taoxh 0:43bbdf0aa88a 31 int senderror2 () {
taoxh 0:43bbdf0aa88a 32 pc.printf("Host Error");
taoxh 0:43bbdf0aa88a 33 interrupt=0;
taoxh 0:43bbdf0aa88a 34 return 0;
taoxh 0:43bbdf0aa88a 35 }
taoxh 0:43bbdf0aa88a 36
taoxh 0:43bbdf0aa88a 37 int senderror3 () {
taoxh 0:43bbdf0aa88a 38 pc.printf("Plz input the start char (S):");
taoxh 0:43bbdf0aa88a 39 interrupt=0;
taoxh 0:43bbdf0aa88a 40 return 0;
taoxh 0:43bbdf0aa88a 41 }
taoxh 0:43bbdf0aa88a 42 int senderror4 () {
taoxh 0:43bbdf0aa88a 43 pc.printf("The allocated space is too small."); interrupt=0;
taoxh 0:43bbdf0aa88a 44 return 0;
taoxh 0:43bbdf0aa88a 45 }
taoxh 0:43bbdf0aa88a 46
taoxh 0:43bbdf0aa88a 47 int sendmatch () {
taoxh 0:43bbdf0aa88a 48 pc.printf("\nMATCH"); interrupt=0;
taoxh 0:43bbdf0aa88a 49 return 0;
taoxh 0:43bbdf0aa88a 50 }
taoxh 0:43bbdf0aa88a 51
taoxh 0:43bbdf0aa88a 52 int store1 () { //store the '1' input
taoxh 0:43bbdf0aa88a 53 myled1 = 1;
taoxh 0:43bbdf0aa88a 54 myled2=0;
taoxh 0:43bbdf0aa88a 55 pc.putc('a');
taoxh 0:43bbdf0aa88a 56 shown=0;
taoxh 0:43bbdf0aa88a 57 while(1) {if (!test1()){break;} if (touchSense2()&& touchSense1()) {break;}}
taoxh 0:43bbdf0aa88a 58 // if (str[count]=='0') {judge=0;}
taoxh 0:43bbdf0aa88a 59 // judge=1;
taoxh 0:43bbdf0aa88a 60 // pc.putc(str[count]);
taoxh 0:43bbdf0aa88a 61 if (count>length){
taoxh 0:43bbdf0aa88a 62 for (int i=1;i<count;i++)
taoxh 0:43bbdf0aa88a 63 { str2[i-1]=str2[i];
taoxh 0:43bbdf0aa88a 64 pc.putc(str2[i-1]);}
taoxh 0:43bbdf0aa88a 65 str2[count-1]='1';
taoxh 0:43bbdf0aa88a 66 pc.putc(str2[count-1]);}
taoxh 0:43bbdf0aa88a 67 else { str2[count]='1';count++;}
taoxh 0:43bbdf0aa88a 68 interrupt=0;
taoxh 0:43bbdf0aa88a 69 return 0;
taoxh 0:43bbdf0aa88a 70 }
taoxh 0:43bbdf0aa88a 71
taoxh 0:43bbdf0aa88a 72 int store2() { //store the '0' input
taoxh 0:43bbdf0aa88a 73 myled1 = 0;
taoxh 0:43bbdf0aa88a 74 myled2=1;
taoxh 0:43bbdf0aa88a 75 pc.putc('b');
taoxh 0:43bbdf0aa88a 76 shown=0;
taoxh 0:43bbdf0aa88a 77 while(1) {if (!test2()){break;} if (touchSense2()&& touchSense1()) {break;} }
taoxh 0:43bbdf0aa88a 78 // if (str[count]=='1') {judge=0;}
taoxh 0:43bbdf0aa88a 79 // judge=0;
taoxh 0:43bbdf0aa88a 80 // pc.putc(str[count]);
taoxh 0:43bbdf0aa88a 81 if (count>length){
taoxh 0:43bbdf0aa88a 82 for (int i=1;i<count;i++)
taoxh 0:43bbdf0aa88a 83 { str2[i-1]=str2[i];
taoxh 0:43bbdf0aa88a 84 pc.putc(str2[i-1]);}
taoxh 0:43bbdf0aa88a 85 str2[count-1]='0';
taoxh 0:43bbdf0aa88a 86 pc.putc(str2[count-1]);}
taoxh 0:43bbdf0aa88a 87 else { str2[count]='0';count++;}
taoxh 0:43bbdf0aa88a 88 interrupt=0;
taoxh 0:43bbdf0aa88a 89 return 0;
taoxh 0:43bbdf0aa88a 90 }
taoxh 0:43bbdf0aa88a 91
taoxh 0:43bbdf0aa88a 92 void gtchar () //foreground event triggered; pc input
taoxh 0:43bbdf0aa88a 93 { pc.printf("plz input the trigger str\n");
taoxh 0:43bbdf0aa88a 94 while(pc.getc()!='S') {interrupt=3; if (interrupt==3) {senderror3();}}
taoxh 0:43bbdf0aa88a 95 length=0; count=0;
taoxh 0:43bbdf0aa88a 96 while(1)
taoxh 0:43bbdf0aa88a 97 { char temp=pc.getc();
taoxh 0:43bbdf0aa88a 98 if (temp=='E'){ break;}
taoxh 0:43bbdf0aa88a 99 else if (temp=='1'){
taoxh 0:43bbdf0aa88a 100 str[length]='1';length++;}
taoxh 0:43bbdf0aa88a 101 else if (temp=='0'){
taoxh 0:43bbdf0aa88a 102 str[length]='0';length++;}
taoxh 0:43bbdf0aa88a 103 else if (temp==' '){}
taoxh 0:43bbdf0aa88a 104 else interrupt=2;
taoxh 0:43bbdf0aa88a 105 if (length>40) {interrupt=4;}
taoxh 0:43bbdf0aa88a 106 if (interrupt==2) {senderror2();}
taoxh 0:43bbdf0aa88a 107 if (interrupt==4) {senderror4();break;}
taoxh 0:43bbdf0aa88a 108 }
taoxh 0:43bbdf0aa88a 109 for (int i=0;i<length; i++)
taoxh 0:43bbdf0aa88a 110 { pc.putc(str[i]);}
taoxh 0:43bbdf0aa88a 111 pc.printf("\n");
taoxh 0:43bbdf0aa88a 112 // char p=char(length);
taoxh 0:43bbdf0aa88a 113 // pc.putc(p);
taoxh 0:43bbdf0aa88a 114 length--;
taoxh 0:43bbdf0aa88a 115 // return length-1;
taoxh 0:43bbdf0aa88a 116 }
taoxh 0:43bbdf0aa88a 117
taoxh 0:43bbdf0aa88a 118 int main() {
taoxh 0:43bbdf0aa88a 119
taoxh 0:43bbdf0aa88a 120 int pre_cir_pro=0;
taoxh 0:43bbdf0aa88a 121 int judge=1;
taoxh 0:43bbdf0aa88a 122 pc.attach(&gtchar); //FOREGROUND start pc input
taoxh 0:43bbdf0aa88a 123 while(1)
taoxh 0:43bbdf0aa88a 124 {
taoxh 0:43bbdf0aa88a 125 if (touchSense1()&& !touchSense2()) {interrupt=5;}
taoxh 0:43bbdf0aa88a 126 if (touchSense2()&& !touchSense1() && pre_cir_pro>30) {interrupt=6;}
taoxh 0:43bbdf0aa88a 127 if (touchSense2()&& touchSense1()) {interrupt=1;}
taoxh 0:43bbdf0aa88a 128 if (!touchSense2()&& !touchSense1()) {}
taoxh 0:43bbdf0aa88a 129 pre_cir_pro++;
taoxh 0:43bbdf0aa88a 130 if (interrupt==1) {senderror();}
taoxh 0:43bbdf0aa88a 131 if (interrupt==5) {store1();}
taoxh 0:43bbdf0aa88a 132 if (interrupt==6) {store2();}
taoxh 0:43bbdf0aa88a 133 if (count>=length) { //match judge and output
taoxh 0:43bbdf0aa88a 134 for (int i=0;i<=length;i++)
taoxh 0:43bbdf0aa88a 135 { if (str[i]!=str2[i]) {judge=0;} }
taoxh 0:43bbdf0aa88a 136 if (judge==1 && shown==0 && length!=0) {interrupt=7;shown=1;}
taoxh 0:43bbdf0aa88a 137 judge=1;}
taoxh 0:43bbdf0aa88a 138 if (interrupt==7) {sendmatch();}
taoxh 0:43bbdf0aa88a 139 }
taoxh 0:43bbdf0aa88a 140 }
taoxh 0:43bbdf0aa88a 141
taoxh 0:43bbdf0aa88a 142 int touchSense1(void) {
taoxh 0:43bbdf0aa88a 143 float sample;
taoxh 0:43bbdf0aa88a 144 ground1 = 0;
taoxh 0:43bbdf0aa88a 145 charger1.mode(PullUp);
taoxh 0:43bbdf0aa88a 146 charger1.mode(PullNone);
taoxh 0:43bbdf0aa88a 147 sample=input1.read();
taoxh 0:43bbdf0aa88a 148 if (sample < 0.3) {
taoxh 0:43bbdf0aa88a 149 return 1;
taoxh 0:43bbdf0aa88a 150 } else {
taoxh 0:43bbdf0aa88a 151 return 0;
taoxh 0:43bbdf0aa88a 152 }
taoxh 0:43bbdf0aa88a 153 }
taoxh 0:43bbdf0aa88a 154
taoxh 0:43bbdf0aa88a 155 int touchSense2(void) {
taoxh 0:43bbdf0aa88a 156 float sample;
taoxh 0:43bbdf0aa88a 157 ground2 = 0;
taoxh 0:43bbdf0aa88a 158 charger2.mode(PullUp);
taoxh 0:43bbdf0aa88a 159 charger2.mode(PullNone);
taoxh 0:43bbdf0aa88a 160 sample=input2.read();
taoxh 0:43bbdf0aa88a 161 if (sample < 0.3) {
taoxh 0:43bbdf0aa88a 162 return 1;
taoxh 0:43bbdf0aa88a 163 } else {
taoxh 0:43bbdf0aa88a 164 return 0;
taoxh 0:43bbdf0aa88a 165 }
taoxh 0:43bbdf0aa88a 166 }
taoxh 0:43bbdf0aa88a 167
taoxh 0:43bbdf0aa88a 168
taoxh 0:43bbdf0aa88a 169 int test1()
taoxh 0:43bbdf0aa88a 170 {
taoxh 0:43bbdf0aa88a 171 int j=1000;
taoxh 0:43bbdf0aa88a 172 int judge=0;
taoxh 0:43bbdf0aa88a 173 for (int i=0;i<j;i++)
taoxh 0:43bbdf0aa88a 174 { if(touchSense1()) {judge=1;}}
taoxh 0:43bbdf0aa88a 175 return judge;
taoxh 0:43bbdf0aa88a 176 }
taoxh 0:43bbdf0aa88a 177
taoxh 0:43bbdf0aa88a 178 int test2()
taoxh 0:43bbdf0aa88a 179 {
taoxh 0:43bbdf0aa88a 180 int j=1000;
taoxh 0:43bbdf0aa88a 181 int judge=0;
taoxh 0:43bbdf0aa88a 182 for (int i=0;i<j;i++)
taoxh 0:43bbdf0aa88a 183 { if(touchSense2()) {judge=1;}}
taoxh 0:43bbdf0aa88a 184 return judge;
taoxh 0:43bbdf0aa88a 185 }