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:2ae03ea235cf, 2010-12-01 (annotated)
- Committer:
- liqingtaobkd
- Date:
- Wed Dec 01 01:02:56 2010 +0000
- Revision:
- 0:2ae03ea235cf
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| liqingtaobkd | 0:2ae03ea235cf | 1 | #include "mbed.h" |
| liqingtaobkd | 0:2ae03ea235cf | 2 | |
| liqingtaobkd | 0:2ae03ea235cf | 3 | #define numsamples 1 |
| liqingtaobkd | 0:2ae03ea235cf | 4 | int touchSense1(void); |
| liqingtaobkd | 0:2ae03ea235cf | 5 | int touchSense2(void); |
| liqingtaobkd | 0:2ae03ea235cf | 6 | int comparestr(int x,int y,char a[],char b[]); |
| liqingtaobkd | 0:2ae03ea235cf | 7 | void getstring(void); |
| liqingtaobkd | 0:2ae03ea235cf | 8 | |
| liqingtaobkd | 0:2ae03ea235cf | 9 | DigitalOut myled1(LED1); |
| liqingtaobkd | 0:2ae03ea235cf | 10 | DigitalOut myled2(LED2); |
| liqingtaobkd | 0:2ae03ea235cf | 11 | AnalogIn input1(p20); |
| liqingtaobkd | 0:2ae03ea235cf | 12 | AnalogIn input2(p15); |
| liqingtaobkd | 0:2ae03ea235cf | 13 | DigitalIn charger1(p19); |
| liqingtaobkd | 0:2ae03ea235cf | 14 | DigitalIn charger2(p16); |
| liqingtaobkd | 0:2ae03ea235cf | 15 | DigitalOut ground1(p18); |
| liqingtaobkd | 0:2ae03ea235cf | 16 | DigitalOut ground2(p17); |
| liqingtaobkd | 0:2ae03ea235cf | 17 | Serial pc(USBTX, USBRX); // tx, rx |
| liqingtaobkd | 0:2ae03ea235cf | 18 | |
| liqingtaobkd | 0:2ae03ea235cf | 19 | static int j; |
| liqingtaobkd | 0:2ae03ea235cf | 20 | static char str[50]; |
| liqingtaobkd | 0:2ae03ea235cf | 21 | |
| liqingtaobkd | 0:2ae03ea235cf | 22 | int main() { |
| liqingtaobkd | 0:2ae03ea235cf | 23 | pc.printf("Please input the trigger string (start with S and end with E):\r\n"); |
| liqingtaobkd | 0:2ae03ea235cf | 24 | getstring(); |
| liqingtaobkd | 0:2ae03ea235cf | 25 | pc.printf("Waiting for human input.\r\n"); |
| liqingtaobkd | 0:2ae03ea235cf | 26 | pc.attach(&getstring); |
| liqingtaobkd | 0:2ae03ea235cf | 27 | int i=0,k=0; |
| liqingtaobkd | 0:2ae03ea235cf | 28 | char str1[50]; |
| liqingtaobkd | 0:2ae03ea235cf | 29 | int match=0; |
| liqingtaobkd | 0:2ae03ea235cf | 30 | while (1) { |
| liqingtaobkd | 0:2ae03ea235cf | 31 | if (touchSense1()) { |
| liqingtaobkd | 0:2ae03ea235cf | 32 | myled1 = 1; |
| liqingtaobkd | 0:2ae03ea235cf | 33 | } else { |
| liqingtaobkd | 0:2ae03ea235cf | 34 | myled1 = 0; |
| liqingtaobkd | 0:2ae03ea235cf | 35 | } |
| liqingtaobkd | 0:2ae03ea235cf | 36 | if (touchSense2()) { |
| liqingtaobkd | 0:2ae03ea235cf | 37 | myled2 = 1; |
| liqingtaobkd | 0:2ae03ea235cf | 38 | } else { |
| liqingtaobkd | 0:2ae03ea235cf | 39 | myled2 = 0; |
| liqingtaobkd | 0:2ae03ea235cf | 40 | } |
| liqingtaobkd | 0:2ae03ea235cf | 41 | switch (i) { |
| liqingtaobkd | 0:2ae03ea235cf | 42 | case 0: |
| liqingtaobkd | 0:2ae03ea235cf | 43 | if (myled1&myled2) { |
| liqingtaobkd | 0:2ae03ea235cf | 44 | pc.printf("TOUCH ERROR\r\n"); |
| liqingtaobkd | 0:2ae03ea235cf | 45 | i=1; |
| liqingtaobkd | 0:2ae03ea235cf | 46 | } |
| liqingtaobkd | 0:2ae03ea235cf | 47 | if (myled1==1&myled2==0) { |
| liqingtaobkd | 0:2ae03ea235cf | 48 | pc.printf("1"); |
| liqingtaobkd | 0:2ae03ea235cf | 49 | i=1; |
| liqingtaobkd | 0:2ae03ea235cf | 50 | str1[k]='1'; |
| liqingtaobkd | 0:2ae03ea235cf | 51 | match=0; |
| liqingtaobkd | 0:2ae03ea235cf | 52 | k++; |
| liqingtaobkd | 0:2ae03ea235cf | 53 | } |
| liqingtaobkd | 0:2ae03ea235cf | 54 | if (myled2==1&myled1==0) { |
| liqingtaobkd | 0:2ae03ea235cf | 55 | pc.printf("0"); |
| liqingtaobkd | 0:2ae03ea235cf | 56 | i=1; |
| liqingtaobkd | 0:2ae03ea235cf | 57 | str1[k]='0'; |
| liqingtaobkd | 0:2ae03ea235cf | 58 | match=0; |
| liqingtaobkd | 0:2ae03ea235cf | 59 | k++; |
| liqingtaobkd | 0:2ae03ea235cf | 60 | } |
| liqingtaobkd | 0:2ae03ea235cf | 61 | case 1: |
| liqingtaobkd | 0:2ae03ea235cf | 62 | if ((myled1==0)&(myled2==0)) { |
| liqingtaobkd | 0:2ae03ea235cf | 63 | i=0; |
| liqingtaobkd | 0:2ae03ea235cf | 64 | } |
| liqingtaobkd | 0:2ae03ea235cf | 65 | } |
| liqingtaobkd | 0:2ae03ea235cf | 66 | int x,y; |
| liqingtaobkd | 0:2ae03ea235cf | 67 | x=k-1,y=j; |
| liqingtaobkd | 0:2ae03ea235cf | 68 | if (match==0) { |
| liqingtaobkd | 0:2ae03ea235cf | 69 | match=comparestr(x,y,str,str1); |
| liqingtaobkd | 0:2ae03ea235cf | 70 | if (match==1) pc.printf("\r\nMATCH\r\n"); |
| liqingtaobkd | 0:2ae03ea235cf | 71 | } |
| liqingtaobkd | 0:2ae03ea235cf | 72 | wait (0.05); |
| liqingtaobkd | 0:2ae03ea235cf | 73 | } |
| liqingtaobkd | 0:2ae03ea235cf | 74 | } |
| liqingtaobkd | 0:2ae03ea235cf | 75 | |
| liqingtaobkd | 0:2ae03ea235cf | 76 | void getstring(void) { |
| liqingtaobkd | 0:2ae03ea235cf | 77 | char a; |
| liqingtaobkd | 0:2ae03ea235cf | 78 | j=0; |
| liqingtaobkd | 0:2ae03ea235cf | 79 | X: |
| liqingtaobkd | 0:2ae03ea235cf | 80 | while (1) { |
| liqingtaobkd | 0:2ae03ea235cf | 81 | a=pc.getc(); |
| liqingtaobkd | 0:2ae03ea235cf | 82 | pc.printf("%c",a); |
| liqingtaobkd | 0:2ae03ea235cf | 83 | if (a=='S') break; |
| liqingtaobkd | 0:2ae03ea235cf | 84 | else { |
| liqingtaobkd | 0:2ae03ea235cf | 85 | pc.printf("\r\nHOST ERROR,TRY AGAIN\r\n"); |
| liqingtaobkd | 0:2ae03ea235cf | 86 | continue; |
| liqingtaobkd | 0:2ae03ea235cf | 87 | } |
| liqingtaobkd | 0:2ae03ea235cf | 88 | } |
| liqingtaobkd | 0:2ae03ea235cf | 89 | while (1) { |
| liqingtaobkd | 0:2ae03ea235cf | 90 | a=pc.getc(); |
| liqingtaobkd | 0:2ae03ea235cf | 91 | pc.printf("%c",a); |
| liqingtaobkd | 0:2ae03ea235cf | 92 | if (a=='E') { |
| liqingtaobkd | 0:2ae03ea235cf | 93 | pc.printf("\r\n"); |
| liqingtaobkd | 0:2ae03ea235cf | 94 | break; |
| liqingtaobkd | 0:2ae03ea235cf | 95 | } else if (a=='0'|a=='1') { |
| liqingtaobkd | 0:2ae03ea235cf | 96 | str[j]=a; |
| liqingtaobkd | 0:2ae03ea235cf | 97 | j++; |
| liqingtaobkd | 0:2ae03ea235cf | 98 | } else if (a==' ') continue; |
| liqingtaobkd | 0:2ae03ea235cf | 99 | else { |
| liqingtaobkd | 0:2ae03ea235cf | 100 | pc.printf("\r\nHOST ERROR,TRY AGAIN\r\n"); |
| liqingtaobkd | 0:2ae03ea235cf | 101 | goto X; |
| liqingtaobkd | 0:2ae03ea235cf | 102 | } |
| liqingtaobkd | 0:2ae03ea235cf | 103 | } |
| liqingtaobkd | 0:2ae03ea235cf | 104 | j=j-1; |
| liqingtaobkd | 0:2ae03ea235cf | 105 | } |
| liqingtaobkd | 0:2ae03ea235cf | 106 | int touchSense1(void) { |
| liqingtaobkd | 0:2ae03ea235cf | 107 | float sample; |
| liqingtaobkd | 0:2ae03ea235cf | 108 | ground1 = 0; |
| liqingtaobkd | 0:2ae03ea235cf | 109 | charger1.mode(PullUp); |
| liqingtaobkd | 0:2ae03ea235cf | 110 | charger1.mode(PullNone); |
| liqingtaobkd | 0:2ae03ea235cf | 111 | sample=input1.read(); |
| liqingtaobkd | 0:2ae03ea235cf | 112 | if (sample < 0.3) { |
| liqingtaobkd | 0:2ae03ea235cf | 113 | return 1; |
| liqingtaobkd | 0:2ae03ea235cf | 114 | } else { |
| liqingtaobkd | 0:2ae03ea235cf | 115 | return 0; |
| liqingtaobkd | 0:2ae03ea235cf | 116 | } |
| liqingtaobkd | 0:2ae03ea235cf | 117 | } |
| liqingtaobkd | 0:2ae03ea235cf | 118 | |
| liqingtaobkd | 0:2ae03ea235cf | 119 | int touchSense2(void) { |
| liqingtaobkd | 0:2ae03ea235cf | 120 | float sample; |
| liqingtaobkd | 0:2ae03ea235cf | 121 | ground2 = 0; |
| liqingtaobkd | 0:2ae03ea235cf | 122 | charger2.mode(PullUp); |
| liqingtaobkd | 0:2ae03ea235cf | 123 | charger2.mode(PullNone); |
| liqingtaobkd | 0:2ae03ea235cf | 124 | sample=input2.read(); |
| liqingtaobkd | 0:2ae03ea235cf | 125 | if (sample < 0.3) { |
| liqingtaobkd | 0:2ae03ea235cf | 126 | return 1; |
| liqingtaobkd | 0:2ae03ea235cf | 127 | } else { |
| liqingtaobkd | 0:2ae03ea235cf | 128 | return 0; |
| liqingtaobkd | 0:2ae03ea235cf | 129 | } |
| liqingtaobkd | 0:2ae03ea235cf | 130 | } |
| liqingtaobkd | 0:2ae03ea235cf | 131 | |
| liqingtaobkd | 0:2ae03ea235cf | 132 | int comparestr(int x,int y,char a[],char b[]) { |
| liqingtaobkd | 0:2ae03ea235cf | 133 | while (y>=0) { |
| liqingtaobkd | 0:2ae03ea235cf | 134 | if (a[y]==b[x]) { |
| liqingtaobkd | 0:2ae03ea235cf | 135 | x--; |
| liqingtaobkd | 0:2ae03ea235cf | 136 | y--; |
| liqingtaobkd | 0:2ae03ea235cf | 137 | } else goto A; |
| liqingtaobkd | 0:2ae03ea235cf | 138 | } |
| liqingtaobkd | 0:2ae03ea235cf | 139 | return 1; |
| liqingtaobkd | 0:2ae03ea235cf | 140 | A: |
| liqingtaobkd | 0:2ae03ea235cf | 141 | return 0; |
| liqingtaobkd | 0:2ae03ea235cf | 142 | } |