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:f6d212e09078, 2010-12-01 (annotated)
- Committer:
- linxinda
- Date:
- Wed Dec 01 05:29:04 2010 +0000
- Revision:
- 0:f6d212e09078
hw2.3 xinda
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| linxinda | 0:f6d212e09078 | 1 | #include "mbed.h" |
| linxinda | 0:f6d212e09078 | 2 | |
| linxinda | 0:f6d212e09078 | 3 | #define numsamples 1 |
| linxinda | 0:f6d212e09078 | 4 | int touchSense1(void); |
| linxinda | 0:f6d212e09078 | 5 | int touchSense2(void); |
| linxinda | 0:f6d212e09078 | 6 | void readSerial(); |
| linxinda | 0:f6d212e09078 | 7 | char touchDetect (); |
| linxinda | 0:f6d212e09078 | 8 | |
| linxinda | 0:f6d212e09078 | 9 | DigitalOut myled1(LED1); |
| linxinda | 0:f6d212e09078 | 10 | DigitalOut myled2(LED2); |
| linxinda | 0:f6d212e09078 | 11 | DigitalOut myled3(LED3); |
| linxinda | 0:f6d212e09078 | 12 | AnalogIn input1(p20); |
| linxinda | 0:f6d212e09078 | 13 | DigitalIn charger1(p19); |
| linxinda | 0:f6d212e09078 | 14 | DigitalOut ground1(p18); |
| linxinda | 0:f6d212e09078 | 15 | AnalogIn input2(p17); |
| linxinda | 0:f6d212e09078 | 16 | DigitalIn charger2(p16); |
| linxinda | 0:f6d212e09078 | 17 | DigitalOut ground2(p15); |
| linxinda | 0:f6d212e09078 | 18 | |
| linxinda | 0:f6d212e09078 | 19 | |
| linxinda | 0:f6d212e09078 | 20 | Serial pc(USBTX, USBRX); // tx, rx |
| linxinda | 0:f6d212e09078 | 21 | |
| linxinda | 0:f6d212e09078 | 22 | char *serial = new char [100]; |
| linxinda | 0:f6d212e09078 | 23 | |
| linxinda | 0:f6d212e09078 | 24 | int main() { |
| linxinda | 0:f6d212e09078 | 25 | |
| linxinda | 0:f6d212e09078 | 26 | pc.attach (&readSerial); //Foreground: read the serial |
| linxinda | 0:f6d212e09078 | 27 | |
| linxinda | 0:f6d212e09078 | 28 | |
| linxinda | 0:f6d212e09078 | 29 | while (1){ //Background: compare the serial value and user input. |
| linxinda | 0:f6d212e09078 | 30 | int i=1,j=0; |
| linxinda | 0:f6d212e09078 | 31 | while (serial[j]!='E') { //Print the host value; |
| linxinda | 0:f6d212e09078 | 32 | pc.printf ("%c", serial[j]); |
| linxinda | 0:f6d212e09078 | 33 | j++; |
| linxinda | 0:f6d212e09078 | 34 | } |
| linxinda | 0:f6d212e09078 | 35 | pc.printf ("E"); |
| linxinda | 0:f6d212e09078 | 36 | |
| linxinda | 0:f6d212e09078 | 37 | while (1) { |
| linxinda | 0:f6d212e09078 | 38 | pc.printf (" The next input should be :"); //Hint |
| linxinda | 0:f6d212e09078 | 39 | pc.printf ("%c", serial[i]); |
| linxinda | 0:f6d212e09078 | 40 | if (serial[i]=='E'){ |
| linxinda | 0:f6d212e09078 | 41 | pc.printf (" MATCH "); |
| linxinda | 0:f6d212e09078 | 42 | pc.printf ("\r\n"); |
| linxinda | 0:f6d212e09078 | 43 | i=1; |
| linxinda | 0:f6d212e09078 | 44 | break; |
| linxinda | 0:f6d212e09078 | 45 | } |
| linxinda | 0:f6d212e09078 | 46 | else if (serial[i]==touchDetect()){ |
| linxinda | 0:f6d212e09078 | 47 | i++; |
| linxinda | 0:f6d212e09078 | 48 | wait (0.5); |
| linxinda | 0:f6d212e09078 | 49 | } |
| linxinda | 0:f6d212e09078 | 50 | else{ |
| linxinda | 0:f6d212e09078 | 51 | pc.printf (" TOUCH ERROR "); |
| linxinda | 0:f6d212e09078 | 52 | pc.printf ("\r\n"); //If the user touch both sensor, the retun value is 3, which will lead to touch error. |
| linxinda | 0:f6d212e09078 | 53 | break;} //Also, if the user mistouch the sensor, it's also a touch error. |
| linxinda | 0:f6d212e09078 | 54 | } |
| linxinda | 0:f6d212e09078 | 55 | } |
| linxinda | 0:f6d212e09078 | 56 | } |
| linxinda | 0:f6d212e09078 | 57 | |
| linxinda | 0:f6d212e09078 | 58 | void readSerial() |
| linxinda | 0:f6d212e09078 | 59 | { |
| linxinda | 0:f6d212e09078 | 60 | char b; |
| linxinda | 0:f6d212e09078 | 61 | int flag=1,i=0; |
| linxinda | 0:f6d212e09078 | 62 | b=pc.getc(); |
| linxinda | 0:f6d212e09078 | 63 | pc.printf("%c",b); |
| linxinda | 0:f6d212e09078 | 64 | if (b=='S'){ |
| linxinda | 0:f6d212e09078 | 65 | serial[0]='S'; |
| linxinda | 0:f6d212e09078 | 66 | i++; |
| linxinda | 0:f6d212e09078 | 67 | while (flag){ |
| linxinda | 0:f6d212e09078 | 68 | serial[i]=pc.getc(); |
| linxinda | 0:f6d212e09078 | 69 | pc.printf ("%c",serial[i]); |
| linxinda | 0:f6d212e09078 | 70 | if (serial[i]=='E'){ |
| linxinda | 0:f6d212e09078 | 71 | flag=0;} |
| linxinda | 0:f6d212e09078 | 72 | else if (serial[i]=='S'){ |
| linxinda | 0:f6d212e09078 | 73 | pc.printf (" HOST ERROR \r\n "); //Host sent two S before E. Host error. |
| linxinda | 0:f6d212e09078 | 74 | break;} |
| linxinda | 0:f6d212e09078 | 75 | i++; |
| linxinda | 0:f6d212e09078 | 76 | } |
| linxinda | 0:f6d212e09078 | 77 | } |
| linxinda | 0:f6d212e09078 | 78 | } |
| linxinda | 0:f6d212e09078 | 79 | |
| linxinda | 0:f6d212e09078 | 80 | char touchDetect () //Detect the user input |
| linxinda | 0:f6d212e09078 | 81 | { |
| linxinda | 0:f6d212e09078 | 82 | while (1) { |
| linxinda | 0:f6d212e09078 | 83 | if (touchSense1()){ |
| linxinda | 0:f6d212e09078 | 84 | pc.printf (" T1 "); //T1 means sensor1 is pressed |
| linxinda | 0:f6d212e09078 | 85 | if (touchSense2()){ |
| linxinda | 0:f6d212e09078 | 86 | return '3'; |
| linxinda | 0:f6d212e09078 | 87 | } |
| linxinda | 0:f6d212e09078 | 88 | while (touchSense1()){ |
| linxinda | 0:f6d212e09078 | 89 | wait(0.5);} |
| linxinda | 0:f6d212e09078 | 90 | return '1'; |
| linxinda | 0:f6d212e09078 | 91 | } |
| linxinda | 0:f6d212e09078 | 92 | else if (touchSense2()){ |
| linxinda | 0:f6d212e09078 | 93 | pc.printf (" T0 "); |
| linxinda | 0:f6d212e09078 | 94 | if (touchSense1()){ |
| linxinda | 0:f6d212e09078 | 95 | return '3'; |
| linxinda | 0:f6d212e09078 | 96 | } |
| linxinda | 0:f6d212e09078 | 97 | while (touchSense2()){ |
| linxinda | 0:f6d212e09078 | 98 | wait(0.5);} |
| linxinda | 0:f6d212e09078 | 99 | return '0'; |
| linxinda | 0:f6d212e09078 | 100 | |
| linxinda | 0:f6d212e09078 | 101 | } |
| linxinda | 0:f6d212e09078 | 102 | wait (0.05); |
| linxinda | 0:f6d212e09078 | 103 | } |
| linxinda | 0:f6d212e09078 | 104 | } |
| linxinda | 0:f6d212e09078 | 105 | |
| linxinda | 0:f6d212e09078 | 106 | int touchSense1(void) |
| linxinda | 0:f6d212e09078 | 107 | { |
| linxinda | 0:f6d212e09078 | 108 | float sample; |
| linxinda | 0:f6d212e09078 | 109 | ground1 = 0; |
| linxinda | 0:f6d212e09078 | 110 | charger1.mode(PullUp); |
| linxinda | 0:f6d212e09078 | 111 | charger1.mode(PullNone); |
| linxinda | 0:f6d212e09078 | 112 | sample=input1.read(); |
| linxinda | 0:f6d212e09078 | 113 | if (sample < 0.3) { |
| linxinda | 0:f6d212e09078 | 114 | return 1; |
| linxinda | 0:f6d212e09078 | 115 | } else { |
| linxinda | 0:f6d212e09078 | 116 | return 0; |
| linxinda | 0:f6d212e09078 | 117 | } |
| linxinda | 0:f6d212e09078 | 118 | } |
| linxinda | 0:f6d212e09078 | 119 | |
| linxinda | 0:f6d212e09078 | 120 | int touchSense2(void) |
| linxinda | 0:f6d212e09078 | 121 | { |
| linxinda | 0:f6d212e09078 | 122 | float sample; |
| linxinda | 0:f6d212e09078 | 123 | ground2 = 0; |
| linxinda | 0:f6d212e09078 | 124 | charger2.mode(PullUp); |
| linxinda | 0:f6d212e09078 | 125 | charger2.mode(PullNone); |
| linxinda | 0:f6d212e09078 | 126 | sample=input2.read(); |
| linxinda | 0:f6d212e09078 | 127 | if (sample < 0.3) { |
| linxinda | 0:f6d212e09078 | 128 | return 1; |
| linxinda | 0:f6d212e09078 | 129 | } else { |
| linxinda | 0:f6d212e09078 | 130 | return 0; |
| linxinda | 0:f6d212e09078 | 131 | } |
| linxinda | 0:f6d212e09078 | 132 | } |