Advertisement
btdat2506

First Edition - UART_FLAG_RXNE

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