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_single_program.cpp@0:077b18c8febc, 2010-12-03 (annotated)
- Committer:
- edufascin
- Date:
- Fri Dec 03 06:35:42 2010 +0000
- Revision:
- 0:077b18c8febc
Embedded EE202 HW2
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| edufascin | 0:077b18c8febc | 1 | #include "mbed.h" |
| edufascin | 0:077b18c8febc | 2 | #include "string.h" |
| edufascin | 0:077b18c8febc | 3 | #include "time.h" |
| edufascin | 0:077b18c8febc | 4 | |
| edufascin | 0:077b18c8febc | 5 | #define numsamples 1 |
| edufascin | 0:077b18c8febc | 6 | |
| edufascin | 0:077b18c8febc | 7 | int touchsense1(void); |
| edufascin | 0:077b18c8febc | 8 | int touchsense2(void); |
| edufascin | 0:077b18c8febc | 9 | DigitalOut myLed1(LED1); |
| edufascin | 0:077b18c8febc | 10 | DigitalOut myLed2(LED2); |
| edufascin | 0:077b18c8febc | 11 | DigitalOut myLed3(LED3); |
| edufascin | 0:077b18c8febc | 12 | AnalogIn input1(p20); |
| edufascin | 0:077b18c8febc | 13 | DigitalIn charger1(p19); |
| edufascin | 0:077b18c8febc | 14 | DigitalOut ground1(p18); |
| edufascin | 0:077b18c8febc | 15 | AnalogIn input2(p17); |
| edufascin | 0:077b18c8febc | 16 | DigitalIn charger2(p16); |
| edufascin | 0:077b18c8febc | 17 | DigitalOut ground2(p15); |
| edufascin | 0:077b18c8febc | 18 | Serial pc(USBTX,USBRX); |
| edufascin | 0:077b18c8febc | 19 | |
| edufascin | 0:077b18c8febc | 20 | |
| edufascin | 0:077b18c8febc | 21 | |
| edufascin | 0:077b18c8febc | 22 | //This is called when the T0 capacitor is touched. |
| edufascin | 0:077b18c8febc | 23 | int touchsense1(void) { |
| edufascin | 0:077b18c8febc | 24 | float sample; |
| edufascin | 0:077b18c8febc | 25 | ground1 = 0; |
| edufascin | 0:077b18c8febc | 26 | charger1.mode(PullUp); //p19 used for charging the capacitor. |
| edufascin | 0:077b18c8febc | 27 | charger1.mode(PullNone); |
| edufascin | 0:077b18c8febc | 28 | sample = input1.read(); |
| edufascin | 0:077b18c8febc | 29 | if (sample < 0.3) { |
| edufascin | 0:077b18c8febc | 30 | return 1; |
| edufascin | 0:077b18c8febc | 31 | } else { |
| edufascin | 0:077b18c8febc | 32 | return 0; |
| edufascin | 0:077b18c8febc | 33 | } |
| edufascin | 0:077b18c8febc | 34 | } |
| edufascin | 0:077b18c8febc | 35 | |
| edufascin | 0:077b18c8febc | 36 | //This function is called when the T1 capacitor is touched. |
| edufascin | 0:077b18c8febc | 37 | |
| edufascin | 0:077b18c8febc | 38 | int touchsense2(void) { |
| edufascin | 0:077b18c8febc | 39 | float sample; |
| edufascin | 0:077b18c8febc | 40 | ground2 = 0; |
| edufascin | 0:077b18c8febc | 41 | charger2.mode(PullUp); |
| edufascin | 0:077b18c8febc | 42 | charger2.mode(PullNone); |
| edufascin | 0:077b18c8febc | 43 | sample = input2.read(); |
| edufascin | 0:077b18c8febc | 44 | if (sample < 0.4) { |
| edufascin | 0:077b18c8febc | 45 | return 1; |
| edufascin | 0:077b18c8febc | 46 | } else { |
| edufascin | 0:077b18c8febc | 47 | return 0; |
| edufascin | 0:077b18c8febc | 48 | } |
| edufascin | 0:077b18c8febc | 49 | } |
| edufascin | 0:077b18c8febc | 50 | |
| edufascin | 0:077b18c8febc | 51 | |
| edufascin | 0:077b18c8febc | 52 | |
| edufascin | 0:077b18c8febc | 53 | |
| edufascin | 0:077b18c8febc | 54 | int main() { |
| edufascin | 0:077b18c8febc | 55 | //Getting the trigger string from the user |
| edufascin | 0:077b18c8febc | 56 | /* Module-1 */ |
| edufascin | 0:077b18c8febc | 57 | char triggerString1[1]; |
| edufascin | 0:077b18c8febc | 58 | char triggerString2[1]; |
| edufascin | 0:077b18c8febc | 59 | int index1=0,index2 = 0; |
| edufascin | 0:077b18c8febc | 60 | char realTriggerString[20]; |
| edufascin | 0:077b18c8febc | 61 | //char finalTriggerString[20]; |
| edufascin | 0:077b18c8febc | 62 | int count=0,i = 0; |
| edufascin | 0:077b18c8febc | 63 | bool isSensor1Pressed = false; |
| edufascin | 0:077b18c8febc | 64 | bool isSensor2Pressed = false; |
| edufascin | 0:077b18c8febc | 65 | char inputChar; |
| edufascin | 0:077b18c8febc | 66 | |
| edufascin | 0:077b18c8febc | 67 | int triggerStringlength; |
| edufascin | 0:077b18c8febc | 68 | bool isStartEncountered = false; |
| edufascin | 0:077b18c8febc | 69 | bool bitsEncountered = false; |
| edufascin | 0:077b18c8febc | 70 | |
| edufascin | 0:077b18c8febc | 71 | int countBits = 0; |
| edufascin | 0:077b18c8febc | 72 | int k = 0; |
| edufascin | 0:077b18c8febc | 73 | //The user is allowed to enter the trigger string. |
| edufascin | 0:077b18c8febc | 74 | a: |
| edufascin | 0:077b18c8febc | 75 | char triggerString[30];char triggerStringBits[20]; count = 0; |
| edufascin | 0:077b18c8febc | 76 | pc.printf("Enter the Trigger String: "); |
| edufascin | 0:077b18c8febc | 77 | |
| edufascin | 0:077b18c8febc | 78 | while (1) { |
| edufascin | 0:077b18c8febc | 79 | pc.putc(inputChar = pc.getc()); |
| edufascin | 0:077b18c8febc | 80 | if (inputChar == 'e' || inputChar == 'E') { |
| edufascin | 0:077b18c8febc | 81 | triggerString[k] = inputChar; |
| edufascin | 0:077b18c8febc | 82 | k++; |
| edufascin | 0:077b18c8febc | 83 | break; |
| edufascin | 0:077b18c8febc | 84 | } else { |
| edufascin | 0:077b18c8febc | 85 | triggerString[k] = inputChar; |
| edufascin | 0:077b18c8febc | 86 | k++; |
| edufascin | 0:077b18c8febc | 87 | } |
| edufascin | 0:077b18c8febc | 88 | } |
| edufascin | 0:077b18c8febc | 89 | //All the entered characters from the user are strored in the array. |
| edufascin | 0:077b18c8febc | 90 | triggerString[k] = '\0'; |
| edufascin | 0:077b18c8febc | 91 | //Getting the total length of the trigger string. |
| edufascin | 0:077b18c8febc | 92 | while (triggerString[count] != '\0') { |
| edufascin | 0:077b18c8febc | 93 | count++; |
| edufascin | 0:077b18c8febc | 94 | } |
| edufascin | 0:077b18c8febc | 95 | triggerStringlength = count; |
| edufascin | 0:077b18c8febc | 96 | //Checking for the validity of the input validation string. |
| edufascin | 0:077b18c8febc | 97 | for (int i = 0 ;i<triggerStringlength; i++) |
| edufascin | 0:077b18c8febc | 98 | { |
| edufascin | 0:077b18c8febc | 99 | if(triggerString[0] != 's' && triggerString[0] != 'S') |
| edufascin | 0:077b18c8febc | 100 | { |
| edufascin | 0:077b18c8febc | 101 | pc.printf("\n\nInvalid Trigger String 1\n"); |
| edufascin | 0:077b18c8febc | 102 | for(i=0;i<triggerStringlength;i++) |
| edufascin | 0:077b18c8febc | 103 | { |
| edufascin | 0:077b18c8febc | 104 | triggerString[i]='\0'; |
| edufascin | 0:077b18c8febc | 105 | } |
| edufascin | 0:077b18c8febc | 106 | k=0; |
| edufascin | 0:077b18c8febc | 107 | goto a; |
| edufascin | 0:077b18c8febc | 108 | } |
| edufascin | 0:077b18c8febc | 109 | |
| edufascin | 0:077b18c8febc | 110 | for(int j=1;j<triggerStringlength-2;j++) |
| edufascin | 0:077b18c8febc | 111 | { |
| edufascin | 0:077b18c8febc | 112 | if(triggerString[j] == '0' || triggerString[j] == '1' || triggerString[j] == ' ') |
| edufascin | 0:077b18c8febc | 113 | { |
| edufascin | 0:077b18c8febc | 114 | continue; |
| edufascin | 0:077b18c8febc | 115 | } |
| edufascin | 0:077b18c8febc | 116 | else |
| edufascin | 0:077b18c8febc | 117 | { |
| edufascin | 0:077b18c8febc | 118 | pc.printf("\n\nInvalid Trigger String2\n"); |
| edufascin | 0:077b18c8febc | 119 | for(i=0;i<triggerStringlength;i++) |
| edufascin | 0:077b18c8febc | 120 | { |
| edufascin | 0:077b18c8febc | 121 | triggerString[i]='\0'; |
| edufascin | 0:077b18c8febc | 122 | } |
| edufascin | 0:077b18c8febc | 123 | k=0; |
| edufascin | 0:077b18c8febc | 124 | goto a; |
| edufascin | 0:077b18c8febc | 125 | } |
| edufascin | 0:077b18c8febc | 126 | } |
| edufascin | 0:077b18c8febc | 127 | |
| edufascin | 0:077b18c8febc | 128 | |
| edufascin | 0:077b18c8febc | 129 | |
| edufascin | 0:077b18c8febc | 130 | //If the input string contains spaces, simply continue. |
| edufascin | 0:077b18c8febc | 131 | if (triggerString[i] == ' ') { |
| edufascin | 0:077b18c8febc | 132 | continue; |
| edufascin | 0:077b18c8febc | 133 | } |
| edufascin | 0:077b18c8febc | 134 | |
| edufascin | 0:077b18c8febc | 135 | if (triggerString[i] == 'S' || triggerString[i] == 's') { |
| edufascin | 0:077b18c8febc | 136 | isStartEncountered = true; |
| edufascin | 0:077b18c8febc | 137 | continue; |
| edufascin | 0:077b18c8febc | 138 | } |
| edufascin | 0:077b18c8febc | 139 | |
| edufascin | 0:077b18c8febc | 140 | if (isStartEncountered && (triggerString[i]!= 'E' || triggerString[i] != 'e')) { |
| edufascin | 0:077b18c8febc | 141 | if (triggerString[i] == ' ') { |
| edufascin | 0:077b18c8febc | 142 | continue; |
| edufascin | 0:077b18c8febc | 143 | } else { |
| edufascin | 0:077b18c8febc | 144 | if (triggerString[i] == '0' || triggerString[i] == '1') { |
| edufascin | 0:077b18c8febc | 145 | triggerStringBits[countBits] = triggerString[i]; |
| edufascin | 0:077b18c8febc | 146 | bitsEncountered = true; |
| edufascin | 0:077b18c8febc | 147 | countBits++; |
| edufascin | 0:077b18c8febc | 148 | } |
| edufascin | 0:077b18c8febc | 149 | } |
| edufascin | 0:077b18c8febc | 150 | } else { |
| edufascin | 0:077b18c8febc | 151 | //If the user enters E character first without enterting the S character then the user has entered and invalid trigger string. |
| edufascin | 0:077b18c8febc | 152 | if (isStartEncountered && !bitsEncountered && (triggerString[i] == 'E' || triggerString[i] == 'e')) { |
| edufascin | 0:077b18c8febc | 153 | pc.printf("Please enter a valid Trigger String"); |
| edufascin | 0:077b18c8febc | 154 | } else { |
| edufascin | 0:077b18c8febc | 155 | if (isStartEncountered && (triggerString[i] == 'E' || triggerString[i] == 'e')) { |
| edufascin | 0:077b18c8febc | 156 | break; |
| edufascin | 0:077b18c8febc | 157 | } |
| edufascin | 0:077b18c8febc | 158 | } |
| edufascin | 0:077b18c8febc | 159 | } |
| edufascin | 0:077b18c8febc | 160 | } |
| edufascin | 0:077b18c8febc | 161 | //Ending the triggerString Bit array. |
| edufascin | 0:077b18c8febc | 162 | triggerStringBits[countBits] = '\0'; |
| edufascin | 0:077b18c8febc | 163 | pc.printf("\n\nPlease input on sensor:"); |
| edufascin | 0:077b18c8febc | 164 | /* Module -2 Verifying the trigger string with the input from the user */ |
| edufascin | 0:077b18c8febc | 165 | //Getting the touch sensor inputs from the user and verifying it with the host string. |
| edufascin | 0:077b18c8febc | 166 | count =0; |
| edufascin | 0:077b18c8febc | 167 | int bufferSeconds = 10; |
| edufascin | 0:077b18c8febc | 168 | clock_t endwait; |
| edufascin | 0:077b18c8febc | 169 | clock_t storeendwait; |
| edufascin | 0:077b18c8febc | 170 | endwait = clock() + bufferSeconds * CLOCKS_PER_SEC ; |
| edufascin | 0:077b18c8febc | 171 | storeendwait = endwait; |
| edufascin | 0:077b18c8febc | 172 | |
| edufascin | 0:077b18c8febc | 173 | while (1) { |
| edufascin | 0:077b18c8febc | 174 | if (touchsense2()) { |
| edufascin | 0:077b18c8febc | 175 | triggerString2[index2] = '1'; |
| edufascin | 0:077b18c8febc | 176 | myLed2 = 1; |
| edufascin | 0:077b18c8febc | 177 | wait(0.01); |
| edufascin | 0:077b18c8febc | 178 | isSensor2Pressed = true; |
| edufascin | 0:077b18c8febc | 179 | bufferSeconds = 3; |
| edufascin | 0:077b18c8febc | 180 | //Increasing the endwait time with every touch. |
| edufascin | 0:077b18c8febc | 181 | endwait = storeendwait + bufferSeconds * CLOCKS_PER_SEC; |
| edufascin | 0:077b18c8febc | 182 | } else { |
| edufascin | 0:077b18c8febc | 183 | if (isSensor2Pressed) { |
| edufascin | 0:077b18c8febc | 184 | realTriggerString[count] = triggerString2[0]; |
| edufascin | 0:077b18c8febc | 185 | count++; |
| edufascin | 0:077b18c8febc | 186 | } |
| edufascin | 0:077b18c8febc | 187 | myLed2 = 0; |
| edufascin | 0:077b18c8febc | 188 | for (i=0;i<strlen(triggerString2);i++) { |
| edufascin | 0:077b18c8febc | 189 | triggerString2[i] = '\0'; |
| edufascin | 0:077b18c8febc | 190 | } |
| edufascin | 0:077b18c8febc | 191 | isSensor2Pressed = false; |
| edufascin | 0:077b18c8febc | 192 | } |
| edufascin | 0:077b18c8febc | 193 | |
| edufascin | 0:077b18c8febc | 194 | if (touchsense1()) { |
| edufascin | 0:077b18c8febc | 195 | triggerString1[index1] = '0'; |
| edufascin | 0:077b18c8febc | 196 | myLed1 = 1; |
| edufascin | 0:077b18c8febc | 197 | wait(0.05); |
| edufascin | 0:077b18c8febc | 198 | isSensor1Pressed = true; |
| edufascin | 0:077b18c8febc | 199 | bufferSeconds = 3 ; |
| edufascin | 0:077b18c8febc | 200 | endwait = storeendwait + bufferSeconds * CLOCKS_PER_SEC; |
| edufascin | 0:077b18c8febc | 201 | } else { |
| edufascin | 0:077b18c8febc | 202 | if (isSensor1Pressed) { |
| edufascin | 0:077b18c8febc | 203 | realTriggerString[count] = triggerString1[0]; |
| edufascin | 0:077b18c8febc | 204 | count++; |
| edufascin | 0:077b18c8febc | 205 | } |
| edufascin | 0:077b18c8febc | 206 | myLed1 = 0; |
| edufascin | 0:077b18c8febc | 207 | for (i=0;i<strlen(triggerString1);i++) { |
| edufascin | 0:077b18c8febc | 208 | triggerString1[i] = '\0'; |
| edufascin | 0:077b18c8febc | 209 | } |
| edufascin | 0:077b18c8febc | 210 | isSensor1Pressed = false; |
| edufascin | 0:077b18c8febc | 211 | } |
| edufascin | 0:077b18c8febc | 212 | if (clock() > endwait) { |
| edufascin | 0:077b18c8febc | 213 | break; |
| edufascin | 0:077b18c8febc | 214 | } |
| edufascin | 0:077b18c8febc | 215 | } |
| edufascin | 0:077b18c8febc | 216 | //Marking the end of the trigger string captured through the sensors from the users. This value needs to be matched to the trigger string entered from the user through pc. |
| edufascin | 0:077b18c8febc | 217 | realTriggerString[count] = '\0'; |
| edufascin | 0:077b18c8febc | 218 | int matchCount = 0; |
| edufascin | 0:077b18c8febc | 219 | //Only if the trigger string length equals the length of the string entered by the user, then only the validation for each character should take place. |
| edufascin | 0:077b18c8febc | 220 | if (strlen(realTriggerString) == strlen(triggerStringBits)) { |
| edufascin | 0:077b18c8febc | 221 | for (i=0;i<strlen(realTriggerString);i++) { |
| edufascin | 0:077b18c8febc | 222 | if (realTriggerString[i] == triggerStringBits[i]) { |
| edufascin | 0:077b18c8febc | 223 | matchCount++; |
| edufascin | 0:077b18c8febc | 224 | } |
| edufascin | 0:077b18c8febc | 225 | //if (realTriggerString[i]== '0' || realTriggerString[i] == '1') |
| edufascin | 0:077b18c8febc | 226 | // pc.printf("%c",realTriggerString[i]); |
| edufascin | 0:077b18c8febc | 227 | } |
| edufascin | 0:077b18c8febc | 228 | if (matchCount == strlen(realTriggerString)) { |
| edufascin | 0:077b18c8febc | 229 | pc.printf("HOST MATCH\n"); |
| edufascin | 0:077b18c8febc | 230 | } |
| edufascin | 0:077b18c8febc | 231 | } else { |
| edufascin | 0:077b18c8febc | 232 | pc.printf("HOST ERROR or TOUCH ERROR\n"); |
| edufascin | 0:077b18c8febc | 233 | } |
| edufascin | 0:077b18c8febc | 234 | } |
| edufascin | 0:077b18c8febc | 235 |