Buzz Access
Dependencies: 4DGL-uLCD-SE KeyPad PinDetect HID_Wiegand mbed-rtos mbed
main.cpp@1:c41a71a200d6, 2015-04-30 (annotated)
- Committer:
- otis22894
- Date:
- Thu Apr 30 01:37:26 2015 +0000
- Revision:
- 1:c41a71a200d6
- Parent:
- 0:0d31da041b5c
- Child:
- 2:ec211fc4bdc2
Commit #2;
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 | 0:0d31da041b5c | 18 | void changeCode(); |
otis22894 | 0:0d31da041b5c | 19 | void changeSecretKnock(); |
otis22894 | 0:0d31da041b5c | 20 | void captureKnock(); |
otis22894 | 0:0d31da041b5c | 21 | void checkKnocks(); |
otis22894 | 0:0d31da041b5c | 22 | void onCardRead(); |
otis22894 | 0:0d31da041b5c | 23 | int readCardsFromFile(FILE *fp); |
otis22894 | 0:0d31da041b5c | 24 | int readCardsFromFile(); |
otis22894 | 0:0d31da041b5c | 25 | bool isAllowed(const char *card); |
otis22894 | 0:0d31da041b5c | 26 | void storeCard(const char *card); |
otis22894 | 1:c41a71a200d6 | 27 | void menuShiftUp(); |
otis22894 | 1:c41a71a200d6 | 28 | void menuShiftDown(); |
otis22894 | 1:c41a71a200d6 | 29 | void display(); |
otis22894 | 1:c41a71a200d6 | 30 | void menuSelect(); |
otis22894 | 0:0d31da041b5c | 31 | |
otis22894 | 0:0d31da041b5c | 32 | Serial pc(USBTX, USBRX); |
otis22894 | 0:0d31da041b5c | 33 | KeyPad2 keypad(p25, p27, p23, p26, p21, p22, p24); |
otis22894 | 0:0d31da041b5c | 34 | static const int arr[] = {1,2,3,4}; |
otis22894 | 0:0d31da041b5c | 35 | std::vector<int> master(arr, arr + sizeof(arr) / sizeof(arr[0]) ); |
otis22894 | 0:0d31da041b5c | 36 | std::vector<int> keys; |
otis22894 | 1:c41a71a200d6 | 37 | std::vector<int> key; |
otis22894 | 0:0d31da041b5c | 38 | std::vector<int> knock; |
otis22894 | 0:0d31da041b5c | 39 | std::vector<int> userKnock; |
otis22894 | 1:c41a71a200d6 | 40 | 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 | 41 | std::vector<string> menuOptions(arr2, arr2 + sizeof(arr2) / sizeof(arr2[0])); |
otis22894 | 0:0d31da041b5c | 42 | DigitalOut led2(LED2); |
otis22894 | 0:0d31da041b5c | 43 | DigitalOut led1(LED1); |
otis22894 | 0:0d31da041b5c | 44 | uLCD_4DGL lcd(p9, p10, p8); |
otis22894 | 1:c41a71a200d6 | 45 | PinDetect menuUp(p13); |
otis22894 | 1:c41a71a200d6 | 46 | PinDetect select(p6); |
otis22894 | 1:c41a71a200d6 | 47 | PinDetect menuDown(p7); |
otis22894 | 0:0d31da041b5c | 48 | Timer timer; |
otis22894 | 0:0d31da041b5c | 49 | AnalogIn ain(p15); |
otis22894 | 0:0d31da041b5c | 50 | Wiegand rfid(p30, p29, p28, &onCardRead); |
otis22894 | 0:0d31da041b5c | 51 | char **allowedCards; |
otis22894 | 0:0d31da041b5c | 52 | int allowedCount = 0; |
otis22894 | 0:0d31da041b5c | 53 | bool changeCodeBool = false; |
otis22894 | 0:0d31da041b5c | 54 | LocalFileSystem fileSystem("local"); |
otis22894 | 1:c41a71a200d6 | 55 | int selected = 0; |
otis22894 | 1:c41a71a200d6 | 56 | int oldSelected = -1; |
otis22894 | 0:0d31da041b5c | 57 | |
otis22894 | 0:0d31da041b5c | 58 | int main() { |
otis22894 | 0:0d31da041b5c | 59 | timer.start(); |
otis22894 | 1:c41a71a200d6 | 60 | Thread keypadThread(keypad_thread); |
otis22894 | 0:0d31da041b5c | 61 | //Thread knockThread(knock_thread); |
otis22894 | 0:0d31da041b5c | 62 | //Thread rfidThread(rfid_thread); |
otis22894 | 1:c41a71a200d6 | 63 | lcd.baudrate(500000); |
otis22894 | 1:c41a71a200d6 | 64 | menuUp.mode(PullUp); |
otis22894 | 1:c41a71a200d6 | 65 | menuDown.mode(PullUp); |
otis22894 | 1:c41a71a200d6 | 66 | select.mode(PullUp); |
otis22894 | 1:c41a71a200d6 | 67 | Thread::wait(500); |
otis22894 | 1:c41a71a200d6 | 68 | menuUp.attach_deasserted(&menuShiftUp); |
otis22894 | 1:c41a71a200d6 | 69 | menuDown.attach_deasserted(&menuShiftDown); |
otis22894 | 1:c41a71a200d6 | 70 | select.attach_deasserted(&menuSelect); |
otis22894 | 1:c41a71a200d6 | 71 | menuUp.setSampleFrequency(); |
otis22894 | 1:c41a71a200d6 | 72 | menuDown.setSampleFrequency(); |
otis22894 | 1:c41a71a200d6 | 73 | select.setSampleFrequency(); |
otis22894 | 1:c41a71a200d6 | 74 | int count = 0; |
otis22894 | 0:0d31da041b5c | 75 | led1 = 0; |
otis22894 | 0:0d31da041b5c | 76 | while(1){ |
otis22894 | 1:c41a71a200d6 | 77 | if(count > 100){ |
otis22894 | 1:c41a71a200d6 | 78 | count = 0; |
otis22894 | 1:c41a71a200d6 | 79 | led1 = !led1; |
otis22894 | 1:c41a71a200d6 | 80 | } |
otis22894 | 1:c41a71a200d6 | 81 | display(); |
otis22894 | 1:c41a71a200d6 | 82 | Thread::wait(1); |
otis22894 | 1:c41a71a200d6 | 83 | count++; |
otis22894 | 0:0d31da041b5c | 84 | } |
otis22894 | 0:0d31da041b5c | 85 | |
otis22894 | 0:0d31da041b5c | 86 | } |
otis22894 | 0:0d31da041b5c | 87 | |
otis22894 | 1:c41a71a200d6 | 88 | void display(){ |
otis22894 | 1:c41a71a200d6 | 89 | if(changeCodeBool){ |
otis22894 | 1:c41a71a200d6 | 90 | lcd.cls(); |
otis22894 | 1:c41a71a200d6 | 91 | lcd.locate(0,0); |
otis22894 | 1:c41a71a200d6 | 92 | lcd.printf("%s","Enter New Code"); |
otis22894 | 1:c41a71a200d6 | 93 | lcd.locate(0,1); |
otis22894 | 1:c41a71a200d6 | 94 | bool first = true; |
otis22894 | 1:c41a71a200d6 | 95 | while(changeCodeBool){ |
otis22894 | 1:c41a71a200d6 | 96 | if(key.size()>0 && (!first && key.front()==0)){ |
otis22894 | 1:c41a71a200d6 | 97 | lcd.printf("%d ",key.front()); |
otis22894 | 1:c41a71a200d6 | 98 | Thread::wait(150); |
otis22894 | 1:c41a71a200d6 | 99 | first = false; |
otis22894 | 1:c41a71a200d6 | 100 | } |
otis22894 | 1:c41a71a200d6 | 101 | } |
otis22894 | 1:c41a71a200d6 | 102 | }else if(oldSelected != selected){ |
otis22894 | 1:c41a71a200d6 | 103 | for(int i = 0;i<menuOptions.size();i++){ |
otis22894 | 1:c41a71a200d6 | 104 | lcd.locate(0,i); |
otis22894 | 1:c41a71a200d6 | 105 | if(selected==i){ |
otis22894 | 1:c41a71a200d6 | 106 | lcd.textbackground_color(WHITE); |
otis22894 | 1:c41a71a200d6 | 107 | }else{ |
otis22894 | 1:c41a71a200d6 | 108 | lcd.textbackground_color(BLACK); |
otis22894 | 1:c41a71a200d6 | 109 | } |
otis22894 | 1:c41a71a200d6 | 110 | if(menuOptions[i].size()>17){ |
otis22894 | 1:c41a71a200d6 | 111 | lcd.printf("%s",menuOptions[i].substr(0,18).c_str()); |
otis22894 | 1:c41a71a200d6 | 112 | }else{ |
otis22894 | 1:c41a71a200d6 | 113 | lcd.printf("%s",menuOptions[i].c_str() + std::string(18-menuOptions[i].size(),' ')); |
otis22894 | 1:c41a71a200d6 | 114 | } |
otis22894 | 1:c41a71a200d6 | 115 | } |
otis22894 | 1:c41a71a200d6 | 116 | oldSelected = selected; |
otis22894 | 1:c41a71a200d6 | 117 | } |
otis22894 | 1:c41a71a200d6 | 118 | } |
otis22894 | 1:c41a71a200d6 | 119 | |
otis22894 | 1:c41a71a200d6 | 120 | void menuSelect(){ |
otis22894 | 1:c41a71a200d6 | 121 | changeCodeBool = true; |
otis22894 | 1:c41a71a200d6 | 122 | } |
otis22894 | 1:c41a71a200d6 | 123 | |
otis22894 | 1:c41a71a200d6 | 124 | void menuShiftUp(){ |
otis22894 | 1:c41a71a200d6 | 125 | selected = (selected + 1) % menuOptions.size(); |
otis22894 | 1:c41a71a200d6 | 126 | } |
otis22894 | 1:c41a71a200d6 | 127 | |
otis22894 | 1:c41a71a200d6 | 128 | void menuShiftDown(){ |
otis22894 | 1:c41a71a200d6 | 129 | selected--; |
otis22894 | 1:c41a71a200d6 | 130 | if(selected == -1){ |
otis22894 | 1:c41a71a200d6 | 131 | selected = menuOptions.size() - 1; |
otis22894 | 1:c41a71a200d6 | 132 | } |
otis22894 | 1:c41a71a200d6 | 133 | } |
otis22894 | 1:c41a71a200d6 | 134 | |
otis22894 | 0:0d31da041b5c | 135 | void rfid_thread(void const *args){ |
otis22894 | 0:0d31da041b5c | 136 | allowedCards = new char*[CARDSALLOWED]; |
otis22894 | 0:0d31da041b5c | 137 | for (int i = 0; i < CARDSALLOWED; ++i){ |
otis22894 | 0:0d31da041b5c | 138 | allowedCards[i] = new char[11]; |
otis22894 | 0:0d31da041b5c | 139 | } |
otis22894 | 0:0d31da041b5c | 140 | readCardsFromFile(); |
otis22894 | 0:0d31da041b5c | 141 | while(1){ |
otis22894 | 0:0d31da041b5c | 142 | rfid.doEvents(); |
otis22894 | 0:0d31da041b5c | 143 | Thread::wait(100); |
otis22894 | 0:0d31da041b5c | 144 | } |
otis22894 | 0:0d31da041b5c | 145 | } |
otis22894 | 0:0d31da041b5c | 146 | |
otis22894 | 0:0d31da041b5c | 147 | void onCardRead() { |
otis22894 | 0:0d31da041b5c | 148 | /*pc.printf("%d\n",rfid.bitsRead()); |
otis22894 | 0:0d31da041b5c | 149 | uint64_t rawCardData = rfid.getBits(14,33); |
otis22894 | 0:0d31da041b5c | 150 | pc.printf("%" PRIx64 "\n",rawCardData); |
otis22894 | 0:0d31da041b5c | 151 | pc.printf("%" PRIx64 "\n",rfid.getBits(0,34));*/ |
otis22894 | 0:0d31da041b5c | 152 | uint64_t cardData = rfid.getBits(0,34); |
otis22894 | 0:0d31da041b5c | 153 | ostringstream o; |
otis22894 | 0:0d31da041b5c | 154 | string str; |
otis22894 | 0:0d31da041b5c | 155 | o << cardData; |
otis22894 | 0:0d31da041b5c | 156 | str += o.str(); |
otis22894 | 0:0d31da041b5c | 157 | storeCard(str.c_str()); |
otis22894 | 0:0d31da041b5c | 158 | } |
otis22894 | 0:0d31da041b5c | 159 | |
otis22894 | 0:0d31da041b5c | 160 | bool isAllowed(const char *card){ |
otis22894 | 0:0d31da041b5c | 161 | for(int i = 0;i<allowedCount;i++){ |
otis22894 | 0:0d31da041b5c | 162 | for(int j = 0;j<11;j++){ |
otis22894 | 0:0d31da041b5c | 163 | if(card[j]!=allowedCards[i][j]){ |
otis22894 | 0:0d31da041b5c | 164 | break; |
otis22894 | 0:0d31da041b5c | 165 | }else if(j==10){ |
otis22894 | 0:0d31da041b5c | 166 | return true; |
otis22894 | 0:0d31da041b5c | 167 | } |
otis22894 | 0:0d31da041b5c | 168 | } |
otis22894 | 0:0d31da041b5c | 169 | } |
otis22894 | 0:0d31da041b5c | 170 | return false; |
otis22894 | 0:0d31da041b5c | 171 | } |
otis22894 | 0:0d31da041b5c | 172 | |
otis22894 | 0:0d31da041b5c | 173 | void storeCard(const char *card){ |
otis22894 | 0:0d31da041b5c | 174 | if(allowedCount < 10 && !isAllowed(card)){ |
otis22894 | 0:0d31da041b5c | 175 | FILE *fp = fopen("/local/cards.txt", "a"); |
otis22894 | 0:0d31da041b5c | 176 | if(fp==NULL){ |
otis22894 | 1:c41a71a200d6 | 177 | //ERROR |
otis22894 | 0:0d31da041b5c | 178 | } |
otis22894 | 0:0d31da041b5c | 179 | 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 | 180 | fclose(fp); |
otis22894 | 0:0d31da041b5c | 181 | for(int transfer = 0;transfer<11;transfer++){ |
otis22894 | 0:0d31da041b5c | 182 | allowedCards[allowedCount][transfer] = card[transfer]; |
otis22894 | 0:0d31da041b5c | 183 | } |
otis22894 | 0:0d31da041b5c | 184 | allowedCount++; |
otis22894 | 0:0d31da041b5c | 185 | } |
otis22894 | 0:0d31da041b5c | 186 | } |
otis22894 | 0:0d31da041b5c | 187 | |
otis22894 | 0:0d31da041b5c | 188 | int readCardsFromFile(){ |
otis22894 | 0:0d31da041b5c | 189 | FILE *fp = fopen("/local/cards.txt", "r"); |
otis22894 | 0:0d31da041b5c | 190 | int ret = readCardsFromFile(fp); |
otis22894 | 0:0d31da041b5c | 191 | return ret; |
otis22894 | 0:0d31da041b5c | 192 | } |
otis22894 | 0:0d31da041b5c | 193 | |
otis22894 | 0:0d31da041b5c | 194 | int readCardsFromFile(FILE *fp){ |
otis22894 | 0:0d31da041b5c | 195 | int c,transfer = 0; |
otis22894 | 0:0d31da041b5c | 196 | if(fp!=NULL){ |
otis22894 | 0:0d31da041b5c | 197 | c = fgetc(fp); |
otis22894 | 0:0d31da041b5c | 198 | while(c!=EOF){ |
otis22894 | 0:0d31da041b5c | 199 | allowedCards[allowedCount][transfer] = c; |
otis22894 | 0:0d31da041b5c | 200 | transfer++; |
otis22894 | 0:0d31da041b5c | 201 | if(transfer==11){ |
otis22894 | 0:0d31da041b5c | 202 | allowedCount++; |
otis22894 | 0:0d31da041b5c | 203 | if(allowedCount==CARDSALLOWED){ |
otis22894 | 0:0d31da041b5c | 204 | break; |
otis22894 | 0:0d31da041b5c | 205 | } |
otis22894 | 0:0d31da041b5c | 206 | transfer = 0; |
otis22894 | 0:0d31da041b5c | 207 | fgetc(fp); |
otis22894 | 0:0d31da041b5c | 208 | fgetc(fp); |
otis22894 | 0:0d31da041b5c | 209 | } |
otis22894 | 0:0d31da041b5c | 210 | c = fgetc(fp); |
otis22894 | 0:0d31da041b5c | 211 | } |
otis22894 | 0:0d31da041b5c | 212 | fclose(fp); |
otis22894 | 0:0d31da041b5c | 213 | return 1; |
otis22894 | 0:0d31da041b5c | 214 | }else{ |
otis22894 | 0:0d31da041b5c | 215 | return 0; |
otis22894 | 0:0d31da041b5c | 216 | } |
otis22894 | 0:0d31da041b5c | 217 | } |
otis22894 | 0:0d31da041b5c | 218 | |
otis22894 | 0:0d31da041b5c | 219 | void knock_thread(void const *args){ |
otis22894 | 0:0d31da041b5c | 220 | Thread::wait(1000); |
otis22894 | 0:0d31da041b5c | 221 | changeSecretKnock(); |
otis22894 | 0:0d31da041b5c | 222 | float newval; |
otis22894 | 0:0d31da041b5c | 223 | while(1){ |
otis22894 | 0:0d31da041b5c | 224 | newval = ain.read(); |
otis22894 | 0:0d31da041b5c | 225 | if(newval > 0.508 && newval < 0.538){ |
otis22894 | 0:0d31da041b5c | 226 | Thread::wait(50); |
otis22894 | 0:0d31da041b5c | 227 | captureKnock(); |
otis22894 | 0:0d31da041b5c | 228 | checkKnocks(); |
otis22894 | 0:0d31da041b5c | 229 | } |
otis22894 | 0:0d31da041b5c | 230 | Thread::wait(1); |
otis22894 | 0:0d31da041b5c | 231 | } |
otis22894 | 0:0d31da041b5c | 232 | } |
otis22894 | 0:0d31da041b5c | 233 | |
otis22894 | 0:0d31da041b5c | 234 | void checkKnocks(){ |
otis22894 | 0:0d31da041b5c | 235 | if(knock.size() != userKnock.size()){ |
otis22894 | 1:c41a71a200d6 | 236 | //DENIED |
otis22894 | 0:0d31da041b5c | 237 | }else{ |
otis22894 | 0:0d31da041b5c | 238 | bool access = true; |
otis22894 | 0:0d31da041b5c | 239 | for(int i = 0; i<knock.size(); i++){ |
otis22894 | 0:0d31da041b5c | 240 | if(!(abs(1-((float)knock[i])/((float)userKnock[i]))<0.15)){ |
otis22894 | 0:0d31da041b5c | 241 | access = false; |
otis22894 | 0:0d31da041b5c | 242 | } |
otis22894 | 0:0d31da041b5c | 243 | } |
otis22894 | 0:0d31da041b5c | 244 | if(access){ |
otis22894 | 1:c41a71a200d6 | 245 | //GRANTED |
otis22894 | 0:0d31da041b5c | 246 | }else{ |
otis22894 | 1:c41a71a200d6 | 247 | //GRANTED |
otis22894 | 0:0d31da041b5c | 248 | } |
otis22894 | 0:0d31da041b5c | 249 | } |
otis22894 | 0:0d31da041b5c | 250 | } |
otis22894 | 0:0d31da041b5c | 251 | |
otis22894 | 0:0d31da041b5c | 252 | void captureKnock(){ |
otis22894 | 0:0d31da041b5c | 253 | float val; |
otis22894 | 0:0d31da041b5c | 254 | userKnock.clear(); |
otis22894 | 0:0d31da041b5c | 255 | timer.reset(); |
otis22894 | 0:0d31da041b5c | 256 | int time; |
otis22894 | 0:0d31da041b5c | 257 | while(timer.read() < 2){ |
otis22894 | 0:0d31da041b5c | 258 | val = ain.read(); |
otis22894 | 0:0d31da041b5c | 259 | if(val > 0.508 && val < 0.538){ |
otis22894 | 0:0d31da041b5c | 260 | time = timer.read_ms(); |
otis22894 | 0:0d31da041b5c | 261 | userKnock.push_back(time); |
otis22894 | 0:0d31da041b5c | 262 | Thread::wait(50); |
otis22894 | 0:0d31da041b5c | 263 | timer.reset(); |
otis22894 | 0:0d31da041b5c | 264 | } |
otis22894 | 0:0d31da041b5c | 265 | Thread::wait(1); |
otis22894 | 0:0d31da041b5c | 266 | } |
otis22894 | 0:0d31da041b5c | 267 | } |
otis22894 | 0:0d31da041b5c | 268 | |
otis22894 | 0:0d31da041b5c | 269 | void changeSecretKnock(){ |
otis22894 | 0:0d31da041b5c | 270 | float val; |
otis22894 | 0:0d31da041b5c | 271 | Timer t; |
otis22894 | 0:0d31da041b5c | 272 | while(true){ |
otis22894 | 0:0d31da041b5c | 273 | val = ain.read(); |
otis22894 | 0:0d31da041b5c | 274 | if(val > 0.508 && val < 0.538){ |
otis22894 | 0:0d31da041b5c | 275 | break; |
otis22894 | 0:0d31da041b5c | 276 | } |
otis22894 | 0:0d31da041b5c | 277 | Thread::wait(1); |
otis22894 | 0:0d31da041b5c | 278 | } |
otis22894 | 0:0d31da041b5c | 279 | t.start(); |
otis22894 | 0:0d31da041b5c | 280 | int time; |
otis22894 | 0:0d31da041b5c | 281 | while(t.read() < 2){ |
otis22894 | 0:0d31da041b5c | 282 | val = ain.read(); |
otis22894 | 0:0d31da041b5c | 283 | if(val > 0.508 && val < 0.538){ |
otis22894 | 0:0d31da041b5c | 284 | time = t.read_ms(); |
otis22894 | 0:0d31da041b5c | 285 | knock.push_back(time); |
otis22894 | 0:0d31da041b5c | 286 | Thread::wait(50); |
otis22894 | 0:0d31da041b5c | 287 | t.reset(); |
otis22894 | 0:0d31da041b5c | 288 | } |
otis22894 | 0:0d31da041b5c | 289 | Thread::wait(1); |
otis22894 | 0:0d31da041b5c | 290 | } |
otis22894 | 0:0d31da041b5c | 291 | } |
otis22894 | 0:0d31da041b5c | 292 | |
otis22894 | 0:0d31da041b5c | 293 | bool checkCode(std::vector<int> master,std::vector<int> v1){ |
otis22894 | 0:0d31da041b5c | 294 | if(v1.size() != master.size()){ |
otis22894 | 0:0d31da041b5c | 295 | return false; |
otis22894 | 0:0d31da041b5c | 296 | }else{ |
otis22894 | 0:0d31da041b5c | 297 | for(int i = 0;i<master.size();i++){ |
otis22894 | 0:0d31da041b5c | 298 | if(master[i] != v1[i]){ |
otis22894 | 0:0d31da041b5c | 299 | return false; |
otis22894 | 0:0d31da041b5c | 300 | } |
otis22894 | 0:0d31da041b5c | 301 | } |
otis22894 | 0:0d31da041b5c | 302 | return true; |
otis22894 | 0:0d31da041b5c | 303 | } |
otis22894 | 0:0d31da041b5c | 304 | } |
otis22894 | 0:0d31da041b5c | 305 | |
otis22894 | 0:0d31da041b5c | 306 | void changeCode(){ |
otis22894 | 0:0d31da041b5c | 307 | std::vector<int> tempMaster; |
otis22894 | 0:0d31da041b5c | 308 | key.push_back(0); |
otis22894 | 0:0d31da041b5c | 309 | while(key.front() != 12){ |
otis22894 | 0:0d31da041b5c | 310 | key = keypad.getkey(); |
otis22894 | 0:0d31da041b5c | 311 | if(key.size()>0){ |
otis22894 | 0:0d31da041b5c | 312 | tempMaster.push_back(key.front()); |
otis22894 | 0:0d31da041b5c | 313 | } |
otis22894 | 0:0d31da041b5c | 314 | Thread::wait(100); |
otis22894 | 0:0d31da041b5c | 315 | } |
otis22894 | 0:0d31da041b5c | 316 | tempMaster.pop_back(); |
otis22894 | 0:0d31da041b5c | 317 | if(tempMaster.size() >= 4){ |
otis22894 | 0:0d31da041b5c | 318 | master.clear(); |
otis22894 | 0:0d31da041b5c | 319 | for(int i = 0;i<tempMaster.size();i++){ |
otis22894 | 0:0d31da041b5c | 320 | master.push_back(tempMaster[i]); |
otis22894 | 0:0d31da041b5c | 321 | } |
otis22894 | 1:c41a71a200d6 | 322 | }else{ |
otis22894 | 0:0d31da041b5c | 323 | } |
otis22894 | 0:0d31da041b5c | 324 | changeCodeBool = false; |
otis22894 | 1:c41a71a200d6 | 325 | oldSelected = -1; |
otis22894 | 0:0d31da041b5c | 326 | } |
otis22894 | 0:0d31da041b5c | 327 | |
otis22894 | 0:0d31da041b5c | 328 | void keypad_thread(void const *args){ |
otis22894 | 0:0d31da041b5c | 329 | std::vector<int> key; |
otis22894 | 0:0d31da041b5c | 330 | key.push_back(0); |
otis22894 | 0:0d31da041b5c | 331 | while(1) { |
otis22894 | 0:0d31da041b5c | 332 | while(key.front() != 12){ |
otis22894 | 0:0d31da041b5c | 333 | if(changeCodeBool){ |
otis22894 | 0:0d31da041b5c | 334 | changeCode(); |
otis22894 | 0:0d31da041b5c | 335 | } |
otis22894 | 0:0d31da041b5c | 336 | key = keypad.getkey(); |
otis22894 | 0:0d31da041b5c | 337 | if(key.size()>0){ |
otis22894 | 0:0d31da041b5c | 338 | keys.push_back(key.front()); |
otis22894 | 0:0d31da041b5c | 339 | } |
otis22894 | 0:0d31da041b5c | 340 | Thread::wait(100); |
otis22894 | 0:0d31da041b5c | 341 | } |
otis22894 | 0:0d31da041b5c | 342 | keys.pop_back(); |
otis22894 | 0:0d31da041b5c | 343 | bool result = checkCode(master,keys); |
otis22894 | 0:0d31da041b5c | 344 | int size = keys.size(); |
otis22894 | 0:0d31da041b5c | 345 | for(int i = 0;i<size;i++){ |
otis22894 | 0:0d31da041b5c | 346 | keys.erase(keys.begin()); |
otis22894 | 1:c41a71a200d6 | 347 | } |
otis22894 | 0:0d31da041b5c | 348 | keys.clear(); |
otis22894 | 0:0d31da041b5c | 349 | key.clear(); |
otis22894 | 0:0d31da041b5c | 350 | key.push_back(0); |
otis22894 | 0:0d31da041b5c | 351 | Thread::wait(100); |
otis22894 | 0:0d31da041b5c | 352 | } |
otis22894 | 0:0d31da041b5c | 353 | } |