/
mk01_scope.py
executable file
·138 lines (103 loc) · 4.35 KB
/
mk01_scope.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
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
#!/usr/bin/python3
import sys
import numpy as np
import time
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import lappdIfc # Board+firmware *specific* stuff
import lappdProtocol # Board+firmware *independent* stuff
import lappdTool # UX shortcuts: mixed board and protocol stuff
#################### COMMON TOOL INITIALIZATION BEGIN ################
# Set up a new tool, with arguments common to the event digestion
# subsystem. All tools will share the same baseline syntax and semantics.
parser = lappdTool.create("Measure per capacitor gain slopes between two given voltages")
# Add some specific options for this tool
parser.add_argument('--xmin', metavar='XMIN', type=float, default=0, help='Leftmost x-axis point')
parser.add_argument('--xmax', metavar='XMAX', type=float, default=1024, help='Rightmost x-axis point.')
parser.add_argument('--ymin', metavar='YMIN', type=float, default=-0.01, help='Bottommost y-axis point')
parser.add_argument('--ymax', metavar='YMAX', type=float, default=0.1, help='Topmost y-axis point.')
parser.add_argument('--gain', metavar='GAIN', type=int, default=51242, help='ADC counts per volt')
parser.add_argument('--threshold', metavar='THRESHOLD', type=float, help='Only "trigger" when above threshold')
# Handle common configuration due to the common arguments
ifc, args, eventQueue = lappdTool.connect(parser)
# This is the fork() point, so it needs to be inside the
# script called.
if __name__ == "__main__":
intakeProcesses = lappdTool.spawn(args, eventQueue)
############## COMMON TOOL INITIALIZATION END ##############
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Set up for multiplicative correction
args.gain = 1.0/args.gain
#fig, ax = plt.subplots()
fig = plt.figure()
ax = plt.axes()
ax.set_ylim(args.ymin, args.ymax)
ax.set_xlim(args.xmin, args.xmax)
ifc.brd.pokenow(0x320, (1 << 6), readback=False, silent=True)
# Notify that a trigger was sent
print("Initial trigger sent...", file=sys.stderr)
evt = eventQueue.get()
# Doesnt work with dictionaries?
lines = []
chans = []
for chan in evt.channels.keys():
line = ax.plot(np.linspace(args.xmin, args.xmax, 10), [0]*10, linestyle=('solid' if chan < 31 else 'dashed'))[0]
line.set_label('Channel %d' % chan)
lines.append(line)
chans.append(chan)
ax.legend()
zeros = [0.0]*1024
prevTrigs = []
def animate(i):
global prevTrigs
if not args.external:
ifc.brd.pokenow(0x320, (1 << 6), readback=False, silent=True)
# Notify that a trigger was sent
print("Trigger sent...", file=sys.stderr)
time.sleep(args.i)
#for i in range(20):
evt = eventQueue.get()
#
# for chan, ampls in evt.channels:
currentTrigs = []
if isinstance(evt.channels[chans[0]][0], tuple):
found = False
for n,line in enumerate(lines):
xdata, ydata = zip(*evt.channels[chans[n]])
ydata = [y * args.gain if y is not None else float('nan') for y in ydata]
for y in ydata:
if y > args.threshold:
# Fire
line.set_data(xdata, ydata)
found = True
currentTrigs.append(line)
print("Channel %d over threshold!" % chans[n])
break
if found:
# Clear all other previous triggers
for prev in prevTrigs:
if not prev in currentTrigs:
print("Clearing stale line")
prev.set_data(xdata, zeros)
# Make the current triggers the previous ones
prevTrigs = currentTrigs
else:
for n,line in enumerate(lines):
xdata, ydata = zip(*enumerate(evt.channels[chans[n]]))
ydata = [y * args.gain if y is not None else float('nan') for y in ydata]
found = False
for y in ydata:
if y > args.threshold:
# Fire
line.set_data(xdata, ydata)
found = True
break
#if not found:
# # Zero it out
# line.set_data(xdata, zeros)
eventQueue.task_done()
return lines#,
ani = animation.FuncAnimation(fig, animate, interval=10, blit=True, save_count=10)
plt.show()