<commit_before>#!/usr/bin/python -u

import RPi.GPIO as GPIO
import random
import sys
import time

CHECKPOINT_NUM=10 # Write a checkpoint file after this many combination attempts
DIGIT_DELAY=0.25  # Delay after each digit press
CHECK_DELAY=0.00  # Additional delay before checking for success IN ADDITION to DIGIT_DELAY
POWER_INTER_DELAY=0.05 # Wait time between turning power off and turning back on
POWER_POST_DELAY=0.05  # Wait time before checking next combo after power cycle

gpioPowerControl = 22
gpioSuccessInput = 26

gpioDigitMap = [ 14, # digit 0
                 15, # digit 1
                 18, # digit 2
                 23, # digit 3
                 24, # digit 4
                 25, # digit 5
                  8, # digit 6
                  7, # digit 7
                 17, # digit 8
                 27, # digit 9 
               ]

successCombos = ""
triedCombos = {}

def gpioInit():
    print "Initializing GPIO..."
    GPIO.setwarnings(False)
    GPIO.cleanup()
    GPIO.setmode(GPIO.BCM)
    for i in range(0,10):
        print "    Setting BCM # %d as output for digit %d..." % (gpioDigitMap[i], i)
        GPIO.setup(gpioDigitMap[i], GPIO.OUT, initial=1)
    print "    Setting BCM # %d as success input..." % gpioSuccessInput
    GPIO.setup(gpioSuccessInput, GPIO.IN)
    print "    Setting BCM # %d as power control..." % gpioPowerControl
    GPIO.setup(gpioPowerControl, GPIO.OUT, initial=0)
    cyclePower()

def cyclePower():
    GPIO.output(gpioPowerControl, GPIO.HIGH)
    time.sleep(POWER_INTER_DELAY)
    GPIO.output(gpioPowerControl, GPIO.LOW)
    time.sleep(POWER_POST_DELAY)

def resetDigits():
    for i in range(0,10):
        GPIO.output(gpioDigitMap[i], GPIO.HIGH)
    time.sleep(DIGIT_DELAY)

def enterDigit(d):
    resetDigits()
    GPIO.output(gpioDigitMap[d], GPIO.LOW)
    time.sleep(DIGIT_DELAY)
#    if GPIO.input(gpioSuccessInput) != True:
#        print
#        print
#        print "ERROR:  The green LED did not activate from the keypress!"
#        print "        Check the electrical connections to the safe's PCB."
#        print
#        resetDigits()
#        GPIO.cleanup()
#        sys.exit(1)
    resetDigits()

def tryCombination(c):
    global successCombos
    digits = [ c/10000,
               (c/1000)%10,
               (c/100)%10,
               (c/10)%10,
               c%10
             ]
    print "Trying combination:  ",
    for i in range(0,5):
        print "%d" % digits[i],
        enterDigit(digits[i])
    time.sleep(CHECK_DELAY)
    success = GPIO.input(gpioSuccessInput)
    if success == 1:
        print "    **** FOUND A COMBO! ****",
        successCombos += "   %05d" % c
    if successCombos != "":
        print "    Valid combos are: %s" % successCombos
    else:
        print

def saveState():
    print "Saving state to checkpoint file..."
    try:
        f = open('pysafehack.state', 'w')
        f.write(successCombos)
        for c in triedCombos.keys():
            f.write(str(c)+"\n")
        f.close()
    except IOError:
        print
        print "ERROR: Could not save checkpoint file!"
        sys.exit(1)

def loadState():
    global successCombos
    global triedCombos
    print "Loading state from checkpoint file..."
    try:
        f = open('pysafehack.state', 'r')
        successCombos = f.readline()<commit_msg>Fix state checkpointing; re-enable hardware error checking<commit_after>#!/usr/bin/python -u

import RPi.GPIO as GPIO
import random
import sys
import time

CHECKPOINT_NUM=10 # Write a checkpoint file after this many combination attempts
DIGIT_DELAY=0.25  # Delay after each digit press
CHECK_DELAY=0.00  # Additional delay before checking for success IN ADDITION to DIGIT_DELAY
POWER_INTER_DELAY=0.05 # Wait time between turning power off and turning back on
POWER_POST_DELAY=0.05  # Wait time before checking next combo after power cycle

gpioPowerControl = 22
gpioSuccessInput = 26

gpioDigitMap = [ 14, # digit 0
                 15, # digit 1
                 18, # digit 2
                 23, # digit 3
                 24, # digit 4
                 25, # digit 5
                  8, # digit 6
                  7, # digit 7
                 17, # digit 8
                 27, # digit 9 
               ]

successCombos = ""
triedCombos = {}

def gpioInit():
    print "Initializing GPIO..."
    GPIO.setwarnings(False)
    GPIO.cleanup()
    GPIO.setmode(GPIO.BCM)
    for i in range(0,10):
        print "    Setting BCM # %d as output for digit %d..." % (gpioDigitMap[i], i)
        GPIO.setup(gpioDigitMap[i], GPIO.OUT, initial=1)
    print "    Setting BCM # %d as success input..." % gpioSuccessInput
    GPIO.setup(gpioSuccessInput, GPIO.IN)
    print "    Setting BCM # %d as power control..." % gpioPowerControl
    GPIO.setup(gpioPowerControl, GPIO.OUT, initial=0)
    cyclePower()

def cyclePower():
    GPIO.output(gpioPowerControl, GPIO.HIGH)
    time.sleep(POWER_INTER_DELAY)
    GPIO.output(gpioPowerControl, GPIO.LOW)
    time.sleep(POWER_POST_DELAY)

def resetDigits():
    for i in range(0,10):
        GPIO.output(gpioDigitMap[i], GPIO.HIGH)
    time.sleep(DIGIT_DELAY)

def enterDigit(d):
    resetDigits()
    GPIO.output(gpioDigitMap[d], GPIO.LOW)
    time.sleep(DIGIT_DELAY)
    if GPIO.input(gpioSuccessInput) != True:
        print
        print
        print "ERROR:  The green LED did not activate from the keypress!"
        print "        Check the electrical connections to the safe's PCB."
        print
        resetDigits()
        GPIO.cleanup()
        sys.exit(1)
    resetDigits()

def tryCombination(c):
    global successCombos
    digits = [ c/10000,
               (c/1000)%10,
               (c/100)%10,
               (c/10)%10,
               c%10
             ]
    print "Trying combination:  ",
    for i in range(0,5):
        print "%d" % digits[i],
        enterDigit(digits[i])
    time.sleep(CHECK_DELAY)
    success = GPIO.input(gpioSuccessInput)
    if success == 1:
        print "    **** FOUND A COMBO! ****",
        successCombos += "   %05d" % c
    if successCombos != "":
        print "    Valid combos are: %s" % successCombos
    else:
        print

def saveState():
    print "Saving state to checkpoint file..."
    try:
        f = open('pisafehack.state', 'w')
        f.write(successCombos + "\n")
        for c in triedCombos.keys():
            f.write(str(c)+"\n")
        f.close()
    except IOError:
        print
        print "ERROR: Could not save checkpoint file!"
        sys.exit(1)

def loadState():
    global successCombos
    global triedCombos
    print "Loading state from checkpoint file..."
    try:
        f = open('pisafehack.state', 'r')
        successCombos = f.readline()
{'fraction_non_alphanumeric': 0.10773357174787662, 'fraction_numerical': 0.02473550886604083, 'mean_word_length': 2.1349836524988324, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '292528', 'n_tokens_mistral': 2216, 'n_tokens_neox': 2086, 'n_words': 586}