-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
68 lines (53 loc) · 1.63 KB
/
main.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
"""
To test game inputs, run this program in a terminal and then try to
decode the input from reading a terminal.
"""
import time
import argparse
import subprocess
from fir_tester import FPGA, Controller
NIOS_TERMINAL = "nios2-terminal.exe"
SAMPLING_RATE = 1000
def simulate(sample_rate):
# The sampling rate of the FPGA accelerometer
fpga = FPGA()
controller = Controller()
while True:
output = fpga.output(
controller.make_random_movement(),
0,
controller.get_key(),
controller.get_key()
)
print(FPGA.uart_decode(output))
time.sleep(1.0 / sample_rate)
def real():
process = subprocess.Popen([NIOS_TERMINAL],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL)
# Skip first 100 lines (e.g. "nios2-terminal: connected to hardware target")
for _ in range(100):
process.stdout.readline()
while True:
line = process.stdout.readline().decode('utf-8').strip()
print(FPGA.uart_decode(line))
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"-r", "--real",
action="store_true",
help="Run the program with real inputs"
)
parser.add_argument(
"-s", "--sample-rate",
default=SAMPLING_RATE,
type=int,
help="Set the sample rate of the FPGA accelerometer (Hz)"
)
args = parser.parse_args()
if args.real:
real()
else:
simulate(args.sample_rate)
if __name__ == '__main__':
main()