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

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

working classifier

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