Library for fingerprint R503 Adaptation from Arduino Library

Dependencies:   Fingerprint-Lib

This a library and demo for fingerprint R503

For datasheet, see here -> https://cdn-shop.adafruit.com/product-files/4651/4651_R503%20fingerprint%20module%20user%20manual.pdf

This is an adaptation of Adafruit library (Arduino) to ARM MBED, tested on NUCLEO L073RZ

https://os.mbed.com/media/uploads/cdupaty/r503b.jpg https://os.mbed.com/media/uploads/cdupaty/r503-nucleol073rzpetit.jpg

For this exemple default connexions on NUCLEOL073RZ are :

TX <-> PC_0

RX <-> PC_1

WAKEUP <-> PB_0 , this is an Interrupt , you must connect a pull-up resistor (4K to 15k) between WAKEUP and DC3.3v

Power Supply 3.3v on Arduino connector

3.3VT : 5v on Arduino connector

IMPORTANT !!! There ara yet some bugs with communications UART

Revision:
9:0e0ddc4a53e3
Parent:
7:afa17a46252b
Child:
11:ed67623832a3
--- a/main.cpp	Sat Mar 20 22:59:15 2021 +0000
+++ b/main.cpp	Sun Mar 21 18:14:25 2021 +0000
@@ -15,14 +15,22 @@
 
 #include <Fingerprint.h>
 #include "mbed.h"
+//#include "R503lib.h"
+//#include "R503lib.cpp"
 
 DigitalOut ledV(LED1); // for various information without terminal
+
 Serial pc(USBTX, USBRX); // tx, rx
 Fingerprint finger(PC_1,PC_0,0x0); // TX,TX,pass
 InterruptIn fD(PB_0);               // IT from fingerprint detection (see datasheet WAKEUP)
+
+uint8_t id=1;
+
+bool fingerON=false;
+
 DigitalIn btnBleu(USER_BUTTON);     // to start enroll
 
-bool fingerON=false;
+
 
 // interrupt funtion 
 void fingerDetect(void)
@@ -34,11 +42,13 @@
 }
 
 
+
 // original setup fonction on Arduino
 void setup()
 {
   pc.baud(9600);
   wait_ms(100);
+  fD.fall(&fingerDetect);  
   pc.printf("\nR503 Finger detect test\nSTM32 version with MBED compiler and library\n");
 
   // set the data rate for the sensor serial port
@@ -76,7 +86,7 @@
     pc.printf("Waiting for valid finger...\n");
     pc.printf("Sensor contains : %d templates\n",finger.templateCount);
   }
-  fD.fall(&fingerDetect);  
+
 }
 
 // --------------------------------------
@@ -166,26 +176,172 @@
 void demoLED(void)
 {
 // control (3 on)(4off), speed (0-255) , color (1 red, 2 blue, 3 purple), cycles (0 infinit,- 255)
-// red
-    finger.LEDcontrol(3,128,1,10);
-    wait_ms(500);  
-// blue
-    finger.LEDcontrol(3,128,2,10);  
-    wait_ms(500); 
-// cyan
-    finger.LEDcontrol(3,128,3,10);
-    wait_ms(500);   
-// flash 5
-    finger.LEDcontrol(2,10,1,5);
-    wait_ms(1000);  
-// slow light on
-    finger.LEDcontrol(5,50,2,5);
-    wait_ms(1000);  
-// flash slow
-    finger.LEDcontrol(1,50,2,5);
-    wait_ms(1000); 
+// LED fully on
+  finger.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_RED);
+  wait_ms(250);
+  finger.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_BLUE);
+  wait_ms(250);
+  finger.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_PURPLE);
+  wait_ms(250);
+
+  // flash red LED
+  finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_RED, 10);
+  wait_ms(2000);
+  // Breathe blue LED till we say to stop
+  finger.LEDcontrol(FINGERPRINT_LED_BREATHING, 100, FINGERPRINT_LED_BLUE);
+  wait_ms(3000);
+  finger.LEDcontrol(FINGERPRINT_LED_GRADUAL_ON, 200, FINGERPRINT_LED_PURPLE);
+  wait_ms(2000);
+  finger.LEDcontrol(FINGERPRINT_LED_GRADUAL_OFF, 200, FINGERPRINT_LED_PURPLE);
+  wait_ms(2000);
 }
 
