source: trunk/3DEES/UserInterface.tcl@ 185

Last change on this file since 185 was 184, checked in by demin, 11 years ago

fix classifier

File size: 45.1 KB
RevLine 
[73]1package require XOTcl
2
3package require BLT
4package require swt
5package require usb
6
[85]7package require zlib
[73]8
[85]9wm minsize . 1000 700
10
[73]11namespace eval ::mca {
12 namespace import ::xotcl::*
13
14 namespace import ::blt::vector
15 namespace import ::blt::graph
16 namespace import ::blt::tabnotebook
17
[143]18# -------------------------------------------------------------------------
19
20 variable oscCodes
21 array set oscCodes {
22 1 {Channel 1}
23 2 {Channel 2}
24 3 {Channel 3}
25 4 {Channel 4}
26 5 {Channel 5}
27 6 {Trigger}
28 }
29
30# -------------------------------------------------------------------------
31
32 variable adcCodes
33 array set adcCodes {
[181]34 1 {S1_F}
35 2 {S1_S}
36 3 {S2}
37 4 {D1}
38 5 {D2}
39 6 {D3}
[143]40 }
41
42# -------------------------------------------------------------------------
43
[181]44 variable cfgCodes
45 array set cfgCodes {
46 {1_0} 0
47 {1_1} 6
[184]48 {1_2} 7
49 {1_3} 8
[181]50 {2_0} 1
[184]51 {2_1} 9
52 {2_2} 10
53 {2_3} 11
[181]54 {3_0} 2
[184]55 {3_1} 12
56 {3_2} 13
57 {3_3} 14
[181]58 {4_0} 3
[184]59 {4_1} 15
60 {4_2} 16
61 {4_3} 17
[181]62 {5_0} 4
[184]63 {5_1} 18
64 {5_2} 19
65 {5_3} 20
[181]66 {6_0} 5
[184]67 {6_1} 21
68 {6_2} 22
69 {6_3} 23
[181]70 }
71# -------------------------------------------------------------------------
72
73 variable cfgThrs
74 array set cfgThrs {
[184]75 0 8
76 6 150
77 7 200
78 8 3000
79 1 8
80 9 150
81 10 200
82 11 3000
83 2 10
84 12 31
85 13 52
86 14 79
87 3 60
88 15 135
89 16 171
90 17 233
91 4 249
92 18 348
93 19 495
94 20 693
95 5 505
96 21 606
97 22 707
98 23 808
[181]99 }
100
101
102# -------------------------------------------------------------------------
103
[143]104 variable inpCodes
105 array set inpCodes {
[183]106 0 {rs}
107 1 {cs}
108 2 {av}
109 3 {af}
110 4 {bn}
[143]111 }
112
113# -------------------------------------------------------------------------
114
115 proc validate {max size value} {
[85]116 if {![regexp {^[0-9]*$} $value]} {
[73]117 return 0
[143]118 } elseif {[regexp {^0[0-9]+$} $value]} {
119 return 0
[85]120 } elseif {$value > $max} {
[73]121 return 0
[143]122 } elseif {[string length $value] > $size} {
[73]123 return 0
124 } else {
125 return 1
126 }
127 }
128
129# -------------------------------------------------------------------------
130
[143]131 proc doublevalidate {max value} {
132 if {![regexp {^[0-9]{0,2}\.?[0-9]{0,3}$} $value]} {
133 return 0
134 } elseif {[regexp {^0[0-9]+$} $value]} {
135 return 0
136 } elseif {$value > $max} {
137 return 0
138 } else {
139 return 1
140 }
141 }
[85]142
143# -------------------------------------------------------------------------
144
[143]145 proc legendLabel {master row key title} {
146 label ${master}.${key}_label -anchor w -text ${title}
147 label ${master}.${key}_value -width 10 -anchor e -text {}
[85]148
[143]149 grid ${master}.${key}_label -row ${row} -column 1 -sticky w
150 grid ${master}.${key}_value -row ${row} -column 2 -sticky ew
151 }
152
153# -------------------------------------------------------------------------
154
155 proc legendButton {master row key title var bg {fg black}} {
156 checkbutton ${master}.${key}_check -variable $var
157 label ${master}.${key}_label -anchor w -text ${title} -bg ${bg} -fg $fg
158 label ${master}.${key}_value -width 10 -anchor e -text {} -bg ${bg} -fg $fg
159
160 grid ${master}.${key}_check -row ${row} -column 0 -sticky w
161 grid ${master}.${key}_label -row ${row} -column 1 -sticky w
162 grid ${master}.${key}_value -row ${row} -column 2 -sticky ew
163 }
164
165# -------------------------------------------------------------------------
166
167 Class UsbController
168
169# -------------------------------------------------------------------------
170
[180]171 UsbController instproc init {} {
172
173 my set ignore false
174
175 next
176 }
177
178# -------------------------------------------------------------------------
179
[143]180 UsbController instproc usbConnect {} {
[180]181 my instvar handle ignore
[143]182
183 puts usbConnect
184
185 if {[my exists handle]} {
186 $handle disconnect
187 unset handle
[85]188 }
[180]189 if {!$ignore} {
190 while {[catch {usb::connect 0x09FB 0x6001 1 1 0} result]} {
191 set answer [tk_messageBox -icon error -type abortretryignore \
192 -message {Cannot access USB device} -detail $result]
193 if {[string equal $answer abort]} exit
194 if {[string equal $answer ignore]} {
195 set ignore true
196 return
197 }
198 }
[143]199
[180]200 set handle $result
[143]201
[180]202 }
[85]203 }
204
205# -------------------------------------------------------------------------
206
[143]207 UsbController instproc usbHandle {} {
[180]208 my instvar handle ignore
[85]209
[143]210 if {[my exists handle]} {
211 return $handle
[180]212 } elseif {!$ignore} {
[143]213 my usbConnect
214 }
215 }
[85]216
[143]217# -------------------------------------------------------------------------
218
219 UsbController instproc usbCmd {command} {
220 set code [catch {[my usbHandle] writeRaw [usb::convert $command]} result]
221 switch -- $code {
222 1 {
223# puts $result
224 my usbConnect
225 }
[85]226 }
227
228 }
229
230# -------------------------------------------------------------------------
231
[143]232 UsbController instproc usbCmdReadRaw {command size data} {
233 my usbCmd $command
[85]234
[143]235 set code [catch {[my usbHandle] readRaw $size} result]
236 switch -- $code {
237 0 {
238 set $data $result
239 }
240 1 {
241# puts $result
242 my usbConnect
243 }
244 5 {
245# puts Busy
246 }
247 }
248 }
249
250# -------------------------------------------------------------------------
251
252 UsbController instproc usbCmdReadRaw {command size data} {
[85]253 my usbCmd $command
254
[143]255 set code [catch {[my usbHandle] readRaw $size} result]
256 switch -- $code {
257 0 {
258 set $data $result
259 }
260 1 {
261# puts $result
262 my usbConnect
263 }
264 5 {
265# puts Busy
266 }
[85]267 }
[143]268 }
[85]269
[143]270# -------------------------------------------------------------------------
271
272 UsbController instproc usbCmdReadHex {command width size data} {
273 my usbCmd $command
274
275 set code [catch {[my usbHandle] readHex $width $size} result]
276 switch -- $code {
277 0 {
278 set $data $result
279 }
280 1 {
281# puts $result
282 my usbConnect
283 }
284 5 {
285# puts Busy
286 }
287 }
[85]288 }
289
290# -------------------------------------------------------------------------
291
[180]292 Class CfgDisplay -parameter {
[77]293 {master}
[143]294 {controller}
[77]295 }
296
297# -------------------------------------------------------------------------
298
[180]299 CfgDisplay instproc init {} {
[77]300
301 my setup
302
303 next
304 }
305
306# -------------------------------------------------------------------------
307
[180]308 CfgDisplay instproc destroy {} {
[77]309 next
310 }
311
312# -------------------------------------------------------------------------
313
[180]314 CfgDisplay instproc start {} {
315 variable adcCodes
[181]316 variable cfgThrs
[77]317
[180]318 foreach {ch id} [array get adcCodes] {
[181]319 my set delay($ch) 32
320 my set decay($ch) 1000
[180]321 }
322
[181]323 foreach {i value} [array get cfgThrs] {
324 my set thrs($i) $value
[180]325 }
326
[181]327 trace add variable [myvar decay] write [myproc decay_update]
328 trace add variable [myvar delay] write [myproc delay_update]
329 trace add variable [myvar thrs] write [myproc thrs_update]
330
331 my delay_update
332 my thrs_update
[77]333 }
334
335# -------------------------------------------------------------------------
336
[180]337 CfgDisplay instproc setup {} {
338 variable adcCodes
[181]339 variable cfgCodes
[77]340 my instvar number master
341 my instvar config
342
[180]343 set thrs [frame ${master}.thrs]
344 set clip [frame ${master}.clip]
[77]345
[180]346 set config(thrs) [labelframe ${thrs}.frame -borderwidth 1 -relief sunken -text {Thresholds}]
[77]347
[180]348 set column 0
[184]349 foreach {input} [list "ADC" "thrs 1" "thrs 2" "thrs 3" "thrs 4"] {
[180]350 label ${config(thrs)}.label_${column} -text "${input}"
351 grid ${config(thrs)}.label_${column} -row 0 -column ${column} -sticky ew -padx 5 -pady 7
352 incr column
353 }
[77]354
[184]355 foreach {ch id} [array get adcCodes] {
[181]356 label ${config(thrs)}.chan_${ch} -text "${id} "
[180]357 grid ${config(thrs)}.chan_${ch} -row ${ch} -column 0 -sticky ew -padx 5 -pady 7
[184]358 foreach {num} [list 0 1 2 3] {
[180]359 set column [expr {$num + 1}]
[181]360 set value $cfgCodes(${ch}_${num})
[180]361 spinbox ${config(thrs)}.thrs_${value} -from 0 -to 4095 \
362 -increment 10 -width 10 -textvariable [myvar thrs($value)] \
363 -validate all -vcmd {::mca::validate 4095 4 %P}
364 grid ${config(thrs)}.thrs_${value} -row ${ch} -column ${column} -sticky w -padx 5 -pady 7
365 }
366 }
[77]367
[180]368 grid $config(thrs) -row 0 -column 0 -sticky news -padx 10
[77]369
[180]370 set config(clip) [labelframe ${clip}.frame -borderwidth 1 -relief sunken -text {Signal clipping}]
[77]371
[180]372 set column 0
373 foreach {input} [list "ADC" "delay" "decay"] {
374 label ${config(clip)}.label_${column} -text "${input}"
375 grid ${config(clip)}.label_${column} -row 0 -column ${column} -sticky ew -padx 5 -pady 7
376 incr column
377 }
[143]378
[180]379 foreach {ch id} [array get adcCodes] {
[181]380 label ${config(clip)}.chan_${ch} -text "${id} "
[180]381 grid ${config(clip)}.chan_${ch} -row ${ch} -column 0 -sticky ew -padx 5 -pady 7
382 spinbox ${config(clip)}.delay_${ch} -from 0 -to 62 \
383 -increment 2 -width 10 -textvariable [myvar delay($ch)] \
384 -validate all -vcmd {::mca::validate 63 5 %P}
385 grid ${config(clip)}.delay_${ch} -row ${ch} -column 1 -sticky w -padx 5 -pady 7
386 spinbox ${config(clip)}.decay_${ch} -from 0 -to 65535 \
387 -increment 10 -width 10 -textvariable [myvar decay($ch)] \
388 -validate all -vcmd {::mca::validate 65535 5 %P}
389 grid ${config(clip)}.decay_${ch} -row ${ch} -column 2 -sticky w -padx 5 -pady 7
390 }
[143]391
[180]392 grid $config(clip) -row 0 -column 0 -sticky news -padx 10
[77]393
[184]394 grid ${thrs} -row 0 -column 1 -sticky news
[180]395 grid ${clip} -row 0 -column 0 -sticky news
[77]396
[180]397 grid columnconfigure ${master} 0 -weight 1
398 grid columnconfigure ${master} 1 -weight 1
399 grid rowconfigure ${master} 0 -weight 1
[77]400
[180]401 grid rowconfigure ${thrs} 0 -weight 0
402 grid rowconfigure ${clip} 0 -weight 0
[77]403 }
404
405# -------------------------------------------------------------------------
406
[180]407 CfgDisplay instproc decay_update args {
408 my instvar controller decay delay
[77]409
[143]410 set command {}
[180]411 for {set i 1} {$i <= 6} {incr i} {
412 set a $delay($i).0
413 set b $decay($i).0
[181]414 set value [expr int(exp(-${a}/${b})*1024*20)]
[184]415 append command [format {000200%02x0004%04x} [expr {34 + 2 * (${i} - 1)}] $value]
[180]416 }
[77]417
[143]418 $controller usbCmd $command
[73]419 }
420
421# -------------------------------------------------------------------------
422
[180]423 CfgDisplay instproc delay_update args {
424 my instvar controller delay
[73]425
[143]426 set command {}
[180]427 for {set i 1} {$i <= 6} {incr i} {
[184]428 append command [format {000200%02x0004%04x} [expr {35 + 2 * (${i} - 1)}] $delay($i)]
[180]429 }
[73]430
[143]431 $controller usbCmd $command
[73]432
[180]433 my decay_update
[73]434 }
435
436# -------------------------------------------------------------------------
437
[180]438 CfgDisplay instproc thrs_update args {
439 my instvar controller thrs
[73]440
[143]441 set command {}
[184]442 for {set i 0} {$i <= 23} {incr i} {
[180]443 append command [format {000200%02x0004%04x} [expr {10 + ${i}}] $thrs($i)]
444 }
[143]445
446 $controller usbCmd $command
[73]447 }
448
449# -------------------------------------------------------------------------
450
[143]451 Class MuxDisplay -parameter {
452 {master}
453 {controller}
[85]454 }
455
456# -------------------------------------------------------------------------
457
[143]458 MuxDisplay instproc init {} {
[73]459
[143]460 my setup
[73]461
[143]462 next
[73]463 }
464
465# -------------------------------------------------------------------------
466
[143]467 MuxDisplay instproc destroy {} {
468 next
[73]469 }
470
471# -------------------------------------------------------------------------
472
[143]473 MuxDisplay instproc start {} {
474 variable adcCodes
475 my instvar config chan_val
[74]476
[143]477 set chan_val(1) 0
478 set chan_val(2) 0
479 set chan_val(3) 0
480 set chan_val(4) 0
481 set chan_val(5) 0
482 set chan_val(6) 0
[85]483
[143]484 trace add variable [myvar chan_val] write [myproc chan_val_update]
485 trace add variable [myvar polar] write [myproc polar_update]
[74]486
[183]487 $config(1).chan_1_1 select
488 $config(2).chan_1_2 select
489 $config(3).chan_1_3 select
490 $config(4).chan_1_4 select
491 $config(5).chan_1_5 select
492 $config(6).chan_2_4 select
[143]493
494 foreach {ch dummy} [array get adcCodes] {
495 $config(inv).polar_${ch} deselect
496 }
[73]497 }
498
499# -------------------------------------------------------------------------
500
[143]501 MuxDisplay instproc setup {} {
502 variable oscCodes
503 variable adcCodes
504 variable inpCodes
505 my instvar master
506 my instvar config
[74]507
[143]508 set size [array size inpCodes]
509 set oscList [array get oscCodes]
510 set adcList [array get adcCodes]
511 set inpList [array get inpCodes]
[74]512
[143]513 set mux [frame ${master}.mux]
514 set key [frame ${master}.key]
515 set inv [frame ${master}.inv]
[180]516
[143]517 foreach {osc title} $oscList {
518 set config($osc) [labelframe ${mux}.$osc -borderwidth 1 -relief sunken -text $title]
519 foreach {code input} $inpList {
[183]520 set column [expr {$code + 1}]
[143]521 label $config($osc).input_${input} -text " ${input}"
522 grid $config($osc).input_${input} -row 0 -column ${column} -sticky w
[74]523 }
[181]524 foreach {ch id} $adcList {
525 label $config($osc).chan_${ch} -text "${id} "
[143]526 grid $config($osc).chan_${ch} -row ${ch} -column 0 -sticky ew
527 foreach {code input} $inpList {
528 set column [expr {$code + 1}]
529 set value [expr {$size * ($ch - 1) + $code}]
530 radiobutton $config($osc).chan_${code}_${ch} -variable [myvar chan_val($osc)] -value ${value}
531 grid $config($osc).chan_${code}_${ch} -row ${ch} -column ${column} -sticky w
532 }
[74]533 }
[183]534 set column [expr {($osc - 1) % 3}]
535 set row [expr {($osc - 1) / 3}]
[143]536 grid $config($osc) -row ${row} -column ${column} -sticky news -padx 10
[74]537 }
538
[143]539 set config(key) [labelframe ${key}.frame -borderwidth 1 -relief sunken -text {legend}]
[74]540
[183]541 label $config(key).rs -text "rs - raw signal"
542 grid $config(key).rs -row 0 -column 0 -sticky news
[74]543
[183]544 label $config(key).cs -text "cs - filtered and clipped signal"
545 grid $config(key).cs -row 0 -column 1 -sticky news
[180]546
[183]547 label $config(key).av -text "av - amplitude value"
548 grid $config(key).av -row 0 -column 2 -sticky news
[180]549
[183]550 label $config(key).af -text "af - amplitude flag"
551 grid $config(key).af -row 0 -column 3 -sticky news
[74]552
[183]553 label $config(key).bn -text "bn - bin number"
554 grid $config(key).bn -row 0 -column 4 -sticky news
555
556 label $config(key).bf -text "bf - bin flag"
557 grid $config(key).bf -row 0 -column 5 -sticky news
558
[143]559 grid $config(key) -row 0 -column 0 -sticky news -padx 10
560
[180]561
[143]562 set config(inv) [labelframe ${inv}.frame -borderwidth 1 -relief sunken -text {polarity inversion}]
563 label $config(inv).chan_label -text "channel "
564 grid $config(inv).chan_label -row 0 -column 0 -sticky e
565 label $config(inv).polar_label -text "polarity"
566 grid $config(inv).polar_label -row 1 -column 0 -sticky e
567 foreach {ch dummy} $adcList {
568 label $config(inv).chan_${ch} -text "${ch} "
569 grid $config(inv).chan_${ch} -row 0 -column ${ch} -sticky ew
570 checkbutton $config(inv).polar_${ch} -variable [myvar polar($ch)]
571 grid $config(inv).polar_${ch} -row 1 -column ${ch} -sticky w
[74]572 }
[143]573 grid $config(inv) -row 0 -column 0 -sticky news -padx 10
[74]574
[143]575 grid ${key} -row 0 -column 0 -sticky news
576 grid ${mux} -row 1 -column 0 -sticky news
577 grid ${inv} -row 2 -column 0 -sticky news
[74]578
[143]579 grid columnconfigure ${master} 0 -weight 1
580 grid rowconfigure ${master} 0 -weight 1
581 grid rowconfigure ${master} 1 -weight 1
582 grid rowconfigure ${master} 2 -weight 1
[73]583
[143]584 grid columnconfigure ${inv} 0 -weight 1
[73]585
[143]586 grid columnconfigure ${key} 0 -weight 1
587 grid columnconfigure $config(key) 0 -weight 1
588 grid columnconfigure $config(key) 1 -weight 1
589 grid columnconfigure $config(key) 2 -weight 1
590 grid columnconfigure $config(key) 3 -weight 1
[183]591 grid columnconfigure $config(key) 4 -weight 1
592 grid columnconfigure $config(key) 5 -weight 1
[143]593
[180]594
[143]595 grid columnconfigure ${mux} 0 -weight 1
596 grid columnconfigure ${mux} 1 -weight 1
597 grid columnconfigure ${mux} 2 -weight 1
[73]598 }
599
[143]600# ------------------------------------------------------------------------
[73]601
[143]602 MuxDisplay instproc chan_val_update args {
603 my instvar controller chan_val
[73]604
[143]605 set byte1 [format {%02x%02x} $chan_val(2) $chan_val(1)]
606 set byte2 [format {%02x%02x} $chan_val(4) $chan_val(3)]
607 set byte3 [format {%02x%02x} $chan_val(6) $chan_val(5)]
[73]608
[143]609 $controller usbCmd 000200020004${byte1}000200030004${byte2}000200040004${byte3}
[73]610 }
611
612# -------------------------------------------------------------------------
613
[143]614 MuxDisplay instproc polar_update args {
615 my instvar controller polar
[180]616
[143]617 set value {0b}
[180]618 for {set i 6} {$i >= 1} {incr i -1} {
619 append value $polar($i)
[143]620 }
[73]621
[143]622 set value [format {%04x} $value]
[73]623
[143]624 $controller usbCmd 000200010004${value}
[73]625 }
626
627# -------------------------------------------------------------------------
628
[143]629 Class HstDisplay -parameter {
[73]630 {number}
631 {master}
[143]632 {controller}
[73]633 }
634
635# -------------------------------------------------------------------------
636
637 HstDisplay instproc init {} {
638
[143]639 my set data {}
[73]640
[180]641 vector create [myvar xvec](64)
642 vector create [myvar yvec](64)
[73]643
[180]644 # fill one vector for the x axis with 64 points
645 [myvar xvec] seq -0.5 63.5
[143]646
[73]647 my setup
648
649 next
650 }
651
652# -------------------------------------------------------------------------
653
654 HstDisplay instproc destroy {} {
655 next
656 }
657
658# -------------------------------------------------------------------------
659
660 HstDisplay instproc start {} {
[143]661 my instvar config
[73]662
[143]663 trace add variable [myvar data] write [myproc data_update]
[73]664
[85]665 trace add variable [myvar axis] write [myproc axis_update]
[73]666
[143]667 ${config}.axis_check select
[85]668
[143]669 my set yvec_bak 0.0
670 my set yvec_old 0.0
[73]671 }
672
673# -------------------------------------------------------------------------
674
675 HstDisplay instproc setup {} {
676 my instvar number master
[143]677 my instvar xvec yvec graph
678 my instvar config thrs thrs_val base base_typ base_val
679 my instvar cntr_h cntr_m cntr_s
[73]680
681 # create a graph widget and show a grid
682 set graph [graph ${master}.graph -height 250 -leftmargin 80]
[143]683 $graph crosshairs configure -hide no -linewidth 1 -color darkblue -dashes {2 2}
[73]684 $graph grid configure -hide no
685 $graph legend configure -hide yes
[180]686 $graph axis configure x -min 0 -max 64
[73]687
[143]688 set config [frame ${master}.config -width 170]
[73]689
[85]690 checkbutton ${config}.axis_check -text {log scale} -variable [myvar axis]
[73]691
[143]692 frame ${config}.spc1 -width 170 -height 30
[73]693
[143]694 frame ${config}.chan_frame -borderwidth 0 -width 170
695 legendLabel ${config}.chan_frame 0 axisy {Bin entries}
696 legendLabel ${config}.chan_frame 1 axisx {Bin number}
[85]697
[180]698 frame ${config}.spc2 -width 170 -height 30
[85]699
[143]700 button ${config}.start -text Start \
701 -bg yellow -activebackground yellow -command [myproc cntr_start]
702 button ${config}.reset -text Reset \
703 -bg red -activebackground red -command [myproc cntr_reset]
704
[180]705 frame ${config}.spc3 -width 170 -height 30
[143]706
[73]707 button ${config}.register -text Register \
708 -bg lightblue -activebackground lightblue -command [myproc register]
709
[85]710 grid ${config}.axis_check -sticky w
711 grid ${config}.spc1
[180]712 grid ${config}.chan_frame -sticky ew -padx 5
[85]713 grid ${config}.spc2
[143]714 grid ${config}.start -sticky ew -pady 3 -padx 5
715 grid ${config}.reset -sticky ew -pady 3 -padx 5
[180]716 grid ${config}.spc3
[73]717 grid ${config}.register -sticky ew -pady 3 -padx 5
718
719 grid ${graph} -row 0 -column 0 -sticky news
720 grid ${config} -row 0 -column 1
721
[77]722 grid rowconfigure ${master} 0 -weight 1
723 grid columnconfigure ${master} 0 -weight 1
724 grid columnconfigure ${master} 1 -weight 0 -minsize 80
725
[143]726 grid columnconfigure ${config}.chan_frame 1 -weight 1
727
[73]728 # enable zooming
729 Blt_ZoomStack $graph
730
[143]731 my crosshairs $graph
732
[73]733 #bind .graph <Motion> {%W crosshairs configure -position @%x,%y}
734
735 # create one element with data for the x and y axis, no dots
[143]736 $graph element create Spectrum1 -color blue -linewidth 2 -symbol none -smooth step -xdata [myvar xvec] -ydata [myvar yvec]
[73]737 }
738
739# -------------------------------------------------------------------------
740
[143]741 HstDisplay instproc coor_update {W x y} {
742 my instvar config graph
[85]743
[143]744 $W crosshairs configure -position @${x},${y}
[85]745
[143]746 set index [$W axis invtransform x $x]
747 set index [::tcl::mathfunc::round $index]
748 catch {
749 ${config}.chan_frame.axisy_value configure -text [[myvar yvec] index $index]
750 ${config}.chan_frame.axisx_value configure -text ${index}.0
751 }
[85]752 }
753# -------------------------------------------------------------------------
754
[143]755 HstDisplay instproc crosshairs {graph} {
756 set method [myproc coor_update]
757 bind $graph <Motion> [list [self] coor_update %W %x %y]
758 bind $graph <Leave> {
759 %W crosshairs off
[73]760 }
[143]761 bind $graph <Enter> {
762 %W crosshairs on
763 }
[73]764 }
765
766# -------------------------------------------------------------------------
767
[143]768 HstDisplay instproc axis_update args {
769 my instvar axis graph
770 if {$axis} {
771 $graph axis configure y -min 1 -max 1E10 -logscale yes
[73]772 } else {
[143]773 $graph axis configure y -min {} -max {} -logscale no
[73]774 }
775 }
776
777# -------------------------------------------------------------------------
778
[143]779 HstDisplay instproc cntr_reset {} {
[180]780 my instvar controller number
[143]781
782 my cntr_stop
783
784 set value [format %04x [expr {1 << (5 + ${number})}]]
785 $controller usbCmd 000200000004${value}0002000000040000
786
787 my acquire
788 }
789
790# -------------------------------------------------------------------------
791
792 HstDisplay instproc cntr_start {} {
793 my instvar controller config number auto
794
[180]795 set val_addr [format %02x [expr {6 + ${number}}]]
[143]796
[180]797 ${config}.start configure -text Stop -command [myproc cntr_stop]
[143]798# ${config}.reset configure -state disabled
799
[180]800 $controller usbCmd 000200${val_addr}00040001
[143]801
802 set auto 1
803
804 after 100 [myproc acquire_loop]
805 }
806
807# -------------------------------------------------------------------------
808
809 HstDisplay instproc cntr_stop {} {
810 my instvar controller config number auto
811
[180]812 set val_addr [format %02x [expr {6 + ${number}}]]
[143]813
[180]814 ${config}.start configure -text Start -command [myproc cntr_start]
815
[143]816 $controller usbCmd 000200${val_addr}00040000
817
818 set auto 0
[180]819
820 my acquire
[143]821 }
822
823# -------------------------------------------------------------------------
824
825 HstDisplay instproc data_update args {
826 my instvar data
827 usb::convertBlt $data 4 [myvar yvec]
828 }
829
830# -------------------------------------------------------------------------
831
[85]832 HstDisplay instproc acquire_loop {} {
[143]833 my instvar cntr_val auto
[73]834
[85]835 my acquire
836
[180]837 if {$auto} {
[143]838 after 1000 [myproc acquire_loop]
839 }
[85]840 }
841
842# -------------------------------------------------------------------------
843
[143]844 HstDisplay instproc acquire {} {
845 my instvar controller config number
[85]846
[180]847 set size 64
[143]848
849 set prefix [format {%x} [expr {$number + 2}]]
850
851 set value [format {%08x} [expr {$size * 2}]]
852
853 set command 0001${prefix}000000200000001[string range $value 0 3]0003[string range $value 4 7]00050000
854
855 $controller usbCmdReadRaw $command [expr {$size * 4}] [myvar data]
856 }
857
[85]858# -------------------------------------------------------------------------
859
[143]860 HstDisplay instproc save_data {data} {
861 my instvar number
[85]862
[143]863 set types {
864 {{Data Files} {.dat} }
865 {{All Files} * }
866 }
[85]867
[143]868 set stamp [clock format [clock seconds] -format %Y%m%d_%H%M%S]
869 set fname spectrum_[expr {$number + 1}]_${stamp}.dat
870
871 set fname [tk_getSaveFile -filetypes $types -initialfile $fname]
872 if {[string equal $fname {}]} {
873 return
[73]874 }
[85]875
[143]876 set x [catch {
877 set fid [open $fname w+]
878 puts $fid $data
879 close $fid
880 }]
[85]881
[143]882 if { $x || ![file exists $fname] || ![file isfile $fname] || ![file readable $fname] } {
883 tk_messageBox -icon error \
884 -message "An error occurred while writing to \"$fname\""
885 } else {
886 tk_messageBox -icon info \
887 -message "File \"$fname\" written successfully"
888 }
889 }
[85]890
[143]891# -------------------------------------------------------------------------
[85]892
[143]893 HstDisplay instproc register {} {
[180]894 my save_data [join [[myvar yvec] range 0 63] \n]
[73]895 }
896
897# -------------------------------------------------------------------------
898
[143]899 Class OscDisplay -parameter {
[85]900 {master}
[143]901 {controller}
[85]902 }
903
904# -------------------------------------------------------------------------
905
[143]906 OscDisplay instproc init {} {
907 my instvar sequence data xvec yvec
[85]908
[143]909 set data {}
[180]910
[143]911 set sequence 0
[85]912
[143]913# set xvec [vector create #auto(262144)]
[180]914# set xvec [vector create #auto(10000)]
915 set xvec [vector create #auto(60000)]
[143]916
917 for {set i 1} {$i <= 9} {incr i} {
918# dict set yvec $i [vector create #auto(262144)]
[180]919# dict set yvec $i [vector create #auto(10000)]
920 dict set yvec $i [vector create #auto(60000)]
[143]921 }
922
[85]923 # fill one vector for the x axis
[143]924# $xvec seq 0 262143
[180]925# $xvec seq 0 10000
926 $xvec seq 0 60000
[85]927
928 my setup
929
930 next
931 }
932
933# -------------------------------------------------------------------------
934
[143]935 OscDisplay instproc destroy {} {
[85]936 next
937 }
938
939# -------------------------------------------------------------------------
940
[143]941 OscDisplay instproc start {} {
942 my instvar config
943 my instvar recs_val directory
[85]944
[180]945 set directory $::env(HOME)
[143]946 set recs_val 100
[85]947
[143]948 trace add variable [myvar chan] write [myproc chan_update]
[85]949
[143]950 trace add variable [myvar data] write [myproc data_update]
[85]951
[143]952 trace add variable [myvar auto] write [myproc auto_update]
[85]953
[143]954 trace add variable [myvar thrs] write [myproc thrs_update 0]
955 trace add variable [myvar thrs_val] write [myproc thrs_update 0]
956
957 trace add variable [myvar recs_val] write [myproc recs_val_update]
958
959 trace add variable [myvar last] write [myproc last_update]
960
[180]961 for {set i 1} {$i <= 6} {incr i} {
962 ${config}.chan_frame.chan${i}_check select
963 ${config}.chan_frame.chan${i}_value configure -text 0.0
964 }
965 ${config}.chan_frame.axisx_value configure -text 0.0
[143]966
967 ${config}.thrs_check select
[183]968 ${config}.thrs_field set 60
[85]969 }
970
971# -------------------------------------------------------------------------
972
[143]973 OscDisplay instproc setup {} {
[85]974 my instvar master
[143]975 my instvar xvec yvec graph
976 my instvar config
[85]977
978 # create a graph widget and show a grid
979 set graph [graph ${master}.graph -height 250 -leftmargin 80]
[143]980 $graph crosshairs configure -hide no -linewidth 1 -color darkblue -dashes {2 2}
[85]981 $graph grid configure -hide no
982 $graph legend configure -hide yes
[180]983 $graph axis configure x -min 0 -max 60000
[143]984 $graph axis configure y -min 0 -max 4100
[85]985
[143]986# scale ${master}.last -orient horizontal -from 1 -to 27 -tickinterval 0 -showvalue no -variable [myvar last]
[85]987
[143]988 set config [frame ${master}.config -width 170]
[85]989
[143]990 frame ${config}.chan_frame -width 170
991 legendButton ${config}.chan_frame 0 chan1 {Channel 1} [myvar chan(1)] turquoise2
992 legendButton ${config}.chan_frame 1 chan2 {Channel 2} [myvar chan(2)] SpringGreen2
993 legendButton ${config}.chan_frame 2 chan3 {Channel 3} [myvar chan(3)] orchid2
994 legendButton ${config}.chan_frame 3 chan4 {Channel 4} [myvar chan(4)] orange2
995 legendButton ${config}.chan_frame 4 chan5 {Channel 5} [myvar chan(5)] blue1 white
996 legendButton ${config}.chan_frame 5 chan6 {Channel 6} [myvar chan(6)] gray65 white
997 legendLabel ${config}.chan_frame 6 axisx {Time axis}
998
999 frame ${config}.spc1 -width 170 -height 30
1000
1001 checkbutton ${config}.auto_check -text {auto update} -variable [myvar auto]
1002
1003 frame ${config}.spc2 -width 170 -height 30
1004
1005 checkbutton ${config}.thrs_check -text threshold -variable [myvar thrs]
1006 spinbox ${config}.thrs_field -from 1 -to 4095 \
1007 -increment 5 -width 10 -textvariable [myvar thrs_val] \
1008 -validate all -vcmd {::mca::validate 4095 4 %P}
1009
1010 frame ${config}.spc3 -width 170 -height 30
1011
[85]1012 button ${config}.acquire -text Acquire \
[143]1013 -bg green -activebackground green -command [myproc acquire_start]
[85]1014 button ${config}.register -text Register \
1015 -bg lightblue -activebackground lightblue -command [myproc register]
1016
[143]1017 frame ${config}.spc4 -width 170 -height 30
1018
1019 label ${config}.recs -text {number of records}
1020 spinbox ${config}.recs_field -from 0 -to 10000 \
1021 -increment 10 -width 10 -textvariable [myvar recs_val] \
1022 -validate all -vcmd {::mca::validate 10000 5 %P}
1023
1024 frame ${config}.spc5 -width 170 -height 10
1025
1026 button ${config}.sequence -text {Start Recording} -command [myproc sequence_start] \
1027 -bg yellow -activebackground yellow
1028
[180]1029 frame ${config}.spc6 -width 170 -height 30
[143]1030
1031 button ${config}.recover -text {Read file} \
1032 -bg lightblue -activebackground lightblue -command [myproc recover]
1033
1034 grid ${config}.chan_frame -sticky ew
1035 grid ${config}.spc1
1036 grid ${config}.auto_check -sticky w
1037 grid ${config}.spc2
1038 grid ${config}.thrs_check -sticky w
1039 grid ${config}.thrs_field -sticky ew -pady 1 -padx 5
1040 grid ${config}.spc3
[85]1041 grid ${config}.acquire -sticky ew -pady 3 -padx 5
1042 grid ${config}.register -sticky ew -pady 3 -padx 5
[143]1043 grid ${config}.spc4
1044 grid ${config}.recs -sticky w -pady 1 -padx 3
1045 grid ${config}.recs_field -sticky ew -pady 1 -padx 5
1046 grid ${config}.spc5
1047 grid ${config}.sequence -sticky ew -pady 3 -padx 5
1048 grid ${config}.spc6
1049 grid ${config}.recover -sticky ew -pady 3 -padx 5
[85]1050
1051 grid ${graph} -row 0 -column 0 -sticky news
1052 grid ${config} -row 0 -column 1
1053
[143]1054# grid ${master}.last -row 1 -column 0 -columnspan 2 -sticky ew
[85]1055
1056 grid rowconfigure ${master} 0 -weight 1
1057 grid columnconfigure ${master} 0 -weight 1
[143]1058 grid columnconfigure ${master} 1 -weight 0 -minsize 120
[85]1059
[143]1060 grid columnconfigure ${config}.chan_frame 2 -weight 1
1061
[85]1062 # enable zooming
1063 Blt_ZoomStack $graph
1064
[143]1065 my crosshairs $graph
[85]1066
1067 # create one element with data for the x and y axis, no dots
[143]1068 $graph pen create pen1 -color turquoise3 -linewidth 2 -symbol none
1069 $graph pen create pen2 -color SpringGreen3 -linewidth 2 -symbol none
1070 $graph pen create pen3 -color orchid3 -linewidth 2 -symbol none
1071 $graph pen create pen4 -color orange3 -linewidth 2 -symbol none
1072 $graph pen create pen5 -color blue2 -linewidth 2 -symbol none
1073 $graph pen create pen6 -color gray55 -linewidth 2 -symbol none
1074
1075 $graph element create Spectrum1 -pen pen1 -xdata $xvec -ydata [dict get $yvec 1]
1076 $graph element create Spectrum2 -pen pen2 -xdata $xvec -ydata [dict get $yvec 2]
1077 $graph element create Spectrum3 -pen pen3 -xdata $xvec -ydata [dict get $yvec 3]
1078 $graph element create Spectrum4 -pen pen4 -xdata $xvec -ydata [dict get $yvec 4]
1079 $graph element create Spectrum5 -pen pen5 -xdata $xvec -ydata [dict get $yvec 5]
1080 $graph element create Spectrum6 -pen pen6 -xdata $xvec -ydata [dict get $yvec 6]
[85]1081 }
1082
1083# -------------------------------------------------------------------------
1084
[143]1085 OscDisplay instproc coor_update {W x y} {
1086 my instvar xvec yvec graph
1087 my instvar config
[85]1088
[143]1089 $W crosshairs configure -position @${x},${y}
1090
1091 set index [$W axis invtransform x $x]
1092 set index [::tcl::mathfunc::round $index]
1093 catch {
1094 ${config}.chan_frame.chan1_value configure -text [[dict get $yvec 1] index $index]
1095 ${config}.chan_frame.chan2_value configure -text [[dict get $yvec 2] index $index]
1096 ${config}.chan_frame.chan3_value configure -text [[dict get $yvec 3] index $index]
1097 ${config}.chan_frame.chan4_value configure -text [[dict get $yvec 4] index $index]
1098 ${config}.chan_frame.chan5_value configure -text [[dict get $yvec 5] index $index]
1099 ${config}.chan_frame.chan6_value configure -text [[dict get $yvec 6] index $index]
1100 ${config}.chan_frame.axisx_value configure -text ${index}.0
1101 }
1102 }
1103# -------------------------------------------------------------------------
1104
1105 OscDisplay instproc crosshairs {graph} {
1106 set method [myproc coor_update]
1107 bind $graph <Motion> [list [self] coor_update %W %x %y]
1108 bind $graph <Leave> {
1109 %W crosshairs off
1110 }
1111 bind $graph <Enter> {
1112 %W crosshairs on
1113 }
1114 }
1115
1116# -------------------------------------------------------------------------
1117
1118 OscDisplay instproc chan_update {name key op} {
1119 my instvar config graph chan
1120
1121 if {$chan(${key})} {
1122 $graph pen configure pen${key} -linewidth 2
1123 } else {
1124 $graph pen configure pen${key} -linewidth 0
1125 }
1126 }
1127
1128# -------------------------------------------------------------------------
1129
1130 OscDisplay instproc recs_val_update args {
1131 my instvar recs_val
1132 if {[string equal $recs_val {}]} {
1133 set recs_val 0
1134 }
1135 }
1136
1137# -------------------------------------------------------------------------
1138
1139 OscDisplay instproc last_update args {
1140 my instvar graph last
1141
[85]1142 set first [expr {$last - 1}]
[143]1143
1144 $graph axis configure x -min ${first}0000 -max ${last}0000
[85]1145 }
1146
1147# -------------------------------------------------------------------------
1148
[143]1149 OscDisplay instproc thrs_update {reset args} {
1150 my instvar controller config thrs thrs_val
1151
1152 if {[string equal $thrs_val {}]} {
1153 set thrs_val 0
1154 }
1155
1156 if {$thrs} {
1157 ${config}.thrs_field configure -state normal
[180]1158 set value [format {%03x} $thrs_val]
[143]1159 } else {
1160 ${config}.thrs_field configure -state disabled
1161 set value 000
1162 }
1163
1164 set command {}
1165 if {$reset} {
1166 append command 0002000500041${value}
1167 }
1168 append command 0002000500040${value}
1169
1170 $controller usbCmd $command
1171 }
1172
1173# -------------------------------------------------------------------------
1174
1175 OscDisplay instproc data_update args {
1176 my instvar data yvec
1177 my instvar graph chan waiting sequence auto
1178
1179 usb::convertOsc $data $yvec
1180
1181 foreach {key value} [array get chan] {
1182 $graph pen configure pen${key} -dashes 0
1183 }
1184
1185 set waiting 0
1186
1187 if {$sequence} {
1188 my sequence_register
1189 } elseif {$auto} {
1190 after 1000 [myproc acquire_start]
1191 }
1192 }
1193
1194# -------------------------------------------------------------------------
1195
1196 OscDisplay instproc acquire_start {} {
1197 my instvar graph chan controller waiting
1198
1199 foreach {key value} [array get chan] {
1200 $graph pen configure pen${key} -dashes dot
1201 }
1202
1203 # restart
1204 my thrs_update 1
1205
1206 set waiting 1
1207
1208 after 200 [myproc acquire_loop]
1209 }
1210
1211# -------------------------------------------------------------------------
1212
1213 OscDisplay instproc acquire_loop {} {
1214 my instvar controller waiting
1215
1216# set size 262144
[180]1217# set size 10000
1218 set size 60000
[143]1219
1220 set value [format {%08x} [expr {$size * 4}]]
1221
1222 set command 00011000000200000001[string range $value 0 3]0003[string range $value 4 7]00050000
[180]1223
[143]1224 $controller usbCmdReadRaw $command [expr {$size * 8}] [myvar data]
1225
1226 if {$waiting} {
1227 after 200 [myproc acquire_loop]
1228 }
1229 }
1230
1231# -------------------------------------------------------------------------
1232
1233 OscDisplay instproc auto_update args {
1234 my instvar config auto
1235
1236 if {$auto} {
1237 ${config}.recs_field configure -state disabled
1238 ${config}.sequence configure -state disabled
1239 ${config}.acquire configure -state disabled
1240 ${config}.register configure -state disabled
1241 ${config}.recover configure -state disabled
1242
1243 my acquire_start
1244 } else {
1245 ${config}.recs_field configure -state normal
1246 ${config}.sequence configure -state active
1247 ${config}.acquire configure -state active
1248 ${config}.register configure -state active
1249 ${config}.recover configure -state active
1250 }
1251 }
1252
1253# -------------------------------------------------------------------------
1254
1255 OscDisplay instproc save_data {fname} {
1256 my instvar data
1257
1258 set fid [open $fname w+]
1259 fconfigure $fid -translation binary -encoding binary
1260
1261# puts -nonewline $fid [binary format "H*iH*" "1f8b0800" [clock seconds] "0003"]
1262# puts -nonewline $fid [zlib deflate $data]
1263 puts -nonewline $fid $data
1264# puts -nonewline $fid [binary format i [zlib crc32 $data]]
1265# puts -nonewline $fid [binary format i [string length $data]]
1266
1267 close $fid
1268 }
1269
1270# -------------------------------------------------------------------------
1271
1272 OscDisplay instproc open_data {} {
1273 set types {
1274 {{Data Files} {.dat} }
1275 {{All Files} * }
1276 }
1277
1278 set fname [tk_getOpenFile -filetypes $types]
1279 if {[string equal $fname {}]} {
[85]1280 return
1281 }
1282
[143]1283 set x [catch {
1284 set fid [open $fname r+]
1285 fconfigure $fid -translation binary -encoding binary
1286# set size [file size $fname]
1287# seek $fid 10
1288# my set data [zlib inflate [read $fid [expr {$size - 18}]]]
1289 my set data [read $fid]
1290 close $fid
1291 }]
[85]1292
[143]1293 if { $x || ![file exists $fname] || ![file isfile $fname] || ![file readable $fname] } {
[85]1294 tk_messageBox -icon error \
[143]1295 -message "An error occurred while reading \"$fname\""
[85]1296 } else {
1297 tk_messageBox -icon info \
[143]1298 -message "File \"$fname\" read successfully"
[85]1299 }
1300 }
1301
1302# -------------------------------------------------------------------------
1303
[143]1304 OscDisplay instproc register {} {
1305 set types {
1306 {{Data Files} {.dat} }
1307 {{All Files} * }
1308 }
[85]1309
[180]1310 set stamp [clock format [clock seconds] -format {%Y%m%d_%H%M%S}]
[143]1311 set fname oscillogram_${stamp}.dat
1312
1313 set fname [tk_getSaveFile -filetypes $types -initialfile $fname]
1314 if {[string equal $fname {}]} {
1315 return
1316 }
[180]1317
[143]1318 if {[catch {my save_data $fname} result]} {
1319 tk_messageBox -icon error \
1320 -message "An error occurred while writing to \"$fname\""
1321 } else {
1322 tk_messageBox -icon info \
1323 -message "File \"$fname\" written successfully"
1324 }
1325 }
1326
[85]1327# -------------------------------------------------------------------------
1328
[143]1329 OscDisplay instproc recover {} {
1330 my open_data
[85]1331 }
1332
1333# -------------------------------------------------------------------------
1334
[143]1335 OscDisplay instproc sequence_start {} {
1336 my instvar config recs_val recs_bak directory counter sequence
1337
1338 set counter 1
1339 if {$counter > $recs_val} {
1340 return
1341 }
1342
1343 set directory [tk_chooseDirectory -initialdir $directory -title {Choose a directory}]
1344
1345 if {[string equal $directory {}]} {
1346 return
1347 }
1348
1349 ${config}.recs_field configure -state disabled
1350 ${config}.sequence configure -text {Stop Recording} -command [myproc sequence_stop]
1351 ${config}.acquire configure -state disabled
1352 ${config}.register configure -state disabled
1353 ${config}.recover configure -state disabled
[180]1354
[143]1355 set recs_bak $recs_val
1356
1357 set sequence 1
1358
1359 my acquire_start
1360 }
1361
1362# -------------------------------------------------------------------------
1363
1364 OscDisplay instproc sequence_register {} {
1365 my instvar config recs_val recs_bak directory counter
1366
1367 set fname [file join $directory oscillogram_$counter.dat]
1368
1369 my incr counter
1370
1371 if {[catch {my save_data $fname} result]} {
1372 tk_messageBox -icon error \
1373 -message "An error occurred while writing to \"$fname\""
1374 } elseif {$counter <= $recs_bak} {
1375 set recs_val [expr {$recs_bak - $counter}]
1376 my acquire_start
1377 return
1378 }
[180]1379
[143]1380 my sequence_stop
1381 }
1382
1383# -------------------------------------------------------------------------
1384
1385 OscDisplay instproc sequence_stop {} {
1386 my instvar config recs_val recs_bak sequence
1387
1388 set sequence 0
[180]1389
[143]1390 set recs_val $recs_bak
1391
1392 ${config}.recs_field configure -state normal
1393 ${config}.sequence configure -text {Start Recording} -command [myproc sequence_start]
1394 ${config}.acquire configure -state active
1395 ${config}.register configure -state active
1396 ${config}.recover configure -state active
1397 }
1398
1399# -------------------------------------------------------------------------
1400
1401 namespace export MuxDisplay
[73]1402 namespace export HstDisplay
[180]1403 namespace export CfgDisplay
[73]1404 namespace export OscDisplay
1405}
1406
[78]1407set notebook [::blt::tabnotebook .notebook -borderwidth 1 -selectforeground black -side bottom]
[73]1408
[143]1409grid ${notebook} -row 0 -column 0 -sticky news -pady 5
[73]1410
[78]1411grid rowconfigure . 0 -weight 1
[143]1412grid columnconfigure . 0 -weight 1
[78]1413
[143]1414::mca::UsbController usb
[73]1415
[143]1416set window [frame ${notebook}.mux]
1417$notebook insert end -text "Interconnect" -window $window -fill both
1418::mca::MuxDisplay mux -master $window -controller usb
[73]1419
[180]1420set window [frame ${notebook}.cfg]
1421$notebook insert end -text "Configuration" -window $window -fill both
1422::mca::CfgDisplay cfg -master $window -controller usb
1423
1424set window [frame ${notebook}.hst]
1425$notebook insert end -text "Histogram" -window $window -fill both
1426::mca::HstDisplay hst -number 0 -master $window -controller usb
1427
[85]1428set window [frame ${notebook}.ept]
[143]1429$notebook insert end -text "Oscilloscope" -window $window -fill both
1430::mca::OscDisplay osc -master $window -controller usb
[85]1431
[143]1432update
[85]1433
[180]1434cfg start
[73]1435
[143]1436mux start
[73]1437
[180]1438hst start
1439
[143]1440osc start
Note: See TracBrowser for help on using the repository browser.