Buzz Access
Dependencies: 4DGL-uLCD-SE KeyPad PinDetect HID_Wiegand mbed-rtos mbed
main.cpp@2:ec211fc4bdc2, 2015-04-30 (annotated)
- Committer:
- otis22894
- Date:
- Thu Apr 30 06:24:40 2015 +0000
- Revision:
- 2:ec211fc4bdc2
- Parent:
- 1:c41a71a200d6
Commit #3;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
otis22894 | 0:0d31da041b5c | 1 | #include "mbed.h" |
otis22894 | 0:0d31da041b5c | 2 | #include "KeyPad.h" |
otis22894 | 0:0d31da041b5c | 3 | #include "rtos.h" |
otis22894 | 0:0d31da041b5c | 4 | #include "uLCD_4DGL.h" |
otis22894 | 0:0d31da041b5c | 5 | #include <iostream> |
otis22894 | 0:0d31da041b5c | 6 | #include "PinDetect.h" |
otis22894 | 0:0d31da041b5c | 7 | #include "Wiegand.h" |
otis22894 | 0:0d31da041b5c | 8 | #include <string> |
otis22894 | 0:0d31da041b5c | 9 | #include <sstream> |
otis22894 | 0:0d31da041b5c | 10 | #define __STDC_FORMAT_MACROS |
otis22894 | 0:0d31da041b5c | 11 | #include <inttypes.h> |
otis22894 | 0:0d31da041b5c | 12 | #define CARDSALLOWED 10 |
otis22894 | 0:0d31da041b5c | 13 | |
otis22894 | 0:0d31da041b5c | 14 | bool checkCode(std::vector<int>,std::vector<int>); |
otis22894 | 0:0d31da041b5c | 15 | void keypad_thread(void const *args); |
otis22894 | 0:0d31da041b5c | 16 | void knock_thread(void const *args); |
otis22894 | 0:0d31da041b5c | 17 | void rfid_thread(void const *args); |
otis22894 | 2:ec211fc4bdc2 | 18 | void beep_thread(void const *args); |
otis22894 | 0:0d31da041b5c | 19 | void changeCode(); |
otis22894 | 0:0d31da041b5c | 20 | void changeSecretKnock(); |
otis22894 | 0:0d31da041b5c | 21 | void captureKnock(); |
otis22894 | 0:0d31da041b5c | 22 | void checkKnocks(); |
otis22894 | 0:0d31da041b5c | 23 | void onCardRead(); |
otis22894 | 0:0d31da041b5c | 24 | int readCardsFromFile(FILE *fp); |
otis22894 | 0:0d31da041b5c | 25 | int readCardsFromFile(); |
otis22894 | 0:0d31da041b5c | 26 | bool isAllowed(const char *card); |
otis22894 | 0:0d31da041b5c | 27 | void storeCard(const char *card); |
otis22894 | 1:c41a71a200d6 | 28 | void menuShiftUp(); |
otis22894 | 1:c41a71a200d6 | 29 | void menuShiftDown(); |
otis22894 | 1:c41a71a200d6 | 30 | void display(); |
otis22894 | 2:ec211fc4bdc2 | 31 | void display2(); |
otis22894 | 1:c41a71a200d6 | 32 | void menuSelect(); |
otis22894 | 0:0d31da041b5c | 33 | |
otis22894 | 0:0d31da041b5c | 34 | Serial pc(USBTX, USBRX); |
otis22894 | 0:0d31da041b5c | 35 | KeyPad2 keypad(p25, p27, p23, p26, p21, p22, p24); |
otis22894 | 0:0d31da041b5c | 36 | static const int arr[] = {1,2,3,4}; |
otis22894 | 0:0d31da041b5c | 37 | std::vector<int> master(arr, arr + sizeof(arr) / sizeof(arr[0]) ); |
otis22894 | 0:0d31da041b5c | 38 | std::vector<int> keys; |
otis22894 | 1:c41a71a200d6 | 39 | std::vector<int> key; |
otis22894 | 0:0d31da041b5c | 40 | std::vector<int> knock; |
otis22894 | 0:0d31da041b5c | 41 | std::vector<int> userKnock; |
otis22894 | 1:c41a71a200d6 | 42 | static const std::string arr2[] = {"Lock Door","Unlock Door","Change Key Code","Change Knock","Add Card","Delete Card","This is wayyyyyyyyyyyyyyy too long"}; |
otis22894 | 1:c41a71a200d6 | 43 | std::vector<string> menuOptions(arr2, arr2 + sizeof(arr2) / sizeof(arr2[0])); |
otis22894 | 0:0d31da041b5c | 44 | DigitalOut led2(LED2); |
otis22894 | 0:0d31da041b5c | 45 | DigitalOut led1(LED1); |
otis22894 | 2:ec211fc4bdc2 | 46 | DigitalOut beeper(p28,1); |
otis22894 | 2:ec211fc4bdc2 | 47 | uLCD_4DGL lcd(p9, p10, p8); |
otis22894 | 2:ec211fc4bdc2 | 48 | uLCD_4DGL lcd2(p13, p14, p12); |
otis22894 | 2:ec211fc4bdc2 | 49 | PinDetect menuUp(p5); |
otis22894 | 1:c41a71a200d6 | 50 | PinDetect select(p6); |
otis22894 | 1:c41a71a200d6 | 51 | PinDetect menuDown(p7); |
otis22894 | 0:0d31da041b5c | 52 | Timer timer; |
otis22894 | 0:0d31da041b5c | 53 | AnalogIn ain(p15); |
otis22894 | 0:0d31da041b5c | 54 | Wiegand rfid(p30, p29, p28, &onCardRead); |
otis22894 | 0:0d31da041b5c | 55 | char **allowedCards; |
otis22894 | 0:0d31da041b5c | 56 | int allowedCount = 0; |
otis22894 | 0:0d31da041b5c | 57 | bool changeCodeBool = false; |
otis22894 | 0:0d31da041b5c | 58 | LocalFileSystem fileSystem("local"); |
otis22894 | 1:c41a71a200d6 | 59 | int selected = 0; |
otis22894 | 1:c41a71a200d6 | 60 | int oldSelected = -1; |
otis22894 | 2:ec211fc4bdc2 | 61 | bool access = false; |
otis22894 | 2:ec211fc4bdc2 | 62 | bool denied = false; |
otis22894 | 2:ec211fc4bdc2 | 63 | bool addCard = false; |
otis22894 | 2:ec211fc4bdc2 | 64 | bool removeCard = false; |
otis22894 | 2:ec211fc4bdc2 | 65 | bool changeKnockBool = false; |
otis22894 | 0:0d31da041b5c | 66 | |
otis22894 | 0:0d31da041b5c | 67 | int main() { |
otis22894 | 2:ec211fc4bdc2 | 68 | pc.printf("Start...\n"); |
otis22894 | 2:ec211fc4bdc2 | 69 | beeper = 1; |
otis22894 | 0:0d31da041b5c | 70 | timer.start(); |
otis22894 | 2:ec211fc4bdc2 | 71 | //Thread keypadThread(keypad_thread); |
otis22894 | 2:ec211fc4bdc2 | 72 | Thread knockThread(knock_thread); |
otis22894 | 0:0d31da041b5c | 73 | //Thread rfidThread(rfid_thread); |
otis22894 | 2:ec211fc4bdc2 | 74 | Thread beepThread(beep_thread); |
otis22894 | 1:c41a71a200d6 | 75 | lcd.baudrate(500000); |
otis22894 | 1:c41a71a200d6 | 76 | menuUp.mode(PullUp); |
otis22894 | 1:c41a71a200d6 | 77 | menuDown.mode(PullUp); |
otis22894 | 1:c41a71a200d6 | 78 | select.mode(PullUp); |
otis22894 | 1:c41a71a200d6 | 79 | Thread::wait(500); |
otis22894 | 1:c41a71a200d6 | 80 | menuUp.attach_deasserted(&menuShiftUp); |
otis22894 | 1:c41a71a200d6 | 81 | menuDown.attach_deasserted(&menuShiftDown); |
otis22894 | 1:c41a71a200d6 | 82 | select.attach_deasserted(&menuSelect); |
otis22894 | 1:c41a71a200d6 | 83 | menuUp.setSampleFrequency(); |
otis22894 | 1:c41a71a200d6 | 84 | menuDown.setSampleFrequency(); |
otis22894 | 1:c41a71a200d6 | 85 | select.setSampleFrequency(); |
otis22894 | 1:c41a71a200d6 | 86 | int count = 0; |
otis22894 | 0:0d31da041b5c | 87 | led1 = 0; |
otis22894 | 0:0d31da041b5c | 88 | while(1){ |
otis22894 | 1:c41a71a200d6 | 89 | if(count > 100){ |
otis22894 | 1:c41a71a200d6 | 90 | count = 0; |
otis22894 | 1:c41a71a200d6 | 91 | led1 = !led1; |
otis22894 | 1:c41a71a200d6 | 92 | } |
otis22894 | 1:c41a71a200d6 | 93 | display(); |
otis22894 | 2:ec211fc4bdc2 | 94 | display2(); |
otis22894 | 1:c41a71a200d6 | 95 | Thread::wait(1); |
otis22894 | 1:c41a71a200d6 | 96 | count++; |
otis22894 | 0:0d31da041b5c | 97 | } |
otis22894 | 0:0d31da041b5c | 98 | |
otis22894 | 0:0d31da041b5c | 99 | } |
otis22894 | 0:0d31da041b5c | 100 | |
otis22894 | 2:ec211fc4bdc2 | 101 | void beep_thread(void const *args){ |
otis22894 | 2:ec211fc4bdc2 | 102 | while(1){ |
otis22894 | 2:ec211fc4bdc2 | 103 | if(access){ |
otis22894 | 2:ec211fc4bdc2 | 104 | beeper = 0; |
otis22894 | 2:ec211fc4bdc2 | 105 | Thread::wait(750); |
otis22894 | 2:ec211fc4bdc2 | 106 | beeper = 1; |
otis22894 | 2:ec211fc4bdc2 | 107 | Thread::wait(1000); |
otis22894 | 2:ec211fc4bdc2 | 108 | }else if(denied){ |
otis22894 | 2:ec211fc4bdc2 | 109 | for(int i = 0;i<4;i++){ |
otis22894 | 2:ec211fc4bdc2 | 110 | beeper = 0; |
otis22894 | 2:ec211fc4bdc2 | 111 | Thread::wait(100); |
otis22894 | 2:ec211fc4bdc2 | 112 | beeper = 1; |
otis22894 | 2:ec211fc4bdc2 | 113 | Thread::wait(50); |
otis22894 | 2:ec211fc4bdc2 | 114 | } |
otis22894 | 2:ec211fc4bdc2 | 115 | Thread::wait(1000); |
otis22894 | 2:ec211fc4bdc2 | 116 | } |
otis22894 | 2:ec211fc4bdc2 | 117 | Thread::wait(1); |
otis22894 | 2:ec211fc4bdc2 | 118 | } |
otis22894 | 2:ec211fc4bdc2 | 119 | } |
otis22894 | 2:ec211fc4bdc2 | 120 | |
otis22894 | 2:ec211fc4bdc2 | 121 | void display2() { |
otis22894 | 2:ec211fc4bdc2 | 122 | if(access){ |
otis22894 | 2:ec211fc4bdc2 | 123 | lcd2.background_color(GREEN); |
otis22894 | 2:ec211fc4bdc2 | 124 | lcd2.cls(); |
otis22894 | 2:ec211fc4bdc2 | 125 | lcd2.locate(0,0); |
otis22894 | 2:ec211fc4bdc2 | 126 | lcd2.printf("Access Granted!"); |
otis22894 | 2:ec211fc4bdc2 | 127 | Thread::wait(1000); |
otis22894 | 2:ec211fc4bdc2 | 128 | access = false; |
otis22894 | 2:ec211fc4bdc2 | 129 | oldSelected = -1; |
otis22894 | 2:ec211fc4bdc2 | 130 | lcd2.background_color(BLACK); |
otis22894 | 2:ec211fc4bdc2 | 131 | lcd2.cls(); |
otis22894 | 2:ec211fc4bdc2 | 132 | }else if(denied){ |
otis22894 | 2:ec211fc4bdc2 | 133 | lcd2.background_color(RED); |
otis22894 | 2:ec211fc4bdc2 | 134 | lcd2.cls(); |
otis22894 | 2:ec211fc4bdc2 | 135 | lcd2.locate(0,0); |
otis22894 | 2:ec211fc4bdc2 | 136 | lcd2.printf("Access Denied"); |
otis22894 | 2:ec211fc4bdc2 | 137 | Thread::wait(1000); |
otis22894 | 2:ec211fc4bdc2 | 138 | denied = false; |
otis22894 | 2:ec211fc4bdc2 | 139 | oldSelected = -1; |
otis22894 | 2:ec211fc4bdc2 | 140 | lcd2.background_color(BLACK); |
otis22894 | 2:ec211fc4bdc2 | 141 | lcd2.cls(); |
otis22894 | 2:ec211fc4bdc2 | 142 | } else { |
otis22894 | 2:ec211fc4bdc2 | 143 | if(key.size()>0 && key.front()!=12){ |
otis22894 | 2:ec211fc4bdc2 | 144 | lcd2.printf("%d ",key.front()); |
otis22894 | 2:ec211fc4bdc2 | 145 | Thread::wait(150); |
otis22894 | 2:ec211fc4bdc2 | 146 | }else{ |
otis22894 | 2:ec211fc4bdc2 | 147 | //pc.printf("%d %d\n\n",key.size(),key.front()); |
otis22894 | 2:ec211fc4bdc2 | 148 | } |
otis22894 | 2:ec211fc4bdc2 | 149 | } |
otis22894 | 2:ec211fc4bdc2 | 150 | } |
otis22894 | 2:ec211fc4bdc2 | 151 | |
otis22894 | 1:c41a71a200d6 | 152 | void display(){ |
otis22894 | 1:c41a71a200d6 | 153 | if(changeCodeBool){ |
otis22894 | 1:c41a71a200d6 | 154 | lcd.cls(); |
otis22894 | 1:c41a71a200d6 | 155 | lcd.locate(0,0); |
otis22894 | 1:c41a71a200d6 | 156 | lcd.printf("%s","Enter New Code"); |
otis22894 | 1:c41a71a200d6 | 157 | lcd.locate(0,1); |
otis22894 | 1:c41a71a200d6 | 158 | bool first = true; |
otis22894 | 1:c41a71a200d6 | 159 | while(changeCodeBool){ |
otis22894 | 2:ec211fc4bdc2 | 160 | if(key.size()>0 && (!first || key.front()!=0) && key.front()!=12){ |
otis22894 | 1:c41a71a200d6 | 161 | lcd.printf("%d ",key.front()); |
otis22894 | 1:c41a71a200d6 | 162 | Thread::wait(150); |
otis22894 | 1:c41a71a200d6 | 163 | first = false; |
otis22894 | 1:c41a71a200d6 | 164 | } |
otis22894 | 1:c41a71a200d6 | 165 | } |
otis22894 | 2:ec211fc4bdc2 | 166 | oldSelected = -1; |
otis22894 | 2:ec211fc4bdc2 | 167 | }else if(addCard){ |
otis22894 | 2:ec211fc4bdc2 | 168 | lcd.cls(); |
otis22894 | 2:ec211fc4bdc2 | 169 | lcd.locate(0,0); |
otis22894 | 2:ec211fc4bdc2 | 170 | lcd.printf("Tap New Card"); |
otis22894 | 2:ec211fc4bdc2 | 171 | while(addCard){Thread::wait(10);} |
otis22894 | 2:ec211fc4bdc2 | 172 | oldSelected = -1; |
otis22894 | 2:ec211fc4bdc2 | 173 | }else if(removeCard){ |
otis22894 | 2:ec211fc4bdc2 | 174 | lcd.cls(); |
otis22894 | 2:ec211fc4bdc2 | 175 | lcd.locate(0,0); |
otis22894 | 2:ec211fc4bdc2 | 176 | lcd.printf("Tap The Card To Remove"); |
otis22894 | 2:ec211fc4bdc2 | 177 | while(removeCard){Thread::wait(10);} |
otis22894 | 2:ec211fc4bdc2 | 178 | oldSelected = -1; |
otis22894 | 2:ec211fc4bdc2 | 179 | }else if(changeKnockBool){ |
otis22894 | 2:ec211fc4bdc2 | 180 | lcd.cls(); |
otis22894 | 2:ec211fc4bdc2 | 181 | lcd.locate(0,0); |
otis22894 | 2:ec211fc4bdc2 | 182 | lcd.printf("Listening For New Knock"); |
otis22894 | 2:ec211fc4bdc2 | 183 | while(changeKnockBool){Thread::wait(10);} |
otis22894 | 2:ec211fc4bdc2 | 184 | lcd.cls(); |
otis22894 | 2:ec211fc4bdc2 | 185 | lcd.printf("New Knock Recorded"); |
otis22894 | 2:ec211fc4bdc2 | 186 | Thread::wait(500); |
otis22894 | 2:ec211fc4bdc2 | 187 | oldSelected = -1; |
otis22894 | 1:c41a71a200d6 | 188 | }else if(oldSelected != selected){ |
otis22894 | 1:c41a71a200d6 | 189 | for(int i = 0;i<menuOptions.size();i++){ |
otis22894 | 1:c41a71a200d6 | 190 | lcd.locate(0,i); |
otis22894 | 1:c41a71a200d6 | 191 | if(selected==i){ |
otis22894 | 1:c41a71a200d6 | 192 | lcd.textbackground_color(WHITE); |
otis22894 | 1:c41a71a200d6 | 193 | }else{ |
otis22894 | 1:c41a71a200d6 | 194 | lcd.textbackground_color(BLACK); |
otis22894 | 1:c41a71a200d6 | 195 | } |
otis22894 | 1:c41a71a200d6 | 196 | if(menuOptions[i].size()>17){ |
otis22894 | 1:c41a71a200d6 | 197 | lcd.printf("%s",menuOptions[i].substr(0,18).c_str()); |
otis22894 | 1:c41a71a200d6 | 198 | }else{ |
otis22894 | 2:ec211fc4bdc2 | 199 | lcd.printf("%s",(menuOptions[i] + std::string(18-menuOptions[i].size(),' ')).c_str()); |
otis22894 | 1:c41a71a200d6 | 200 | } |
otis22894 | 1:c41a71a200d6 | 201 | } |
otis22894 | 1:c41a71a200d6 | 202 | oldSelected = selected; |
otis22894 | 1:c41a71a200d6 | 203 | } |
otis22894 | 1:c41a71a200d6 | 204 | } |
otis22894 | 1:c41a71a200d6 | 205 | |
otis22894 | 1:c41a71a200d6 | 206 | void menuSelect(){ |
otis22894 | 2:ec211fc4bdc2 | 207 | if(selected==2){ |
otis22894 | 2:ec211fc4bdc2 | 208 | changeCodeBool = true; |
otis22894 | 2:ec211fc4bdc2 | 209 | }else if(selected == 3){ |
otis22894 | 2:ec211fc4bdc2 | 210 | changeKnockBool = true; |
otis22894 | 2:ec211fc4bdc2 | 211 | }else if(selected == 4){ |
otis22894 | 2:ec211fc4bdc2 | 212 | addCard = true; |
otis22894 | 2:ec211fc4bdc2 | 213 | }else if(selected == 5){ |
otis22894 | 2:ec211fc4bdc2 | 214 | //removeCard = true; FIX LATER |
otis22894 | 2:ec211fc4bdc2 | 215 | } |
otis22894 | 1:c41a71a200d6 | 216 | } |
otis22894 | 1:c41a71a200d6 | 217 | |
otis22894 | 1:c41a71a200d6 | 218 | void menuShiftUp(){ |
otis22894 | 1:c41a71a200d6 | 219 | selected = (selected + 1) % menuOptions.size(); |
otis22894 | 1:c41a71a200d6 | 220 | } |
otis22894 | 1:c41a71a200d6 | 221 | |
otis22894 | 1:c41a71a200d6 | 222 | void menuShiftDown(){ |
otis22894 | 1:c41a71a200d6 | 223 | selected--; |
otis22894 | 1:c41a71a200d6 | 224 | if(selected == -1){ |
otis22894 | 1:c41a71a200d6 | 225 | selected = menuOptions.size() - 1; |
otis22894 | 1:c41a71a200d6 | 226 | } |
otis22894 | 1:c41a71a200d6 | 227 | } |
otis22894 | 1:c41a71a200d6 | 228 | |
otis22894 | 0:0d31da041b5c | 229 | void rfid_thread(void const *args){ |
otis22894 | 0:0d31da041b5c | 230 | allowedCards = new char*[CARDSALLOWED]; |
otis22894 | 0:0d31da041b5c | 231 | for (int i = 0; i < CARDSALLOWED; ++i){ |
otis22894 | 0:0d31da041b5c | 232 | allowedCards[i] = new char[11]; |
otis22894 | 0:0d31da041b5c | 233 | } |
otis22894 | 0:0d31da041b5c | 234 | readCardsFromFile(); |
otis22894 | 0:0d31da041b5c | 235 | while(1){ |
otis22894 | 0:0d31da041b5c | 236 | rfid.doEvents(); |
otis22894 | 0:0d31da041b5c | 237 | Thread::wait(100); |
otis22894 | 0:0d31da041b5c | 238 | } |
otis22894 | 0:0d31da041b5c | 239 | } |
otis22894 | 0:0d31da041b5c | 240 | |
otis22894 | 0:0d31da041b5c | 241 | void onCardRead() { |
otis22894 | 2:ec211fc4bdc2 | 242 | /*pc.printf("%d\n",rfid.bitsRead()); |
otis22894 | 2:ec211fc4bdc2 | 243 | uint64_t rawCardData = rfid.getBits(14,33); |
otis22894 | 2:ec211fc4bdc2 | 244 | pc.printf("%" PRIx64 "\n",rawCardData); |
otis22894 | 2:ec211fc4bdc2 | 245 | pc.printf("%" PRIx64 "\n",rfid.getBits(0,34));*/ |
otis22894 | 2:ec211fc4bdc2 | 246 | uint64_t cardData = rfid.getBits(0,34); |
otis22894 | 2:ec211fc4bdc2 | 247 | ostringstream o; |
otis22894 | 2:ec211fc4bdc2 | 248 | string str; |
otis22894 | 2:ec211fc4bdc2 | 249 | o << cardData; |
otis22894 | 2:ec211fc4bdc2 | 250 | str += o.str(); |
otis22894 | 2:ec211fc4bdc2 | 251 | if(addCard){ |
otis22894 | 2:ec211fc4bdc2 | 252 | storeCard(str.c_str()); |
otis22894 | 2:ec211fc4bdc2 | 253 | }else if(removeCard){ |
otis22894 | 2:ec211fc4bdc2 | 254 | |
otis22894 | 2:ec211fc4bdc2 | 255 | }else if(isAllowed(str.c_str())) { |
otis22894 | 2:ec211fc4bdc2 | 256 | access = true; |
otis22894 | 2:ec211fc4bdc2 | 257 | }else{ |
otis22894 | 2:ec211fc4bdc2 | 258 | denied = true; |
otis22894 | 2:ec211fc4bdc2 | 259 | } |
otis22894 | 0:0d31da041b5c | 260 | } |
otis22894 | 0:0d31da041b5c | 261 | |
otis22894 | 0:0d31da041b5c | 262 | bool isAllowed(const char *card){ |
otis22894 | 0:0d31da041b5c | 263 | for(int i = 0;i<allowedCount;i++){ |
otis22894 | 0:0d31da041b5c | 264 | for(int j = 0;j<11;j++){ |
otis22894 | 0:0d31da041b5c | 265 | if(card[j]!=allowedCards[i][j]){ |
otis22894 | 0:0d31da041b5c | 266 | break; |
otis22894 | 0:0d31da041b5c | 267 | }else if(j==10){ |
otis22894 | 0:0d31da041b5c | 268 | return true; |
otis22894 | 0:0d31da041b5c | 269 | } |
otis22894 | 0:0d31da041b5c | 270 | } |
otis22894 | 0:0d31da041b5c | 271 | } |
otis22894 | 0:0d31da041b5c | 272 | return false; |
otis22894 | 0:0d31da041b5c | 273 | } |
otis22894 | 0:0d31da041b5c | 274 | |
otis22894 | 0:0d31da041b5c | 275 | void storeCard(const char *card){ |
otis22894 | 0:0d31da041b5c | 276 | if(allowedCount < 10 && !isAllowed(card)){ |
otis22894 | 0:0d31da041b5c | 277 | FILE *fp = fopen("/local/cards.txt", "a"); |
otis22894 | 0:0d31da041b5c | 278 | if(fp==NULL){ |
otis22894 | 1:c41a71a200d6 | 279 | //ERROR |
otis22894 | 0:0d31da041b5c | 280 | } |
otis22894 | 0:0d31da041b5c | 281 | fprintf(fp, "%c%c%c%c%c%c%c%c%c%c%c\r\n",card[0],card[1],card[2],card[3],card[4],card[5],card[6],card[7],card[8],card[9],card[10]); |
otis22894 | 0:0d31da041b5c | 282 | fclose(fp); |
otis22894 | 0:0d31da041b5c | 283 | for(int transfer = 0;transfer<11;transfer++){ |
otis22894 | 0:0d31da041b5c | 284 | allowedCards[allowedCount][transfer] = card[transfer]; |
otis22894 | 0:0d31da041b5c | 285 | } |
otis22894 | 0:0d31da041b5c | 286 | allowedCount++; |
otis22894 | 0:0d31da041b5c | 287 | } |
otis22894 | 2:ec211fc4bdc2 | 288 | addCard = false; |
otis22894 | 2:ec211fc4bdc2 | 289 | } |
otis22894 | 2:ec211fc4bdc2 | 290 | |
otis22894 | 2:ec211fc4bdc2 | 291 | //FIX LATER |
otis22894 | 2:ec211fc4bdc2 | 292 | void removeCardFromFile(const char *card){ |
otis22894 | 2:ec211fc4bdc2 | 293 | if(isAllowed(card)){ |
otis22894 | 2:ec211fc4bdc2 | 294 | FILE *fp = fopen("/local/cards.txt", "r"); |
otis22894 | 2:ec211fc4bdc2 | 295 | } |
otis22894 | 0:0d31da041b5c | 296 | } |
otis22894 | 0:0d31da041b5c | 297 | |
otis22894 | 0:0d31da041b5c | 298 | int readCardsFromFile(){ |
otis22894 | 0:0d31da041b5c | 299 | FILE *fp = fopen("/local/cards.txt", "r"); |
otis22894 | 0:0d31da041b5c | 300 | int ret = readCardsFromFile(fp); |
otis22894 | 0:0d31da041b5c | 301 | return ret; |
otis22894 | 0:0d31da041b5c | 302 | } |
otis22894 | 0:0d31da041b5c | 303 | |
otis22894 | 0:0d31da041b5c | 304 | int readCardsFromFile(FILE *fp){ |
otis22894 | 0:0d31da041b5c | 305 | int c,transfer = 0; |
otis22894 | 0:0d31da041b5c | 306 | if(fp!=NULL){ |
otis22894 | 0:0d31da041b5c | 307 | c = fgetc(fp); |
otis22894 | 0:0d31da041b5c | 308 | while(c!=EOF){ |
otis22894 | 0:0d31da041b5c | 309 | allowedCards[allowedCount][transfer] = c; |
otis22894 | 0:0d31da041b5c | 310 | transfer++; |
otis22894 | 0:0d31da041b5c | 311 | if(transfer==11){ |
otis22894 | 0:0d31da041b5c | 312 | allowedCount++; |
otis22894 | 0:0d31da041b5c | 313 | if(allowedCount==CARDSALLOWED){ |
otis22894 | 0:0d31da041b5c | 314 | break; |
otis22894 | 0:0d31da041b5c | 315 | } |
otis22894 | 0:0d31da041b5c | 316 | transfer = 0; |
otis22894 | 0:0d31da041b5c | 317 | fgetc(fp); |
otis22894 | 0:0d31da041b5c | 318 | fgetc(fp); |
otis22894 | 0:0d31da041b5c | 319 | } |
otis22894 | 0:0d31da041b5c | 320 | c = fgetc(fp); |
otis22894 | 0:0d31da041b5c | 321 | } |
otis22894 | 0:0d31da041b5c | 322 | fclose(fp); |
otis22894 | 0:0d31da041b5c | 323 | return 1; |
otis22894 | 0:0d31da041b5c | 324 | }else{ |
otis22894 | 0:0d31da041b5c | 325 | return 0; |
otis22894 | 0:0d31da041b5c | 326 | } |
otis22894 | 0:0d31da041b5c | 327 | } |
otis22894 | 0:0d31da041b5c | 328 | |
otis22894 | 0:0d31da041b5c | 329 | void knock_thread(void const *args){ |
otis22894 | 0:0d31da041b5c | 330 | Thread::wait(1000); |
otis22894 | 0:0d31da041b5c | 331 | changeSecretKnock(); |
otis22894 | 0:0d31da041b5c | 332 | float newval; |
otis22894 | 0:0d31da041b5c | 333 | while(1){ |
otis22894 | 0:0d31da041b5c | 334 | newval = ain.read(); |
otis22894 | 0:0d31da041b5c | 335 | if(newval > 0.508 && newval < 0.538){ |
otis22894 | 0:0d31da041b5c | 336 | Thread::wait(50); |
otis22894 | 0:0d31da041b5c | 337 | captureKnock(); |
otis22894 | 0:0d31da041b5c | 338 | checkKnocks(); |
otis22894 | 2:ec211fc4bdc2 | 339 | }else if(changeKnockBool){ |
otis22894 | 2:ec211fc4bdc2 | 340 | changeSecretKnock(); |
otis22894 | 0:0d31da041b5c | 341 | } |
otis22894 | 0:0d31da041b5c | 342 | Thread::wait(1); |
otis22894 | 0:0d31da041b5c | 343 | } |
otis22894 | 0:0d31da041b5c | 344 | } |
otis22894 | 0:0d31da041b5c | 345 | |
otis22894 | 0:0d31da041b5c | 346 | void checkKnocks(){ |
otis22894 | 2:ec211fc4bdc2 | 347 | if(userKnock.size()!=0){ |
otis22894 | 2:ec211fc4bdc2 | 348 | if(knock.size() != userKnock.size()){ |
otis22894 | 2:ec211fc4bdc2 | 349 | denied = true; |
otis22894 | 2:ec211fc4bdc2 | 350 | }else{ |
otis22894 | 2:ec211fc4bdc2 | 351 | bool accessCheck = true; |
otis22894 | 2:ec211fc4bdc2 | 352 | for(int i = 0; i<knock.size(); i++){ |
otis22894 | 2:ec211fc4bdc2 | 353 | if(!(abs(1-((float)knock[i])/((float)userKnock[i]))<0.15)){ |
otis22894 | 2:ec211fc4bdc2 | 354 | accessCheck = false; |
otis22894 | 2:ec211fc4bdc2 | 355 | } |
otis22894 | 0:0d31da041b5c | 356 | } |
otis22894 | 2:ec211fc4bdc2 | 357 | if(accessCheck){ |
otis22894 | 2:ec211fc4bdc2 | 358 | access = true; |
otis22894 | 2:ec211fc4bdc2 | 359 | }else{ |
otis22894 | 2:ec211fc4bdc2 | 360 | denied = true; |
otis22894 | 2:ec211fc4bdc2 | 361 | } |
otis22894 | 0:0d31da041b5c | 362 | } |
otis22894 | 0:0d31da041b5c | 363 | } |
otis22894 | 0:0d31da041b5c | 364 | } |
otis22894 | 0:0d31da041b5c | 365 | |
otis22894 | 0:0d31da041b5c | 366 | void captureKnock(){ |
otis22894 | 0:0d31da041b5c | 367 | float val; |
otis22894 | 0:0d31da041b5c | 368 | userKnock.clear(); |
otis22894 | 0:0d31da041b5c | 369 | timer.reset(); |
otis22894 | 0:0d31da041b5c | 370 | int time; |
otis22894 | 0:0d31da041b5c | 371 | while(timer.read() < 2){ |
otis22894 | 0:0d31da041b5c | 372 | val = ain.read(); |
otis22894 | 0:0d31da041b5c | 373 | if(val > 0.508 && val < 0.538){ |
otis22894 | 0:0d31da041b5c | 374 | time = timer.read_ms(); |
otis22894 | 0:0d31da041b5c | 375 | userKnock.push_back(time); |
otis22894 | 0:0d31da041b5c | 376 | Thread::wait(50); |
otis22894 | 2:ec211fc4bdc2 | 377 | pc.printf("%d\n",time); |
otis22894 | 0:0d31da041b5c | 378 | timer.reset(); |
otis22894 | 0:0d31da041b5c | 379 | } |
otis22894 | 0:0d31da041b5c | 380 | Thread::wait(1); |
otis22894 | 0:0d31da041b5c | 381 | } |
otis22894 | 0:0d31da041b5c | 382 | } |
otis22894 | 0:0d31da041b5c | 383 | |
otis22894 | 0:0d31da041b5c | 384 | void changeSecretKnock(){ |
otis22894 | 2:ec211fc4bdc2 | 385 | pc.printf("Changing Secret Knock\n"); |
otis22894 | 0:0d31da041b5c | 386 | float val; |
otis22894 | 0:0d31da041b5c | 387 | Timer t; |
otis22894 | 0:0d31da041b5c | 388 | while(true){ |
otis22894 | 0:0d31da041b5c | 389 | val = ain.read(); |
otis22894 | 0:0d31da041b5c | 390 | if(val > 0.508 && val < 0.538){ |
otis22894 | 2:ec211fc4bdc2 | 391 | Thread::wait(50); |
otis22894 | 0:0d31da041b5c | 392 | break; |
otis22894 | 0:0d31da041b5c | 393 | } |
otis22894 | 0:0d31da041b5c | 394 | Thread::wait(1); |
otis22894 | 0:0d31da041b5c | 395 | } |
otis22894 | 0:0d31da041b5c | 396 | t.start(); |
otis22894 | 0:0d31da041b5c | 397 | int time; |
otis22894 | 0:0d31da041b5c | 398 | while(t.read() < 2){ |
otis22894 | 0:0d31da041b5c | 399 | val = ain.read(); |
otis22894 | 0:0d31da041b5c | 400 | if(val > 0.508 && val < 0.538){ |
otis22894 | 0:0d31da041b5c | 401 | time = t.read_ms(); |
otis22894 | 0:0d31da041b5c | 402 | knock.push_back(time); |
otis22894 | 0:0d31da041b5c | 403 | Thread::wait(50); |
otis22894 | 2:ec211fc4bdc2 | 404 | pc.printf("%d\n",time); |
otis22894 | 0:0d31da041b5c | 405 | t.reset(); |
otis22894 | 0:0d31da041b5c | 406 | } |
otis22894 | 0:0d31da041b5c | 407 | Thread::wait(1); |
otis22894 | 0:0d31da041b5c | 408 | } |
otis22894 | 2:ec211fc4bdc2 | 409 | pc.printf("Size: %d\n",knock.size()); |
otis22894 | 2:ec211fc4bdc2 | 410 | changeKnockBool = false; |
otis22894 | 0:0d31da041b5c | 411 | } |
otis22894 | 0:0d31da041b5c | 412 | |
otis22894 | 0:0d31da041b5c | 413 | bool checkCode(std::vector<int> master,std::vector<int> v1){ |
otis22894 | 0:0d31da041b5c | 414 | if(v1.size() != master.size()){ |
otis22894 | 0:0d31da041b5c | 415 | return false; |
otis22894 | 0:0d31da041b5c | 416 | }else{ |
otis22894 | 0:0d31da041b5c | 417 | for(int i = 0;i<master.size();i++){ |
otis22894 | 0:0d31da041b5c | 418 | if(master[i] != v1[i]){ |
otis22894 | 0:0d31da041b5c | 419 | return false; |
otis22894 | 0:0d31da041b5c | 420 | } |
otis22894 | 0:0d31da041b5c | 421 | } |
otis22894 | 0:0d31da041b5c | 422 | return true; |
otis22894 | 0:0d31da041b5c | 423 | } |
otis22894 | 0:0d31da041b5c | 424 | } |
otis22894 | 0:0d31da041b5c | 425 | |
otis22894 | 0:0d31da041b5c | 426 | void changeCode(){ |
otis22894 | 0:0d31da041b5c | 427 | std::vector<int> tempMaster; |
otis22894 | 2:ec211fc4bdc2 | 428 | key.clear(); |
otis22894 | 0:0d31da041b5c | 429 | key.push_back(0); |
otis22894 | 0:0d31da041b5c | 430 | while(key.front() != 12){ |
otis22894 | 0:0d31da041b5c | 431 | key = keypad.getkey(); |
otis22894 | 0:0d31da041b5c | 432 | if(key.size()>0){ |
otis22894 | 0:0d31da041b5c | 433 | tempMaster.push_back(key.front()); |
otis22894 | 0:0d31da041b5c | 434 | } |
otis22894 | 0:0d31da041b5c | 435 | Thread::wait(100); |
otis22894 | 0:0d31da041b5c | 436 | } |
otis22894 | 2:ec211fc4bdc2 | 437 | key.clear(); |
otis22894 | 2:ec211fc4bdc2 | 438 | key.push_back(0); |
otis22894 | 0:0d31da041b5c | 439 | tempMaster.pop_back(); |
otis22894 | 0:0d31da041b5c | 440 | if(tempMaster.size() >= 4){ |
otis22894 | 0:0d31da041b5c | 441 | master.clear(); |
otis22894 | 0:0d31da041b5c | 442 | for(int i = 0;i<tempMaster.size();i++){ |
otis22894 | 0:0d31da041b5c | 443 | master.push_back(tempMaster[i]); |
otis22894 | 0:0d31da041b5c | 444 | } |
otis22894 | 1:c41a71a200d6 | 445 | }else{ |
otis22894 | 0:0d31da041b5c | 446 | } |
otis22894 | 0:0d31da041b5c | 447 | changeCodeBool = false; |
otis22894 | 0:0d31da041b5c | 448 | } |
otis22894 | 0:0d31da041b5c | 449 | |
otis22894 | 0:0d31da041b5c | 450 | void keypad_thread(void const *args){ |
otis22894 | 2:ec211fc4bdc2 | 451 | key.clear(); |
otis22894 | 0:0d31da041b5c | 452 | key.push_back(0); |
otis22894 | 0:0d31da041b5c | 453 | while(1) { |
otis22894 | 0:0d31da041b5c | 454 | while(key.front() != 12){ |
otis22894 | 0:0d31da041b5c | 455 | if(changeCodeBool){ |
otis22894 | 0:0d31da041b5c | 456 | changeCode(); |
otis22894 | 0:0d31da041b5c | 457 | } |
otis22894 | 0:0d31da041b5c | 458 | key = keypad.getkey(); |
otis22894 | 0:0d31da041b5c | 459 | if(key.size()>0){ |
otis22894 | 0:0d31da041b5c | 460 | keys.push_back(key.front()); |
otis22894 | 0:0d31da041b5c | 461 | } |
otis22894 | 0:0d31da041b5c | 462 | Thread::wait(100); |
otis22894 | 0:0d31da041b5c | 463 | } |
otis22894 | 0:0d31da041b5c | 464 | keys.pop_back(); |
otis22894 | 0:0d31da041b5c | 465 | bool result = checkCode(master,keys); |
otis22894 | 2:ec211fc4bdc2 | 466 | if(result){ |
otis22894 | 2:ec211fc4bdc2 | 467 | access = true; |
otis22894 | 2:ec211fc4bdc2 | 468 | }else{ |
otis22894 | 2:ec211fc4bdc2 | 469 | denied = true; |
otis22894 | 2:ec211fc4bdc2 | 470 | } |
otis22894 | 0:0d31da041b5c | 471 | int size = keys.size(); |
otis22894 | 0:0d31da041b5c | 472 | for(int i = 0;i<size;i++){ |
otis22894 | 0:0d31da041b5c | 473 | keys.erase(keys.begin()); |
otis22894 | 1:c41a71a200d6 | 474 | } |
otis22894 | 0:0d31da041b5c | 475 | keys.clear(); |
otis22894 | 0:0d31da041b5c | 476 | key.clear(); |
otis22894 | 0:0d31da041b5c | 477 | key.push_back(0); |
otis22894 | 0:0d31da041b5c | 478 | Thread::wait(100); |
otis22894 | 0:0d31da041b5c | 479 | } |
otis22894 | 0:0d31da041b5c | 480 | } |