Created
October 21, 2018 12:17
-
-
Save MelulekiDube/ae30bd1d0e1a71dd0b9c637e0b904de0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# imports | |
import _thread | |
import spidev | |
import time as time | |
from array import * | |
from datetime import datetime, timedelta | |
import RPi.GPIO as GPIO | |
import atexit | |
# Define Variables | |
delay = 0.5 | |
ldr_channel = 0 | |
base = 0 | |
log = [] | |
direction = [] | |
Progress=True | |
SW1=11 | |
# Create SPI | |
spi = spidev.SpiDev() | |
spi.open(0, 0) | |
spi.max_speed_hz = 1350000 # setting the maximum frequency of the spi | |
# define input pins for the buttons: | |
RESET_SWITCH = 11 # button for reset the timer and cleaning the console | |
FREQUENCY_CHANGE_SWITCH = 15 # button to be used to change the frequency of the application | |
STOP_START_SWITCH = 29 # the button to be used to start and stop the taking of data | |
DISPLAY_SWITCH = 33 # button to display the results | |
# variables to be used in the program | |
frequency = .5 | |
readings_recorded = [None] * 5 | |
timer = 0 | |
recording = False | |
index = 0 | |
def exit_handler(): | |
print("Cleaning up") | |
GPIO.cleanup() | |
atexit.register(exit_handler) | |
def delay(): | |
time.sleep(0.1) | |
def freq_change_handler(channel_number): | |
global frequency | |
print("Change frequency presse") | |
if frequency == .5: | |
frequency = 1 | |
elif frequency == 1: | |
frequency = 2 | |
elif frequency == 2: | |
frequency = .5 | |
delay() | |
def reset_handler(channel_number): | |
print("\033[H\033[J") | |
print("reset pressed") | |
delay() | |
def start_stop_handler(channel_number): | |
global recording | |
print("Stop/Start pressed\n") | |
recording = True if (recording == False) else False | |
delay() | |
# setting board and gpio pins | |
GPIO.setmode(GPIO.BOARD) | |
def readadc(adcnum): | |
# read SPI data from the MCP3008, 8 channels in total | |
if adcnum > 7 or adcnum < 0: | |
return -1 | |
r = spi.xfer2([1, 8 + adcnum << 4, 0]) | |
data = ((r[1] & 3) << 8) + r[2] | |
return data | |
def evaluateDirection(initpotvalue, secondarypotvalue): | |
if secondarypotvalue < initpotvalue: | |
d = 'L' | |
elif secondarypotvalue > initpotvalue: | |
d = 'R' | |
return d | |
def stop(): | |
print("Stop Listener Started") | |
global Progress | |
Stopped=False | |
Potvalue=readPot() | |
while not Stopped: | |
newTempPotValue=readPot() | |
if sameValue(Potvalue, newTempPotValue): | |
time_start = time.time() | |
while (sameValue(Potvalue, readPot())): | |
time_current = time.time() | |
delta_t = time_current - time_start | |
if (delta_t >= 10): | |
Progress=False | |
Stopped=True | |
break | |
Potvalue = readPot() | |
print("Returned") | |
return | |
def readCombination(): | |
global Progress | |
Potvalue = readPot() | |
knobTimeStart=0 | |
direction="" | |
i=0 | |
#_thread.start_new_thread(stop,()) | |
while Progress: | |
global_time=time.time() | |
while(sameValue(Potvalue,readPot()) and i==0): | |
if(time.time()-global_time>=5): | |
print("5 sec over") | |
return | |
newTempPotValue=readPot() | |
if not(sameValue(Potvalue, newTempPotValue)) and i==0: | |
print("Time Started") | |
knobTimeStart=time.time() | |
direction = evaluateDirection(Potvalue, newTempPotValue) | |
i=i+1 | |
# print(direction) | |
elif sameValue(Potvalue, newTempPotValue): | |
time_start = time.time() | |
while (sameValue(Potvalue, readPot())): | |
# you know combination is to be recorded | |
time_current = time.time() | |
delta_t = time_current - time_start | |
if (delta_t >= 3 ): | |
duration=time.time()-delta_t-knobTimeStart | |
recordData(direction,duration) | |
i=0 | |
print("Value is recorded") | |
# print (direction + str(duration)) | |
break | |
Potvalue = readPot() | |
print("We Outchea Boi") | |
def sameValue(initpotvalue, secondarypotvalue): | |
same = False | |
for i in range(-3, 4): | |
if secondarypotvalue + i == initpotvalue: | |
same = True | |
break | |
return same | |
def recordData(direct, time): | |
global log | |
global direction | |
log.append(time) | |
direction.append(direct) | |
# print(direct+str(time)) | |
def printData(): | |
global log | |
global direction | |
for i in range(len(log)): | |
print(direction[i]+str(log[i])) | |
def sort(arr): | |
for i in range (len(arr)): | |
min=arr[i]; | |
minindex=i; | |
for j in range(i+1,len(arr)): | |
if arr[j] < min: | |
min=arr[j] | |
minindex=j | |
temp=arr[i]; | |
arr[i]=min; | |
arr[minindex]=temp; | |
def evaluateCombination(mode, ComboDir, ComboDuration): | |
global log | |
global direction | |
if len(log) != len(ComboDuration): | |
print("Wrong Password") | |
return False | |
Correct = True | |
# secureMode | |
if (mode == 1): | |
for i in range(len(log)): | |
if not (direction[i] == ComboDir[i] and durationEquality(log[i], ComboDuration[i])): | |
print("Wrong Password") | |
Correct = False | |
break | |
if Correct: | |
print("Code Correct") | |
# UnsecureMode | |
else: | |
sort(log) | |
sort(ComboDuration) | |
print(log) | |
print(ComboDuration) | |
for i in range(len(log)): | |
if not (durationEquality(log[i], ComboDuration[i])): | |
print("Wrong Password") | |
Correct = False | |
break | |
if Correct: | |
print("Code Correct") | |
def durationEquality(recorded, expected): | |
TLevel = 0.5 | |
if (expected - TLevel < recorded < expected + TLevel): | |
return True | |
else: | |
return False | |
def readPot(): | |
pot_reading = readadc(0) | |
delay() | |
return pot_reading | |
def main(): | |
global log | |
global direction | |
global SW1 | |
GPIO.setup(SW1, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
comboDir=["L","R","L"] | |
comboLog=[2,3,3] | |
while True: | |
print("Waiting PushButton S") | |
while(GPIO.input(SW1)): | |
pass | |
print("pressed") | |
readCombination() | |
evaluateCombination(1, comboDir, comboLog) | |
print("Combo Direction") | |
print(comboDir) | |
print("Recorded Direction") | |
print(direction) | |
print("Combo Duration") | |
print(comboLog) | |
print("Recorded Duration") | |
print(log) | |
print() | |
print() | |
#print("Waiting for Button Press") | |
if __name__ == "__main__": main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment