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

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

fix classifier

File size: 45.1 KB
Line 
1package require XOTcl
2
3package require BLT
4package require swt
5package require usb
6
7package require zlib
8
9wm minsize . 1000 700
10
11namespace eval ::mca {
12 namespace import ::xotcl::*
13
14 namespace import ::blt::vector
15 namespace import ::blt::graph
16 namespace import ::blt::tabnotebook
17
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 {
34 1 {S1_F}
35 2 {S1_S}
36 3 {S2}
37 4 {D1}
38 5 {D2}
39 6 {D3}
40 }
41
42# -------------------------------------------------------------------------
43
44 variable cfgCodes
45 array set cfgCodes {
46 {1_0} 0
47 {1_1} 6
48 {1_2} 7
49 {1_3} 8
50 {2_0} 1
51 {2_1} 9
52 {2_2} 10
53 {2_3} 11
54 {3_0} 2
55 {3_1} 12
56 {3_2} 13
57 {3_3} 14
58 {4_0} 3
59 {4_1} 15
60 {4_2} 16
61 {4_3} 17
62 {5_0} 4
63 {5_1} 18
64 {5_2} 19
65 {5_3} 20
66 {6_0} 5
67 {6_1} 21
68 {6_2} 22
69 {6_3} 23
70 }
71# -------------------------------------------------------------------------
72
73 variable cfgThrs
74 array set cfgThrs {
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
99 }
100
101
102# -------------------------------------------------------------------------
103
104 variable inpCodes
105 array set inpCodes {
106 0 {rs}
107 1 {cs}
108 2 {av}
109 3 {af}
110 4 {bn}
111 }
112
113# -------------------------------------------------------------------------
114
115 proc validate {max size value} {
116 if {![regexp {^[0-9]*$} $value]} {
117 return 0
118 } elseif {[regexp {^0[0-9]+$} $value]} {
119 return 0
120 } elseif {$value > $max} {
121 return 0
122 } elseif {[string length $value] > $size} {
123 return 0
124 } else {
125 return 1
126 }
127 }
128
129# -------------------------------------------------------------------------
130
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 }
142
143# -------------------------------------------------------------------------
144
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 {}
148
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
171 UsbController instproc init {} {
172
173 my set ignore false
174
175 next
176 }
177
178# -------------------------------------------------------------------------
179
180 UsbController instproc usbConnect {} {
181 my instvar handle ignore
182
183 puts usbConnect
184
185 if {[my exists handle]} {
186 $handle disconnect
187 unset handle
188 }
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 }
199
200 set handle $result
201
202 }
203 }
204
205# -------------------------------------------------------------------------
206
207 UsbController instproc usbHandle {} {
208 my instvar handle ignore
209
210 if {[my exists handle]} {
211 return $handle
212 } elseif {!$ignore} {
213 my usbConnect
214 }
215 }
216
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 }
226 }
227
228 }
229
230# -------------------------------------------------------------------------
231
232 UsbController instproc usbCmdReadRaw {command size data} {
233 my usbCmd $command
234
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} {
253 my usbCmd $command
254
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 }
267 }
268 }
269
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 }
288 }
289
290# -------------------------------------------------------------------------
291
292 Class CfgDisplay -parameter {
293 {master}
294 {controller}
295 }
296
297# -------------------------------------------------------------------------
298
299 CfgDisplay instproc init {} {
300
301 my setup
302
303 next
304 }
305
306# -------------------------------------------------------------------------
307
308 CfgDisplay instproc destroy {} {
309 next
310 }
311
312# -------------------------------------------------------------------------
313
314 CfgDisplay instproc start {} {
315 variable adcCodes
316 variable cfgThrs
317
318 foreach {ch id} [array get adcCodes] {
319 my set delay($ch) 32
320 my set decay($ch) 1000
321 }
322
323 foreach {i value} [array get cfgThrs] {
324 my set thrs($i) $value
325 }
326
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
333 }
334
335# -------------------------------------------------------------------------
336
337 CfgDisplay instproc setup {} {
338 variable adcCodes
339 variable cfgCodes
340 my instvar number master
341 my instvar config
342
343 set thrs [frame ${master}.thrs]
344 set clip [frame ${master}.clip]
345
346 set config(thrs) [labelframe ${thrs}.frame -borderwidth 1 -relief sunken -text {Thresholds}]
347
348 set column 0
349 foreach {input} [list "ADC" "thrs 1" "thrs 2" "thrs 3" "thrs 4"] {
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 }
354
355 foreach {ch id} [array get adcCodes] {
356 label ${config(thrs)}.chan_${ch} -text "${id} "
357 grid ${config(thrs)}.chan_${ch} -row ${ch} -column 0 -sticky ew -padx 5 -pady 7
358 foreach {num} [list 0 1 2 3] {
359 set column [expr {$num + 1}]
360 set value $cfgCodes(${ch}_${num})
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 }
367
368 grid $config(thrs) -row 0 -column 0 -sticky news -padx 10
369
370 set config(clip) [labelframe ${clip}.frame -borderwidth 1 -relief sunken -text {Signal clipping}]
371
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 }
378
379 foreach {ch id} [array get adcCodes] {
380 label ${config(clip)}.chan_${ch} -text "${id} "
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 }
391
392 grid $config(clip) -row 0 -column 0 -sticky news -padx 10
393
394 grid ${thrs} -row 0 -column 1 -sticky news
395 grid ${clip} -row 0 -column 0 -sticky news
396
397 grid columnconfigure ${master} 0 -weight 1
398 grid columnconfigure ${master} 1 -weight 1
399 grid rowconfigure ${master} 0 -weight 1
400
401 grid rowconfigure ${thrs} 0 -weight 0
402 grid rowconfigure ${clip} 0 -weight 0
403 }
404
405# -------------------------------------------------------------------------
406
407 CfgDisplay instproc decay_update args {
408 my instvar controller decay delay
409
410 set command {}
411 for {set i 1} {$i <= 6} {incr i} {
412 set a $delay($i).0
413 set b $decay($i).0
414 set value [expr int(exp(-${a}/${b})*1024*20)]
415 append command [format {000200%02x0004%04x} [expr {34 + 2 * (${i} - 1)}] $value]
416 }
417
418 $controller usbCmd $command
419 }
420
421# -------------------------------------------------------------------------
422
423 CfgDisplay instproc delay_update args {
424 my instvar controller delay
425
426 set command {}
427 for {set i 1} {$i <= 6} {incr i} {
428 append command [format {000200%02x0004%04x} [expr {35 + 2 * (${i} - 1)}] $delay($i)]
429 }
430
431 $controller usbCmd $command
432
433 my decay_update
434 }
435
436# -------------------------------------------------------------------------
437
438 CfgDisplay instproc thrs_update args {
439 my instvar controller thrs
440
441 set command {}
442 for {set i 0} {$i <= 23} {incr i} {
443 append command [format {000200%02x0004%04x} [expr {10 + ${i}}] $thrs($i)]
444 }
445
446 $controller usbCmd $command
447 }
448
449# -------------------------------------------------------------------------
450
451 Class MuxDisplay -parameter {
452 {master}
453 {controller}
454 }
455
456# -------------------------------------------------------------------------
457
458 MuxDisplay instproc init {} {
459
460 my setup
461
462 next
463 }
464
465# -------------------------------------------------------------------------
466
467 MuxDisplay instproc destroy {} {
468 next
469 }
470
471# -------------------------------------------------------------------------
472
473 MuxDisplay instproc start {} {
474 variable adcCodes
475 my instvar config chan_val
476
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
483
484 trace add variable [myvar chan_val] write [myproc chan_val_update]
485 trace add variable [myvar polar] write [myproc polar_update]
486
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
493
494 foreach {ch dummy} [array get adcCodes] {
495 $config(inv).polar_${ch} deselect
496 }
497 }
498
499# -------------------------------------------------------------------------
500
501 MuxDisplay instproc setup {} {
502 variable oscCodes
503 variable adcCodes
504 variable inpCodes
505 my instvar master
506 my instvar config
507
508 set size [array size inpCodes]
509 set oscList [array get oscCodes]
510 set adcList [array get adcCodes]
511 set inpList [array get inpCodes]
512
513 set mux [frame ${master}.mux]
514 set key [frame ${master}.key]
515 set inv [frame ${master}.inv]
516
517 foreach {osc title} $oscList {
518 set config($osc) [labelframe ${mux}.$osc -borderwidth 1 -relief sunken -text $title]
519 foreach {code input} $inpList {
520 set column [expr {$code + 1}]
521 label $config($osc).input_${input} -text " ${input}"
522 grid $config($osc).input_${input} -row 0 -column ${column} -sticky w
523 }
524 foreach {ch id} $adcList {
525 label $config($osc).chan_${ch} -text "${id} "
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 }
533 }
534 set column [expr {($osc - 1) % 3}]
535 set row [expr {($osc - 1) / 3}]
536 grid $config($osc) -row ${row} -column ${column} -sticky news -padx 10
537 }
538
539 set config(key) [labelframe ${key}.frame -borderwidth 1 -relief sunken -text {legend}]
540
541 label $config(key).rs -text "rs - raw signal"
542 grid $config(key).rs -row 0 -column 0 -sticky news
543
544 label $config(key).cs -text "cs - filtered and clipped signal"
545 grid $config(key).cs -row 0 -column 1 -sticky news
546
547 label $config(key).av -text "av - amplitude value"
548 grid $config(key).av -row 0 -column 2 -sticky news
549
550 label $config(key).af -text "af - amplitude flag"
551 grid $config(key).af -row 0 -column 3 -sticky news
552
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
559 grid $config(key) -row 0 -column 0 -sticky news -padx 10
560
561
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
572 }
573 grid $config(inv) -row 0 -column 0 -sticky news -padx 10
574
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
578
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
583
584 grid columnconfigure ${inv} 0 -weight 1
585
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
591 grid columnconfigure $config(key) 4 -weight 1
592 grid columnconfigure $config(key) 5 -weight 1
593
594
595 grid columnconfigure ${mux} 0 -weight 1
596 grid columnconfigure ${mux} 1 -weight 1
597 grid columnconfigure ${mux} 2 -weight 1
598 }
599
600# ------------------------------------------------------------------------
601
602 MuxDisplay instproc chan_val_update args {
603 my instvar controller chan_val
604
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)]
608
609 $controller usbCmd 000200020004${byte1}000200030004${byte2}000200040004${byte3}
610 }
611
612# -------------------------------------------------------------------------
613
614 MuxDisplay instproc polar_update args {
615 my instvar controller polar
616
617 set value {0b}
618 for {set i 6} {$i >= 1} {incr i -1} {
619 append value $polar($i)
620 }
621
622 set value [format {%04x} $value]
623
624 $controller usbCmd 000200010004${value}
625 }
626
627# -------------------------------------------------------------------------
628
629 Class HstDisplay -parameter {
630 {number}
631 {master}
632 {controller}
633 }
634
635# -------------------------------------------------------------------------
636
637 HstDisplay instproc init {} {
638
639 my set data {}
640
641 vector create [myvar xvec](64)
642 vector create [myvar yvec](64)
643
644 # fill one vector for the x axis with 64 points
645 [myvar xvec] seq -0.5 63.5
646
647 my setup
648
649 next
650 }
651
652# -------------------------------------------------------------------------
653
654 HstDisplay instproc destroy {} {
655 next
656 }
657
658# -------------------------------------------------------------------------
659
660 HstDisplay instproc start {} {
661 my instvar config
662
663 trace add variable [myvar data] write [myproc data_update]
664
665 trace add variable [myvar axis] write [myproc axis_update]
666
667 ${config}.axis_check select
668
669 my set yvec_bak 0.0
670 my set yvec_old 0.0
671 }
672
673# -------------------------------------------------------------------------
674
675 HstDisplay instproc setup {} {
676 my instvar number master
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
680
681 # create a graph widget and show a grid
682 set graph [graph ${master}.graph -height 250 -leftmargin 80]
683 $graph crosshairs configure -hide no -linewidth 1 -color darkblue -dashes {2 2}
684 $graph grid configure -hide no
685 $graph legend configure -hide yes
686 $graph axis configure x -min 0 -max 64
687
688 set config [frame ${master}.config -width 170]
689
690 checkbutton ${config}.axis_check -text {log scale} -variable [myvar axis]
691
692 frame ${config}.spc1 -width 170 -height 30
693
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}
697
698 frame ${config}.spc2 -width 170 -height 30
699
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
705 frame ${config}.spc3 -width 170 -height 30
706
707 button ${config}.register -text Register \
708 -bg lightblue -activebackground lightblue -command [myproc register]
709
710 grid ${config}.axis_check -sticky w
711 grid ${config}.spc1
712 grid ${config}.chan_frame -sticky ew -padx 5
713 grid ${config}.spc2
714 grid ${config}.start -sticky ew -pady 3 -padx 5
715 grid ${config}.reset -sticky ew -pady 3 -padx 5
716 grid ${config}.spc3
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
722 grid rowconfigure ${master} 0 -weight 1
723 grid columnconfigure ${master} 0 -weight 1
724 grid columnconfigure ${master} 1 -weight 0 -minsize 80
725
726 grid columnconfigure ${config}.chan_frame 1 -weight 1
727
728 # enable zooming
729 Blt_ZoomStack $graph
730
731 my crosshairs $graph
732
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
736 $graph element create Spectrum1 -color blue -linewidth 2 -symbol none -smooth step -xdata [myvar xvec] -ydata [myvar yvec]
737 }
738
739# -------------------------------------------------------------------------
740
741 HstDisplay instproc coor_update {W x y} {
742 my instvar config graph
743
744 $W crosshairs configure -position @${x},${y}
745
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 }
752 }
753# -------------------------------------------------------------------------
754
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
760 }
761 bind $graph <Enter> {
762 %W crosshairs on
763 }
764 }
765
766# -------------------------------------------------------------------------
767
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
772 } else {
773 $graph axis configure y -min {} -max {} -logscale no
774 }
775 }
776
777# -------------------------------------------------------------------------
778
779 HstDisplay instproc cntr_reset {} {
780 my instvar controller number
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
795 set val_addr [format %02x [expr {6 + ${number}}]]
796
797 ${config}.start configure -text Stop -command [myproc cntr_stop]
798# ${config}.reset configure -state disabled
799
800 $controller usbCmd 000200${val_addr}00040001
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
812 set val_addr [format %02x [expr {6 + ${number}}]]
813
814 ${config}.start configure -text Start -command [myproc cntr_start]
815
816 $controller usbCmd 000200${val_addr}00040000
817
818 set auto 0
819
820 my acquire
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
832 HstDisplay instproc acquire_loop {} {
833 my instvar cntr_val auto
834
835 my acquire
836
837 if {$auto} {
838 after 1000 [myproc acquire_loop]
839 }
840 }
841
842# -------------------------------------------------------------------------
843
844 HstDisplay instproc acquire {} {
845 my instvar controller config number
846
847 set size 64
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
858# -------------------------------------------------------------------------
859
860 HstDisplay instproc save_data {data} {
861 my instvar number
862
863 set types {
864 {{Data Files} {.dat} }
865 {{All Files} * }
866 }
867
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
874 }
875
876 set x [catch {
877 set fid [open $fname w+]
878 puts $fid $data
879 close $fid
880 }]
881
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 }
890
891# -------------------------------------------------------------------------
892
893 HstDisplay instproc register {} {
894 my save_data [join [[myvar yvec] range 0 63] \n]
895 }
896
897# -------------------------------------------------------------------------
898
899 Class OscDisplay -parameter {
900 {master}
901 {controller}
902 }
903
904# -------------------------------------------------------------------------
905
906 OscDisplay instproc init {} {
907 my instvar sequence data xvec yvec
908
909 set data {}
910
911 set sequence 0
912
913# set xvec [vector create #auto(262144)]
914# set xvec [vector create #auto(10000)]
915 set xvec [vector create #auto(60000)]
916
917 for {set i 1} {$i <= 9} {incr i} {
918# dict set yvec $i [vector create #auto(262144)]
919# dict set yvec $i [vector create #auto(10000)]
920 dict set yvec $i [vector create #auto(60000)]
921 }
922
923 # fill one vector for the x axis
924# $xvec seq 0 262143
925# $xvec seq 0 10000
926 $xvec seq 0 60000
927
928 my setup
929
930 next
931 }
932
933# -------------------------------------------------------------------------
934
935 OscDisplay instproc destroy {} {
936 next
937 }
938
939# -------------------------------------------------------------------------
940
941 OscDisplay instproc start {} {
942 my instvar config
943 my instvar recs_val directory
944
945 set directory $::env(HOME)
946 set recs_val 100
947
948 trace add variable [myvar chan] write [myproc chan_update]
949
950 trace add variable [myvar data] write [myproc data_update]
951
952 trace add variable [myvar auto] write [myproc auto_update]
953
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
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
966
967 ${config}.thrs_check select
968 ${config}.thrs_field set 60
969 }
970
971# -------------------------------------------------------------------------
972
973 OscDisplay instproc setup {} {
974 my instvar master
975 my instvar xvec yvec graph
976 my instvar config
977
978 # create a graph widget and show a grid
979 set graph [graph ${master}.graph -height 250 -leftmargin 80]
980 $graph crosshairs configure -hide no -linewidth 1 -color darkblue -dashes {2 2}
981 $graph grid configure -hide no
982 $graph legend configure -hide yes
983 $graph axis configure x -min 0 -max 60000
984 $graph axis configure y -min 0 -max 4100
985
986# scale ${master}.last -orient horizontal -from 1 -to 27 -tickinterval 0 -showvalue no -variable [myvar last]
987
988 set config [frame ${master}.config -width 170]
989
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
1012 button ${config}.acquire -text Acquire \
1013 -bg green -activebackground green -command [myproc acquire_start]
1014 button ${config}.register -text Register \
1015 -bg lightblue -activebackground lightblue -command [myproc register]
1016
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
1029 frame ${config}.spc6 -width 170 -height 30
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
1041 grid ${config}.acquire -sticky ew -pady 3 -padx 5
1042 grid ${config}.register -sticky ew -pady 3 -padx 5
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
1050
1051 grid ${graph} -row 0 -column 0 -sticky news
1052 grid ${config} -row 0 -column 1
1053
1054# grid ${master}.last -row 1 -column 0 -columnspan 2 -sticky ew
1055
1056 grid rowconfigure ${master} 0 -weight 1
1057 grid columnconfigure ${master} 0 -weight 1
1058 grid columnconfigure ${master} 1 -weight 0 -minsize 120
1059
1060 grid columnconfigure ${config}.chan_frame 2 -weight 1
1061
1062 # enable zooming
1063 Blt_ZoomStack $graph
1064
1065 my crosshairs $graph
1066
1067 # create one element with data for the x and y axis, no dots
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]
1081 }
1082
1083# -------------------------------------------------------------------------
1084
1085 OscDisplay instproc coor_update {W x y} {
1086 my instvar xvec yvec graph
1087 my instvar config
1088
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
1142 set first [expr {$last - 1}]
1143
1144 $graph axis configure x -min ${first}0000 -max ${last}0000
1145 }
1146
1147# -------------------------------------------------------------------------
1148
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
1158 set value [format {%03x} $thrs_val]
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
1217# set size 10000
1218 set size 60000
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
1223
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 {}]} {
1280 return
1281 }
1282
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 }]
1292
1293 if { $x || ![file exists $fname] || ![file isfile $fname] || ![file readable $fname] } {
1294 tk_messageBox -icon error \
1295 -message "An error occurred while reading \"$fname\""
1296 } else {
1297 tk_messageBox -icon info \
1298 -message "File \"$fname\" read successfully"
1299 }
1300 }
1301
1302# -------------------------------------------------------------------------
1303
1304 OscDisplay instproc register {} {
1305 set types {
1306 {{Data Files} {.dat} }
1307 {{All Files} * }
1308 }
1309
1310 set stamp [clock format [clock seconds] -format {%Y%m%d_%H%M%S}]
1311 set fname oscillogram_${stamp}.dat
1312
1313 set fname [tk_getSaveFile -filetypes $types -initialfile $fname]
1314 if {[string equal $fname {}]} {
1315 return
1316 }
1317
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
1327# -------------------------------------------------------------------------
1328
1329 OscDisplay instproc recover {} {
1330 my open_data
1331 }
1332
1333# -------------------------------------------------------------------------
1334
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
1354
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 }
1379
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
1389
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
1402 namespace export HstDisplay
1403 namespace export CfgDisplay
1404 namespace export OscDisplay
1405}
1406
1407set notebook [::blt::tabnotebook .notebook -borderwidth 1 -selectforeground black -side bottom]
1408
1409grid ${notebook} -row 0 -column 0 -sticky news -pady 5
1410
1411grid rowconfigure . 0 -weight 1
1412grid columnconfigure . 0 -weight 1
1413
1414::mca::UsbController usb
1415
1416set window [frame ${notebook}.mux]
1417$notebook insert end -text "Interconnect" -window $window -fill both
1418::mca::MuxDisplay mux -master $window -controller usb
1419
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
1428set window [frame ${notebook}.ept]
1429$notebook insert end -text "Oscilloscope" -window $window -fill both
1430::mca::OscDisplay osc -master $window -controller usb
1431
1432update
1433
1434cfg start
1435
1436mux start
1437
1438hst start
1439
1440osc start
Note: See TracBrowser for help on using the repository browser.