|
| 1 | +import time |
| 2 | +import board |
| 3 | +import busio |
| 4 | +import adafruit_fingerprint |
| 5 | + |
| 6 | +uart = busio.UART(board.TX, board.RX, baudrate=57600) |
| 7 | + |
| 8 | +# If using with a computer such as Linux/RaspberryPi, Mac, Windows with USB/serial converter: |
| 9 | +# import serial |
| 10 | +# uart = serial.Serial("/dev/ttyUSB0", baudrate=57600, timeout=1) |
| 11 | + |
| 12 | +# If using with Linux/Raspberry Pi and hardware UART: |
| 13 | +# import serial |
| 14 | +# uart = serial.Serial("/dev/ttyS0", baudrate=57600, timeout=1) |
| 15 | + |
| 16 | +finger = adafruit_fingerprint.Adafruit_Fingerprint(uart) |
| 17 | + |
| 18 | +################################################## |
| 19 | + |
| 20 | + |
| 21 | +def get_fingerprint(): |
| 22 | + """Get a finger print image, template it, and see if it matches!""" |
| 23 | + print("Waiting for image...") |
| 24 | + while finger.get_image() != adafruit_fingerprint.OK: |
| 25 | + pass |
| 26 | + print("Templating...") |
| 27 | + if finger.image_2_tz(1) != adafruit_fingerprint.OK: |
| 28 | + return False |
| 29 | + print("Searching...") |
| 30 | + if finger.finger_search() != adafruit_fingerprint.OK: |
| 31 | + return False |
| 32 | + return True |
| 33 | + |
| 34 | + |
| 35 | +# pylint: disable=too-many-branches |
| 36 | +def get_fingerprint_detail(): |
| 37 | + """Get a finger print image, template it, and see if it matches! |
| 38 | + This time, print out each error instead of just returning on failure""" |
| 39 | + print("Getting image...", end="", flush=True) |
| 40 | + i = finger.get_image() |
| 41 | + if i == adafruit_fingerprint.OK: |
| 42 | + print("Image taken") |
| 43 | + else: |
| 44 | + if i == adafruit_fingerprint.NOFINGER: |
| 45 | + print("No finger detected") |
| 46 | + elif i == adafruit_fingerprint.IMAGEFAIL: |
| 47 | + print("Imaging error") |
| 48 | + else: |
| 49 | + print("Other error") |
| 50 | + return False |
| 51 | + |
| 52 | + print("Templating...", end="", flush=True) |
| 53 | + i = finger.image_2_tz(1) |
| 54 | + if i == adafruit_fingerprint.OK: |
| 55 | + print("Templated") |
| 56 | + else: |
| 57 | + if i == adafruit_fingerprint.IMAGEMESS: |
| 58 | + print("Image too messy") |
| 59 | + elif i == adafruit_fingerprint.FEATUREFAIL: |
| 60 | + print("Could not identify features") |
| 61 | + elif i == adafruit_fingerprint.INVALIDIMAGE: |
| 62 | + print("Image invalid") |
| 63 | + else: |
| 64 | + print("Other error") |
| 65 | + return False |
| 66 | + |
| 67 | + print("Searching...", end="", flush=True) |
| 68 | + i = finger.finger_fast_search() |
| 69 | + # pylint: disable=no-else-return |
| 70 | + # This block needs to be refactored when it can be tested. |
| 71 | + if i == adafruit_fingerprint.OK: |
| 72 | + print("Found fingerprint!") |
| 73 | + return True |
| 74 | + else: |
| 75 | + if i == adafruit_fingerprint.NOTFOUND: |
| 76 | + print("No match found") |
| 77 | + else: |
| 78 | + print("Other error") |
| 79 | + return False |
| 80 | + |
| 81 | + |
| 82 | +# pylint: disable=too-many-statements |
| 83 | +def enroll_finger(location): |
| 84 | + """Take a 2 finger images and template it, then store in 'location'""" |
| 85 | + for fingerimg in range(1, 3): |
| 86 | + if fingerimg == 1: |
| 87 | + print("Place finger on sensor...", end="", flush=True) |
| 88 | + else: |
| 89 | + print("Place same finger again...", end="", flush=True) |
| 90 | + |
| 91 | + while True: |
| 92 | + i = finger.get_image() |
| 93 | + if i == adafruit_fingerprint.OK: |
| 94 | + print("Image taken") |
| 95 | + break |
| 96 | + if i == adafruit_fingerprint.NOFINGER: |
| 97 | + print(".", end="", flush=True) |
| 98 | + elif i == adafruit_fingerprint.IMAGEFAIL: |
| 99 | + print("Imaging error") |
| 100 | + return False |
| 101 | + else: |
| 102 | + print("Other error") |
| 103 | + return False |
| 104 | + |
| 105 | + print("Templating...", end="", flush=True) |
| 106 | + i = finger.image_2_tz(fingerimg) |
| 107 | + if i == adafruit_fingerprint.OK: |
| 108 | + print("Templated") |
| 109 | + else: |
| 110 | + if i == adafruit_fingerprint.IMAGEMESS: |
| 111 | + print("Image too messy") |
| 112 | + elif i == adafruit_fingerprint.FEATUREFAIL: |
| 113 | + print("Could not identify features") |
| 114 | + elif i == adafruit_fingerprint.INVALIDIMAGE: |
| 115 | + print("Image invalid") |
| 116 | + else: |
| 117 | + print("Other error") |
| 118 | + return False |
| 119 | + |
| 120 | + if fingerimg == 1: |
| 121 | + print("Remove finger") |
| 122 | + time.sleep(1) |
| 123 | + while i != adafruit_fingerprint.NOFINGER: |
| 124 | + i = finger.get_image() |
| 125 | + |
| 126 | + print("Creating model...", end="", flush=True) |
| 127 | + i = finger.create_model() |
| 128 | + if i == adafruit_fingerprint.OK: |
| 129 | + print("Created") |
| 130 | + else: |
| 131 | + if i == adafruit_fingerprint.ENROLLMISMATCH: |
| 132 | + print("Prints did not match") |
| 133 | + else: |
| 134 | + print("Other error") |
| 135 | + return False |
| 136 | + |
| 137 | + print("Storing model #%d..." % location, end="", flush=True) |
| 138 | + i = finger.store_model(location) |
| 139 | + if i == adafruit_fingerprint.OK: |
| 140 | + print("Stored") |
| 141 | + else: |
| 142 | + if i == adafruit_fingerprint.BADLOCATION: |
| 143 | + print("Bad storage location") |
| 144 | + elif i == adafruit_fingerprint.FLASHERR: |
| 145 | + print("Flash storage error") |
| 146 | + else: |
| 147 | + print("Other error") |
| 148 | + return False |
| 149 | + |
| 150 | + return True |
| 151 | + |
| 152 | + |
| 153 | +################################################## |
| 154 | + |
| 155 | + |
| 156 | +def get_num(): |
| 157 | + """Use input() to get a valid number from 1 to 127. Retry till success!""" |
| 158 | + i = 0 |
| 159 | + while (i > 127) or (i < 1): |
| 160 | + try: |
| 161 | + i = int(input("Enter ID # from 1-127: ")) |
| 162 | + except ValueError: |
| 163 | + pass |
| 164 | + return i |
| 165 | + |
| 166 | + |
| 167 | +# initialize LED color |
| 168 | +led_color = 1 |
| 169 | +led_mode = 3 |
| 170 | +while True: |
| 171 | + # Turn on LED |
| 172 | + finger.set_led(color=led_color, mode=led_mode) |
| 173 | + print("----------------") |
| 174 | + if finger.read_templates() != adafruit_fingerprint.OK: |
| 175 | + raise RuntimeError("Failed to read templates") |
| 176 | + print("Fingerprint templates:", finger.templates) |
| 177 | + print("e) enroll print") |
| 178 | + print("f) find print") |
| 179 | + print("d) delete print") |
| 180 | + print("l) set LED") |
| 181 | + print("----------------") |
| 182 | + c = input("> ") |
| 183 | + |
| 184 | + if c == "l": |
| 185 | + c = input("color(r,b,p anything else=off)> ") |
| 186 | + led_mode = 3 |
| 187 | + if c == "r": |
| 188 | + led_color = 1 |
| 189 | + elif c == "b": |
| 190 | + led_color = 2 |
| 191 | + elif c == "p": |
| 192 | + led_color = 3 |
| 193 | + else: |
| 194 | + led_color = 1 |
| 195 | + led_mode = 4 |
| 196 | + elif c == "e": |
| 197 | + enroll_finger(get_num()) |
| 198 | + elif c == "f": |
| 199 | + # breathing LED |
| 200 | + finger.set_led(color=3, mode=1) |
| 201 | + if get_fingerprint(): |
| 202 | + print("Detected #", finger.finger_id, "with confidence", finger.confidence) |
| 203 | + else: |
| 204 | + print("Finger not found") |
| 205 | + elif c == "d": |
| 206 | + if finger.delete_model(get_num()) == adafruit_fingerprint.OK: |
| 207 | + print("Deleted!") |
| 208 | + else: |
| 209 | + print("Failed to delete") |
| 210 | + else: |
| 211 | + print("Invalid choice: Try again") |
0 commit comments