0

I'm currently writing a python program, that initializes the fingerprint sensor at first then showing the number of used templates on the fingerprint sensor after pressing "1" on the matrix membrane keypad. I need another interrupt from the matrix membrane keypad that could jump to the declared function "enroll_fingerprint" after pressing either "#" or "" via "keypad.registerKeyPressHandler(enroll_fingerprint)". The "keypad.registerKeyPressHandler(settings_selection)" works fine, it does jump from the "main()" function to the "settings_selection" function. But the "keypad.registerKeyPressHandler(enroll_fingerprint) does not work (i.e. pressing "" or "#" doesn't jump the the "enroll_fingerprint" function. Maybe I used the pad4pi library wrongly. Any guide..?

import RPi.GPIO as GPIO
import I2C_LCD_driver
import time
from pad4pi import rpi_gpio                         # From pad4pi file, import rpi_gpio file.
from pyfingerprint.pyfingerprint import PyFingerprint   # Import PyFingerprint class from pyfingerprint.py



# ==============================GPIO configurations==============================#

GPIO.setwarnings(False)     # Disable warnings.
GPIO.setmode(GPIO.BCM)  # Setup BCM numbering.


# ==============================Global variables with values==============================#

# ----Keypad----
r1 = 26     # GPIO6...
r2 = 19     # GPIO13...
r3 = 13     # GPIO19...
r4 = 6      # GPIO26 as output, connected to keypad
c1 = 21     # GPIO16...
c2 = 20     # GPIO20...
c3 = 16     # GPIO21 as input, connected to keypad

fingerprint_VCC = 17
fingerprint_IRQ = 27
fingerprint_VT = 22



# ==============================Setups & Configurations==============================#
# ----GPIO setup for Keypad----
KEYPAD = [
    ["1","2","3"],
    ["4","5","6"],
    ["7","8","9"],
    ["*","0","#"]
]

ROW = [r1,r2,r3,r4]
COL = [c1,c2,c3]        # GPIO BCM numbering setup.

factory = rpi_gpio.KeypadFactory()      # factory = KeypadFactory() class in rpi_gpio.py file.
keypad = factory.create_keypad(keypad = KEYPAD, row_pins = ROW, col_pins = COL)
# keypad = create_keypad() function in KeypadFactory() class.

mylcd = I2C_LCD_driver.lcd()    # mylcd = lcd() class in I2C_LCD_driver.py file.


# ----GPIO setup for Fingerprint Sensor----
GPIO.setup(fingerprint_IRQ,GPIO.IN)     # Set GPIO27 as input.

GPIO.setup(fingerprint_VT,GPIO.OUT)     # Set GPIO22 as output, to provide 3.3V for Finger Detection Power.
GPIO.output(fingerprint_VT,0)           # Set VT pin LOW initially.


# ==============================Declared functions============================== #
def settings_selection(key):    
    if key == "1":
        keypad.unregisterKeyPressHandler(settings_selection)
        # ^ settings_selection will not be called upon key presses.

        # Tries to initialize fingerprint.
        try:
            fingerprint = PyFingerprint('/dev/ttyS0', 57600, 0xFFFFFFFF, 0x00000000)
            # '/dev/ttyS0' is the UART device connected to GPIO14 & 15.
            # 57600 is the baud rate of 9600*N, where N is 6 by default (based on R300 datasheet).
            # 0xFFFFFFFF is R300's default address.
            # 0x00000000 is the correct password.

            if (fingerprint.verifyPassword() == False):     #If the password of the fingerprint sensor is not 00h,
                mylcd.lcd_clear()
                mylcd.lcd_display_string("Incompatible", 1, 2)  # Display string from Row 1, Col 3.
                mylcd.lcd_display_string("fgrprnt sensor", 2, 1)  # Display string from Row 2, Col 2.
                raise ValueError('The connected fingerprint sensor password is incorrect.')  #  Display error when the password set on the fingerprint sensor does not match the password of 00h.

        except Exception as e:
            print('The fingerprint sensor could not be initialized!')
            print('Exception message: ' + str(e))
            mylcd.lcd_clear()
            mylcd.lcd_display_string("Sensor cannot be",1,0)
            mylcd.lcd_display_string("initialized!",2,2)


        try:
            keypad.registerKeyPressHandler(enroll_fingerprint) # Any key presses will interrupt.

            while True:
                mylcd.lcd_clear()
                mylcd.lcd_display_string("Used templates:",1,0)
                mylcd.lcd_display_string(str(fingerprint.getTemplateCount()) + "/" 
                                       + str(fingerprint.getStorageCapacity()),2,5)   
                                       # Templates' count.
                time.sleep(5)

                mylcd.lcd_clear()
                mylcd.lcd_display_string("# to enroll",1,2) # Display at R1,C3.
                mylcd.lcd_display_string("finger",2,5)      # Display at R2,C6
                time.sleep(3)

                mylcd.lcd_clear()
                mylcd.lcd_display_string("* to exit",1,3)   # Display at R1,C4.
                mylcd.lcd_display_string("enrollment",2,3)  # Display at R2,C4.
                time.sleep(3)


        except Exception as e:
            print('Could not proceed to enrollment!')
            print('Exception message: ' + str(e))
            mylcd.lcd_clear()
            mylcd.lcd_display_string("Sensor could not",1,0)
            mylcd.lcd_display_string("enroll",2,5)



