Christian Dupaty / Fingerprint-Lib

Dependents:   R503_fingerprint_HelloWorldV4

Revision:
2:6f273d942c43
Parent:
1:f392aee31272
Child:
3:394a5735eea0
diff -r f392aee31272 -r 6f273d942c43 Fingerprint.cpp
--- a/Fingerprint.cpp	Wed Mar 17 14:12:47 2021 +0000
+++ b/Fingerprint.cpp	Sat Mar 20 22:54:10 2021 +0000
@@ -1,34 +1,27 @@
 /*!
  * @file Fingerprint.cpp
- *
  * @mainpage Adafruit Fingerprint Sensor Library
- *
  * @section intro_sec Introduction
- *
  * This is a library for our optical 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
  * interface
  * Adafruit invests time and resources providing this open source code,
  * please support Adafruit and open-source hardware by purchasing
  * products from Adafruit!
- *
  * @section author Author
- *
  * Written by Limor Fried/Ladyada for Adafruit Industries.
+ * @section license License
+ * BSD license, all text above must be included in any redistribution
  *
- * @section license License
- *
- * BSD license, all text above must be included in any redistribution
+ * STM32 adaptation by Christian Dupaty 03/2021
  *
  */
 
-#include "Fingerprint.h"
+#include "Fingerprint.h" 
+#include "mbed.h"
 
-//#define FINGERPRINT_DEBUG
 
 /*!
  * @brief Gets the command packet
@@ -61,24 +54,25 @@
     @param  password 32-bit integer password (default is 0)
 */
 /**************************************************************************/
-Fingerprint::Fingerprint(PinName serialTX, PinName serialRX, PinName reset, uint32_t password) :
-    R503Serial(serialTX, serialRX), _reset(reset) 
+Fingerprint::Fingerprint(PinName serialTX, PinName serialRX, uint32_t password) :   R503Serial(serialTX, serialRX) 
 {
   thePassword = password;
   theAddress = 0xFFFFFFFF;
-  
+
   status_reg = 0x0; ///< The status register (set by getParameters)
   system_id = 0x0;  ///< The system identifier (set by getParameters)
   capacity = 64; ///< The fingerprint capacity (set by getParameters)
   security_level = 0; ///< The security level (set by getParameters)
-  device_addr = 0xFFFFFFFF;             ///< The device address (set by getParameters)
+  device_addr = 0xFFFFFFFF;      ///< The device address (set by getParameters)
   packet_len = 64;   ///< The max packet length (set by getParameters)
   baud_rate = 57600; ///< The UART baud rate (set by getParameters)
-  
+  // Init buffer de reception, les deux pointeurs egaux
+  pe=buffUART;
+  pl=buffUART;
+  // active IT sur reception UART vers methode receiveUART
+  R503Serial.attach(callback(this,&Fingerprint::receiveUART),RawSerial::RxIrq);
 }
 
-
-
 /**************************************************************************/
 /*!
     @brief  Initializes serial interface and baud rate
@@ -394,45 +388,36 @@
   R503Serial.putc((uint8_t)(wire_length >> 8));
   R503Serial.putc((uint8_t)(wire_length & 0xFF));
 
-#ifdef FINGERPRINT_DEBUG
-  Serial.print(". 0x");
-  Serial.print((uint8_t)(packet.start_code >> 8), HEX);
-  Serial.print(", 0x");
-  Serial.print((uint8_t)(packet.start_code & 0xFF), HEX);
-  Serial.print(", 0x");
-  Serial.print(packet.address[0], HEX);
-  Serial.print(", 0x");
-  Serial.print(packet.address[1], HEX);
-  Serial.print(", 0x");
-  Serial.print(packet.address[2], HEX);
-  Serial.print(", 0x");
-  Serial.print(packet.address[3], HEX);
-  Serial.print(", 0x");
-  Serial.print(packet.type, HEX);
-  Serial.print(", 0x");
-  Serial.print((uint8_t)(wire_length >> 8), HEX);
-  Serial.print(", 0x");
-  Serial.print((uint8_t)(wire_length & 0xFF), HEX);
-#endif
+  #ifdef FINGERPRINT_DEBUG
+  printf("-> Send packet \n-> ");
+  printf("0x%02X%02X ",(uint8_t)(packet.start_code >> 8),(uint8_t)(packet.start_code & 0xFF));
+  printf(", 0x%02X ",packet.address[0]);
+  printf(", 0x%02X ",packet.address[1]);
+  printf(", 0x%02X ",packet.address[2]);
+  printf(", 0x%02X ",packet.address[3]);
+  printf(", 0x%02X ",packet.type);
+  printf(", 0x%02X ",(uint8_t)(wire_length >> 8));
+  printf(", 0x%02X \n-> Data  ",(uint8_t)(wire_length & 0xFF));
+  #endif
 
   uint16_t sum = ((wire_length) >> 8) + ((wire_length)&0xFF) + packet.type;
-  for (uint8_t i = 0; i < packet.length; i++) {
+  for (uint8_t i = 0; i < packet.length; i++) 
+  {
     R503Serial.putc(packet.data[i]);
     sum += packet.data[i];
-#ifdef FINGERPRINT_DEBUG
-    Serial.print(", 0x");
-    Serial.print(packet.data[i], HEX);
-#endif
+  #ifdef FINGERPRINT_DEBUG
+    printf(", 0x%02X ",packet.data[i]);
+  #endif
   }
+//  #ifdef FINGERPRINT_DEBUG
+  printf("\n-\n");
+//  #endif
 
   R503Serial.putc((uint8_t)(sum >> 8));
   R503Serial.putc((uint8_t)(sum & 0xFF));
 
 #ifdef FINGERPRINT_DEBUG
-  Serial.print(", 0x");
-  Serial.print((uint8_t)(sum >> 8), HEX);
-  Serial.print(", 0x");
-  Serial.println((uint8_t)(sum & 0xFF), HEX);
+  printf("-> chksum = 0x%02X%02X \n",(uint8_t)(sum >> 8),(uint8_t)(sum & 0xFF));
 #endif
 
   return;
@@ -449,71 +434,102 @@
    <code>FINGERPRINT_BADPACKET</code> on failure
 */
 /**************************************************************************/
