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:ffe2b06e7d06, 2010-11-30 (annotated)
- Committer:
- taoxh
- Date:
- Tue Nov 30 23:47:58 2010 +0000
- Revision:
- 0:ffe2b06e7d06
LOOP VERSION 1
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| taoxh | 0:ffe2b06e7d06 | 1 | #include "mbed.h" |
| taoxh | 0:ffe2b06e7d06 | 2 | |
| taoxh | 0:ffe2b06e7d06 | 3 | #define numsamples 1 |
| taoxh | 0:ffe2b06e7d06 | 4 | int touchSense1(void); //t0.t1 |
| taoxh | 0:ffe2b06e7d06 | 5 | int touchSense2(void); |
| taoxh | 0:ffe2b06e7d06 | 6 | int senderror (); |
| taoxh | 0:ffe2b06e7d06 | 7 | int test1(); //the time estimated for 5ms. In case for touch trigger |
| taoxh | 0:ffe2b06e7d06 | 8 | int test2(); |
| taoxh | 0:ffe2b06e7d06 | 9 | int gtchar (); //get the pc input |
| taoxh | 0:ffe2b06e7d06 | 10 | DigitalOut myled1(LED1); |
| taoxh | 0:ffe2b06e7d06 | 11 | AnalogIn input1(p20); |
| taoxh | 0:ffe2b06e7d06 | 12 | DigitalIn charger1(p19); |
| taoxh | 0:ffe2b06e7d06 | 13 | DigitalOut ground1(p18); |
| taoxh | 0:ffe2b06e7d06 | 14 | |
| taoxh | 0:ffe2b06e7d06 | 15 | DigitalOut myled2(LED2); |
| taoxh | 0:ffe2b06e7d06 | 16 | AnalogIn input2(p15); |
| taoxh | 0:ffe2b06e7d06 | 17 | DigitalIn charger2(p16); |
| taoxh | 0:ffe2b06e7d06 | 18 | DigitalOut ground2(p17); |
| taoxh | 0:ffe2b06e7d06 | 19 | Serial pc(USBTX, USBRX); // tx, rx |
| taoxh | 0:ffe2b06e7d06 | 20 | char str[40]; //pc input storage |
| taoxh | 0:ffe2b06e7d06 | 21 | char str2[40]; //touch input storage |
| taoxh | 0:ffe2b06e7d06 | 22 | int main() { |
| taoxh | 0:ffe2b06e7d06 | 23 | int length; //the total length of the pc input |
| taoxh | 0:ffe2b06e7d06 | 24 | length=gtchar(); |
| taoxh | 0:ffe2b06e7d06 | 25 | int count=0; |
| taoxh | 0:ffe2b06e7d06 | 26 | int pre_cir_pro=0; |
| taoxh | 0:ffe2b06e7d06 | 27 | int judge=1; |
| taoxh | 0:ffe2b06e7d06 | 28 | int shown=0; |
| taoxh | 0:ffe2b06e7d06 | 29 | // int write=-1; |
| taoxh | 0:ffe2b06e7d06 | 30 | while (1) { |
| taoxh | 0:ffe2b06e7d06 | 31 | if (touchSense1()&& !touchSense2()) { //t0 touches |
| taoxh | 0:ffe2b06e7d06 | 32 | myled1 = 1; |
| taoxh | 0:ffe2b06e7d06 | 33 | myled2=0; |
| taoxh | 0:ffe2b06e7d06 | 34 | pc.putc('a'); // for reference only |
| taoxh | 0:ffe2b06e7d06 | 35 | shown=0; //match shown or not |
| taoxh | 0:ffe2b06e7d06 | 36 | while(1) {if (!test1()){break;} if (touchSense2()&& touchSense1()) {break;}} //Emit the touch trigger |
| taoxh | 0:ffe2b06e7d06 | 37 | // if (str[count]=='0') {judge=0;} |
| taoxh | 0:ffe2b06e7d06 | 38 | // judge=1; |
| taoxh | 0:ffe2b06e7d06 | 39 | // pc.putc(str[count]); |
| taoxh | 0:ffe2b06e7d06 | 40 | if (count>length){ //store the touch input |
| taoxh | 0:ffe2b06e7d06 | 41 | for (int i=1;i<count;i++) |
| taoxh | 0:ffe2b06e7d06 | 42 | { str2[i-1]=str2[i]; |
| taoxh | 0:ffe2b06e7d06 | 43 | pc.putc(str2[i-1]); |
| taoxh | 0:ffe2b06e7d06 | 44 | } |
| taoxh | 0:ffe2b06e7d06 | 45 | str2[count-1]='1'; |
| taoxh | 0:ffe2b06e7d06 | 46 | pc.putc(str2[count-1]);} |
| taoxh | 0:ffe2b06e7d06 | 47 | else { str2[count]='1';count++;} |
| taoxh | 0:ffe2b06e7d06 | 48 | |
| taoxh | 0:ffe2b06e7d06 | 49 | //process_char(); |
| taoxh | 0:ffe2b06e7d06 | 50 | } else if (touchSense2()&& !touchSense1()) { //t1 touches |
| taoxh | 0:ffe2b06e7d06 | 51 | if (pre_cir_pro>30){ |
| taoxh | 0:ffe2b06e7d06 | 52 | myled1 = 0; |
| taoxh | 0:ffe2b06e7d06 | 53 | myled2=1; |
| taoxh | 0:ffe2b06e7d06 | 54 | pc.putc('b'); |
| taoxh | 0:ffe2b06e7d06 | 55 | shown=0; |
| taoxh | 0:ffe2b06e7d06 | 56 | while(1) {if (!test2()){break;} if (touchSense2()&& touchSense1()) {break;} } |
| taoxh | 0:ffe2b06e7d06 | 57 | //if (str[count]=='1') {judge=0;} |
| taoxh | 0:ffe2b06e7d06 | 58 | //judge=0; |
| taoxh | 0:ffe2b06e7d06 | 59 | //pc.putc(str[count]); |
| taoxh | 0:ffe2b06e7d06 | 60 | if (count>length){ |
| taoxh | 0:ffe2b06e7d06 | 61 | for (int i=1;i<count;i++) |
| taoxh | 0:ffe2b06e7d06 | 62 | { str2[i-1]=str2[i]; |
| taoxh | 0:ffe2b06e7d06 | 63 | pc.putc(str2[i-1]); |
| taoxh | 0:ffe2b06e7d06 | 64 | } |
| taoxh | 0:ffe2b06e7d06 | 65 | str2[count-1]='0'; |
| taoxh | 0:ffe2b06e7d06 | 66 | pc.putc(str2[count-1]);} |
| taoxh | 0:ffe2b06e7d06 | 67 | else { str2[count]='0';count++;} |
| taoxh | 0:ffe2b06e7d06 | 68 | } |
| taoxh | 0:ffe2b06e7d06 | 69 | } else if (touchSense2()&& touchSense1()) { //touch error |
| taoxh | 0:ffe2b06e7d06 | 70 | myled1 = 1; |
| taoxh | 0:ffe2b06e7d06 | 71 | myled2=1; |
| taoxh | 0:ffe2b06e7d06 | 72 | senderror(); |
| taoxh | 0:ffe2b06e7d06 | 73 | } else { |
| taoxh | 0:ffe2b06e7d06 | 74 | myled1=0; |
| taoxh | 0:ffe2b06e7d06 | 75 | myled2=0; |
| taoxh | 0:ffe2b06e7d06 | 76 | } |
| taoxh | 0:ffe2b06e7d06 | 77 | pre_cir_pro++; //In case of the initial trigger |
| taoxh | 0:ffe2b06e7d06 | 78 | |
| taoxh | 0:ffe2b06e7d06 | 79 | if (count>=length) { |
| taoxh | 0:ffe2b06e7d06 | 80 | for (int i=0;i<=length;i++) |
| taoxh | 0:ffe2b06e7d06 | 81 | { if (str[i]!=str2[i]) {judge=0;} } |
| taoxh | 0:ffe2b06e7d06 | 82 | if (judge==1 && shown==0) {pc.printf("\nMATCH");shown=1;} |
| taoxh | 0:ffe2b06e7d06 | 83 | judge=1;} |
| taoxh | 0:ffe2b06e7d06 | 84 | } |
| taoxh | 0:ffe2b06e7d06 | 85 | } |
| taoxh | 0:ffe2b06e7d06 | 86 | // if () {pc.printf("\nMATCH");count=0;} |
| taoxh | 0:ffe2b06e7d06 | 87 | // for (int i=0;i<count;i++) |
| taoxh | 0:ffe2b06e7d06 | 88 | // {if (str[i]!=str2[i]) {judge2=0;}} |
| taoxh | 0:ffe2b06e7d06 | 89 | // if (count>=length) { |
| taoxh | 0:ffe2b06e7d06 | 90 | // if (judge2==1) {pc.printf("\nMATCH");count=0;} |
| taoxh | 0:ffe2b06e7d06 | 91 | // else {pc.printf("\n not MATCH");count=0;} |
| taoxh | 0:ffe2b06e7d06 | 92 | // } |
| taoxh | 0:ffe2b06e7d06 | 93 | // if (!touchSense2()&& !touchSense1()) {if (write!=-1) { |
| taoxh | 0:ffe2b06e7d06 | 94 | // if (write==1&&str[count]=='0') {judge=1;} |
| taoxh | 0:ffe2b06e7d06 | 95 | // else if (write==0&&str[count]=='1') {judge=1;}; write=-1;count++;} |
| taoxh | 0:ffe2b06e7d06 | 96 | // } |
| taoxh | 0:ffe2b06e7d06 | 97 | // if (count>5) {if (judge==1) {pc.printf("not match");} else {pc.printf("not match");}} |
| taoxh | 0:ffe2b06e7d06 | 98 | |
| taoxh | 0:ffe2b06e7d06 | 99 | int touchSense1(void) { |
| taoxh | 0:ffe2b06e7d06 | 100 | float sample; |
| taoxh | 0:ffe2b06e7d06 | 101 | ground1 = 0; |
| taoxh | 0:ffe2b06e7d06 | 102 | charger1.mode(PullUp); |
| taoxh | 0:ffe2b06e7d06 | 103 | charger1.mode(PullNone); |
| taoxh | 0:ffe2b06e7d06 | 104 | sample=input1.read(); |
| taoxh | 0:ffe2b06e7d06 | 105 | if (sample < 0.3) { |
| taoxh | 0:ffe2b06e7d06 | 106 | return 1; |
| taoxh | 0:ffe2b06e7d06 | 107 | } else { |
| taoxh | 0:ffe2b06e7d06 | 108 | return 0; |
| taoxh | 0:ffe2b06e7d06 | 109 | } |
| taoxh | 0:ffe2b06e7d06 | 110 | } |
| taoxh | 0:ffe2b06e7d06 | 111 | |
| taoxh | 0:ffe2b06e7d06 | 112 | int touchSense2(void) { |
| taoxh | 0:ffe2b06e7d06 | 113 | float sample; |
| taoxh | 0:ffe2b06e7d06 | 114 | ground2 = 0; |
| taoxh | 0:ffe2b06e7d06 | 115 | charger2.mode(PullUp); |
| taoxh | 0:ffe2b06e7d06 | 116 | charger2.mode(PullNone); |
| taoxh | 0:ffe2b06e7d06 | 117 | sample=input2.read(); |
| taoxh | 0:ffe2b06e7d06 | 118 | if (sample < 0.3) { |
| taoxh | 0:ffe2b06e7d06 | 119 | return 1; |
| taoxh | 0:ffe2b06e7d06 | 120 | } else { |
| taoxh | 0:ffe2b06e7d06 | 121 | return 0; |
| taoxh | 0:ffe2b06e7d06 | 122 | } |
| taoxh | 0:ffe2b06e7d06 | 123 | } |
| taoxh | 0:ffe2b06e7d06 | 124 | |
| taoxh | 0:ffe2b06e7d06 | 125 | int senderror () { |
| taoxh | 0:ffe2b06e7d06 | 126 | pc.printf("Touch Error"); |
| taoxh | 0:ffe2b06e7d06 | 127 | return 0; |
| taoxh | 0:ffe2b06e7d06 | 128 | } |
| taoxh | 0:ffe2b06e7d06 | 129 | |
| taoxh | 0:ffe2b06e7d06 | 130 | int senderror2 () { |
| taoxh | 0:ffe2b06e7d06 | 131 | pc.printf("Host Error"); |
| taoxh | 0:ffe2b06e7d06 | 132 | return 0; |
| taoxh | 0:ffe2b06e7d06 | 133 | } |
| taoxh | 0:ffe2b06e7d06 | 134 | |
| taoxh | 0:ffe2b06e7d06 | 135 | int gtchar () //get pc input |
| taoxh | 0:ffe2b06e7d06 | 136 | { pc.printf("plz input the trigger str\n"); |
| taoxh | 0:ffe2b06e7d06 | 137 | int length=0; |
| taoxh | 0:ffe2b06e7d06 | 138 | while(pc.getc()!='S') {pc.printf("Plz input the start char (S):");} |
| taoxh | 0:ffe2b06e7d06 | 139 | while(1) |
| taoxh | 0:ffe2b06e7d06 | 140 | { char temp=pc.getc(); |
| taoxh | 0:ffe2b06e7d06 | 141 | if (temp=='E'){ break;} |
| taoxh | 0:ffe2b06e7d06 | 142 | else if (temp=='1'){ |
| taoxh | 0:ffe2b06e7d06 | 143 | str[length]='1';length++;} |
| taoxh | 0:ffe2b06e7d06 | 144 | else if (temp=='0'){ |
| taoxh | 0:ffe2b06e7d06 | 145 | str[length]='0';length++;} |
| taoxh | 0:ffe2b06e7d06 | 146 | else if (temp==' '){} |
| taoxh | 0:ffe2b06e7d06 | 147 | else senderror2(); |
| taoxh | 0:ffe2b06e7d06 | 148 | if (length>40) {pc.printf("The allocated space is too small.");break;} |
| taoxh | 0:ffe2b06e7d06 | 149 | } |
| taoxh | 0:ffe2b06e7d06 | 150 | for (int i=0;i<length; i++) |
| taoxh | 0:ffe2b06e7d06 | 151 | { pc.putc(str[i]);} |
| taoxh | 0:ffe2b06e7d06 | 152 | pc.printf("\n"); |
| taoxh | 0:ffe2b06e7d06 | 153 | // char p=char(length); |
| taoxh | 0:ffe2b06e7d06 | 154 | // pc.putc(p); |
| taoxh | 0:ffe2b06e7d06 | 155 | return length-1; |
| taoxh | 0:ffe2b06e7d06 | 156 | } |
| taoxh | 0:ffe2b06e7d06 | 157 | |
| taoxh | 0:ffe2b06e7d06 | 158 | |
| taoxh | 0:ffe2b06e7d06 | 159 | int test1() |
| taoxh | 0:ffe2b06e7d06 | 160 | { |
| taoxh | 0:ffe2b06e7d06 | 161 | int j=1000; |
| taoxh | 0:ffe2b06e7d06 | 162 | int judge=0; |
| taoxh | 0:ffe2b06e7d06 | 163 | for (int i=0;i<j;i++) |
| taoxh | 0:ffe2b06e7d06 | 164 | { if(touchSense1()) {judge=1;}} |
| taoxh | 0:ffe2b06e7d06 | 165 | return judge; |
| taoxh | 0:ffe2b06e7d06 | 166 | } |
| taoxh | 0:ffe2b06e7d06 | 167 | |
| taoxh | 0:ffe2b06e7d06 | 168 | int test2() |
| taoxh | 0:ffe2b06e7d06 | 169 | { |
| taoxh | 0:ffe2b06e7d06 | 170 | int j=1000; |
| taoxh | 0:ffe2b06e7d06 | 171 | int judge=0; |
| taoxh | 0:ffe2b06e7d06 | 172 | for (int i=0;i<j;i++) |
| taoxh | 0:ffe2b06e7d06 | 173 | { if(touchSense2()) {judge=1;}} |
| taoxh | 0:ffe2b06e7d06 | 174 | return judge; |
| taoxh | 0:ffe2b06e7d06 | 175 | } |