diff options
author | Mateusz Maciaś <ursereg@gmail.com> | 2011-11-09 17:34:42 +0100 |
---|---|---|
committer | Mateusz Maciaś <ursereg@gmail.com> | 2011-11-09 17:34:42 +0100 |
commit | 2221cf504fc75511e7f52af8ab6bc2ab41f60e1f (patch) | |
tree | 86c0028841819efbcfe59d9b6bde183e3446a54f | |
parent | d94957a8cb430c49b9748974c41c61114e4ce595 (diff) | |
download | doorman-2221cf504fc75511e7f52af8ab6bc2ab41f60e1f.tar.gz doorman-2221cf504fc75511e7f52af8ab6bc2ab41f60e1f.tar.bz2 doorman-2221cf504fc75511e7f52af8ab6bc2ab41f60e1f.tar.xz doorman-2221cf504fc75511e7f52af8ab6bc2ab41f60e1f.zip |
Added pin compare.
-rw-r--r-- | arduino/arduino.pde | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/arduino/arduino.pde b/arduino/arduino.pde index 0a7c41a..d12d2d7 100644 --- a/arduino/arduino.pde +++ b/arduino/arduino.pde @@ -60,16 +60,26 @@ void setup() { /** Main loop. */ void loop(){ //Communication with mifare reader. - rf_comm(); + unsigned long rfid; + unsigned int id; + if (rf_comm(&rfid,&id)){ + unsigned int pin; + //User rfid is authorized, read pin here! + if (emem_check_pin(id,pin)) { + //Open door + } else { + //Wrong pin + } + } //Communication with PC (optional). pc_comm(); } /** * Function reads record with given id from EEPROM. - * @param adr Address of record to read. - * @param data Pointer to space for data. - * @param id Pointer to space for id. + * @param adr Address of record to read, or NULL. + * @param data Pointer to space for data, or NULL. + * @param id Pointer to space for id, or NULL. * @return Function returns true when data was red, false otherwise. */ boolean emem_get_record(unsigned int adr,unsigned long * data,unsigned int * id,unsigned int * pin){ @@ -81,9 +91,15 @@ boolean emem_get_record(unsigned int adr,unsigned long * data,unsigned int * id, for (int ii=0;ii<EMEM_RECORD_SIZE;ii++){ rec[ii] = EEPROM.read(adr*EMEM_RECORD_SIZE+ii); } - (*id)=rec[0]; - (*data)=(long(rec[1])<<24)|(long(rec[2])<<16)|(long(rec[3])<<8)|(long(rec[4])); - (*pin)=(rec[5]<<8)|(rec[6]); + if (id != NULL){ + (*id)=rec[0]; + } + if (data != NULL){ + (*data)=(long(rec[1])<<24)|(long(rec[2])<<16)|(long(rec[3])<<8)|(long(rec[4])); + } + if (pin != NULL){ + (*pin)=(rec[5]<<8)|(rec[6]); + } return true; } @@ -104,6 +120,18 @@ boolean emem_del_record(unsigned int adr){ } /** + * Checks if pin is correct. + * @param if User id. + * @param pin Pin code. + * @return True if pin is correct, false otherwise. + */ +boolean emem_check_pin(unsigned int id, unsigned int pin){ + unsigned int epin; + emem_get_record(id,NULL,NULL,&pin); + return (epin==pin); +} + +/** * Finds record with given ID. * @param id Record id. * @return Address of found record or EMEM_INV_ADR. @@ -426,10 +454,13 @@ boolean pc_parse(){ /** * Handles communication with rfid reader. + * @return Returns true when authorized user rfid was red, false otherwise. */ -void rf_comm() { +boolean rf_comm(unsigned long * p_rfid,unsigned int * p_id) { static unsigned long last_millis = 0; static unsigned long new_millis = 0; + (*p_id) = EMEM_INV_ADR; + (*p_rfid) = 0; new_millis = millis(); if (new_millis<last_millis){ last_millis=new_millis; @@ -446,10 +477,17 @@ void rf_comm() { if (pc_send_flag){ pc_send_flag=false; pc_print('S',data,id); - + + } + if (id!=EMEM_INV_ADR){ + //User is authorized + (*p_id) = id; + (*p_rfid) = data; + return true; } } } + return false; } /** |