-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTasterRaetsel.py
86 lines (70 loc) · 2.38 KB
/
TasterRaetsel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import beat_the_room
import RPi.GPIO as GPIO
import time
class TasterRaetsel(beat_the_room.Puzzle):
# gpios initialisieren
def init(self):
self.id = None
GPIO.setmode(GPIO.BCM)
self.id = 98 # platzhalter
self.ports = [17, 18, 27, 22]
for i in range(4):
GPIO.setup(self.ports[i], GPIO.OUT)
self.states = [False for i in range(4)]
self.clickCounts = [0 for i in range(4)]
self.currentPin = 0
self.key = [3, 5, 3, 1]
pins = ""
for i in range(4):
pins += str(self.ports[i])+", "
print("init(" + pins + ")")
# reservier alle benötigten
def interact(self):
schwubbeldibubbeldi = time.time()
while not self.solved:
pin = self.checkClicks()
if pin != -1:
if pin == self.currentPin:
self.click()
elif pin == self.currentPin + 1:
self.currentPin += 1
self.click()
elif pin == 0:
self.currentPin = 0
self.clickCounts = [0 for i in range(4)]
self.click()
time.sleep(0.05)
if time.time() > schwubbeldibubbeldi + 100:
if input() != "":
self.solved = True
self.clickCounts = [0 for i in range(4)]
else:
schwubbeldibubbeldi = time.time()
def deinit(self):
pins = ""
for i in range(4):
pins += str(self.ports[i])+", "
print("deinitialising(" + pins + ")")
GPIO.cleanup()
def click(self):
self.clickCounts[self.currentPin] += 1
self.clickCounts[self.currentPin] = self.clickCounts[self.currentPin] % 10
self.solved = self.isSolved()
def isSolved(self):
return self.clickCounts == self.key
def checkClicks(self):
self.printStates()
for i in range(4):
if GPIO.input(self.ports[i]):
print('!')
if not self.states[i]:
self.states[i] = True
return i
else:
self.states[i] = False
print("?")
return -1
def printStates(self):
for i in range(4):
print(self.clickCounts[i], end="")
print()