-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.py
More file actions
293 lines (260 loc) · 8.23 KB
/
Calculator.py
File metadata and controls
293 lines (260 loc) · 8.23 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import re
import RPi.GPIO as GPIO
SIGNS_1 = ('*', '/')
SIGNS_2 = ('+', '-')
SIGNS_3 = ('(', ')')
def reset_pins_to_low():
for pin in range(2, 13):
GPIO.output(pin, GPIO.LOW)
def send_instruction(num1, sign, num2):
acknowledged = False
finished = False
error = False
overflow = False
solution_int = 0
# Set pins as in and output
GPIO.setmode(GPIO.BCM)
for pin in range(2, 13):
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
for pin in range(13, 28):
GPIO.setup(pin, GPIO.IN)
#check for overflow before sending
if num1 > 1023 or num2 > 1023 or num1 < -1024 or num2 < -1024:
# Send Error
GPIO.output(2, GPIO.HIGH)
GPIO.output(3, GPIO.HIGH)
GPIO.output(4, GPIO.HIGH)
GPIO.output(5, GPIO.HIGH)
return "Input number too high"
#make sbutract calculations into add calculations
if sign == '-':
num2 *= -1
#make num1 into two's commplement binary
two_complement_num1 = bin(num1 & int("1"*11, 2))[2:]
two_complement_11bit_num1 = ("{0:0>%s}" % (11)).format(two_complement_num1)
print(two_complement_11bit_num1)
#make num2 into two's commplement binary
two_complement_num2 = bin(num2 & int("1"*11, 2))[2:]
two_complement_11bit_num2 = ("{0:0>%s}" % (11)).format(two_complement_num2)
print(two_complement_11bit_num2)
#communicate with the fpga to get the calculations
while not finished:
error = False
# Send begin calculation
GPIO.output(2, GPIO.LOW)
GPIO.output(3, GPIO.LOW)
GPIO.output(4, GPIO.LOW)
GPIO.output(5, GPIO.HIGH)
#wait for ack start calc
#print("waiting for ack start calc")
while(not acknowledged):
if (GPIO.input(13) == 0 and GPIO.input(14) == 1 and GPIO.input(15) == 0):
acknowledged = True
elif (GPIO.input(13) == 1 and GPIO.input(14) == 1 and GPIO.input(15) == 1):
error = True
print("Received error")
break
acknowledged = False
if error or overflow:
reset_pins_to_low()
break
#send operation type:
# GPIO.output(11, GPIO.HIGH)
if sign == '+' :
print('+')
GPIO.output(2, GPIO.LOW)
GPIO.output(3, GPIO.HIGH)
GPIO.output(4, GPIO.HIGH)
GPIO.output(5, GPIO.LOW)
elif sign == '-':
print('-')
GPIO.output(2, GPIO.LOW)
GPIO.output(3, GPIO.HIGH)
GPIO.output(4, GPIO.HIGH)
GPIO.output(5, GPIO.LOW)
elif sign == '*':
print('*')
GPIO.output(2, GPIO.HIGH)
GPIO.output(3, GPIO.LOW)
GPIO.output(4, GPIO.LOW)
GPIO.output(5, GPIO.LOW)
elif sign == '/':
print('/')
GPIO.output(2, GPIO.HIGH)
GPIO.output(3, GPIO.LOW)
GPIO.output(4, GPIO.LOW)
GPIO.output(5, GPIO.HIGH)
#wait for ack operation type
while(not acknowledged):
if (GPIO.input(13) == 0 and GPIO.input(14) == 1 and GPIO.input(15) == 1):
acknowledged = True
elif (GPIO.input(13) == 1 and GPIO.input(14) == 1 and GPIO.input(15) == 1):
error = True
print("Received error")
break
# print("waiting for ack operation type")
acknowledged = False
if error or overflow:
reset_pins_to_low()
break
#send num1
for pin in range (2, 13):
if two_complement_11bit_num1[pin-2] == '1':
GPIO.output(pin, GPIO.HIGH)
# print("%s%d" % ("PinH", pin))
else:
GPIO.output(pin, GPIO.LOW)
# print("%s%d" % ("PinL", pin))
#wait for ack num1
while(not acknowledged):
if (GPIO.input(13) == 1 and GPIO.input(14) == 0 and GPIO.input(15) == 0):
acknowledged = True
elif (GPIO.input(13) == 1 and GPIO.input(14) == 1 and GPIO.input(15) == 1):
error = True
print("Received error")
break
elif (GPIO.input(13) == 0 and GPIO.input(14) == 0 and GPIO.input(15) == 1):
print("debug")
# print("waiting for ack num1")
acknowledged = False
if error or overflow:
reset_pins_to_low()
break
#send num2
for pin in range (2, 13):
if two_complement_11bit_num2[pin-2] == '1':
GPIO.output(pin, GPIO.HIGH)
else:
GPIO.output(pin, GPIO.LOW)
#wait for ack num2
while(not acknowledged):
if (GPIO.input(13) == 1 and GPIO.input(14) == 0 and GPIO.input(15) == 1):
acknowledged = True
elif (GPIO.input(13) == 1 and GPIO.input(14) == 1 and GPIO.input(15) == 1):
error = True
print("Received error")
break
# print("waiting for ack num2")
acknowledged = False
if error or overflow:
reset_pins_to_low()
break
#wait for solution header
while(not acknowledged):
if (GPIO.input(13) == 0 and GPIO.input(14) == 0 and GPIO.input(15) == 1):
acknowledged = True
elif (GPIO.input(13) == 1 and GPIO.input(14) == 1 and GPIO.input(15) == 1):
error = True
print("Received Error")
break
elif (GPIO.input(13) == 0 and GPIO.input(14) == 1 and GPIO.input(15) == 1):
overflow = True
print("Received Overflow")
break;
# print("waiting for ack solution header")
acknowledged = False
if error or overflow:
reset_pins_to_low()
break
#get the solution (15 bits long)
solution = [0]*12
for pin in range (16, 28):
solution[pin-16] = GPIO.input(pin)
#reset all out pins to low
reset_pins_to_low()
#give back solution
solution_str = ""
for binary in solution:
solution_str += str(binary)
print(solution_str)
print(isinstance(solution_str, str))
solution_int = int(solution_str, 2)
if solution_str[0] == '1':
solution_int = solution_int - 4096
print(solution_int)
finished = True
return solution_int
# Communicates a certain calculation to the fpga and waits for an answer.
#change to communicate to fpga
def fpga_calc(num1, sign, num2):
string = str(num1)
string += str(sign)
string += str(num2)
integer = int(eval(string))
return integer
# Gets what calculations need to be done on the numbers and symbols in the array
def get_calcs(start_array, signs):
end_array = []
for i in range(0, len(start_array)):
if start_array[i] in signs:
end_array.pop(len(end_array)-1)
start_array[i+1] = send_instruction(start_array[i-1], start_array[i], start_array[i+1])
else:
end_array.append(start_array[i])
return end_array
# Takes the formula string and puts it into an array with ints for integers and char for symbols.
# Keeps the integers as big as possible (255 stays 255 and does not become 2, 5, 5)
def formula_to_array(formula):
print(formula)
#check for brackets
while ')' in formula:
#take part up to the first )
old_partition = formula.partition(')')[0]
#reverse that part and take the part up to the first (
reversed_partition = old_partition[::-1]
reversed_partition = reversed_partition.partition('(')[0]
#reverse that part again and put that part as a new formula into this function (recursion)
new_partition = reversed_partition[::-1]
answer = formula_to_array(new_partition)
#replace the taken formula part with the answer
replace_str = ''.join(['(' , new_partition, ')'])
formula = formula.replace(replace_str, str(answer), 1)
print(formula)
temp = ""
array = []
skip = False
for index, character in enumerate(formula):
if re.match(r"[0-9]", character):
temp += character
elif character == '-' and (formula[index-1] in SIGNS_1 or formula[index-1] in SIGNS_2 or (index == 0)):
temp += character
else:
if len(temp) != 0:
array.append(int(temp))
temp = ""
array.append(character)
if len(temp) != 0:
array.append(int(temp))
print(array)
return get_calcs(get_calcs(array, SIGNS_1), SIGNS_2)[0]
# Executes all functions necessary to calculate the formula string and get the answer
# def get_formula_answer(formula):
# return get_calcs(get_calcs(formula_to_array(formula), SIGNS_1), SIGNS_2)[0]
# Checks if the formula is correct, if not it will return a correct string if the formula is correct or it can be corrected, or "Error" if the formula cannot be corrected
def check_formula(formula):
new_formula = ""
if formula[0] in SIGNS_1:
return "Error"
elif formula[len(formula)-1] in (SIGNS_1 or SIGNS_2):
return "Error"
if formula[0] == '+':
new_formula = formula[1:]
else:
new_formula = formula
for char_pos in range(0, len(new_formula)):
if new_formula[char_pos] in (SIGNS_1 or SIGNS_2):
if new_formula[char_pos - 1] in (SIGNS_1 or SIGNS_2):
return "Error"
return new_formula
# In and output. Should be retrieved form the
# formula
formula = str(input("Enter the operation you want to perform: "))
# formula = "-5*-3--7+666/6"
# formula = check_formula(formula)
fin_answer = 0
if formula != "Error":
# print(get_formula_answer(formula))
print(formula_to_array(formula))
else:
print("Error")