-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimerTest.py
More file actions
85 lines (62 loc) · 2.03 KB
/
timerTest.py
File metadata and controls
85 lines (62 loc) · 2.03 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
"""
Impossible to achieve enough accuracy for timers in Windows.
https://stackoverflow.com/questions/7079864/real-time-operating-via-python
"""
import threading
import time
from vcd import VCDWriter
counter = 0
start = 0
def timer1_func(e, vcd, gpio1):
global counter
global start
counter += 1
if (counter == 20):
e.set()
else:
timer1 = threading.Timer(10e-3, timer1_func, args=(e, vcd, gpio1))
timer1.start()
end = time.perf_counter_ns()
timestamp = end - start
start = time.perf_counter_ns()
print ("Timer-1 ", timestamp/1e6)
print ("Timer-1 ", counter)
end = time.perf_counter_ns()
timestamp = end - start
vcd.change(gpio1, timestamp, 0)
vcd.change(gpio1, timestamp + 1, 1)
vcd.change(gpio1, timestamp + 2, 0)
def timer2_func(e, vcd, gpio2):
global start
if (e.is_set()):
return
else:
timer2 = threading.Timer(15e-3, timer2_func, args=(e, vcd, gpio2))
timer2.start()
print ("Timer-2 ", counter)
end = time.perf_counter_ns()
timestamp = end - start
vcd.change(gpio2, timestamp, 0)
vcd.change(gpio2, timestamp + 1, 1)
vcd.change(gpio2, timestamp + 2, 0)
def main():
global start
vcdFile = open("out.vcd", "w")
vcd = VCDWriter(vcdFile, timescale='1 ns')
gpio1 = vcd.register_var('BLE', 'timer1', 'wire', size=1, ident='!')
gpio2 = vcd.register_var('BLE', 'timer2', 'wire', size=1, ident='$')
start = time.perf_counter_ns()
# event to signal the consumer to stop consuming
e = threading.Event()
timer1 = threading.Timer(10e-3, timer1_func, args=(e, vcd, gpio1))
#timer2 = threading.Timer(15e-3, timer2_func, args=(e, vcd, gpio2))
timer1.start()
time.sleep(10e-3)
#timer2.start()
# wait until all timers terminate
e.wait()
print("Finish")
vcd.close()
vcdFile.close()
if __name__ == '__main__':
main()