diff --git a/Adafruit_Fingerprint.cpp b/Adafruit_Fingerprint.cpp index 3a974d5..1ada553 100755 --- a/Adafruit_Fingerprint.cpp +++ b/Adafruit_Fingerprint.cpp @@ -1,26 +1,25 @@ -/*************************************************** +/*************************************************** This is a library for our optical Fingerprint sensor - Designed specifically to work with the Adafruit Fingerprint sensor + Designed specifically to work with the Adafruit Fingerprint sensor ----> http://www.adafruit.com/products/751 - These displays use TTL Serial to communicate, 2 pins are required to + These displays use TTL Serial to communicate, 2 pins are required to interface - Adafruit invests time and resources providing this open source code, - please support Adafruit and open-source hardware by purchasing + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing products from Adafruit! - Written by Limor Fried/Ladyada for Adafruit Industries. + Written by Limor Fried/Ladyada for Adafruit Industries. BSD license, all text above must be included in any redistribution ****************************************************/ #include "Adafruit_Fingerprint.h" -#ifdef __AVR__ - #include +#if defined(__AVR__) || defined(ARDUINO_ESP8266_NODEMCU) #include #endif -#ifdef __AVR__ +#if defined(__AVR__) || defined(ARDUINO_ESP8266_NODEMCU) Adafruit_Fingerprint::Adafruit_Fingerprint(SoftwareSerial *ss) { thePassword = 0; theAddress = 0xFFFFFFFF; @@ -35,7 +34,7 @@ Adafruit_Fingerprint::Adafruit_Fingerprint(HardwareSerial *ss) { thePassword = 0; theAddress = 0xFFFFFFFF; -#ifdef __AVR__ +#if defined(__AVR__) || defined(ARDUINO_ESP8266_NODEMCU) swSerial = NULL; #endif hwSerial = ss; @@ -46,18 +45,18 @@ void Adafruit_Fingerprint::begin(uint16_t baudrate) { delay(1000); // one second delay to let the sensor 'boot up' if (hwSerial) hwSerial->begin(baudrate); -#ifdef __AVR__ +#if defined(__AVR__) || defined(ARDUINO_ESP8266_NODEMCU) if (swSerial) swSerial->begin(baudrate); #endif } boolean Adafruit_Fingerprint::verifyPassword(void) { - uint8_t packet[] = {FINGERPRINT_VERIFYPASSWORD, + uint8_t packet[] = {FINGERPRINT_VERIFYPASSWORD, (thePassword >> 24), (thePassword >> 16), (thePassword >> 8), thePassword}; writePacket(theAddress, FINGERPRINT_COMMANDPACKET, 7, packet); uint8_t len = getReply(packet); - + if ((len == 1) && (packet[0] == FINGERPRINT_ACKPACKET) && (packet[1] == FINGERPRINT_OK)) return true; @@ -75,7 +74,7 @@ uint8_t Adafruit_Fingerprint::getImage(void) { uint8_t packet[] = {FINGERPRINT_GETIMAGE}; writePacket(theAddress, FINGERPRINT_COMMANDPACKET, 3, packet); uint8_t len = getReply(packet); - + if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET)) return -1; return packet[1]; @@ -85,7 +84,7 @@ uint8_t Adafruit_Fingerprint::image2Tz(uint8_t slot) { uint8_t packet[] = {FINGERPRINT_IMAGE2TZ, slot}; writePacket(theAddress, FINGERPRINT_COMMANDPACKET, sizeof(packet)+2, packet); uint8_t len = getReply(packet); - + if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET)) return -1; return packet[1]; @@ -96,7 +95,7 @@ uint8_t Adafruit_Fingerprint::createModel(void) { uint8_t packet[] = {FINGERPRINT_REGMODEL}; writePacket(theAddress, FINGERPRINT_COMMANDPACKET, sizeof(packet)+2, packet); uint8_t len = getReply(packet); - + if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET)) return -1; return packet[1]; @@ -107,18 +106,18 @@ uint8_t Adafruit_Fingerprint::storeModel(uint16_t id) { uint8_t packet[] = {FINGERPRINT_STORE, 0x01, id >> 8, id & 0xFF}; writePacket(theAddress, FINGERPRINT_COMMANDPACKET, sizeof(packet)+2, packet); uint8_t len = getReply(packet); - + if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET)) return -1; return packet[1]; } - + //read a fingerprint template from flash into Char Buffer 1 uint8_t Adafruit_Fingerprint::loadModel(uint16_t id) { uint8_t packet[] = {FINGERPRINT_LOAD, 0x01, id >> 8, id & 0xFF}; writePacket(theAddress, FINGERPRINT_COMMANDPACKET, sizeof(packet)+2, packet); uint8_t len = getReply(packet); - + if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET)) return -1; return packet[1]; @@ -129,17 +128,17 @@ uint8_t Adafruit_Fingerprint::getModel(void) { uint8_t packet[] = {FINGERPRINT_UPLOAD, 0x01}; writePacket(theAddress, FINGERPRINT_COMMANDPACKET, sizeof(packet)+2, packet); uint8_t len = getReply(packet); - + if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET)) return -1; return packet[1]; } - + uint8_t Adafruit_Fingerprint::deleteModel(uint16_t id) { uint8_t packet[] = {FINGERPRINT_DELETE, id >> 8, id & 0xFF, 0x00, 0x01}; writePacket(theAddress, FINGERPRINT_COMMANDPACKET, sizeof(packet)+2, packet); uint8_t len = getReply(packet); - + if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET)) return -1; return packet[1]; @@ -149,7 +148,7 @@ uint8_t Adafruit_Fingerprint::emptyDatabase(void) { uint8_t packet[] = {FINGERPRINT_EMPTY}; writePacket(theAddress, FINGERPRINT_COMMANDPACKET, sizeof(packet)+2, packet); uint8_t len = getReply(packet); - + if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET)) return -1; return packet[1]; @@ -158,22 +157,22 @@ uint8_t Adafruit_Fingerprint::emptyDatabase(void) { uint8_t Adafruit_Fingerprint::fingerFastSearch(void) { fingerID = 0xFFFF; confidence = 0xFFFF; - // high speed search of slot #1 starting at page 0x0000 and page #0x00A3 + // high speed search of slot #1 starting at page 0x0000 and page #0x00A3 uint8_t packet[] = {FINGERPRINT_HISPEEDSEARCH, 0x01, 0x00, 0x00, 0x00, 0xA3}; writePacket(theAddress, FINGERPRINT_COMMANDPACKET, sizeof(packet)+2, packet); uint8_t len = getReply(packet); - + if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET)) return -1; fingerID = packet[2]; fingerID <<= 8; fingerID |= packet[3]; - + confidence = packet[4]; confidence <<= 8; confidence |= packet[5]; - + return packet[1]; } @@ -183,20 +182,20 @@ uint8_t Adafruit_Fingerprint::getTemplateCount(void) { uint8_t packet[] = {FINGERPRINT_TEMPLATECOUNT}; writePacket(theAddress, FINGERPRINT_COMMANDPACKET, sizeof(packet)+2, packet); uint8_t len = getReply(packet); - + if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET)) return -1; templateCount = packet[2]; templateCount <<= 8; templateCount |= packet[3]; - + return packet[1]; } -void Adafruit_Fingerprint::writePacket(uint32_t addr, uint8_t packettype, +void Adafruit_Fingerprint::writePacket(uint32_t addr, uint8_t packettype, uint16_t len, uint8_t *packet) { #ifdef FINGERPRINT_DEBUG Serial.print("---> 0x"); @@ -218,7 +217,7 @@ void Adafruit_Fingerprint::writePacket(uint32_t addr, uint8_t packettype, Serial.print(" 0x"); Serial.print((uint8_t)(len), HEX); #endif - + #if ARDUINO >= 100 mySerial->write((uint8_t)(FINGERPRINT_STARTCODE >> 8)); mySerial->write((uint8_t)FINGERPRINT_STARTCODE); @@ -240,7 +239,7 @@ void Adafruit_Fingerprint::writePacket(uint32_t addr, uint8_t packettype, mySerial->print((uint8_t)(len >> 8), BYTE); mySerial->print((uint8_t)(len), BYTE); #endif - + uint16_t sum = (len>>8) + (len&0xFF) + packettype; for (uint8_t i=0; i< len-2; i++) { #if ARDUINO >= 100 @@ -271,7 +270,7 @@ void Adafruit_Fingerprint::writePacket(uint32_t addr, uint8_t packettype, uint8_t Adafruit_Fingerprint::getReply(uint8_t packet[], uint16_t timeout) { uint8_t reply[20], idx; uint16_t timer=0; - + idx = 0; #ifdef FINGERPRINT_DEBUG Serial.print("<--- "); @@ -290,7 +289,7 @@ while (true) { if ((idx == 0) && (reply[0] != (FINGERPRINT_STARTCODE >> 8))) continue; idx++; - + // check packet! if (idx >= 9) { if ((reply[0] != (FINGERPRINT_STARTCODE >> 8)) || @@ -304,7 +303,7 @@ while (true) { len -= 2; //Serial.print("Packet len"); Serial.println(len); if (idx <= (len+10)) continue; - packet[0] = packettype; + packet[0] = packettype; for (uint8_t i=0; i http://www.adafruit.com/products/751 - These displays use TTL Serial to communicate, 2 pins are required to + These displays use TTL Serial to communicate, 2 pins are required to interface - Adafruit invests time and resources providing this open source code, - please support Adafruit and open-source hardware by purchasing + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing products from Adafruit! - Written by Limor Fried/Ladyada for Adafruit Industries. + Written by Limor Fried/Ladyada for Adafruit Industries. BSD license, all text above must be included in any redistribution ****************************************************/ #include "Arduino.h" -#ifdef __AVR__ +#if defined(__AVR__) || defined(ARDUINO_ESP8266_NODEMCU) #include #endif @@ -64,14 +64,14 @@ #define FINGERPRINT_HISPEEDSEARCH 0x1B #define FINGERPRINT_TEMPLATECOUNT 0x1D -//#define FINGERPRINT_DEBUG +//#define FINGERPRINT_DEBUG #define DEFAULTTIMEOUT 5000 // milliseconds class Adafruit_Fingerprint { public: -#ifdef __AVR__ +#if defined(__AVR__) || defined(ARDUINO_ESP8266_NODEMCU) Adafruit_Fingerprint(SoftwareSerial *); #endif Adafruit_Fingerprint(HardwareSerial *); @@ -95,12 +95,12 @@ class Adafruit_Fingerprint { uint16_t fingerID, confidence, templateCount; - private: + private: uint32_t thePassword; uint32_t theAddress; Stream *mySerial; -#ifdef __AVR__ +#if defined(__AVR__) || defined(ARDUINO_ESP8266_NODEMCU) SoftwareSerial *swSerial; #endif HardwareSerial *hwSerial;