def enroll_fingerprint(key):
    if key == "#":
        keypad.unregisterKeyPressHandler(enroll_fingerprint)

        while True:
            try:
                while True:
                    mylcd.lcd_clear()
                    mylcd.lcd_display_string("Place your", 1, 3)  # Display string from Row 1, Col 3.
                    mylcd.lcd_display_string("finger...", 2, 3)  # Display string from Row 2, Col 4.

                    while (fingerprint.readImage() == False):  # Waiting for fingerprint to be read.
                        pass  # Loop forever until a fingerprint can be read.


                    fingerprint.convertImage(
                        0x01)  # Converts the read image to characteristics and stores it in charbuffer 1 (0x01).


                    # Checks if the fingerprint is already enrolled.
                    result = fingerprint.searchTemplate()
                    positionNumber = result[0]


                    if positionNumber >= 0:
                        mylcd.lcd_clear()
                        mylcd.lcd_display_string("Tmpl existed at", 1, 0)
                        mylcd.lcd_display_string("position #" + str(positionNumber),2,0)  # Displays the position number of the existing fingerprint template.


                    # If the enrolled fingerprint is confirmed to be a new template.
                    mylcd.lcd_clear()
                    mylcd.lcd_display_string("Please remove", 1, 1)
                    mylcd.lcd_display_string("finger...", 1, 3)
                    time.sleep(3)


                    # Reconfirm the newly enrolled finger.
                    mylcd.lcd_clear()
                    mylcd.lcd_display_string("Place your", 1, 3)
                    mylcd.lcd_display_string("finger again...", 1, 0)


                    while fingerprint.readImage() == False:  # Waiting for fingerprint to be read.
                        pass  # Loop forever until a fingerprint can be read.


                    fingerprint.convertImage(0x02)
                    # Converts the read image to characteristics and stores it in charbuffer 2 (0x02).


                    # Comparing the charbuffers (i.e. comparing the first and second attempt of the same enrolled finger).
                    if fingerprint.compareCharacteristics() == 0:  # If fingers do not match,
                        mylcd.lcd_clear()
                        mylcd.lcd_display_string("Fingers do", 1, 3)
                        mylcd.lcd_display_string("not match!", 2, 3)
                        time.sleep(5)
                        raise Exception('Fingers do not match')


                    elif fingerprint.compareCharacteristics() == 1:
                        break  # Loop will end if both 1st and 2nd attempt of the fingerprint enrollment match.

                    else:
                        pass

                # If both 1st and 2nd try of the fingerprint template matches,
                # then create a template for the newly enrolled fingerprint.
                fingerprint.createTemplate()

                # Saves template at new position number.
                positionNumber = fingerprint.storeTemplate()
                mylcd.lcd_clear()
                mylcd.lcd_display_string("Finger enrolled", 1, 0)
                mylcd.lcd_display_string("successfully!", 2, 1)
                time.sleep(3)

                while (key != "#"):
                    mylcd.lcd_clear()
                    mylcd.lcd_display_string("New template at", 1, 0)
                    mylcd.lcd_display_string("position #" + str(positionNumber),2,2)  # Display the position number of the newly enrolled fingerprint template.
                    time.sleep(5)

                    mylcd.lcd_clear()
                    mylcd.lcd_display_string("Press #", 1, 4)
                    mylcd.lcd_display_string("to exit", 2, 4)
                    time.sleep(2.5)

                break

            except Exception as e:
                print('Operation failed!')
                print('Exception message: ' + str(e))
                mylcd.lcd_clear()
                mylcd.lcd_display_string("Operation", 1, 3)
                mylcd.lcd_display_string("failed!", 2, 4)
                time.sleep(4)
                mylcd.lcd_clear()
                mylcd.lcd_display_string("Enrollment", 1, 3)
                mylcd.lcd_display_string("process ENDS", 2, 2)
                time.sleep(3)
                break


    elif key == "*":
        keypad.unregisterKeyPressHandler(enroll_fingerprint)
        main()





# ==============================MAIN PROGRAM============================== #
def main():
    try:
        mylcd.lcd_clear()
        mylcd.lcd_display_string("1-Add fgr",1,0)  # Print string at Row 1, Col 1.
        mylcd.lcd_display_string("2-New PIN", 2, 0)  # Print string at Row 2, Col 1.
        mylcd.lcd_display_string("3-Del", 1, 10)  # Print string from Row 1, Col 11.
        mylcd.lcd_display_string("fgr",2,12)    # Print string from Row 2, Col 13.

        # settings_selection will be called if keypad is pressed.
        keypad.registerKeyPressHandler(settings_selection)

        while True:
            time.sleep(0.2)

    except KeyboardInterrupt:
        mylcd.lcd_clear()
        GPIO.cleanup()


if __name__ == "__main__":
    main()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.