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:3221a95c5f82, 2010-12-01 (annotated)
- Committer:
- StevenHe
- Date:
- Wed Dec 01 04:37:12 2010 +0000
- Revision:
- 0:3221a95c5f82
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| StevenHe | 0:3221a95c5f82 | 1 | #include "mbed.h" |
| StevenHe | 0:3221a95c5f82 | 2 | |
| StevenHe | 0:3221a95c5f82 | 3 | #define numsamples 1 |
| StevenHe | 0:3221a95c5f82 | 4 | #define sizeofpattern 30 |
| StevenHe | 0:3221a95c5f82 | 5 | #define S_idle 0 |
| StevenHe | 0:3221a95c5f82 | 6 | #define S_HostInput 1 |
| StevenHe | 0:3221a95c5f82 | 7 | #define S_UserInput 2 |
| StevenHe | 0:3221a95c5f82 | 8 | #define S_HostError 3 |
| StevenHe | 0:3221a95c5f82 | 9 | #define S_UserError 4 |
| StevenHe | 0:3221a95c5f82 | 10 | #define S_Match 5 |
| StevenHe | 0:3221a95c5f82 | 11 | |
| StevenHe | 0:3221a95c5f82 | 12 | // function declartion |
| StevenHe | 0:3221a95c5f82 | 13 | int touchSense(int Num_Charger); |
| StevenHe | 0:3221a95c5f82 | 14 | void displayString(int* String); |
| StevenHe | 0:3221a95c5f82 | 15 | void displayMessage(int NumMes); |
| StevenHe | 0:3221a95c5f82 | 16 | |
| StevenHe | 0:3221a95c5f82 | 17 | // I/O declarations |
| StevenHe | 0:3221a95c5f82 | 18 | DigitalOut myled(LED1); |
| StevenHe | 0:3221a95c5f82 | 19 | AnalogIn input1(p20), input0(p17); |
| StevenHe | 0:3221a95c5f82 | 20 | DigitalIn charger1(p19), charger0(p16); |
| StevenHe | 0:3221a95c5f82 | 21 | DigitalOut ground(p18); |
| StevenHe | 0:3221a95c5f82 | 22 | Serial pc(USBTX, USBRX); // tx, rx |
| StevenHe | 0:3221a95c5f82 | 23 | |
| StevenHe | 0:3221a95c5f82 | 24 | |
| StevenHe | 0:3221a95c5f82 | 25 | // variable definitions |
| StevenHe | 0:3221a95c5f82 | 26 | int hostString[sizeofpattern]; |
| StevenHe | 0:3221a95c5f82 | 27 | int currentState = S_idle; |
| StevenHe | 0:3221a95c5f82 | 28 | int nextState = S_idle; |
| StevenHe | 0:3221a95c5f82 | 29 | |
| StevenHe | 0:3221a95c5f82 | 30 | |
| StevenHe | 0:3221a95c5f82 | 31 | int main() { |
| StevenHe | 0:3221a95c5f82 | 32 | int i=0, j=0; |
| StevenHe | 0:3221a95c5f82 | 33 | int token; |
| StevenHe | 0:3221a95c5f82 | 34 | |
| StevenHe | 0:3221a95c5f82 | 35 | while (1) { |
| StevenHe | 0:3221a95c5f82 | 36 | token = 'n'; // n = no input |
| StevenHe | 0:3221a95c5f82 | 37 | |
| StevenHe | 0:3221a95c5f82 | 38 | // accept input token in either touchsense or host input form |
| StevenHe | 0:3221a95c5f82 | 39 | if (pc.readable()) { |
| StevenHe | 0:3221a95c5f82 | 40 | token = pc.getc(); // getc returns int representation of letters and digits |
| StevenHe | 0:3221a95c5f82 | 41 | pc.putc(token); |
| StevenHe | 0:3221a95c5f82 | 42 | } else if (touchSense(0)) { |
| StevenHe | 0:3221a95c5f82 | 43 | myled = 1; |
| StevenHe | 0:3221a95c5f82 | 44 | token = '0'; |
| StevenHe | 0:3221a95c5f82 | 45 | pc.putc('0'); |
| StevenHe | 0:3221a95c5f82 | 46 | } else if (touchSense(1)) { |
| StevenHe | 0:3221a95c5f82 | 47 | myled =1; |
| StevenHe | 0:3221a95c5f82 | 48 | token = '1'; |
| StevenHe | 0:3221a95c5f82 | 49 | pc.putc('1'); |
| StevenHe | 0:3221a95c5f82 | 50 | } else myled = 0; |
| StevenHe | 0:3221a95c5f82 | 51 | |
| StevenHe | 0:3221a95c5f82 | 52 | |
| StevenHe | 0:3221a95c5f82 | 53 | // debugging output |
| StevenHe | 0:3221a95c5f82 | 54 | if (token=='m') { |
| StevenHe | 0:3221a95c5f82 | 55 | printf("current token is %d \n", token); |
| StevenHe | 0:3221a95c5f82 | 56 | printf("current j and jth element are %d %d \n", j, hostString[j]); |
| StevenHe | 0:3221a95c5f82 | 57 | printf("The string received from host is"); |
| StevenHe | 0:3221a95c5f82 | 58 | displayString(hostString); |
| StevenHe | 0:3221a95c5f82 | 59 | printf("current state and next state are %d %d \n", currentState, nextState); |
| StevenHe | 0:3221a95c5f82 | 60 | } |
| StevenHe | 0:3221a95c5f82 | 61 | |
| StevenHe | 0:3221a95c5f82 | 62 | |
| StevenHe | 0:3221a95c5f82 | 63 | if (token == 'n' ) continue; // if no input, skip this cycle |
| StevenHe | 0:3221a95c5f82 | 64 | |
| StevenHe | 0:3221a95c5f82 | 65 | |
| StevenHe | 0:3221a95c5f82 | 66 | // deciding what next state is and generates output |
| StevenHe | 0:3221a95c5f82 | 67 | if (currentState == S_idle) { |
| StevenHe | 0:3221a95c5f82 | 68 | if (token == 'S' ) nextState = S_HostInput; |
| StevenHe | 0:3221a95c5f82 | 69 | else { |
| StevenHe | 0:3221a95c5f82 | 70 | nextState = currentState; |
| StevenHe | 0:3221a95c5f82 | 71 | displayMessage(1); |
| StevenHe | 0:3221a95c5f82 | 72 | } |
| StevenHe | 0:3221a95c5f82 | 73 | } |
| StevenHe | 0:3221a95c5f82 | 74 | |
| StevenHe | 0:3221a95c5f82 | 75 | |
| StevenHe | 0:3221a95c5f82 | 76 | else if (currentState == S_HostInput) { |
| StevenHe | 0:3221a95c5f82 | 77 | if ((token == '0' || token == '1') && i < sizeofpattern) { |
| StevenHe | 0:3221a95c5f82 | 78 | hostString[i] = token; |
| StevenHe | 0:3221a95c5f82 | 79 | i++; |
| StevenHe | 0:3221a95c5f82 | 80 | nextState = currentState; // remain in the same state |
| StevenHe | 0:3221a95c5f82 | 81 | } |
| StevenHe | 0:3221a95c5f82 | 82 | // |
| StevenHe | 0:3221a95c5f82 | 83 | else if (token == 'm') nextState = currentState; |
| StevenHe | 0:3221a95c5f82 | 84 | // |
| StevenHe | 0:3221a95c5f82 | 85 | else if (token == 'E') nextState = S_UserInput; |
| StevenHe | 0:3221a95c5f82 | 86 | else { // S_HostError |
| StevenHe | 0:3221a95c5f82 | 87 | displayMessage(1); |
| StevenHe | 0:3221a95c5f82 | 88 | nextState = S_idle; |
| StevenHe | 0:3221a95c5f82 | 89 | i = 0; |
| StevenHe | 0:3221a95c5f82 | 90 | } |
| StevenHe | 0:3221a95c5f82 | 91 | } |
| StevenHe | 0:3221a95c5f82 | 92 | |
| StevenHe | 0:3221a95c5f82 | 93 | else if (currentState == S_UserInput) { |
| StevenHe | 0:3221a95c5f82 | 94 | |
| StevenHe | 0:3221a95c5f82 | 95 | if (hostString[j] == token) { |
| StevenHe | 0:3221a95c5f82 | 96 | nextState = currentState; |
| StevenHe | 0:3221a95c5f82 | 97 | j++; |
| StevenHe | 0:3221a95c5f82 | 98 | if (j==i) { |
| StevenHe | 0:3221a95c5f82 | 99 | displayMessage(3); // S_Match |
| StevenHe | 0:3221a95c5f82 | 100 | nextState = S_idle; |
| StevenHe | 0:3221a95c5f82 | 101 | j =0; |
| StevenHe | 0:3221a95c5f82 | 102 | i=0; |
| StevenHe | 0:3221a95c5f82 | 103 | } |
| StevenHe | 0:3221a95c5f82 | 104 | } |
| StevenHe | 0:3221a95c5f82 | 105 | |
| StevenHe | 0:3221a95c5f82 | 106 | // |
| StevenHe | 0:3221a95c5f82 | 107 | else if (token == 'm') nextState = currentState; |
| StevenHe | 0:3221a95c5f82 | 108 | // |
| StevenHe | 0:3221a95c5f82 | 109 | else { // S_UserError |
| StevenHe | 0:3221a95c5f82 | 110 | displayMessage(0); |
| StevenHe | 0:3221a95c5f82 | 111 | nextState = S_UserInput; |
| StevenHe | 0:3221a95c5f82 | 112 | j = 0; |
| StevenHe | 0:3221a95c5f82 | 113 | } |
| StevenHe | 0:3221a95c5f82 | 114 | } |
| StevenHe | 0:3221a95c5f82 | 115 | |
| StevenHe | 0:3221a95c5f82 | 116 | /* |
| StevenHe | 0:3221a95c5f82 | 117 | else if (currentState == S_Match) { |
| StevenHe | 0:3221a95c5f82 | 118 | displayMessage(3); |
| StevenHe | 0:3221a95c5f82 | 119 | nextState = S_idle; |
| StevenHe | 0:3221a95c5f82 | 120 | j =0; |
| StevenHe | 0:3221a95c5f82 | 121 | i=0; |
| StevenHe | 0:3221a95c5f82 | 122 | } |
| StevenHe | 0:3221a95c5f82 | 123 | |
| StevenHe | 0:3221a95c5f82 | 124 | |
| StevenHe | 0:3221a95c5f82 | 125 | else if (currentState == S_HostError) { |
| StevenHe | 0:3221a95c5f82 | 126 | displayMessage(1); |
| StevenHe | 0:3221a95c5f82 | 127 | nextState = S_idle; |
| StevenHe | 0:3221a95c5f82 | 128 | i = 0; |
| StevenHe | 0:3221a95c5f82 | 129 | } |
| StevenHe | 0:3221a95c5f82 | 130 | |
| StevenHe | 0:3221a95c5f82 | 131 | |
| StevenHe | 0:3221a95c5f82 | 132 | else if (currentState == S_UserError) { |
| StevenHe | 0:3221a95c5f82 | 133 | displayMessage(0); |
| StevenHe | 0:3221a95c5f82 | 134 | nextState = S_UserInput; |
| StevenHe | 0:3221a95c5f82 | 135 | j = 0; |
| StevenHe | 0:3221a95c5f82 | 136 | } |
| StevenHe | 0:3221a95c5f82 | 137 | */ |
| StevenHe | 0:3221a95c5f82 | 138 | |
| StevenHe | 0:3221a95c5f82 | 139 | |
| StevenHe | 0:3221a95c5f82 | 140 | |
| StevenHe | 0:3221a95c5f82 | 141 | |
| StevenHe | 0:3221a95c5f82 | 142 | |
| StevenHe | 0:3221a95c5f82 | 143 | // state transition |
| StevenHe | 0:3221a95c5f82 | 144 | currentState = nextState; |
| StevenHe | 0:3221a95c5f82 | 145 | |
| StevenHe | 0:3221a95c5f82 | 146 | }// end of while |
| StevenHe | 0:3221a95c5f82 | 147 | |
| StevenHe | 0:3221a95c5f82 | 148 | } // end of main |
| StevenHe | 0:3221a95c5f82 | 149 | |
| StevenHe | 0:3221a95c5f82 | 150 | |
| StevenHe | 0:3221a95c5f82 | 151 | int touchSense(int Num_Charger) { |
| StevenHe | 0:3221a95c5f82 | 152 | float sample1, sample2; |
| StevenHe | 0:3221a95c5f82 | 153 | ground = 0; |
| StevenHe | 0:3221a95c5f82 | 154 | if (Num_Charger ==1) { |
| StevenHe | 0:3221a95c5f82 | 155 | charger1.mode(PullUp); |
| StevenHe | 0:3221a95c5f82 | 156 | charger1.mode(PullNone); |
| StevenHe | 0:3221a95c5f82 | 157 | sample1=input1.read(); // take two samples to ignore temp jump |
| StevenHe | 0:3221a95c5f82 | 158 | wait(0.2); |
| StevenHe | 0:3221a95c5f82 | 159 | sample2=input1.read(); |
| StevenHe | 0:3221a95c5f82 | 160 | } else { |
| StevenHe | 0:3221a95c5f82 | 161 | charger0.mode(PullUp); |
| StevenHe | 0:3221a95c5f82 | 162 | charger0.mode(PullNone); |
| StevenHe | 0:3221a95c5f82 | 163 | sample1=input0.read(); // take two samples to ignore temp jump |
| StevenHe | 0:3221a95c5f82 | 164 | wait(0.2); |
| StevenHe | 0:3221a95c5f82 | 165 | sample2=input0.read(); |
| StevenHe | 0:3221a95c5f82 | 166 | } |
| StevenHe | 0:3221a95c5f82 | 167 | |
| StevenHe | 0:3221a95c5f82 | 168 | if (sample1 < 0.3 && sample2 < 0.3) { |
| StevenHe | 0:3221a95c5f82 | 169 | return 1; |
| StevenHe | 0:3221a95c5f82 | 170 | } else { |
| StevenHe | 0:3221a95c5f82 | 171 | return 0; |
| StevenHe | 0:3221a95c5f82 | 172 | } |
| StevenHe | 0:3221a95c5f82 | 173 | } |
| StevenHe | 0:3221a95c5f82 | 174 | |
| StevenHe | 0:3221a95c5f82 | 175 | // debugging output |
| StevenHe | 0:3221a95c5f82 | 176 | void displayString(int* String) { |
| StevenHe | 0:3221a95c5f82 | 177 | for (int j=0; j<sizeofpattern; j++) |
| StevenHe | 0:3221a95c5f82 | 178 | pc.putc(String[j]); |
| StevenHe | 0:3221a95c5f82 | 179 | |
| StevenHe | 0:3221a95c5f82 | 180 | pc.putc('\n'); |
| StevenHe | 0:3221a95c5f82 | 181 | |
| StevenHe | 0:3221a95c5f82 | 182 | } |
| StevenHe | 0:3221a95c5f82 | 183 | |
| StevenHe | 0:3221a95c5f82 | 184 | void displayMessage(int NumMes) { |
| StevenHe | 0:3221a95c5f82 | 185 | if (NumMes == 1) |
| StevenHe | 0:3221a95c5f82 | 186 | printf("HOST ERROR : Host must send string in the form of Sxxxx...E X=0 or 1 \n"); |
| StevenHe | 0:3221a95c5f82 | 187 | else if (NumMes == 0) printf("TOUCH ERROR: Please enter the string that matched host string exactly through touchsensor\n"); |
| StevenHe | 0:3221a95c5f82 | 188 | else printf("MATCH: Host will send another string\n"); |
| StevenHe | 0:3221a95c5f82 | 189 | |
| StevenHe | 0:3221a95c5f82 | 190 | } |