def main():
    
    #Time wait after Finger Print confirmed
    lockOnTime = 1
    #set-up output pin
    '''GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(outputPin, GPIO.OUT, initial=GPIO.LOW)
    GPIO.setup(redpin, GPIO.OUT, initial = GPIO.HIGH)
    GPIO.setup(inputPinExit, GPIO.IN, pull_up_down=GPIO.PUD_UP)'''
    # They are moved in the front
    
    #Set serial port and baudrate
    Fingerprint.begin()
    
    if(Fingerprint.checkFPComms()):
        print ("Found Fingerprint Sensor")
    else:
        print ("Did not find Fingerprint Sensor")
        main()
    while(True):
        print("1 - Enroll Fingerprints")
        print("2 - Run Fingerprint Sensor")
        print("3 - Save Notepad to File")
        print("4 - Load Notepad from File")
        print("5 - Erase Library")

        print("E - Exit")
        menuInput = input ("Please select option ")
        
        if((menuInput.isdigit()) and (menuInput == '1')):
            enrollIDLoop()
            break
        elif((menuInput.isdigit()) and (menuInput == '2')):
            lockOnTime = 3
            runLoop(lockOnTime)
            break
        elif((menuInput.isdigit()) and (menuInput == '3')):
            totalCount = Fingerprint.getTemplateCount()
            print ("Template count is " + str(totalCount))
            for i in range(1, totalCount + 1):
                if(Fingerprint.getNotepad(i) == True):
                    print("Success Writing ID File for " + str(i))
                else:
                    print("Error Writing ID File")
                    break
        elif((menuInput.isdigit()) and (menuInput == '4')):
            #run Fingerprint.writeNotepad(ID) until False (returns false when no ID found)
            writeID = 1
            while((Fingerprint.writeNotepad(writeID)) == True):
                writeID += 1
            print("Total ID's entered " + str(writeID - 1))
        elif((menuInput.isdigit()) and (menuInput == '5')):
            if(Fingerprint.emptyDatabase() == True):
                pass
            else:
                print("Error erasing library")
        elif((menuInput.isdigit()) and (menuInput == '6')):
            if(imageUpload() == True):
                pass
            else:
                print("Error uploading image and saving to file")
        elif((menuInput.isdigit()) and (menuInput == '7')):
            if(imageDownload() == True):
                pass
            else:
                print("Error opening file and downloading to sensor")
        elif((menuInput == 'E') or (menuInput == 'e')):
            menuInput = ''
            GPIO.cleanup()
            sys.exit()
        else:
            print ("Must select from the menu")
    
    GPIO.cleanup()    
    return 0
def main():
    lockOnTime = 1
    #set-up output pin
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(outputPin, GPIO.OUT, initial=GPIO.LOW)
    GPIO.setup(inputPinExit, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    
    #Set serial port and baudrate
    Fingerprint.begin()
    
    if(Fingerprint.checkFPComms()):
        print ("Found Fingerprint Sensor")
    else:
        print ("Did not find Fingerprint Sensor")
        main()
    while(True):
        print("1 - Enroll Fingerprints")
        print("2 - Run Fingerprint Sensor")
        print("3 - Save Notepad to File")
        print("4 - Load Notepad from File")
        print("5 - Erase Library")
        print("6 - Upload Fingerprint Image to Computer")
        print("7 - Download Fingerprint Image to Sensor")
        print("E - Exit")
        menuInput = input ("Please select option ")
        
        if((menuInput.isdigit()) and (menuInput == '1')):
            enrollIDLoop()
            break
        elif((menuInput.isdigit()) and (menuInput == '2')):
            lockOnTime = 8
            runLoop(lockOnTime)
            break
        elif((menuInput.isdigit()) and (menuInput == '3')):
            totalCount = Fingerprint.getTemplateCount()
            print ("Template count is " + str(totalCount))
            for i in range(1, totalCount + 1):
                if(Fingerprint.getNotepad(i) == True):
                    print("Success Writing ID File for " + str(i))
                else:
                    print("Error Writing ID File")
                    break
        elif((menuInput.isdigit()) and (menuInput == '4')):
            #run Fingerprint.writeNotepad(ID) until False (returns false when no ID found)
            writeID = 1
            while((Fingerprint.writeNotepad(writeID)) == True):
                writeID += 1
            print("Total ID's entered " + str(writeID - 1))
        elif((menuInput.isdigit()) and (menuInput == '5')):
            if(Fingerprint.emptyDatabase() == True):
                pass
            else:
                print("Error erasing library")
        elif((menuInput.isdigit()) and (menuInput == '6')):
            if(imageUpload() == True):
                pass
            else:
                print("Error uploading image and saving to file")
        elif((menuInput.isdigit()) and (menuInput == '7')):
            if(imageDownload() == True):
                pass
            else:
                print("Error opening file and downloading to sensor")
        elif((menuInput == 'E') or (menuInput == 'e')):
            menuInput = ''
            GPIO.cleanup()
            sys.exit()
        else:
            print ("Must select from the menu")
    
    GPIO.cleanup()    
    return 0