-uint8_t
-Fingerprint::getStructuredPacket(Fingerprint_Packet *packet,
-                                          uint16_t timeout) {
+uint8_t Fingerprint::getStructuredPacket(Fingerprint_Packet *packet, uint16_t timeout) 
+{
   uint8_t byte;
   uint16_t idx = 0, timer = 0;
 
 #ifdef FINGERPRINT_DEBUG
-  Serial.print("<- ");
+  printf("\n<----------------------packet reception\n<- ");
 #endif
 
-  while (true) {
-    while (!R503Serial.readable()) {
+  while (true) 
+  {
+    while (pl==pe)   // rien n'est arrivé 
+    {
       wait_ms(1);
       timer++;
-      if (timer >= timeout) {
-#ifdef FINGERPRINT_DEBUG
-        Serial.println("Timed out");
-#endif
+      if (timer >= timeout) 
+      {
+        #ifdef FINGERPRINT_DEBUG
+        printf("Timed out\n");
+        #endif
         return FINGERPRINT_TIMEOUT;
       }
     }
-    byte = R503Serial.getc();
-#ifdef FINGERPRINT_DEBUG
-    Serial.print("0x");
-    Serial.print(byte, HEX);
-    Serial.print(", ");
-#endif
-    switch (idx) {
-    case 0:
-      if (byte != (FINGERPRINT_STARTCODE >> 8))
-        continue;
-      packet->start_code = (uint16_t)byte << 8;
-      break;
-    case 1:
-      packet->start_code |= byte;
-      if (packet->start_code != FINGERPRINT_STARTCODE)
-        return FINGERPRINT_BADPACKET;
-      break;
-    case 2:
-    case 3:
-    case 4:
-    case 5:
-      packet->address[idx - 2] = byte;
-      break;
-    case 6:
-      packet->type = byte;
-      break;
-    case 7:
-      packet->length = (uint16_t)byte << 8;
-      break;
-    case 8:
-      packet->length |= byte;
-      break;
-    default:
-      packet->data[idx - 9] = byte;
-      if ((idx - 8) == packet->length) {
-#ifdef FINGERPRINT_DEBUG
-        Serial.println(" OK ");
-#endif
-        return FINGERPRINT_OK;
-      }
-      break;
+    byte = readUARTbuff();
+
+    #ifdef FINGERPRINT_DEBUG
+    printf("0x%02X, ",byte);
+    #endif
+    switch (idx) 
+    {
+        case 0:
+          if (byte != (FINGERPRINT_STARTCODE >> 8))
+            continue;
+          packet->start_code = (uint16_t)byte << 8;
+          break;
+        case 1:
+          packet->start_code |= byte;
+          if (packet->start_code != FINGERPRINT_STARTCODE)
+            return FINGERPRINT_BADPACKET;
+          break;
+        case 2: // 4 bytes for adress
+        case 3:  
+        case 4:
+        case 5:
+          packet->address[idx - 2] = byte;
+          break;
+        case 6:
+          packet->type = byte;
+          break;
+        case 7:
+          packet->length = (uint16_t)byte << 8;
+          break;
+        case 8:
+          packet->length |= byte;
+          #ifdef FINGERPRINT_DEBUG
+           printf("\n<- Data ");
+          #endif
+          break;
+        default:
+          packet->data[idx - 9] = byte;
+          if ((idx - 8) == packet->length) 
+          {
+                #ifdef FINGERPRINT_DEBUG
+                printf("\n<--------------------packet reception OK \n\n");
+                #endif
+                return FINGERPRINT_OK;
+          }
+          break;
     }
     idx++;
   }
   // Shouldn't get here so...
-  return FINGERPRINT_BADPACKET;
+  // return FINGERPRINT_BADPACKET;
 }
+
+/*
+Added by Christian Dupaty, STM32 adaptation
+*/
+
+/***************************************************************************
+ PRIVATE FUNCTIONS
+ ***************************************************************************/
+
+void Fingerprint::receiveUART(void) {
+    uint8_t c;
+    while(!R503Serial.readable());
+    c=R503Serial.getc();
+    *pe=c;
+    pe++;
+    if (pe>buffUART+sizeof(buffUART)) pe=buffUART;
+}
+
+uint8_t Fingerprint::readUARTbuff(void) {
+   uint8_t c;
+   c=*pl;
+   pl++;
+   if (pl>buffUART+sizeof(buffUART)) pl=buffUART;
+   return c;
+}