Store From Upper ComputerΒΆ

This sample code shows how to store a fingerprint from upper computer into a location in the flash library of the module.

The first part of the code makes use of enroll_to_upper_computer function exposed in the sample code from the Enroll to upper computer example, as seen in line 13 where it is imported and line 37 where it is used.

This saves us the stress of having to always copy and paste a previously stored template into the command line when the program is been ran.

  1# Standard library imports
  2import sys
  3from time import sleep
  4
  5# Third party imports
  6import serial
  7
  8# Adafruit package imports
  9from adafruit_fingerprint import AdafruitFingerprint
 10from adafruit_fingerprint.responses import *
 11
 12# Example module imports
 13from examples.enroll_to_upper_computer import enroll_to_upper_computer
 14
 15
 16def main():
 17    # Attempt to connect to serial port
 18    try:
 19        port = '/dev/ttyUSB0'  # USB TTL converter port
 20        baud_rate = '57600'
 21        serial_port = serial.Serial(port, baud_rate)
 22    except Exception as e:
 23        print(e)
 24        sys.exit()
 25
 26    # Initialize sensor library with serial port connection
 27    finger = AdafruitFingerprint(port=serial_port)
 28
 29    response = finger.vfy_pwd()
 30    if response is not FINGERPRINT_PASSWORD_OK:
 31        print('Did not find fingerprint sensor :(')
 32        sys.exit()
 33    print('Found Fingerprint Sensor!\n')
 34
 35    while True:
 36        print('\nReady to enroll a fingerprint!\n')
 37        template = enroll_to_upper_computer(finger)
 38        if template:
 39            print(f'Template:: {template}')
 40            print(
 41                '\nPlease type in the ID # (from 1 to 255) you want to save this finger as...')
 42            id = read_number()
 43            print(f'Storing template to flash library, with id #{id}\n')
 44            if store_from_upper_computer(finger=finger, template=template, page_id=id):
 45                print('Finished storing\n')
 46        else:
 47            print('Failed to return template')
 48
 49
 50def read_number():
 51    num = 0
 52    while num < 1 or num > 255:
 53        try:
 54            num = int(input())
 55        except ValueError:
 56            print('Please provide an integer')
 57        else:
 58            if num < 1 or num > 255:
 59                print('Please provide an integer in the above range')
 60
 61    return num
 62
 63
 64def store_from_upper_computer(finger, template, page_id):
 65    # Buffer constants
 66    CHAR_BUFF_1 = 0x01
 67    CHAR_BUFF_2 = 0x02
 68
 69    response = finger.down_char(buffer=CHAR_BUFF_1, template=template)
 70    if response is FINGERPRINT_OK:
 71        print('Template downloaded successfully!')
 72        sys.stdout.flush()
 73    if response is FINGERPRINT_PACKETRECEIVER:
 74        print('Communication error')
 75        return False
 76    if response is FINGERPRINT_TEMPLATEDOWNLOADFAIL:
 77        print('Template download error')
 78        return False
 79
 80    response = finger.store(buffer=CHAR_BUFF_1, page_id=page_id)
 81    if response is FINGERPRINT_OK:
 82        print('Template stored successfully!')
 83        sys.stdout.flush()
 84        return page_id
 85    if response is FINGERPRINT_PACKETRECEIVER:
 86        print('Communication error')
 87        return False
 88    if response is FINGERPRINT_BADLOCATION:
 89        print('Could not store in that location')
 90        return False
 91    if response is FINGERPRINT_FLASHER:
 92        print('Error writing to flash')
 93        return False
 94
 95
 96# Expose only store function from module
 97__all__ = ['store_from_upper_computer']
 98
 99
100if __name__ == '__main__':
101    main()