
4180 Final Project
Dependencies: 4DGL-uLCD-SE mbed Servo
main.cpp@9:11a6f6c8c54b, 2016-04-18 (annotated)
- Committer:
- pshabbaki3
- Date:
- Mon Apr 18 22:35:53 2016 +0000
- Revision:
- 9:11a6f6c8c54b
- Parent:
- 8:2344ae984a4c
- Child:
- 10:cf127bd9b857
Set vector ans to vector password in checkResult function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pshabbaki3 | 0:5d35ff3dc9a5 | 1 | #include "mbed.h" |
ecarrick | 6:50a82641d07b | 2 | #include "wifi.h" |
pshabbaki3 | 0:5d35ff3dc9a5 | 3 | #include <string> |
pshabbaki3 | 0:5d35ff3dc9a5 | 4 | #include <vector> |
pshabbaki3 | 0:5d35ff3dc9a5 | 5 | #include <iostream> |
pshabbaki3 | 0:5d35ff3dc9a5 | 6 | #include <sstream> |
pshabbaki3 | 0:5d35ff3dc9a5 | 7 | #include <iterator> |
pshabbaki3 | 0:5d35ff3dc9a5 | 8 | #include <stdio.h> |
pshabbaki3 | 0:5d35ff3dc9a5 | 9 | #include <ctype.h> |
pshabbaki3 | 0:5d35ff3dc9a5 | 10 | #include "uLCD_4DGL.h" |
pshabbaki3 | 0:5d35ff3dc9a5 | 11 | |
mbedTrent | 7:52b51f507ee5 | 12 | #include <list> |
mbedTrent | 7:52b51f507ee5 | 13 | #include <mpr121.h> |
mbedTrent | 7:52b51f507ee5 | 14 | |
ecarrick | 1:dd048edb2716 | 15 | LocalFileSystem local("local"); |
pshabbaki3 | 0:5d35ff3dc9a5 | 16 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 17 | DigitalOut myled(LED1); |
pshabbaki3 | 0:5d35ff3dc9a5 | 18 | Serial pc(USBTX,USBRX); |
pshabbaki3 | 0:5d35ff3dc9a5 | 19 | uLCD_4DGL lcd(p28,p27,p30); // serial tx, serial rx, reset pin; |
pshabbaki3 | 0:5d35ff3dc9a5 | 20 | |
mbedTrent | 7:52b51f507ee5 | 21 | // Create the interrupt receiver object on pin 26 |
mbedTrent | 7:52b51f507ee5 | 22 | InterruptIn interrupt(p26); |
mbedTrent | 7:52b51f507ee5 | 23 | |
mbedTrent | 7:52b51f507ee5 | 24 | // Setup the i2c bus on pins 28 and 27 |
mbedTrent | 7:52b51f507ee5 | 25 | I2C i2c(p9, p10); |
mbedTrent | 7:52b51f507ee5 | 26 | |
mbedTrent | 7:52b51f507ee5 | 27 | // Setup the Mpr121: |
mbedTrent | 7:52b51f507ee5 | 28 | // constructor(i2c object, i2c address of the mpr121) |
mbedTrent | 7:52b51f507ee5 | 29 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
mbedTrent | 7:52b51f507ee5 | 30 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 31 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 32 | vector<char> im; |
pshabbaki3 | 4:e6b198a22216 | 33 | vector<char> pass; |
pshabbaki3 | 0:5d35ff3dc9a5 | 34 | vector<char> name; |
pshabbaki3 | 0:5d35ff3dc9a5 | 35 | vector<char> final; |
pshabbaki3 | 0:5d35ff3dc9a5 | 36 | |
mbedTrent | 7:52b51f507ee5 | 37 | size_t pwSize = 6; //size of password |
mbedTrent | 7:52b51f507ee5 | 38 | vector<char> password(pwSize); |
mbedTrent | 7:52b51f507ee5 | 39 | int index=0; |
mbedTrent | 7:52b51f507ee5 | 40 | |
pshabbaki3 | 9:11a6f6c8c54b | 41 | void fallInterrupt() |
pshabbaki3 | 9:11a6f6c8c54b | 42 | { |
mbedTrent | 7:52b51f507ee5 | 43 | int key_code=0; |
mbedTrent | 7:52b51f507ee5 | 44 | int i=0; |
mbedTrent | 7:52b51f507ee5 | 45 | int k=0; |
mbedTrent | 7:52b51f507ee5 | 46 | int value=mpr121.read(0x00); |
mbedTrent | 7:52b51f507ee5 | 47 | value +=mpr121.read(0x01)<<8; |
mbedTrent | 7:52b51f507ee5 | 48 | // LED demo mod by J. Hamblen |
mbedTrent | 7:52b51f507ee5 | 49 | //pc.printf("MPR value: %x \r\n", value); |
mbedTrent | 7:52b51f507ee5 | 50 | // puts key number out to LEDs for demo |
mbedTrent | 7:52b51f507ee5 | 51 | for (i=0; i<=11; i++) { |
pshabbaki3 | 9:11a6f6c8c54b | 52 | if ((((value>>i)&0x01)==1)&&(i>9)) { |
mbedTrent | 7:52b51f507ee5 | 53 | pc.printf("You pressed %d, please enter a value from 0 to 9.\n", i); |
pshabbaki3 | 9:11a6f6c8c54b | 54 | } else if (((value>>i)&0x01)==1) { |
mbedTrent | 7:52b51f507ee5 | 55 | |
pshabbaki3 | 9:11a6f6c8c54b | 56 | if (index < 5) { |
mbedTrent | 7:52b51f507ee5 | 57 | key_code=i; |
mbedTrent | 7:52b51f507ee5 | 58 | //pc.printf("%d in password[%d]\n", key_code, index-1); |
mbedTrent | 7:52b51f507ee5 | 59 | pc.printf("index: %d; ", index); |
mbedTrent | 7:52b51f507ee5 | 60 | password[index] = (char)key_code; |
mbedTrent | 7:52b51f507ee5 | 61 | pc.printf("password[%d] is %c\n", index, password[index]); |
mbedTrent | 7:52b51f507ee5 | 62 | index++; //determines index of vector to place number |
pshabbaki3 | 9:11a6f6c8c54b | 63 | |
pshabbaki3 | 9:11a6f6c8c54b | 64 | } else { |
mbedTrent | 7:52b51f507ee5 | 65 | pc.printf("\nthe password you entered is: "); |
pshabbaki3 | 9:11a6f6c8c54b | 66 | for (k=0; k<6; k++) { |
mbedTrent | 7:52b51f507ee5 | 67 | pc.printf("%c", password[k]); |
mbedTrent | 7:52b51f507ee5 | 68 | } |
mbedTrent | 7:52b51f507ee5 | 69 | pc.printf(" , is this correct? (Y/N)"); |
mbedTrent | 7:52b51f507ee5 | 70 | //possibly use PB here to select Y/N |
mbedTrent | 8:2344ae984a4c | 71 | //call function to check password============================== checkResult needs to go here |
mbedTrent | 7:52b51f507ee5 | 72 | index=0;//reset index |
mbedTrent | 7:52b51f507ee5 | 73 | //memset(password, 0, sizeof(password)); //clear buffer |
mbedTrent | 7:52b51f507ee5 | 74 | } |
mbedTrent | 7:52b51f507ee5 | 75 | } |
mbedTrent | 7:52b51f507ee5 | 76 | } |
mbedTrent | 7:52b51f507ee5 | 77 | } |
mbedTrent | 7:52b51f507ee5 | 78 | |
pshabbaki3 | 9:11a6f6c8c54b | 79 | void save_to_file() |
pshabbaki3 | 9:11a6f6c8c54b | 80 | { |
ecarrick | 5:a2fbe5bd2be0 | 81 | FILE * fp = fopen("/local/users.txt", "w"); |
pshabbaki3 | 9:11a6f6c8c54b | 82 | if(fp != NULL) { |
pshabbaki3 | 9:11a6f6c8c54b | 83 | for(unsigned i=0; i< im.size(); i++) { |
ecarrick | 6:50a82641d07b | 84 | fprintf(fp,"%c",im[i]); |
ecarrick | 5:a2fbe5bd2be0 | 85 | } |
ecarrick | 5:a2fbe5bd2be0 | 86 | } |
ecarrick | 5:a2fbe5bd2be0 | 87 | fclose(fp); |
ecarrick | 5:a2fbe5bd2be0 | 88 | } |
ecarrick | 5:a2fbe5bd2be0 | 89 | |
pshabbaki3 | 9:11a6f6c8c54b | 90 | void load_from_file() |
pshabbaki3 | 9:11a6f6c8c54b | 91 | { |
ecarrick | 5:a2fbe5bd2be0 | 92 | FILE * fp = fopen("/local/users.txt", "r"); |
pshabbaki3 | 9:11a6f6c8c54b | 93 | if(fp != NULL) { |
ecarrick | 5:a2fbe5bd2be0 | 94 | im.clear(); |
pshabbaki3 | 9:11a6f6c8c54b | 95 | do { |
ecarrick | 6:50a82641d07b | 96 | im.push_back(fgetc(fp)); |
pshabbaki3 | 9:11a6f6c8c54b | 97 | } while(im.back() != '#'); |
ecarrick | 5:a2fbe5bd2be0 | 98 | } |
ecarrick | 5:a2fbe5bd2be0 | 99 | fclose(fp); |
ecarrick | 5:a2fbe5bd2be0 | 100 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 101 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 102 | int i = 0; |
pshabbaki3 | 0:5d35ff3dc9a5 | 103 | |
pshabbaki3 | 4:e6b198a22216 | 104 | void checkResult(vector<char> pass, vector<char> name) |
pshabbaki3 | 0:5d35ff3dc9a5 | 105 | { |
pshabbaki3 | 0:5d35ff3dc9a5 | 106 | // This function will check the keypad values and check if the passcode exists or not |
pshabbaki3 | 0:5d35ff3dc9a5 | 107 | |
pshabbaki3 | 9:11a6f6c8c54b | 108 | vector<char> ans(pwSize); //This vector should be the values coming from keypad |
pshabbaki3 | 9:11a6f6c8c54b | 109 | ans = password; // password from fallInterrupt function |
pshabbaki3 | 9:11a6f6c8c54b | 110 | |
pshabbaki3 | 9:11a6f6c8c54b | 111 | |
pshabbaki3 | 4:e6b198a22216 | 112 | if (pass.size()==6) { |
pshabbaki3 | 0:5d35ff3dc9a5 | 113 | i++; //use "i" to find which person logged in. |
pshabbaki3 | 4:e6b198a22216 | 114 | if (ans == pass) { |
pshabbaki3 | 0:5d35ff3dc9a5 | 115 | // pc.printf("Found"); |
pshabbaki3 | 0:5d35ff3dc9a5 | 116 | final = name; |
pshabbaki3 | 4:e6b198a22216 | 117 | final.insert(name.end(),pass.begin(),pass.end()); |
pshabbaki3 | 0:5d35ff3dc9a5 | 118 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 119 | } else { |
pshabbaki3 | 0:5d35ff3dc9a5 | 120 | // pc.printf("not found"); |
pshabbaki3 | 4:e6b198a22216 | 121 | //lcd.printf("\nnot found\n"); |
pshabbaki3 | 0:5d35ff3dc9a5 | 122 | final.clear(); |
pshabbaki3 | 0:5d35ff3dc9a5 | 123 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 124 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 125 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 126 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 127 | |
pshabbaki3 | 2:693a3af5af49 | 128 | void checkKeyboard(vector<char> im) |
pshabbaki3 | 0:5d35ff3dc9a5 | 129 | { |
pshabbaki3 | 2:693a3af5af49 | 130 | for (int i = 0; i<im.size(); i++) { |
pshabbaki3 | 4:e6b198a22216 | 131 | // find space |
pshabbaki3 | 2:693a3af5af49 | 132 | if (im[i] ==' ') { |
pshabbaki3 | 4:e6b198a22216 | 133 | // Check the result |
pshabbaki3 | 4:e6b198a22216 | 134 | checkResult(pass,name); |
pshabbaki3 | 9:11a6f6c8c54b | 135 | // clear pass, name and final |
pshabbaki3 | 4:e6b198a22216 | 136 | pass.clear(); |
pshabbaki3 | 2:693a3af5af49 | 137 | name.clear(); |
pshabbaki3 | 0:5d35ff3dc9a5 | 138 | // pc.printf("clear"); |
pshabbaki3 | 4:e6b198a22216 | 139 | //lcd.printf("clear"); |
pshabbaki3 | 2:693a3af5af49 | 140 | } else { |
pshabbaki3 | 2:693a3af5af49 | 141 | if (isdigit(im[i])) { |
pshabbaki3 | 4:e6b198a22216 | 142 | // write numbers (char) to vector pass |
pshabbaki3 | 4:e6b198a22216 | 143 | pass.push_back(im[i]); |
pshabbaki3 | 9:11a6f6c8c54b | 144 | |
pshabbaki3 | 2:693a3af5af49 | 145 | } else if (isalpha(im[i])) { |
pshabbaki3 | 2:693a3af5af49 | 146 | // write letters (char) to vector name |
pshabbaki3 | 2:693a3af5af49 | 147 | name.push_back(im[i]); |
pshabbaki3 | 2:693a3af5af49 | 148 | |
pshabbaki3 | 2:693a3af5af49 | 149 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 150 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 151 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 152 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 153 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 154 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 155 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 156 | int main() |
pshabbaki3 | 0:5d35ff3dc9a5 | 157 | { |
mbedTrent | 7:52b51f507ee5 | 158 | interrupt.fall(&fallInterrupt); |
mbedTrent | 7:52b51f507ee5 | 159 | interrupt.mode(PullUp); |
pshabbaki3 | 2:693a3af5af49 | 160 | //load_from_file(); |
pshabbaki3 | 2:693a3af5af49 | 161 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 162 | //interupt driven read from serial port |
pshabbaki3 | 0:5d35ff3dc9a5 | 163 | // calls save_to_file(); |
pshabbaki3 | 0:5d35ff3dc9a5 | 164 | while(1) { |
pshabbaki3 | 0:5d35ff3dc9a5 | 165 | char c = pc.getc(); |
pshabbaki3 | 0:5d35ff3dc9a5 | 166 | if (c!='#') { |
pshabbaki3 | 0:5d35ff3dc9a5 | 167 | im.push_back(c); //writing all the characters into vector |
pshabbaki3 | 2:693a3af5af49 | 168 | checkKeyboard(im); |
pshabbaki3 | 0:5d35ff3dc9a5 | 169 | } else { |
pshabbaki3 | 0:5d35ff3dc9a5 | 170 | for (int cnt = 0; cnt<im.size(); cnt++) { |
pshabbaki3 | 0:5d35ff3dc9a5 | 171 | pc.printf("%c",im[cnt]); |
pshabbaki3 | 4:e6b198a22216 | 172 | //lcd.printf("%c",im[cnt]); |
pshabbaki3 | 0:5d35ff3dc9a5 | 173 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 174 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 175 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 176 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 177 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 178 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 179 |