-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlivegraph_data_listener.coffee
416 lines (318 loc) · 11.5 KB
/
livegraph_data_listener.coffee
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
pixelpulse = (window.pixelpulse ?= {})
class pixelpulse.TimeseriesGraphListener extends server.DataListener
constructor: (@device, @streams, @graphs=[]) ->
super(@device, @streams)
@xaxis = new livegraph.Axis(-10, 0, 's', true)
@xaxis.windowChanged = @checkWindowChange
@updatePending = no
makeGraph: (stream, elem, color) ->
console.assert(stream in @streams, "stream ", stream, "not in", @streams)
g = new TimeseriesGraph(this, stream, elem, color)
@graphs.push(g)
return g
queueWindowUpdate: ->
unless @updatePending
setTimeout(@updateWindow, 10)
@updatePending = true
# run after a window changing operation to fetch new data from the server
updateWindow: (min, max) =>
@updatePending = false
lg = @graphs[0]
return unless lg.width
min ?= @xaxis.visibleMin
max ?= @xaxis.visibleMax
span = max-min
if @trigger
# Adjust the holdoff so a sweep takes a minimum of 0.1s
@trigger.holdoff = Math.max(0, 0.1-span)
else
# Expand the range so scrolling doesn't immedately request new data
# Unnecessary with triggering enabled as it will fetch new data anyway
min = Math.max(min - 0.4*span, @xaxis.min)
max = Math.min(max + 0.4*span, @xaxis.max)
pts = lg.width / 2 * (max - min) / span
@configure(min, max, pts)
@submit()
# As part of a x-axis changing action, check if we need to fetch new server data
checkWindowChange: (min, max, done, target) =>
lg = @graphs[0]
l = this
if target
if (target[1] - target[0]) < 0.5 * (max - min)
# if zooming in, wait until near the end to change the view
return
[min, max] = target
span = max-min
if ((l.xmax < max or l.xmin > min) \ # Off the edge of the data
and max <= @xaxis.max and min >= @xaxis.min) \ # But not off the edge of the available data
or span/(l.xmax - l.xmin)*l.requestedPoints < 0.45 * lg.width # or resolution too low
@updateWindow(min, max)
# Set the timeseries view to the specified window
goToWindow: (min, max, animate=true) ->
if animate
opts = {time: 200}
return new livegraph.AnimateXAction(opts, @graphs[0], min, max, @graphs)
else
@xaxis.window(min, max, true)
@redrawAll(true)
redrawAll: (full=false)->
for lg in @graphs then lg.needsRedraw(full)
onMessage: (m) ->
super(m)
@redrawAll()
cancelAllActions: ->
for lg in @graphs then lg.startAction(null)
zoomCompletelyOut: (animate=true) ->
@goToWindow(@xaxis.min, @xaxis.max, animate)
# Fake autoset by assuming the CEE is sourcing the wave
# Just find out what frequency the source is
fakeAutoset: (animate = true) ->
src = @trigger.stream.parent.source
sampleTime = @device.sampleTime
f = 2
timescale = switch src.source
when 'adv_square'
(src.highSamples + src.lowSamples) * sampleTime*f
when 'sine', 'triangle', 'square', 'arb'
src.period * sampleTime*f
else
0.5
@goToWindow(Math.max(@xaxis.min, -timescale), Math.min(@xaxis.max, timescale), animate)
autozoom: ->
if @trigger
@fakeAutoset()
else
@zoomCompletelyOut()
canChangeView: ->
@trigger or not server.device.captureState
updateDotsAll: ->
i.updateDots() for i in @graphs
isTriggerEnabled: -> return if @trigger then true else false
enableTrigger: ->
@xaxis.min = -1
@xaxis.max = 1
for lg in @graphs
lg.showXgridZero = yes
default_trigger_level = 2.5
tp = if flags.outputTrigger then 'out' else 'in'
@configureTrigger(@streams[0], default_trigger_level, 0.1, 0, 0.5, tp)
@triggerOverlay = new livegraph.TriggerOverlay(@graphs[0])
@triggerOverlay.position(default_trigger_level)
@setTrigger(@streams[0], default_trigger_level, false)
@fakeAutoset(false)
dragTrigger: (stream, level) ->
if stream.isSource() and @device.hasOutTrigger
@trigger.level = level = stream.sourceLevel()
@triggerOverlay.position(level) if level?
updateTriggerForOutput: ->
stream = @trigger.stream
if stream.isSource() != (@trigger.type == 'out') and @device.hasOutTrigger
@setTrigger(stream, @trigger.level)
@dragTrigger(stream)
setTrigger: (stream, level=0, submit=true) ->
@trigger.stream = stream
@trigger.level = level
if stream.isSource() and @device.hasOutTrigger
@trigger.force = 10
@trigger.type = 'out'
else
@trigger.force = 0.5
@trigger.type = 'in'
@dragTrigger(stream, level)
@submit() if submit
if @trigger.type == 'in'
@triggerOverlay.style('#ffaa00', 1)
else
@triggerOverlay.style('#22aa00', 0)
@updateDotsAll()
disableTrigger: ->
@xaxis.min = -10
@xaxis.max = 0
@xaxis.window(-10, 0, true)
for lg in @graphs
lg.showXgridZero = no
@triggerOverlay.remove()
@triggerOverlay = null
super()
@updateDotsAll()
@redrawAll(true)
class DataSeries extends livegraph.Series
constructor: (@listener, @xseries, @yseries) ->
@updated = @listener.updated
@listener.reset.listen @reset
@reset()
reset: =>
@xdata = (if @xseries == 'time'
@listener.xdata
else
@listener.data[@listener.streamIndex(@xseries)])
@ydata = @listener.data[@listener.streamIndex(@yseries)]
class TimeseriesGraph extends livegraph.canvas
constructor: (@timeseries, @stream, elem, color) ->
@yaxis = new livegraph.Axis(@stream.min, @stream.max, @stream.unit, true)
if @stream.unit is 'mA'
@yaxis.prescale = 1000
@yaxis.unit = 'A'
else
@yaxis.prescale = 1
@dseries = new DataSeries(@timeseries, 'time', @stream)
@dseries.color = color
@dots = {}
@dotConfig = ''
super(elem, @timeseries.xaxis, @yaxis, [@dseries])
onClick: (pos, e) =>
[x,y] = pos
if @dotConfig is 'wave' and @dots.offset.isNear(x, y, 10)
new DragDotAction this, pos, (lg, x, y) ->
lg.stream.parent.setAdjust {offset:y}
else if @dotConfig is 'wave' and @dots.period.isNear(x, y, 10)
new DragDotAction this, pos, (lg, x, y) ->
amplitude = y - lg.stream.parent.source.offset
period = Math.max(5, x * 4 / server.device.sampleTime)
lg.stream.parent.setAdjust {amplitude, period}
else if @dotConfig is 'square' and @dots.v1.isNear(x, y, 10)
new DragDotAction this, pos, (lg, x, y) ->
{highSamples, lowSamples} = lg.stream.parent.source
period = highSamples + lowSamples
x = Math.max(0, Math.min(period-1, x/server.device.sampleTime)) + 1
lg.stream.parent.setAdjust
high: y
highSamples: Math.round(x)
lowSamples: period - Math.round(x)
dutyCycleHint: x / period
else if @dotConfig is 'square' and @dots.v2.isNear(x, y, 10)
new DragDotAction this, pos, (lg, x, y) ->
{highSamples, lowSamples, dutyCycleHint} = lg.stream.parent.source
oldPeriod = highSamples + lowSamples
newPeriod = Math.round(Math.max(2, x/server.device.sampleTime + 1))
lg.stream.parent.setAdjust
highSamples: Math.round(dutyCycleHint * newPeriod)
lowSamples: Math.round((1-dutyCycleHint) * newPeriod)
low: y
dutyCycleHint: dutyCycleHint
else if x > @width - 45
new DragDotAction this, pos, (lg, x, y) ->
lg.stream.parent.setConstant(lg.stream.outputMode, y)
else if x < 45 and @timeseries.trigger
if @timeseries.trigger.stream != @stream
@timeseries.triggerOverlay.remove() if @timeseries.triggerOverlay
@timeseries.triggerOverlay = new livegraph.TriggerOverlay(this)
@timeseries.setTrigger(@stream, null, false)
new DragTriggerAction(this, pos)
else if @timeseries.canChangeView()
new livegraph.DragScrollAction(this, pos, @timeseries.graphs)
onDblClick: (e, pos, btn) =>
if not @timeseries.canChangeView() then return
zf = if e.shiftKey or btn==2 then 2 else 0.5
if zf < 1 and @timeseries.xaxis.span() < 40 * @timeseries.device.sampleTime
# Prevent zooming in uselessly far
return
opts = {time: 200, zoomFactor:zf }
return new livegraph.ZoomXAction(opts, this, pos,
@timeseries.graphs)
resetDots: (t) ->
unless @dotConfig is t
for i, v of @dots
v.remove()
@dots = {}
@dotConfig = t
return true
updateDots: ->
isTriggerStream = @timeseries.trigger?.stream == @stream
isSource = @stream.isSource()
s = @stream.parent.source
sampleTime = server.device.sampleTime
if isSource and s.source == 'constant'
if @resetDots('constant')
@dots.d = new livegraph.Dot(this, @dseries.cssColor(), 5, 'r')
@dots.d.position(null, s.value)
else if isSource and server.device.hasOutTrigger and isTriggerStream
if s.source in ['sine', 'triangle', 'square']
if @resetDots('wave')
@dots.offset = new livegraph.Dot(this, @dseries.cssColor(), 5, '')
@dots.period = new livegraph.Dot(this, @dseries.cssColor(), 5, '')
@dots.offset.position(0, s.offset)
@dots.period.position(s.period*sampleTime/4, s.offset+s.amplitude)
else if s.source is 'adv_square'
if @resetDots('square')
@dots.v1 = new livegraph.Dot(this, @dseries.cssColor(), 5, '')
@dots.v2 = new livegraph.Dot(this, @dseries.cssColor(), 5, '')
@dots.v1.position((s.highSamples-1)*sampleTime, s.high)
@dots.v2.position((s.highSamples+s.lowSamples-1)*sampleTime, s.low)
else
@resetDots('')
else
@resetDots('')
sourceChanged: (isSource, m) ->
@updateDots()
if @timeseries.trigger.stream is @stream
@timeseries.updateTriggerForOutput()
gainChanged: (g) ->
@yaxis.window(@yaxis.min/g, @yaxis.max/g, true)
@needsRedraw(true)
onResized: ->
@timeseries.queueWindowUpdate()
class DragDotAction extends livegraph.Action
constructor: (@lg, pos, fn) ->
super(@lg, pos)
(@withPos = fn) if fn
@lg.startDrag(pos)
@transform = livegraph.makeTransform(@lg.geom, @lg.xaxis, @lg.yaxis)
@onDrag(pos)
onDrag: ([x, y]) ->
[x, y] = livegraph.invTransform(x,y,@transform)
y = Math.min(Math.max(y, @lg.stream.min), @lg.stream.max)
@withPos(@lg, x, y)
class DragTriggerAction extends DragDotAction
withPos: (lg, x, @y) ->
pixelpulse.timeseries.dragTrigger(@lg.stream, @y)
onRelease: ->
pixelpulse.timeseries.setTrigger(@lg.stream, @y)
class pixelpulse.XYGraphView
constructor: (@el) ->
@graphdiv = $("<div class='livegraph'>").appendTo(@el)
@xlabel = pixelpulse.makeStreamSelect()
@xlabel.addClass('xaxislabel').appendTo(@el).change(@axisSelectChanged)
@ylabel = pixelpulse.makeStreamSelect()
@ylabel.addClass('yaxislabel').appendTo(@el).change(@axisSelectChanged)
@color = [255, 0, 0]
@lg = new livegraph.canvas(@graphdiv.get(0), false, false, [false],
{xbottom:true, yright:false, xgrid:true})
axisSelectChanged: =>
xaxis = @xlabel.stream()
yaxis = @ylabel.stream()
if xaxis != @xaxis or yaxis != @yaxis
@configure(xaxis, yaxis)
configure: (@xstream, @ystream) ->
@xaxis = new livegraph.Axis(@xstream.min, @xstream.max)
@yaxis = new livegraph.Axis(@ystream.min, @ystream.max)
@lg.xaxis = @xaxis
@lg.yaxis = @yaxis
@xlabel.selectStream(@xstream)
@ylabel.selectStream(@ystream)
@hidden()
@series = new DataSeries(pixelpulse.timeseries, @xstream, @ystream)
@series.color = @color
@lg.series = [@series]
@xstream.gainChanged.listen @xGainChanged
@xGainChanged(@xstream.gain)
@ystream.gainChanged.listen @yGainChanged
@yGainChanged(@ystream.gain)
@series.updated.listen @updated
pixelpulse.layoutChanged.subscribe @relayout
@lg.needsRedraw(true)
hidden: ->
if @series
@series.updated.unListen @updated
pixelpulse.layoutChanged.unListen @relayout
if @xaxis then @xstream.gainChanged.unListen @xGainChanged
if @yaxis then @ystream.gainChanged.unListen @yGainChanged
updated: => @lg.needsRedraw()
xGainChanged: (g) =>
@xaxis.window(@xaxis.min/g, @xaxis.max/g, true)
@lg.needsRedraw(true)
yGainChanged: (g) =>
@yaxis.window(@yaxis.min/g, @yaxis.max/g, true)
@lg.needsRedraw(true)
relayout: =>
@lg.resized()