Debian Codegraph @

demos/stripchart1.tcl

raw
1 #!../src/bltwish
2
3 package require BLT
4 # --------------------------------------------------------------------------
5 # Starting with Tcl 8.x, the BLT commands are stored in their own
6 # namespace called "blt". The idea is to prevent name clashes with
7 # Tcl commands and variables from other packages, such as a "table"
8 # command in two different packages.
9 #
10 # You can access the BLT commands in a couple of ways. You can prefix
11 # all the BLT commands with the namespace qualifier "blt::"
12 #
13 # blt::graph .g
14 # blt::table . .g -resize both
15 #
16 # or you can import all the command into the global namespace.
17 #
18 # namespace import blt::*
19 # graph .g
20 # table . .g -resize both
21 #
22 # --------------------------------------------------------------------------
23 if { $tcl_version >= 8.0 } {
24 namespace import -force blt::*
25 namespace import -force blt::tile::*
26 }
27 source scripts/demo.tcl
28
29 # ----------------------------------------------------------------------
30 # EXAMPLE: simple driver for stripchart widget
31 # ----------------------------------------------------------------------
32 # Michael J. McLennan
33 # [email protected]
34 # Bell Labs Innovations for Lucent Technologies
35 # ======================================================================
36 # Copyright (c) 1996 Lucent Technologies
37 # ======================================================================
38
39 option add *x.range 20.0
40 option add *x.shiftBy 15.0
41 option add *bufferElements no
42 option add *bufferGraph yes
43 option add *symbol triangle
44 option add *Axis.lineWidth 1
45 option add *Axis*Rotate 90
46 option add *pixels 1.25m
47 #option add *PlotPad 25
48 option add *Stripchart.width 6i
49 #option add *Smooth quadratic
50 #option add *Stripchart.invertXY yes
51 #option add *x.descending yes
52
53 # ----------------------------------------------------------------------
54 # USAGE: random ?<max>? ?<min>?
55 #
56 # Returns a random number in the range <min> to <max>.
57 # If <min> is not specified, the default is 0; if max is not
58 # specified, the default is 1.
59 # ----------------------------------------------------------------------
60
61 proc random {{max 1.0} {min 0.0}} {
62 global randomSeed
63
64 set randomSeed [expr (7141*$randomSeed+54773) % 259200]
65 set num [expr $randomSeed/259200.0*($max-$min)+$min]
66 return $num
67 }
68 set randomSeed 14823
69
70 # ----------------------------------------------------------------------
71
72 toplevel .addSource
73 wm title .addSource "Add Source"
74 wm group .addSource .
75 wm withdraw .addSource
76 wm protocol .addSource WM_DELETE_WINDOW {.addSource.controls.cancel invoke}
77
78 frame .addSource.info
79 pack .addSource.info -expand yes -fill both -padx 4 -pady 4
80 label .addSource.info.namel -text "Name:"
81 entry .addSource.info.name
82 label .addSource.info.maxl -text "Maximum:"
83 entry .addSource.info.max
84 label .addSource.info.minl -text "Minimum:"
85 entry .addSource.info.min
86 table .addSource.info \
87 .addSource.info.namel 0,0 -anchor e \
88 .addSource.info.name 0,1 -fill x \
89 .addSource.info.maxl 1,0 -anchor e \
90 .addSource.info.max 1,1 -fill x \
91 .addSource.info.minl 2,0 -anchor e \
92 .addSource.info.min 2,1 -fill x
93
94 frame .addSource.color
95 pack .addSource.color -padx 8 -pady 4
96 frame .addSource.color.sample -width 30 -height 30 -borderwidth 2 -relief raised
97 pack .addSource.color.sample -side top -fill both
98 scale .addSource.color.r -label "Red" -orient vertical \
99 -from 100 -to 0 -command source_color
100 pack .addSource.color.r -side left -fill y
101 scale .addSource.color.g -label "Green" -orient vertical \
102 -from 100 -to 0 -command source_color
103 pack .addSource.color.g -side left -fill y
104 scale .addSource.color.b -label "Blue" -orient vertical \
105 -from 100 -to 0 -command source_color
106 pack .addSource.color.b -side left -fill y
107
108 proc source_color {args} {
109 set r [expr round(2.55*[.addSource.color.r get])]
110 set g [expr round(2.55*[.addSource.color.g get])]
111 set b [expr round(2.55*[.addSource.color.b get])]
112 set color [format "#%2.2x%2.2x%2.2x" $r $g $b]
113 .addSource.color.sample configure -background $color
114 }
115 source_color
116
117 frame .addSource.sep -borderwidth 1 -height 2 -relief sunken
118 pack .addSource.sep -fill x -pady 4
119
120 frame .addSource.controls
121 pack .addSource.controls -fill x -padx 4 -pady 4
122 button .addSource.controls.ok -text "OK" -command {
123 wm withdraw .addSource
124 set name [.addSource.info.name get]
125 set color [.addSource.color.sample cget -background]
126 set max [.addSource.info.max get]
127 set min [.addSource.info.min get]
128 if {[catch {source_create $name $color $min $max} err] != 0} {
129 puts "error: $err"
130 }
131 }
132 pack .addSource.controls.ok -side left -expand yes -padx 4
133 button .addSource.controls.cancel -text "Cancel" -command {
134 wm withdraw .addSource
135 }
136 pack .addSource.controls.cancel -side left -expand yes -padx 4
137
138 set useAxes y
139
140 proc source_create {name color min max} {
141 global sources
142
143 if {[info exists sources($name-controls)]} {
144 error "source \"$name\" already exists"
145 }
146 if {$max <= $min} {
147 error "bad range: $min - $max"
148 }
149
150 set unique 0
151 set win ".sources.nb.s[incr unique]"
152 while {[winfo exists $win]} {
153 set win ".sources.nb.s[incr unique]"
154 }
155
156 set xvname "xvector$unique"
157 set yvname "yvector$unique"
158 set wvname "wvector$unique"
159 global $xvname $yvname $wvname
160 # catch { $xvname delete }
161 # catch { $yvname delete }
162 # catch { $wvname delete }
163 vector create $xvname $yvname $wvname
164
165 if {$xvname == "xvector1"} {
166 $xvname append 0
167 } else {
168 xvector1 variable thisVec
169 $xvname append $thisVec(end)
170 }
171 $yvname append [random $max $min]
172 $wvname append 0
173
174 catch {.sc element delete $name}
175 .sc element create $name -x $xvname -y $yvname -color $color
176 if { $name != "default" } {
177 .sc axis create $name -title $name \
178 -limitscolor $color -limitsformat "%4.4g" -titlecolor ${color}
179 .sc element configure $name -mapy $name
180 global useAxes
181 lappend useAxes $name
182 set count 0
183 if 0 {
184 set yUse {}
185 set y2Use {}
186 foreach axis $useAxes {
187 if { $count & 1 } {
188 lappend yUse $axis
189 .sc axis configure $axis -rotate 90
190 } else {
191 lappend y2Use $axis
192 .sc axis configure $axis -rotate -90
193 }
194 incr count
195 }
196 .sc y2axis use $y2Use
197 .sc yaxis use $yUse
198 } else {
199 .sc yaxis use $useAxes
200 }
201 }
202 set cwin .sources.choices.rb$unique
203 radiobutton $cwin -text $name \
204 -variable choices -value $win -command "
205 foreach w \[pack slaves .sources.nb\] {
206 pack forget \$w
207 }
208 pack $win -fill both
209 "
210 pack $cwin -anchor w
211
212 frame $win
213 pack $win -fill x
214 label $win.limsl -text "Limits:"
215 entry $win.lims
216 bind $win.lims <KeyPress-Return> "
217 .sc yaxis configure -limits {%%g}
218 "
219 label $win.smoothl -text "Smooth:"
220 frame $win.smooth
221 radiobutton $win.smooth.linear -text "Linear" \
222 -variable smooth -value linear -command "
223 .sc element configure $name -smooth linear
224 "
225 pack $win.smooth.linear -side left
226 radiobutton $win.smooth.step -text "Step" \
227 -variable smooth -value step -command "
228 .sc element configure $name -smooth step
229 "
230 pack $win.smooth.step -side left
231 radiobutton $win.smooth.natural -text "Natural" \
232 -variable smooth -value natural -command "
233 .sc element configure $name -smooth natural
234 "
235 pack $win.smooth.natural -side left
236 label $win.ratel -text "Sampling Rate:"
237 scale $win.rate -orient horizontal -from 10 -to 1000
238
239 table $win \
240 $win.smoothl 0,0 -anchor e \
241 $win.smooth 0,1 -fill x -padx 4 \
242 $win.limsl 1,0 -anchor e \
243 $win.lims 1,1 -fill x -padx 4 \
244 $win.ratel 2,0 -anchor e \
245 $win.rate 2,1 -fill x -padx 2
246
247 if {$unique != 1} {
248 button $win.del -text "Delete" -command [list source_delete $name]
249 pack $win.del -anchor w
250 table $win $win.del 3,1 -anchor e -padx 4 -pady 4
251 }
252
253 $win.rate set 100
254 catch {$win.smooth.[.sc element cget $name -smooth] invoke} mesg
255
256 set sources($name-choice) $cwin
257 set sources($name-controls) $win
258 set sources($name-stream) [after 100 [list source_event $name 100]]
259 set sources($name-x) $xvname
260 set sources($name-y) $yvname
261 set sources($name-w) $wvname
262 set sources($name-max) $max
263 set sources($name-min) $min
264 set sources($name-steady) [random $max $min]
265
266 $cwin invoke
267 }
268
269 proc source_delete {name} {
270 global sources
271
272 after cancel $sources($name-stream)
273 destroy $sources($name-choice)
274 destroy $sources($name-controls)
275 unset sources($name-controls)
276
277 set first [lindex [pack slaves .sources.choices] 0]
278 $first invoke
279 }
280
281 proc source_event {name delay} {
282 global sources x y
283
284 set xv $sources($name-x)
285 set yv $sources($name-y)
286 set wv $sources($name-w)
287 global $xv $yv $wv
288
289 $xv variable x
290 set x(++end) [expr $x(end) + 0.001 * $delay]
291
292 $yv variable y
293 if {[random] > 0.97} {
294 set y(++end) [random $sources($name-max) $sources($name-min)]
295 } else {
296 set y(++end) [expr $y(end)+0.1*($sources($name-steady)-$y(end))]
297 }
298 set val [random]
299 if {$val > 0.95} {
300 $wv append 2
301 } elseif {$val > 0.8} {
302 $wv append 1
303 } else {
304 $wv append 0
305 }
306 #$wv notify now
307 if { [$xv length] > 100 } {
308 $xv delete 0
309 $yv delete 0
310 $wv delete 0
311 }
312 update
313 set win $sources($name-controls)
314 set delay [$win.rate get]
315 set sources($name-stream) [after $delay [list source_event $name $delay]]
316 }
317
318 # ----------------------------------------------------------------------
319 frame .mbar -borderwidth 2 -relief raised
320 pack .mbar -fill x
321
322 menubutton .mbar.main -text "Main" -menu .mbar.main.m
323 pack .mbar.main -side left -padx 4
324 menu .mbar.main.m
325 .mbar.main.m add command -label "Add Source..." -command {
326 set x [expr [winfo rootx .]+50]
327 set y [expr [winfo rooty .]+50]
328 wm geometry .addSource +$x+$y
329 wm deiconify .addSource
330 }
331 .mbar.main.m add separator
332 .mbar.main.m add command -label "Quit" -command exit
333
334 menubutton .mbar.prefs -text "Preferences" -menu .mbar.prefs.m
335 pack .mbar.prefs -side left -padx 4
336
337 menu .mbar.prefs.m
338 .mbar.prefs.m add cascade -label "Warning Symbol" -menu .mbar.prefs.m.wm
339 menu .mbar.prefs.m.wm
340 .mbar.prefs.m add cascade -label "Error Symbol" -menu .mbar.prefs.m.em
341 menu .mbar.prefs.m.em
342
343 foreach sym {square circle diamond plus cross triangle} {
344 .mbar.prefs.m.wm add radiobutton -label $sym \
345 -variable warningsym -value $sym \
346 -command {.sc pen configure "warning" -symbol $warningsym}
347
348 .mbar.prefs.m.em add radiobutton -label $sym \
349 -variable errorsym -value $sym \
350 -command {.sc pen configure "error" -symbol $errorsym}
351 }
352 catch {.mbar.prefs.m.wm invoke "circle"}
353 catch {.mbar.prefs.m.em invoke "cross"}
354
355 # ----------------------------------------------------------------------
356 stripchart .sc -title "Stripchart"
357 pack .sc -expand yes -fill both
358
359 .sc xaxis configure -title "Time (s)" -autorange 2.0 -shiftby 0.1
360 .sc yaxis configure -title "Samples"
361
362 frame .sources
363 frame .sources.nb -borderwidth 2 -relief sunken
364 label .sources.title -text "Sources:"
365 frame .sources.choices -borderwidth 2 -relief groove
366
367 if 0 {
368 pack .sources -fill x -padx 10 -pady 4
369 pack .sources.nb -side right -expand yes -fill both -padx 4 -pady 4
370 pack .sources.title -side top -anchor w -padx 4
371 pack .sources.choices -expand yes -fill both -padx 4 -pady 4
372 }
373
374
375 source_create default red 0 10
376 source_create temp blue3 0 10
377 source_create pressure green3 0 200
378 source_create volume orange3 0 1020
379 source_create power yellow3 0 0.01999
380 source_create work magenta3 0 10
381
382 Blt_ZoomStack .sc
383
384 .sc axis bind Y <Enter> {
385 set axis [%W axis get current]
386 set detail [%W axis get detail]
387 if { $detail == "line" } {
388 %W axis configure $axis -background grey
389 }
390 }
391 .sc axis bind Y <Leave> {
392 set axis [%W axis get current]
393 %W axis configure $axis -background ""
394 }
395
396 .sc axis bind Y <ButtonPress-1> {
397 set axis [%W axis get current]
398 # scan [%W axis limits $axis] "%%g %%g" min max
399 # set min [expr $min + (($max - $min) * 0.1)]
400 # set max [expr $max - (($max - $min) * 0.1)]
401 # %W axis configure $axis -min $min -max $max
402 %W axis configure $axis -logscale yes
403 }
404
405 .sc axis bind Y <ButtonPress-3> {
406 set axis [%W axis get current]
407 # %W axis configure $axis -min {} -max {}
408 %W axis configure $axis -logscale no
409 }
410