
4180 Final Project
Dependencies: 4DGL-uLCD-SE mbed Servo
main.cpp@7:52b51f507ee5, 2016-04-18 (annotated)
- Committer:
- mbedTrent
- Date:
- Mon Apr 18 22:19:32 2016 +0000
- Revision:
- 7:52b51f507ee5
- Parent:
- 6:50a82641d07b
- Child:
- 8:2344ae984a4c
added fallInterrupt
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 | |
mbedTrent | 7:52b51f507ee5 | 41 | void fallInterrupt() { |
mbedTrent | 7:52b51f507ee5 | 42 | int key_code=0; |
mbedTrent | 7:52b51f507ee5 | 43 | int i=0; |
mbedTrent | 7:52b51f507ee5 | 44 | int k=0; |
mbedTrent | 7:52b51f507ee5 | 45 | int value=mpr121.read(0x00); |
mbedTrent | 7:52b51f507ee5 | 46 | value +=mpr121.read(0x01)<<8; |
mbedTrent | 7:52b51f507ee5 | 47 | // LED demo mod by J. Hamblen |
mbedTrent | 7:52b51f507ee5 | 48 | //pc.printf("MPR value: %x \r\n", value); |
mbedTrent | 7:52b51f507ee5 | 49 | // puts key number out to LEDs for demo |
mbedTrent | 7:52b51f507ee5 | 50 | for (i=0; i<=11; i++) { |
mbedTrent | 7:52b51f507ee5 | 51 | if ((((value>>i)&0x01)==1)&&(i>9)){ |
mbedTrent | 7:52b51f507ee5 | 52 | pc.printf("You pressed %d, please enter a value from 0 to 9.\n", i); |
mbedTrent | 7:52b51f507ee5 | 53 | } |
mbedTrent | 7:52b51f507ee5 | 54 | else if (((value>>i)&0x01)==1) { |
mbedTrent | 7:52b51f507ee5 | 55 | |
mbedTrent | 7:52b51f507ee5 | 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 |
mbedTrent | 7:52b51f507ee5 | 63 | |
mbedTrent | 7:52b51f507ee5 | 64 | } |
mbedTrent | 7:52b51f507ee5 | 65 | else{ |
mbedTrent | 7:52b51f507ee5 | 66 | pc.printf("\nthe password you entered is: "); |
mbedTrent | 7:52b51f507ee5 | 67 | for (k=0; k<6;k++){ |
mbedTrent | 7:52b51f507ee5 | 68 | pc.printf("%c", password[k]); |
mbedTrent | 7:52b51f507ee5 | 69 | } |
mbedTrent | 7:52b51f507ee5 | 70 | pc.printf(" , is this correct? (Y/N)"); |
mbedTrent | 7:52b51f507ee5 | 71 | //possibly use PB here to select Y/N |
mbedTrent | 7:52b51f507ee5 | 72 | //call function to check password |
mbedTrent | 7:52b51f507ee5 | 73 | index=0;//reset index |
mbedTrent | 7:52b51f507ee5 | 74 | //memset(password, 0, sizeof(password)); //clear buffer |
mbedTrent | 7:52b51f507ee5 | 75 | } |
mbedTrent | 7:52b51f507ee5 | 76 | } |
mbedTrent | 7:52b51f507ee5 | 77 | } |
mbedTrent | 7:52b51f507ee5 | 78 | } |
mbedTrent | 7:52b51f507ee5 | 79 | |
ecarrick | 5:a2fbe5bd2be0 | 80 | void save_to_file(){ |
ecarrick | 5:a2fbe5bd2be0 | 81 | FILE * fp = fopen("/local/users.txt", "w"); |
ecarrick | 5:a2fbe5bd2be0 | 82 | if(fp != NULL){ |
ecarrick | 5:a2fbe5bd2be0 | 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 | |
ecarrick | 5:a2fbe5bd2be0 | 90 | void load_from_file(){ |
ecarrick | 5:a2fbe5bd2be0 | 91 | FILE * fp = fopen("/local/users.txt", "r"); |
ecarrick | 5:a2fbe5bd2be0 | 92 | if(fp != NULL){ |
ecarrick | 5:a2fbe5bd2be0 | 93 | im.clear(); |
ecarrick | 5:a2fbe5bd2be0 | 94 | do{ |
ecarrick | 6:50a82641d07b | 95 | im.push_back(fgetc(fp)); |
ecarrick | 5:a2fbe5bd2be0 | 96 | } |
ecarrick | 6:50a82641d07b | 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 | 0:5d35ff3dc9a5 | 108 | vector<char> ans; //This vector should be the values coming from keypad |
pshabbaki3 | 0:5d35ff3dc9a5 | 109 | ans.push_back('1'); |
pshabbaki3 | 0:5d35ff3dc9a5 | 110 | ans.push_back('2'); |
pshabbaki3 | 0:5d35ff3dc9a5 | 111 | ans.push_back('3'); |
pshabbaki3 | 0:5d35ff3dc9a5 | 112 | ans.push_back('4'); |
pshabbaki3 | 0:5d35ff3dc9a5 | 113 | ans.push_back('5'); |
pshabbaki3 | 0:5d35ff3dc9a5 | 114 | ans.push_back('6'); |
pshabbaki3 | 4:e6b198a22216 | 115 | if (pass.size()==6) { |
pshabbaki3 | 0:5d35ff3dc9a5 | 116 | i++; //use "i" to find which person logged in. |
pshabbaki3 | 4:e6b198a22216 | 117 | if (ans == pass) { |
pshabbaki3 | 0:5d35ff3dc9a5 | 118 | // pc.printf("Found"); |
pshabbaki3 | 0:5d35ff3dc9a5 | 119 | final = name; |
pshabbaki3 | 4:e6b198a22216 | 120 | final.insert(name.end(),pass.begin(),pass.end()); |
pshabbaki3 | 0:5d35ff3dc9a5 | 121 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 122 | } else { |
pshabbaki3 | 0:5d35ff3dc9a5 | 123 | // pc.printf("not found"); |
pshabbaki3 | 4:e6b198a22216 | 124 | //lcd.printf("\nnot found\n"); |
pshabbaki3 | 0:5d35ff3dc9a5 | 125 | final.clear(); |
pshabbaki3 | 0:5d35ff3dc9a5 | 126 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 127 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 128 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 129 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 130 | |
pshabbaki3 | 2:693a3af5af49 | 131 | void checkKeyboard(vector<char> im) |
pshabbaki3 | 0:5d35ff3dc9a5 | 132 | { |
pshabbaki3 | 2:693a3af5af49 | 133 | for (int i = 0; i<im.size(); i++) { |
pshabbaki3 | 4:e6b198a22216 | 134 | // find space |
pshabbaki3 | 2:693a3af5af49 | 135 | if (im[i] ==' ') { |
pshabbaki3 | 4:e6b198a22216 | 136 | // Check the result |
pshabbaki3 | 4:e6b198a22216 | 137 | checkResult(pass,name); |
pshabbaki3 | 4:e6b198a22216 | 138 | // clear pass, name and final |
pshabbaki3 | 4:e6b198a22216 | 139 | pass.clear(); |
pshabbaki3 | 2:693a3af5af49 | 140 | name.clear(); |
pshabbaki3 | 0:5d35ff3dc9a5 | 141 | // pc.printf("clear"); |
pshabbaki3 | 4:e6b198a22216 | 142 | //lcd.printf("clear"); |
pshabbaki3 | 2:693a3af5af49 | 143 | } else { |
pshabbaki3 | 2:693a3af5af49 | 144 | if (isdigit(im[i])) { |
pshabbaki3 | 4:e6b198a22216 | 145 | // write numbers (char) to vector pass |
pshabbaki3 | 4:e6b198a22216 | 146 | pass.push_back(im[i]); |
pshabbaki3 | 2:693a3af5af49 | 147 | |
pshabbaki3 | 2:693a3af5af49 | 148 | } else if (isalpha(im[i])) { |
pshabbaki3 | 2:693a3af5af49 | 149 | // write letters (char) to vector name |
pshabbaki3 | 2:693a3af5af49 | 150 | name.push_back(im[i]); |
pshabbaki3 | 2:693a3af5af49 | 151 | |
pshabbaki3 | 2:693a3af5af49 | 152 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 153 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 154 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 155 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 156 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 157 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 158 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 159 | int main() |
pshabbaki3 | 0:5d35ff3dc9a5 | 160 | { |
mbedTrent | 7:52b51f507ee5 | 161 | interrupt.fall(&fallInterrupt); |
mbedTrent | 7:52b51f507ee5 | 162 | interrupt.mode(PullUp); |
pshabbaki3 | 2:693a3af5af49 | 163 | //load_from_file(); |
pshabbaki3 | 2:693a3af5af49 | 164 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 165 | //interupt driven read from serial port |
pshabbaki3 | 0:5d35ff3dc9a5 | 166 | // calls save_to_file(); |
pshabbaki3 | 0:5d35ff3dc9a5 | 167 | while(1) { |
pshabbaki3 | 0:5d35ff3dc9a5 | 168 | char c = pc.getc(); |
pshabbaki3 | 0:5d35ff3dc9a5 | 169 | if (c!='#') { |
pshabbaki3 | 0:5d35ff3dc9a5 | 170 | im.push_back(c); //writing all the characters into vector |
pshabbaki3 | 2:693a3af5af49 | 171 | checkKeyboard(im); |
pshabbaki3 | 0:5d35ff3dc9a5 | 172 | } else { |
pshabbaki3 | 0:5d35ff3dc9a5 | 173 | for (int cnt = 0; cnt<im.size(); cnt++) { |
pshabbaki3 | 0:5d35ff3dc9a5 | 174 | pc.printf("%c",im[cnt]); |
pshabbaki3 | 4:e6b198a22216 | 175 | //lcd.printf("%c",im[cnt]); |
pshabbaki3 | 0:5d35ff3dc9a5 | 176 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 177 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 178 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 179 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 180 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 181 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 182 |