+// enroll a fingerprint
+uint8_t getFingerprintEnroll() 
+{
+  int p = -1;
+  fD.fall(NULL); 
+  pc.printf("Waiting for valid finger to enroll as #%d\n",id);
+  while (p != FINGERPRINT_OK) 
+  {
+    p = finger.getImage();
+    switch (p) 
+    {
+    case FINGERPRINT_OK:
+      pc.printf("Image taken\n");
+      break;
+    case FINGERPRINT_NOFINGER:
+      pc.printf(".");
+      break;
+    case FINGERPRINT_PACKETRECIEVEERR:
+      pc.printf("Communication error\n");
+      break;
+    case FINGERPRINT_IMAGEFAIL:
+      pc.printf("Imaging error\n");
+      break;
+    default:
+      pc.printf("Unknown error\n");
+      break;
+    }
+  }
+
+  // OK success!
+
+  p = finger.image2Tz(1);
+  switch (p) {
+    case FINGERPRINT_OK:
+      pc.printf("Image converted");
+      break;
+    case FINGERPRINT_IMAGEMESS:
+      pc.printf("Image too messy\n");
+      return p;
+    case FINGERPRINT_PACKETRECIEVEERR:
+      pc.printf("Communication error\n");
+      return p;
+    case FINGERPRINT_FEATUREFAIL:
+      pc.printf("Could not find fingerprint features\n");
+      return p;
+    case FINGERPRINT_INVALIDIMAGE:
+      pc.printf("Could not find fingerprint features\n");
+      return p;
+    default:
+      pc.printf("Unknown error\n");
+      return p;
+  }
+
+  pc.printf("Remove finger\n");
+  wait(2);
+  p = 0;
+  while (p != FINGERPRINT_NOFINGER) {
+    p = finger.getImage();
+  }
+  pc.printf("ID %d\n",id);
+  p = -1;
+  pc.printf("Place same finger again\n");
+  while (p != FINGERPRINT_OK) {
+    p = finger.getImage();
+    switch (p) {
+    case FINGERPRINT_OK:
+      pc.printf("Image taken\n");
+      break;
+    case FINGERPRINT_NOFINGER:
+      pc.printf(".");
+      break;
+    case FINGERPRINT_PACKETRECIEVEERR:
+      pc.printf("Communication error\n");
+      break;
+    case FINGERPRINT_IMAGEFAIL:
+      pc.printf("Imaging error\n");
+      break;
+    default:
+      pc.printf("Unknown error\n");
+      break;
+    }
+  }
+
+  // OK success!
+
+  p = finger.image2Tz(2);
+  switch (p) {
+    case FINGERPRINT_OK:
+      pc.printf("Image converted\n");
+      break;
+    case FINGERPRINT_IMAGEMESS:
+      pc.printf("Image too messy\n");
+      return p;
+    case FINGERPRINT_PACKETRECIEVEERR:
+      pc.printf("Communication error\n");
+      return p;
+    case FINGERPRINT_FEATUREFAIL:
+      pc.printf("Could not find fingerprint features\n");
+      return p;
+    case FINGERPRINT_INVALIDIMAGE:
+      pc.printf("Could not find fingerprint features\n");
+      return p;
+    default:
+      pc.printf("Unknown error\n");
+      return p;
+  }
+
+  // OK converted!
+  pc.printf("Creating model for #%d\n",id); 
+
+  p = finger.createModel();
+  if (p == FINGERPRINT_OK) {
+    pc.printf("Prints matched!\n");
+  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
+    pc.printf("Communication error\n");
+    return p;
+  } else if (p == FINGERPRINT_ENROLLMISMATCH) {
+    pc.printf("Fingerprints did not match\n");
+    return p;
+  } else {
+    pc.printf("Unknown error\n");
+    return p;
+  }
+
+  pc.printf("ID %d\n",id);
+  p = finger.storeModel(id);
+  if (p == FINGERPRINT_OK) {
+    pc.printf("Stored!\n");
+  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
+    pc.printf("Communication error\n");
+    return p;
+  } else if (p == FINGERPRINT_BADLOCATION) {
+    pc.printf("Could not store in that location\n");
+    return p;
+  } else if (p == FINGERPRINT_FLASHERR) {
+    pc.printf("Error writing to flash\n");
+    return p;
+  } else {
+    pc.printf("Unknown error\n");
+    return p;
+  }
+  fD.fall(&fingerDetect); 
+  return true;
+}
+
+
 int main(void)                    // run over and over again
 {
 unsigned char c=1;
@@ -206,10 +362,7 @@
       if (!btnBleu)
       {
         pc.printf("\nEnregistrement empreinte \n");
-        c=finger.createModel();
-        if (c==FINGERPRINT_OK) pc.printf("Empreinte enregistree  \n");
-        if (c==FINGERPRINT_PACKETRECIEVEERR) pc.printf("Erreur de communication \n");
-        if (c==FINGERPRINT_ENROLLMISMATCH) pc.printf("Erreur de scan \n");
+        c=getFingerprintEnroll();
         pc.printf("\nPret ! \n");
       }
       wait_ms(500);            //don't need to run this at full speed.