-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormula_string_to_array.py
More file actions
245 lines (220 loc) · 6.71 KB
/
Formula_string_to_array.py
File metadata and controls
245 lines (220 loc) · 6.71 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
import re
import RPi.GPIO as GPIO
SIGNS_1 = ('*', '/')
SIGNS_2 = ('+', '-')
SIGNS_3 = ('(', ')')
def send_instruction(num1, sign, num2):
acknowledged = False
finished = False
error = False
while not finished:
error = False
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)
# 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:
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.HIGH)
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:
break
#send num1
num1_binary = '{0:11b}'.format(num1)
print(num1_binary)
for pin in range (2, 13):
if num1_binary[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:
break
#send num2
num2_binary = '{0:11b}'.format(num2)
for pin in range (2, 13):
if num2_binary[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:
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;
print("waiting for ack solution header")
acknowledged = False
if error:
break
#get the solution (15 bits long)
solution = [0]*12
for pin in range (16, 28):
solution[pin-16] = GPIO.input(pin)
solution_str = ""
for binary in solution:
solution_str += str(binary)
print(solution_str)
print(isinstance(solution_str, str))
solution_int = int(solution_str, 2)
print(solution_int)
#send end calculation instruction
GPIO.output(2, GPIO.LOW)
GPIO.output(3, GPIO.LOW)
GPIO.output(4, GPIO.HIGH)
GPIO.output(5, GPIO.HIGH)
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)
while ')' in formula:
old_partition = formula.partition(')')[0]
reversed_partition = old_partition[::-1]
reversed_partition = reversed_partition.partition('(')[0]
new_partition = reversed_partition[::-1]
answer = formula_to_array(new_partition)
replace_str = ''.join(['(' , new_partition, ')'])
formula = formula.replace(replace_str, str(answer), 1)
print(formula)
if formula[0] == '-':
formula = formula.replace('-', "0-", 1)
temp = ""
array = []
for character in range (0, len(formula)):
if re.match(r"[0-9]", formula[character]):
temp += str(formula[character])
else:
if len(temp) != 0:
array.append(int(temp))
temp = ""
array.append(formula[character])
if len(temp) != 0:
array.append(int(temp))
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 = "(0-512)+12/6*256-1024-3*12"
formula = "22+3+2+5+8+7+2"
formula = check_formula(formula)
fin_answer = 0
if formula != "Error":
# print(get_formula_answer(formula))
print(formula_to_array(formula))
else:
print("Error")