for R503 fingerprint
Dependents: R503_fingerprint_HelloWorld
Fingerprint.cpp@0:35cd316e4b41, 2021-03-16 (annotated)
- Committer:
- cdupaty
- Date:
- Tue Mar 16 16:31:41 2021 +0000
- Revision:
- 0:35cd316e4b41
Version 0.1 with a lot of bugs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cdupaty | 0:35cd316e4b41 | 1 | /*! |
cdupaty | 0:35cd316e4b41 | 2 | * @file Fingerprint.cpp |
cdupaty | 0:35cd316e4b41 | 3 | * |
cdupaty | 0:35cd316e4b41 | 4 | * @mainpage Adafruit Fingerprint Sensor Library |
cdupaty | 0:35cd316e4b41 | 5 | * |
cdupaty | 0:35cd316e4b41 | 6 | * @section intro_sec Introduction |
cdupaty | 0:35cd316e4b41 | 7 | * |
cdupaty | 0:35cd316e4b41 | 8 | * This is a library for our optical Fingerprint sensor |
cdupaty | 0:35cd316e4b41 | 9 | * |
cdupaty | 0:35cd316e4b41 | 10 | * Designed specifically to work with the Adafruit Fingerprint sensor |
cdupaty | 0:35cd316e4b41 | 11 | * ---. http://www.adafruit.com/products/751 |
cdupaty | 0:35cd316e4b41 | 12 | * |
cdupaty | 0:35cd316e4b41 | 13 | * These displays use TTL Serial to communicate, 2 pins are required to |
cdupaty | 0:35cd316e4b41 | 14 | * interface |
cdupaty | 0:35cd316e4b41 | 15 | * Adafruit invests time and resources providing this open source code, |
cdupaty | 0:35cd316e4b41 | 16 | * please support Adafruit and open-source hardware by purchasing |
cdupaty | 0:35cd316e4b41 | 17 | * products from Adafruit! |
cdupaty | 0:35cd316e4b41 | 18 | * |
cdupaty | 0:35cd316e4b41 | 19 | * @section author Author |
cdupaty | 0:35cd316e4b41 | 20 | * |
cdupaty | 0:35cd316e4b41 | 21 | * Written by Limor Fried/Ladyada for Adafruit Industries. |
cdupaty | 0:35cd316e4b41 | 22 | * |
cdupaty | 0:35cd316e4b41 | 23 | * @section license License |
cdupaty | 0:35cd316e4b41 | 24 | * |
cdupaty | 0:35cd316e4b41 | 25 | * BSD license, all text above must be included in any redistribution |
cdupaty | 0:35cd316e4b41 | 26 | * |
cdupaty | 0:35cd316e4b41 | 27 | */ |
cdupaty | 0:35cd316e4b41 | 28 | |
cdupaty | 0:35cd316e4b41 | 29 | #include "Fingerprint.h" |
cdupaty | 0:35cd316e4b41 | 30 | |
cdupaty | 0:35cd316e4b41 | 31 | //#define FINGERPRINT_DEBUG |
cdupaty | 0:35cd316e4b41 | 32 | |
cdupaty | 0:35cd316e4b41 | 33 | /*! |
cdupaty | 0:35cd316e4b41 | 34 | * @brief Gets the command packet |
cdupaty | 0:35cd316e4b41 | 35 | */ |
cdupaty | 0:35cd316e4b41 | 36 | #define GET_CMD_PACKET(...) \ |
cdupaty | 0:35cd316e4b41 | 37 | uint8_t data[] = {__VA_ARGS__}; \ |
cdupaty | 0:35cd316e4b41 | 38 | Fingerprint_Packet packet(FINGERPRINT_COMMANDPACKET, sizeof(data),data); \ |
cdupaty | 0:35cd316e4b41 | 39 | writeStructuredPacket(packet); \ |
cdupaty | 0:35cd316e4b41 | 40 | if (getStructuredPacket(&packet) != FINGERPRINT_OK) \ |
cdupaty | 0:35cd316e4b41 | 41 | return FINGERPRINT_PACKETRECIEVEERR; \ |
cdupaty | 0:35cd316e4b41 | 42 | if (packet->type != FINGERPRINT_ACKPACKET) \ |
cdupaty | 0:35cd316e4b41 | 43 | return FINGERPRINT_PACKETRECIEVEERR; |
cdupaty | 0:35cd316e4b41 | 44 | |
cdupaty | 0:35cd316e4b41 | 45 | /*! |
cdupaty | 0:35cd316e4b41 | 46 | * @brief Sends the command packet |
cdupaty | 0:35cd316e4b41 | 47 | */ |
cdupaty | 0:35cd316e4b41 | 48 | #define SEND_CMD_PACKET(...) \ |
cdupaty | 0:35cd316e4b41 | 49 | GET_CMD_PACKET(__VA_ARGS__); \ |
cdupaty | 0:35cd316e4b41 | 50 | return packet->data[0]; |
cdupaty | 0:35cd316e4b41 | 51 | |
cdupaty | 0:35cd316e4b41 | 52 | /*************************************************************************** |
cdupaty | 0:35cd316e4b41 | 53 | PUBLIC FUNCTIONS |
cdupaty | 0:35cd316e4b41 | 54 | ***************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 55 | |
cdupaty | 0:35cd316e4b41 | 56 | |
cdupaty | 0:35cd316e4b41 | 57 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 58 | /*! |
cdupaty | 0:35cd316e4b41 | 59 | @brief Instantiates sensor with Software Serial |
cdupaty | 0:35cd316e4b41 | 60 | @param ss Pointer to SoftwareSerial object |
cdupaty | 0:35cd316e4b41 | 61 | @param password 32-bit integer password (default is 0) |
cdupaty | 0:35cd316e4b41 | 62 | */ |
cdupaty | 0:35cd316e4b41 | 63 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 64 | Fingerprint::Fingerprint(PinName serialTX, PinName serialRX, PinName reset, uint32_t password) : |
cdupaty | 0:35cd316e4b41 | 65 | R503Serial(serialTX, serialRX), _reset(reset) |
cdupaty | 0:35cd316e4b41 | 66 | { |
cdupaty | 0:35cd316e4b41 | 67 | thePassword = password; |
cdupaty | 0:35cd316e4b41 | 68 | theAddress = 0xFFFFFFFF; |
cdupaty | 0:35cd316e4b41 | 69 | |
cdupaty | 0:35cd316e4b41 | 70 | status_reg = 0x0; ///< The status register (set by getParameters) |
cdupaty | 0:35cd316e4b41 | 71 | system_id = 0x0; ///< The system identifier (set by getParameters) |
cdupaty | 0:35cd316e4b41 | 72 | capacity = 64; ///< The fingerprint capacity (set by getParameters) |
cdupaty | 0:35cd316e4b41 | 73 | security_level = 0; ///< The security level (set by getParameters) |
cdupaty | 0:35cd316e4b41 | 74 | device_addr = 0xFFFFFFFF; ///< The device address (set by getParameters) |
cdupaty | 0:35cd316e4b41 | 75 | packet_len = 64; ///< The max packet length (set by getParameters) |
cdupaty | 0:35cd316e4b41 | 76 | baud_rate = 57600; ///< The UART baud rate (set by getParameters) |
cdupaty | 0:35cd316e4b41 | 77 | |
cdupaty | 0:35cd316e4b41 | 78 | } |
cdupaty | 0:35cd316e4b41 | 79 | |
cdupaty | 0:35cd316e4b41 | 80 | |
cdupaty | 0:35cd316e4b41 | 81 | |
cdupaty | 0:35cd316e4b41 | 82 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 83 | /*! |
cdupaty | 0:35cd316e4b41 | 84 | @brief Initializes serial interface and baud rate |
cdupaty | 0:35cd316e4b41 | 85 | @param baudrate Sensor's UART baud rate (usually 57600, 9600 or 115200) |
cdupaty | 0:35cd316e4b41 | 86 | */ |
cdupaty | 0:35cd316e4b41 | 87 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 88 | void Fingerprint::begin(uint32_t baudrate) { |
cdupaty | 0:35cd316e4b41 | 89 | R503Serial.baud(baudrate); |
cdupaty | 0:35cd316e4b41 | 90 | } |
cdupaty | 0:35cd316e4b41 | 91 | |
cdupaty | 0:35cd316e4b41 | 92 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 93 | /*! |
cdupaty | 0:35cd316e4b41 | 94 | @brief Verifies the sensors' access password (default password is |
cdupaty | 0:35cd316e4b41 | 95 | 0x0000000). A good way to also check if the sensors is active and responding |
cdupaty | 0:35cd316e4b41 | 96 | @returns True if password is correct |
cdupaty | 0:35cd316e4b41 | 97 | */ |
cdupaty | 0:35cd316e4b41 | 98 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 99 | bool Fingerprint::verifyPassword(void) { |
cdupaty | 0:35cd316e4b41 | 100 | return checkPassword() == FINGERPRINT_OK; |
cdupaty | 0:35cd316e4b41 | 101 | } |
cdupaty | 0:35cd316e4b41 | 102 | |
cdupaty | 0:35cd316e4b41 | 103 | uint8_t Fingerprint::checkPassword(void) { |
cdupaty | 0:35cd316e4b41 | 104 | GET_CMD_PACKET(FINGERPRINT_VERIFYPASSWORD, (uint8_t)(thePassword >> 24),(uint8_t)(thePassword >> 16), (uint8_t)(thePassword >> 8),(uint8_t)(thePassword & 0xFF)); |
cdupaty | 0:35cd316e4b41 | 105 | if (packet->data[0] == FINGERPRINT_OK) |
cdupaty | 0:35cd316e4b41 | 106 | return FINGERPRINT_OK; |
cdupaty | 0:35cd316e4b41 | 107 | else |
cdupaty | 0:35cd316e4b41 | 108 | return FINGERPRINT_PACKETRECIEVEERR; |
cdupaty | 0:35cd316e4b41 | 109 | } |
cdupaty | 0:35cd316e4b41 | 110 | |
cdupaty | 0:35cd316e4b41 | 111 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 112 | /*! |
cdupaty | 0:35cd316e4b41 | 113 | @brief Get the sensors parameters, fills in the member variables |
cdupaty | 0:35cd316e4b41 | 114 | status_reg, system_id, capacity, security_level, device_addr, packet_len |
cdupaty | 0:35cd316e4b41 | 115 | and baud_rate |
cdupaty | 0:35cd316e4b41 | 116 | @returns True if password is correct |
cdupaty | 0:35cd316e4b41 | 117 | */ |
cdupaty | 0:35cd316e4b41 | 118 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 119 | uint8_t Fingerprint::getParameters(void) { |
cdupaty | 0:35cd316e4b41 | 120 | GET_CMD_PACKET(FINGERPRINT_READSYSPARAM); |
cdupaty | 0:35cd316e4b41 | 121 | |
cdupaty | 0:35cd316e4b41 | 122 | status_reg = ((uint16_t)packet->data[1] << 8) | packet->data[2]; |
cdupaty | 0:35cd316e4b41 | 123 | system_id = ((uint16_t)packet->data[3] << 8) | packet->data[4]; |
cdupaty | 0:35cd316e4b41 | 124 | capacity = ((uint16_t)packet->data[5] << 8) | packet->data[6]; |
cdupaty | 0:35cd316e4b41 | 125 | security_level = ((uint16_t)packet->data[7] << 8) | packet->data[8]; |
cdupaty | 0:35cd316e4b41 | 126 | device_addr = ((uint32_t)packet->data[9] << 24) | |
cdupaty | 0:35cd316e4b41 | 127 | ((uint32_t)packet->data[10] << 16) | |
cdupaty | 0:35cd316e4b41 | 128 | ((uint32_t)packet->data[11] << 8) | (uint32_t)packet->data[12]; |
cdupaty | 0:35cd316e4b41 | 129 | packet_len = ((uint16_t)packet->data[13] << 8) | packet->data[14]; |
cdupaty | 0:35cd316e4b41 | 130 | if (packet_len == 0) { |
cdupaty | 0:35cd316e4b41 | 131 | packet_len = 32; |
cdupaty | 0:35cd316e4b41 | 132 | } else if (packet_len == 1) { |
cdupaty | 0:35cd316e4b41 | 133 | packet_len = 64; |
cdupaty | 0:35cd316e4b41 | 134 | } else if (packet_len == 2) { |
cdupaty | 0:35cd316e4b41 | 135 | packet_len = 128; |
cdupaty | 0:35cd316e4b41 | 136 | } else if (packet_len == 3) { |
cdupaty | 0:35cd316e4b41 | 137 | packet_len = 256; |
cdupaty | 0:35cd316e4b41 | 138 | } |
cdupaty | 0:35cd316e4b41 | 139 | baud_rate = (((uint16_t)packet->data[15] << 8) | packet->data[16]) * 9600; |
cdupaty | 0:35cd316e4b41 | 140 | |
cdupaty | 0:35cd316e4b41 | 141 | return packet->data[0]; |
cdupaty | 0:35cd316e4b41 | 142 | } |
cdupaty | 0:35cd316e4b41 | 143 | |
cdupaty | 0:35cd316e4b41 | 144 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 145 | /*! |
cdupaty | 0:35cd316e4b41 | 146 | @brief Ask the sensor to take an image of the finger pressed on surface |
cdupaty | 0:35cd316e4b41 | 147 | @returns <code>FINGERPRINT_OK</code> on success |
cdupaty | 0:35cd316e4b41 | 148 | @returns <code>FINGERPRINT_NOFINGER</code> if no finger detected |
cdupaty | 0:35cd316e4b41 | 149 | @returns <code>FINGERPRINT_PACKETRECIEVEERR</code> on communication error |
cdupaty | 0:35cd316e4b41 | 150 | @returns <code>FINGERPRINT_IMAGEFAIL</code> on imaging error |
cdupaty | 0:35cd316e4b41 | 151 | */ |
cdupaty | 0:35cd316e4b41 | 152 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 153 | uint8_t Fingerprint::getImage(void) { |
cdupaty | 0:35cd316e4b41 | 154 | SEND_CMD_PACKET(FINGERPRINT_GETIMAGE); |
cdupaty | 0:35cd316e4b41 | 155 | } |
cdupaty | 0:35cd316e4b41 | 156 | |
cdupaty | 0:35cd316e4b41 | 157 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 158 | /*! |
cdupaty | 0:35cd316e4b41 | 159 | @brief Ask the sensor to convert image to feature template |
cdupaty | 0:35cd316e4b41 | 160 | @param slot Location to place feature template (put one in 1 and another in |
cdupaty | 0:35cd316e4b41 | 161 | 2 for verification to create model) |
cdupaty | 0:35cd316e4b41 | 162 | @returns <code>FINGERPRINT_OK</code> on success |
cdupaty | 0:35cd316e4b41 | 163 | @returns <code>FINGERPRINT_IMAGEMESS</code> if image is too messy |
cdupaty | 0:35cd316e4b41 | 164 | @returns <code>FINGERPRINT_PACKETRECIEVEERR</code> on communication error |
cdupaty | 0:35cd316e4b41 | 165 | @returns <code>FINGERPRINT_FEATUREFAIL</code> on failure to identify |
cdupaty | 0:35cd316e4b41 | 166 | fingerprint features |
cdupaty | 0:35cd316e4b41 | 167 | @returns <code>FINGERPRINT_INVALIDIMAGE</code> on failure to identify |
cdupaty | 0:35cd316e4b41 | 168 | fingerprint features |
cdupaty | 0:35cd316e4b41 | 169 | */ |
cdupaty | 0:35cd316e4b41 | 170 | uint8_t Fingerprint::image2Tz(uint8_t slot) { |
cdupaty | 0:35cd316e4b41 | 171 | SEND_CMD_PACKET(FINGERPRINT_IMAGE2TZ, slot); |
cdupaty | 0:35cd316e4b41 | 172 | } |
cdupaty | 0:35cd316e4b41 | 173 | |
cdupaty | 0:35cd316e4b41 | 174 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 175 | /*! |
cdupaty | 0:35cd316e4b41 | 176 | @brief Ask the sensor to take two print feature template and create a |
cdupaty | 0:35cd316e4b41 | 177 | model |
cdupaty | 0:35cd316e4b41 | 178 | @returns <code>FINGERPRINT_OK</code> on success |
cdupaty | 0:35cd316e4b41 | 179 | @returns <code>FINGERPRINT_PACKETRECIEVEERR</code> on communication error |
cdupaty | 0:35cd316e4b41 | 180 | @returns <code>FINGERPRINT_ENROLLMISMATCH</code> on mismatch of fingerprints |
cdupaty | 0:35cd316e4b41 | 181 | */ |
cdupaty | 0:35cd316e4b41 | 182 | uint8_t Fingerprint::createModel(void) { |
cdupaty | 0:35cd316e4b41 | 183 | SEND_CMD_PACKET(FINGERPRINT_REGMODEL); |
cdupaty | 0:35cd316e4b41 | 184 | } |
cdupaty | 0:35cd316e4b41 | 185 | |
cdupaty | 0:35cd316e4b41 | 186 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 187 | /*! |
cdupaty | 0:35cd316e4b41 | 188 | @brief Ask the sensor to store the calculated model for later matching |
cdupaty | 0:35cd316e4b41 | 189 | @param location The model location # |
cdupaty | 0:35cd316e4b41 | 190 | @returns <code>FINGERPRINT_OK</code> on success |
cdupaty | 0:35cd316e4b41 | 191 | @returns <code>FINGERPRINT_BADLOCATION</code> if the location is invalid |
cdupaty | 0:35cd316e4b41 | 192 | @returns <code>FINGERPRINT_FLASHERR</code> if the model couldn't be written |
cdupaty | 0:35cd316e4b41 | 193 | to flash memory |
cdupaty | 0:35cd316e4b41 | 194 | @returns <code>FINGERPRINT_PACKETRECIEVEERR</code> on communication error |
cdupaty | 0:35cd316e4b41 | 195 | */ |
cdupaty | 0:35cd316e4b41 | 196 | uint8_t Fingerprint::storeModel(uint16_t location) { |
cdupaty | 0:35cd316e4b41 | 197 | SEND_CMD_PACKET(FINGERPRINT_STORE, 0x01, (uint8_t)(location >> 8), |
cdupaty | 0:35cd316e4b41 | 198 | (uint8_t)(location & 0xFF)); |
cdupaty | 0:35cd316e4b41 | 199 | } |
cdupaty | 0:35cd316e4b41 | 200 | |
cdupaty | 0:35cd316e4b41 | 201 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 202 | /*! |
cdupaty | 0:35cd316e4b41 | 203 | @brief Ask the sensor to load a fingerprint model from flash into buffer 1 |
cdupaty | 0:35cd316e4b41 | 204 | @param location The model location # |
cdupaty | 0:35cd316e4b41 | 205 | @returns <code>FINGERPRINT_OK</code> on success |
cdupaty | 0:35cd316e4b41 | 206 | @returns <code>FINGERPRINT_BADLOCATION</code> if the location is invalid |
cdupaty | 0:35cd316e4b41 | 207 | @returns <code>FINGERPRINT_PACKETRECIEVEERR</code> on communication error |
cdupaty | 0:35cd316e4b41 | 208 | */ |
cdupaty | 0:35cd316e4b41 | 209 | uint8_t Fingerprint::loadModel(uint16_t location) { |
cdupaty | 0:35cd316e4b41 | 210 | SEND_CMD_PACKET(FINGERPRINT_LOAD, 0x01, (uint8_t)(location >> 8), |
cdupaty | 0:35cd316e4b41 | 211 | (uint8_t)(location & 0xFF)); |
cdupaty | 0:35cd316e4b41 | 212 | } |
cdupaty | 0:35cd316e4b41 | 213 | |
cdupaty | 0:35cd316e4b41 | 214 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 215 | /*! |
cdupaty | 0:35cd316e4b41 | 216 | @brief Ask the sensor to transfer 256-byte fingerprint template from the |
cdupaty | 0:35cd316e4b41 | 217 | buffer to the UART |
cdupaty | 0:35cd316e4b41 | 218 | @returns <code>FINGERPRINT_OK</code> on success |
cdupaty | 0:35cd316e4b41 | 219 | @returns <code>FINGERPRINT_PACKETRECIEVEERR</code> on communication error |
cdupaty | 0:35cd316e4b41 | 220 | */ |
cdupaty | 0:35cd316e4b41 | 221 | uint8_t Fingerprint::getModel(void) { |
cdupaty | 0:35cd316e4b41 | 222 | SEND_CMD_PACKET(FINGERPRINT_UPLOAD, 0x01); |
cdupaty | 0:35cd316e4b41 | 223 | } |
cdupaty | 0:35cd316e4b41 | 224 | |
cdupaty | 0:35cd316e4b41 | 225 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 226 | /*! |
cdupaty | 0:35cd316e4b41 | 227 | @brief Ask the sensor to delete a model in memory |
cdupaty | 0:35cd316e4b41 | 228 | @param location The model location # |
cdupaty | 0:35cd316e4b41 | 229 | @returns <code>FINGERPRINT_OK</code> on success |
cdupaty | 0:35cd316e4b41 | 230 | @returns <code>FINGERPRINT_BADLOCATION</code> if the location is invalid |
cdupaty | 0:35cd316e4b41 | 231 | @returns <code>FINGERPRINT_FLASHERR</code> if the model couldn't be written |
cdupaty | 0:35cd316e4b41 | 232 | to flash memory |
cdupaty | 0:35cd316e4b41 | 233 | @returns <code>FINGERPRINT_PACKETRECIEVEERR</code> on communication error |
cdupaty | 0:35cd316e4b41 | 234 | */ |
cdupaty | 0:35cd316e4b41 | 235 | uint8_t Fingerprint::deleteModel(uint16_t location) { |
cdupaty | 0:35cd316e4b41 | 236 | SEND_CMD_PACKET(FINGERPRINT_DELETE, (uint8_t)(location >> 8), |
cdupaty | 0:35cd316e4b41 | 237 | (uint8_t)(location & 0xFF), 0x00, 0x01); |
cdupaty | 0:35cd316e4b41 | 238 | } |
cdupaty | 0:35cd316e4b41 | 239 | |
cdupaty | 0:35cd316e4b41 | 240 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 241 | /*! |
cdupaty | 0:35cd316e4b41 | 242 | @brief Ask the sensor to delete ALL models in memory |
cdupaty | 0:35cd316e4b41 | 243 | @returns <code>FINGERPRINT_OK</code> on success |
cdupaty | 0:35cd316e4b41 | 244 | @returns <code>FINGERPRINT_BADLOCATION</code> if the location is invalid |
cdupaty | 0:35cd316e4b41 | 245 | @returns <code>FINGERPRINT_FLASHERR</code> if the model couldn't be written |
cdupaty | 0:35cd316e4b41 | 246 | to flash memory |
cdupaty | 0:35cd316e4b41 | 247 | @returns <code>FINGERPRINT_PACKETRECIEVEERR</code> on communication error |
cdupaty | 0:35cd316e4b41 | 248 | */ |
cdupaty | 0:35cd316e4b41 | 249 | uint8_t Fingerprint::emptyDatabase(void) { |
cdupaty | 0:35cd316e4b41 | 250 | SEND_CMD_PACKET(FINGERPRINT_EMPTY); |
cdupaty | 0:35cd316e4b41 | 251 | } |
cdupaty | 0:35cd316e4b41 | 252 | |
cdupaty | 0:35cd316e4b41 | 253 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 254 | /*! |
cdupaty | 0:35cd316e4b41 | 255 | @brief Ask the sensor to search the current slot 1 fingerprint features to |
cdupaty | 0:35cd316e4b41 | 256 | match saved templates. The matching location is stored in <b>fingerID</b> and |
cdupaty | 0:35cd316e4b41 | 257 | the matching confidence in <b>confidence</b> |
cdupaty | 0:35cd316e4b41 | 258 | @returns <code>FINGERPRINT_OK</code> on fingerprint match success |
cdupaty | 0:35cd316e4b41 | 259 | @returns <code>FINGERPRINT_NOTFOUND</code> no match made |
cdupaty | 0:35cd316e4b41 | 260 | @returns <code>FINGERPRINT_PACKETRECIEVEERR</code> on communication error |
cdupaty | 0:35cd316e4b41 | 261 | */ |
cdupaty | 0:35cd316e4b41 | 262 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 263 | uint8_t Fingerprint::fingerFastSearch(void) { |
cdupaty | 0:35cd316e4b41 | 264 | // high speed search of slot #1 starting at page 0x0000 and page #0x00A3 |
cdupaty | 0:35cd316e4b41 | 265 | GET_CMD_PACKET(FINGERPRINT_HISPEEDSEARCH, 0x01, 0x00, 0x00, 0x00, 0xA3); |
cdupaty | 0:35cd316e4b41 | 266 | fingerID = 0xFFFF; |
cdupaty | 0:35cd316e4b41 | 267 | confidence = 0xFFFF; |
cdupaty | 0:35cd316e4b41 | 268 | |
cdupaty | 0:35cd316e4b41 | 269 | fingerID = packet->data[1]; |
cdupaty | 0:35cd316e4b41 | 270 | fingerID <<= 8; |
cdupaty | 0:35cd316e4b41 | 271 | fingerID |= packet->data[2]; |
cdupaty | 0:35cd316e4b41 | 272 | |
cdupaty | 0:35cd316e4b41 | 273 | confidence = packet->data[3]; |
cdupaty | 0:35cd316e4b41 | 274 | confidence <<= 8; |
cdupaty | 0:35cd316e4b41 | 275 | confidence |= packet->data[4]; |
cdupaty | 0:35cd316e4b41 | 276 | |
cdupaty | 0:35cd316e4b41 | 277 | return packet->data[0]; |
cdupaty | 0:35cd316e4b41 | 278 | } |
cdupaty | 0:35cd316e4b41 | 279 | |
cdupaty | 0:35cd316e4b41 | 280 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 281 | /*! |
cdupaty | 0:35cd316e4b41 | 282 | @brief Control the built in LED |
cdupaty | 0:35cd316e4b41 | 283 | @param on True if you want LED on, False to turn LED off |
cdupaty | 0:35cd316e4b41 | 284 | @returns <code>FINGERPRINT_OK</code> on success |
cdupaty | 0:35cd316e4b41 | 285 | */ |
cdupaty | 0:35cd316e4b41 | 286 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 287 | uint8_t Fingerprint::LEDcontrol(bool on) { |
cdupaty | 0:35cd316e4b41 | 288 | if (on) { |
cdupaty | 0:35cd316e4b41 | 289 | SEND_CMD_PACKET(FINGERPRINT_LEDON); |
cdupaty | 0:35cd316e4b41 | 290 | } else { |
cdupaty | 0:35cd316e4b41 | 291 | SEND_CMD_PACKET(FINGERPRINT_LEDOFF); |
cdupaty | 0:35cd316e4b41 | 292 | } |
cdupaty | 0:35cd316e4b41 | 293 | } |
cdupaty | 0:35cd316e4b41 | 294 | |
cdupaty | 0:35cd316e4b41 | 295 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 296 | /*! |
cdupaty | 0:35cd316e4b41 | 297 | @brief Control the built in Aura LED (if exists). Check datasheet/manual |
cdupaty | 0:35cd316e4b41 | 298 | for different colors and control codes available |
cdupaty | 0:35cd316e4b41 | 299 | @param control The control code (e.g. breathing, full on) |
cdupaty | 0:35cd316e4b41 | 300 | @param speed How fast to go through the breathing/blinking cycles |
cdupaty | 0:35cd316e4b41 | 301 | @param coloridx What color to light the indicator |
cdupaty | 0:35cd316e4b41 | 302 | @param count How many repeats of blinks/breathing cycles |
cdupaty | 0:35cd316e4b41 | 303 | @returns <code>FINGERPRINT_OK</code> on fingerprint match success |
cdupaty | 0:35cd316e4b41 | 304 | @returns <code>FINGERPRINT_NOTFOUND</code> no match made |
cdupaty | 0:35cd316e4b41 | 305 | @returns <code>FINGERPRINT_PACKETRECIEVEERR</code> on communication error |
cdupaty | 0:35cd316e4b41 | 306 | */ |
cdupaty | 0:35cd316e4b41 | 307 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 308 | uint8_t Fingerprint::LEDcontrol(uint8_t control, uint8_t speed, |
cdupaty | 0:35cd316e4b41 | 309 | uint8_t coloridx, uint8_t count) { |
cdupaty | 0:35cd316e4b41 | 310 | SEND_CMD_PACKET(FINGERPRINT_AURALEDCONFIG, control, speed, coloridx, count); |
cdupaty | 0:35cd316e4b41 | 311 | } |
cdupaty | 0:35cd316e4b41 | 312 | |
cdupaty | 0:35cd316e4b41 | 313 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 314 | /*! |
cdupaty | 0:35cd316e4b41 | 315 | @brief Ask the sensor to search the current slot fingerprint features to |
cdupaty | 0:35cd316e4b41 | 316 | match saved templates. The matching location is stored in <b>fingerID</b> and |
cdupaty | 0:35cd316e4b41 | 317 | the matching confidence in <b>confidence</b> |
cdupaty | 0:35cd316e4b41 | 318 | @param slot The slot to use for the print search, defaults to 1 |
cdupaty | 0:35cd316e4b41 | 319 | @returns <code>FINGERPRINT_OK</code> on fingerprint match success |
cdupaty | 0:35cd316e4b41 | 320 | @returns <code>FINGERPRINT_NOTFOUND</code> no match made |
cdupaty | 0:35cd316e4b41 | 321 | @returns <code>FINGERPRINT_PACKETRECIEVEERR</code> on communication error |
cdupaty | 0:35cd316e4b41 | 322 | */ |
cdupaty | 0:35cd316e4b41 | 323 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 324 | uint8_t Fingerprint::fingerSearch(uint8_t slot) { |
cdupaty | 0:35cd316e4b41 | 325 | // search of slot starting thru the capacity |
cdupaty | 0:35cd316e4b41 | 326 | GET_CMD_PACKET(FINGERPRINT_SEARCH, slot, 0x00, 0x00, capacity >> 8, |
cdupaty | 0:35cd316e4b41 | 327 | capacity & 0xFF); |
cdupaty | 0:35cd316e4b41 | 328 | |
cdupaty | 0:35cd316e4b41 | 329 | fingerID = 0xFFFF; |
cdupaty | 0:35cd316e4b41 | 330 | confidence = 0xFFFF; |
cdupaty | 0:35cd316e4b41 | 331 | |
cdupaty | 0:35cd316e4b41 | 332 | fingerID = packet->data[1]; |
cdupaty | 0:35cd316e4b41 | 333 | fingerID <<= 8; |
cdupaty | 0:35cd316e4b41 | 334 | fingerID |= packet->data[2]; |
cdupaty | 0:35cd316e4b41 | 335 | |
cdupaty | 0:35cd316e4b41 | 336 | confidence = packet->data[3]; |
cdupaty | 0:35cd316e4b41 | 337 | confidence <<= 8; |
cdupaty | 0:35cd316e4b41 | 338 | confidence |= packet->data[4]; |
cdupaty | 0:35cd316e4b41 | 339 | |
cdupaty | 0:35cd316e4b41 | 340 | return packet->data[0]; |
cdupaty | 0:35cd316e4b41 | 341 | } |
cdupaty | 0:35cd316e4b41 | 342 | |
cdupaty | 0:35cd316e4b41 | 343 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 344 | /*! |
cdupaty | 0:35cd316e4b41 | 345 | @brief Ask the sensor for the number of templates stored in memory. The |
cdupaty | 0:35cd316e4b41 | 346 | number is stored in <b>templateCount</b> on success. |
cdupaty | 0:35cd316e4b41 | 347 | @returns <code>FINGERPRINT_OK</code> on success |
cdupaty | 0:35cd316e4b41 | 348 | @returns <code>FINGERPRINT_PACKETRECIEVEERR</code> on communication error |
cdupaty | 0:35cd316e4b41 | 349 | */ |
cdupaty | 0:35cd316e4b41 | 350 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 351 | uint8_t Fingerprint::getTemplateCount(void) { |
cdupaty | 0:35cd316e4b41 | 352 | GET_CMD_PACKET(FINGERPRINT_TEMPLATECOUNT); |
cdupaty | 0:35cd316e4b41 | 353 | |
cdupaty | 0:35cd316e4b41 | 354 | templateCount = packet->data[1]; |
cdupaty | 0:35cd316e4b41 | 355 | templateCount <<= 8; |
cdupaty | 0:35cd316e4b41 | 356 | templateCount |= packet->data[2]; |
cdupaty | 0:35cd316e4b41 | 357 | |
cdupaty | 0:35cd316e4b41 | 358 | return packet->data[0]; |
cdupaty | 0:35cd316e4b41 | 359 | } |
cdupaty | 0:35cd316e4b41 | 360 | |
cdupaty | 0:35cd316e4b41 | 361 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 362 | /*! |
cdupaty | 0:35cd316e4b41 | 363 | @brief Set the password on the sensor (future communication will require |
cdupaty | 0:35cd316e4b41 | 364 | password verification so don't forget it!!!) |
cdupaty | 0:35cd316e4b41 | 365 | @param password 32-bit password code |
cdupaty | 0:35cd316e4b41 | 366 | @returns <code>FINGERPRINT_OK</code> on success |
cdupaty | 0:35cd316e4b41 | 367 | @returns <code>FINGERPRINT_PACKETRECIEVEERR</code> on communication error |
cdupaty | 0:35cd316e4b41 | 368 | */ |
cdupaty | 0:35cd316e4b41 | 369 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 370 | uint8_t Fingerprint::setPassword(uint32_t password) { |
cdupaty | 0:35cd316e4b41 | 371 | SEND_CMD_PACKET(FINGERPRINT_SETPASSWORD, (password >> 24), (password >> 16), |
cdupaty | 0:35cd316e4b41 | 372 | (password >> 8), password); |
cdupaty | 0:35cd316e4b41 | 373 | } |
cdupaty | 0:35cd316e4b41 | 374 | |
cdupaty | 0:35cd316e4b41 | 375 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 376 | /*! |
cdupaty | 0:35cd316e4b41 | 377 | @brief Helper function to process a packet and send it over UART to the |
cdupaty | 0:35cd316e4b41 | 378 | sensor |
cdupaty | 0:35cd316e4b41 | 379 | @param packet A structure containing the bytes to transmit |
cdupaty | 0:35cd316e4b41 | 380 | */ |
cdupaty | 0:35cd316e4b41 | 381 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 382 | |
cdupaty | 0:35cd316e4b41 | 383 | void Fingerprint::writeStructuredPacket(const Fingerprint_Packet &packet) |
cdupaty | 0:35cd316e4b41 | 384 | { |
cdupaty | 0:35cd316e4b41 | 385 | R503Serial.putc((uint8_t)(packet->start_code >> 8)); |
cdupaty | 0:35cd316e4b41 | 386 | R503Serial.putc((uint8_t)(packet->start_code & 0xFF)); |
cdupaty | 0:35cd316e4b41 | 387 | R503Serial.putc(packet->address[0]); |
cdupaty | 0:35cd316e4b41 | 388 | R503Serial.putc(packet->address[1]); |
cdupaty | 0:35cd316e4b41 | 389 | R503Serial.putc(packet->address[2]); |
cdupaty | 0:35cd316e4b41 | 390 | R503Serial.putc(packet->address[3]); |
cdupaty | 0:35cd316e4b41 | 391 | R503Serial.putc(packet->type); |
cdupaty | 0:35cd316e4b41 | 392 | |
cdupaty | 0:35cd316e4b41 | 393 | uint16_t wire_length = packet->length + 2; |
cdupaty | 0:35cd316e4b41 | 394 | R503Serial.putc((uint8_t)(wire_length >> 8)); |
cdupaty | 0:35cd316e4b41 | 395 | R503Serial.putc((uint8_t)(wire_length & 0xFF)); |
cdupaty | 0:35cd316e4b41 | 396 | |
cdupaty | 0:35cd316e4b41 | 397 | #ifdef FINGERPRINT_DEBUG |
cdupaty | 0:35cd316e4b41 | 398 | Serial.print(". 0x"); |
cdupaty | 0:35cd316e4b41 | 399 | Serial.print((uint8_t)(packet->start_code >> 8), HEX); |
cdupaty | 0:35cd316e4b41 | 400 | Serial.print(", 0x"); |
cdupaty | 0:35cd316e4b41 | 401 | Serial.print((uint8_t)(packet->start_code & 0xFF), HEX); |
cdupaty | 0:35cd316e4b41 | 402 | Serial.print(", 0x"); |
cdupaty | 0:35cd316e4b41 | 403 | Serial.print(packet->address[0], HEX); |
cdupaty | 0:35cd316e4b41 | 404 | Serial.print(", 0x"); |
cdupaty | 0:35cd316e4b41 | 405 | Serial.print(packet->address[1], HEX); |
cdupaty | 0:35cd316e4b41 | 406 | Serial.print(", 0x"); |
cdupaty | 0:35cd316e4b41 | 407 | Serial.print(packet->address[2], HEX); |
cdupaty | 0:35cd316e4b41 | 408 | Serial.print(", 0x"); |
cdupaty | 0:35cd316e4b41 | 409 | Serial.print(packet->address[3], HEX); |
cdupaty | 0:35cd316e4b41 | 410 | Serial.print(", 0x"); |
cdupaty | 0:35cd316e4b41 | 411 | Serial.print(packet->type, HEX); |
cdupaty | 0:35cd316e4b41 | 412 | Serial.print(", 0x"); |
cdupaty | 0:35cd316e4b41 | 413 | Serial.print((uint8_t)(wire_length >> 8), HEX); |
cdupaty | 0:35cd316e4b41 | 414 | Serial.print(", 0x"); |
cdupaty | 0:35cd316e4b41 | 415 | Serial.print((uint8_t)(wire_length & 0xFF), HEX); |
cdupaty | 0:35cd316e4b41 | 416 | #endif |
cdupaty | 0:35cd316e4b41 | 417 | |
cdupaty | 0:35cd316e4b41 | 418 | uint16_t sum = ((wire_length) >> 8) + ((wire_length)&0xFF) + packet->type; |
cdupaty | 0:35cd316e4b41 | 419 | for (uint8_t i = 0; i < packet->length; i++) { |
cdupaty | 0:35cd316e4b41 | 420 | R503Serial.putc(packet->data[i]); |
cdupaty | 0:35cd316e4b41 | 421 | sum += packet->data[i]; |
cdupaty | 0:35cd316e4b41 | 422 | #ifdef FINGERPRINT_DEBUG |
cdupaty | 0:35cd316e4b41 | 423 | Serial.print(", 0x"); |
cdupaty | 0:35cd316e4b41 | 424 | Serial.print(packet->data[i], HEX); |
cdupaty | 0:35cd316e4b41 | 425 | #endif |
cdupaty | 0:35cd316e4b41 | 426 | } |
cdupaty | 0:35cd316e4b41 | 427 | |
cdupaty | 0:35cd316e4b41 | 428 | R503Serial.putc((uint8_t)(sum >> 8)); |
cdupaty | 0:35cd316e4b41 | 429 | R503Serial.putc((uint8_t)(sum & 0xFF)); |
cdupaty | 0:35cd316e4b41 | 430 | |
cdupaty | 0:35cd316e4b41 | 431 | #ifdef FINGERPRINT_DEBUG |
cdupaty | 0:35cd316e4b41 | 432 | Serial.print(", 0x"); |
cdupaty | 0:35cd316e4b41 | 433 | Serial.print((uint8_t)(sum >> 8), HEX); |
cdupaty | 0:35cd316e4b41 | 434 | Serial.print(", 0x"); |
cdupaty | 0:35cd316e4b41 | 435 | Serial.println((uint8_t)(sum & 0xFF), HEX); |
cdupaty | 0:35cd316e4b41 | 436 | #endif |
cdupaty | 0:35cd316e4b41 | 437 | |
cdupaty | 0:35cd316e4b41 | 438 | return; |
cdupaty | 0:35cd316e4b41 | 439 | } |
cdupaty | 0:35cd316e4b41 | 440 | |
cdupaty | 0:35cd316e4b41 | 441 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 442 | /*! |
cdupaty | 0:35cd316e4b41 | 443 | @brief Helper function to receive data over UART from the sensor and |
cdupaty | 0:35cd316e4b41 | 444 | process it into a packet |
cdupaty | 0:35cd316e4b41 | 445 | @param packet A structure containing the bytes received |
cdupaty | 0:35cd316e4b41 | 446 | @param timeout how many milliseconds we're willing to wait |
cdupaty | 0:35cd316e4b41 | 447 | @returns <code>FINGERPRINT_OK</code> on success |
cdupaty | 0:35cd316e4b41 | 448 | @returns <code>FINGERPRINT_TIMEOUT</code> or |
cdupaty | 0:35cd316e4b41 | 449 | <code>FINGERPRINT_BADPACKET</code> on failure |
cdupaty | 0:35cd316e4b41 | 450 | */ |
cdupaty | 0:35cd316e4b41 | 451 | /**************************************************************************/ |
cdupaty | 0:35cd316e4b41 | 452 | uint8_t |
cdupaty | 0:35cd316e4b41 | 453 | Fingerprint::getStructuredPacket(Fingerprint_Packet *packet, |
cdupaty | 0:35cd316e4b41 | 454 | uint16_t timeout) { |
cdupaty | 0:35cd316e4b41 | 455 | uint8_t byte; |
cdupaty | 0:35cd316e4b41 | 456 | uint16_t idx = 0, timer = 0; |
cdupaty | 0:35cd316e4b41 | 457 | |
cdupaty | 0:35cd316e4b41 | 458 | #ifdef FINGERPRINT_DEBUG |
cdupaty | 0:35cd316e4b41 | 459 | Serial.print("<- "); |
cdupaty | 0:35cd316e4b41 | 460 | #endif |
cdupaty | 0:35cd316e4b41 | 461 | |
cdupaty | 0:35cd316e4b41 | 462 | while (true) { |
cdupaty | 0:35cd316e4b41 | 463 | while (!R503Serial.readable()) { |
cdupaty | 0:35cd316e4b41 | 464 | wait_ms(1); |
cdupaty | 0:35cd316e4b41 | 465 | timer++; |
cdupaty | 0:35cd316e4b41 | 466 | if (timer >= timeout) { |
cdupaty | 0:35cd316e4b41 | 467 | #ifdef FINGERPRINT_DEBUG |
cdupaty | 0:35cd316e4b41 | 468 | Serial.println("Timed out"); |
cdupaty | 0:35cd316e4b41 | 469 | #endif |
cdupaty | 0:35cd316e4b41 | 470 | return FINGERPRINT_TIMEOUT; |
cdupaty | 0:35cd316e4b41 | 471 | } |
cdupaty | 0:35cd316e4b41 | 472 | } |
cdupaty | 0:35cd316e4b41 | 473 | byte = R503Serial.getc(); |
cdupaty | 0:35cd316e4b41 | 474 | #ifdef FINGERPRINT_DEBUG |
cdupaty | 0:35cd316e4b41 | 475 | Serial.print("0x"); |
cdupaty | 0:35cd316e4b41 | 476 | Serial.print(byte, HEX); |
cdupaty | 0:35cd316e4b41 | 477 | Serial.print(", "); |
cdupaty | 0:35cd316e4b41 | 478 | #endif |
cdupaty | 0:35cd316e4b41 | 479 | switch (idx) { |
cdupaty | 0:35cd316e4b41 | 480 | case 0: |
cdupaty | 0:35cd316e4b41 | 481 | if (byte != (FINGERPRINT_STARTCODE >> 8)) |
cdupaty | 0:35cd316e4b41 | 482 | continue; |
cdupaty | 0:35cd316e4b41 | 483 | packet->start_code = (uint16_t)byte << 8; |
cdupaty | 0:35cd316e4b41 | 484 | break; |
cdupaty | 0:35cd316e4b41 | 485 | case 1: |
cdupaty | 0:35cd316e4b41 | 486 | packet->start_code |= byte; |
cdupaty | 0:35cd316e4b41 | 487 | if (packet->start_code != FINGERPRINT_STARTCODE) |
cdupaty | 0:35cd316e4b41 | 488 | return FINGERPRINT_BADPACKET; |
cdupaty | 0:35cd316e4b41 | 489 | break; |
cdupaty | 0:35cd316e4b41 | 490 | case 2: |
cdupaty | 0:35cd316e4b41 | 491 | case 3: |
cdupaty | 0:35cd316e4b41 | 492 | case 4: |
cdupaty | 0:35cd316e4b41 | 493 | case 5: |
cdupaty | 0:35cd316e4b41 | 494 | packet->address[idx - 2] = byte; |
cdupaty | 0:35cd316e4b41 | 495 | break; |
cdupaty | 0:35cd316e4b41 | 496 | case 6: |
cdupaty | 0:35cd316e4b41 | 497 | packet->type = byte; |
cdupaty | 0:35cd316e4b41 | 498 | break; |
cdupaty | 0:35cd316e4b41 | 499 | case 7: |
cdupaty | 0:35cd316e4b41 | 500 | packet->length = (uint16_t)byte << 8; |
cdupaty | 0:35cd316e4b41 | 501 | break; |
cdupaty | 0:35cd316e4b41 | 502 | case 8: |
cdupaty | 0:35cd316e4b41 | 503 | packet->length |= byte; |
cdupaty | 0:35cd316e4b41 | 504 | break; |
cdupaty | 0:35cd316e4b41 | 505 | default: |
cdupaty | 0:35cd316e4b41 | 506 | packet->data[idx - 9] = byte; |
cdupaty | 0:35cd316e4b41 | 507 | if ((idx - 8) == packet->length) { |
cdupaty | 0:35cd316e4b41 | 508 | #ifdef FINGERPRINT_DEBUG |
cdupaty | 0:35cd316e4b41 | 509 | Serial.println(" OK "); |
cdupaty | 0:35cd316e4b41 | 510 | #endif |
cdupaty | 0:35cd316e4b41 | 511 | return FINGERPRINT_OK; |
cdupaty | 0:35cd316e4b41 | 512 | } |
cdupaty | 0:35cd316e4b41 | 513 | break; |
cdupaty | 0:35cd316e4b41 | 514 | } |
cdupaty | 0:35cd316e4b41 | 515 | idx++; |
cdupaty | 0:35cd316e4b41 | 516 | } |
cdupaty | 0:35cd316e4b41 | 517 | // Shouldn't get here so... |
cdupaty | 0:35cd316e4b41 | 518 | return FINGERPRINT_BADPACKET; |
cdupaty | 0:35cd316e4b41 | 519 | } |