source: trunk/MultiChannelUSB/UserInterface.tcl@ 74

Last change on this file since 74 was 74, checked in by demin, 15 years ago

fix signal shape display configuration

File size: 25.0 KB
Line 
1package require XOTcl
2
3package require BLT
4package require swt
5package require usb
6
7wm minsize . 900 720
8
9namespace eval ::mca {
10 namespace import ::xotcl::*
11
12 namespace import ::blt::vector
13 namespace import ::blt::graph
14 namespace import ::blt::tabnotebook
15
16 proc validate {value} {
17 if {![regexp {^[1-9][0-9]*$} $value]} {
18 return 0
19 } elseif {$value > 4095} {
20 return 0
21 } elseif {[string length $value] > 4} {
22 return 0
23 } else {
24 return 1
25 }
26 }
27
28# -------------------------------------------------------------------------
29
30 Class OscDisplay -parameter {
31 {number}
32 {master}
33 }
34
35# -------------------------------------------------------------------------
36
37 OscDisplay instproc init {} {
38 my instvar data xvec yvec
39
40 set xvec [vector #auto]
41 set yvec [vector #auto]
42 # fill one vector for the x axis with 1025 points
43 $xvec seq 0 1024
44
45 my reset
46
47 my setup
48
49 next
50 }
51
52# -------------------------------------------------------------------------
53
54 OscDisplay instproc destroy {} {
55 next
56 }
57
58# -------------------------------------------------------------------------
59
60 OscDisplay instproc reset {} {
61 my instvar data xvec yvec
62
63 set data {}
64
65 $yvec set {}
66 }
67
68# -------------------------------------------------------------------------
69
70 OscDisplay instproc start {} {
71 my instvar config trig_mux disp_mux
72
73 set trig_mux 2
74 set disp_mux 2
75
76 trace add variable [myvar auto] write [myproc auto_update]
77 trace add variable [myvar thrs] write [myproc thrs_update]
78 trace add variable [myvar thrs_val] write [myproc thrs_val_update]
79 trace add variable [myvar disp_val] write [myproc disp_val_update]
80 trace add variable [myvar trig_val] write [myproc trig_val_update]
81
82 ${config}.auto_check select
83 ${config}.thrs_check select
84 ${config}.thrs_field set 60
85 ${config}.disp_uwt2 select
86 ${config}.trig_uwt2 select
87 }
88
89# -------------------------------------------------------------------------
90
91 OscDisplay instproc setup {} {
92 my instvar number master
93 my instvar data xvec yvec
94 my instvar config auto thrs thrs_val disp_val trig_val
95
96 my set restart_command [usb::convert 0001000${number}00000000]
97 my set acquire_command [usb::convert 0002000${number}00000000]
98
99 # create a graph widget and show a grid
100 set graph [graph ${master}.graph -height 250 -leftmargin 80]
101 $graph crosshairs configure -hide no -linewidth 2 -dashes { 1 1 }
102 $graph grid configure -hide no
103 $graph legend configure -hide yes
104 $graph axis configure x -min 0 -max 1024
105
106 set config [frame ${master}.config]
107
108 checkbutton ${config}.auto_check -text {auto update} -variable [myvar auto]
109
110 frame ${config}.spc1 -width 10 -height 10
111
112 checkbutton ${config}.thrs_check -text threshold -variable [myvar thrs]
113 spinbox ${config}.thrs_field -from 1 -to 4095 \
114 -increment 5 -width 10 -textvariable [myvar thrs_val] \
115 -validate all -vcmd {::mca::validate %P}
116
117 frame ${config}.spc2 -width 10 -height 10
118
119 label ${config}.disp -text {display input}
120 radiobutton ${config}.disp_data -text {raw data} -variable [myvar disp_val] -value data
121 radiobutton ${config}.disp_uwt1 -text {filter 1} -variable [myvar disp_val] -value uwt1
122 radiobutton ${config}.disp_uwt2 -text {filter 2} -variable [myvar disp_val] -value uwt2
123 radiobutton ${config}.disp_uwt3 -text {filter 3} -variable [myvar disp_val] -value uwt3
124 radiobutton ${config}.disp_base -text {baseline} -variable [myvar disp_val] -value base
125# radiobutton ${config}.disp_sum8 -text {sum of 8} -variable [myvar disp_val] -value sum8
126
127 frame ${config}.spc3 -width 10 -height 10
128
129 label ${config}.trig -text {trigger input}
130 radiobutton ${config}.trig_data -text {raw data} -variable [myvar trig_val] -value data
131 radiobutton ${config}.trig_uwt1 -text {filter 1} -variable [myvar trig_val] -value uwt1
132 radiobutton ${config}.trig_uwt2 -text {filter 2} -variable [myvar trig_val] -value uwt2
133 radiobutton ${config}.trig_uwt3 -text {filter 3} -variable [myvar trig_val] -value uwt3
134 radiobutton ${config}.trig_base -text {baseline} -variable [myvar trig_val] -value base
135# radiobutton ${config}.trig_sum8 -text {sum of 8} -variable [myvar trig_val] -value sum8
136
137 frame ${config}.spc4 -width 10 -height 10
138
139 button ${config}.acquire -text Acquire \
140 -bg green -activebackground green -command [myproc acquire]
141 button ${config}.restart -text Restart \
142 -bg yellow -activebackground yellow -command [myproc restart]
143 button ${config}.register -text Register \
144 -bg lightblue -activebackground lightblue -command [myproc register]
145
146 grid ${config}.auto_check -sticky w
147 grid ${config}.spc1
148 grid ${config}.thrs_check -sticky w
149 grid ${config}.thrs_field -sticky ew -pady 1 -padx 5
150 grid ${config}.spc2
151 grid ${config}.disp -sticky w -pady 1 -padx 3
152 grid ${config}.disp_data -sticky w
153 grid ${config}.disp_uwt1 -sticky w
154 grid ${config}.disp_uwt2 -sticky w
155 grid ${config}.disp_uwt3 -sticky w
156 grid ${config}.disp_base -sticky w
157# grid ${config}.disp_sum8 -sticky w
158 grid ${config}.spc3
159 grid ${config}.trig -sticky w -pady 1 -padx 3
160 grid ${config}.trig_data -sticky w
161 grid ${config}.trig_uwt1 -sticky w
162 grid ${config}.trig_uwt2 -sticky w
163 grid ${config}.trig_uwt3 -sticky w
164 grid ${config}.trig_base -sticky w
165# grid ${config}.disp_sum8 -sticky w
166 grid ${config}.spc4
167 grid ${config}.acquire -sticky ew -pady 3 -padx 5
168 grid ${config}.restart -sticky ew -pady 3 -padx 5
169 grid ${config}.register -sticky ew -pady 3 -padx 5
170
171 grid ${graph} -row 0 -column 0 -sticky news
172 grid ${config} -row 0 -column 1
173
174 # enable zooming
175 Blt_ZoomStack $graph
176
177 #bind .graph <Motion> {%W crosshairs configure -position @%x,%y}
178
179 # create one element with data for the x and y axis, no dots
180 $graph element create Spectrum1 -symbol none -xdata $xvec -ydata $yvec
181 }
182
183# -------------------------------------------------------------------------
184
185 OscDisplay instproc auto_update args {
186 my instvar config auto after_handle
187
188 if {$auto} {
189 ${config}.acquire configure -state disabled
190 ${config}.restart configure -state disabled
191 ${config}.register configure -state disabled
192
193 my acquire_restart_loop
194 } else {
195 if {[my exists after_handle]} {
196 after cancel $after_handle
197 }
198 ${config}.acquire configure -state active
199 ${config}.restart configure -state active
200 ${config}.register configure -state active
201 }
202 }
203
204# -------------------------------------------------------------------------
205
206 OscDisplay instproc thrs_update args {
207 my instvar config number thrs thrs_val
208
209 set val_addr [format %04x [expr {17 + ${number}}]]
210
211 if {$thrs} {
212 ${config}.thrs_field configure -state normal
213 my thrs_val_update
214 } else {
215 ${config}.thrs_field configure -state disabled
216 my send_data [usb::convert ${val_addr}000000000000]
217 }
218 }
219
220# -------------------------------------------------------------------------
221
222 OscDisplay instproc thrs_val_update args {
223 my instvar config number thrs_val
224
225 set val_addr [format %04x [expr {17 + ${number}}]]
226 set value [format %04x $thrs_val]
227
228 my send_data [usb::convert ${val_addr}${value}00000000]
229 }
230
231# -------------------------------------------------------------------------
232
233 OscDisplay instproc mux {} {
234 my instvar trig_mux disp_mux
235
236 format {00%x%x} $trig_mux $disp_mux
237 }
238
239# ------------------------------------------------------------------------
240
241 OscDisplay instproc disp_val_update args {
242 my instvar number disp_val disp_mux
243
244 set mux_addr [format %04x [expr {20 + ${number}}]]
245
246 switch -- $disp_val {
247 data {
248 set disp_mux 0
249 my send_data [usb::convert ${mux_addr}[my mux]00000000]
250 }
251 uwt1 {
252 set disp_mux 1
253 my send_data [usb::convert ${mux_addr}[my mux]00000000]
254 }
255 uwt2 {
256 set disp_mux 2
257 my send_data [usb::convert ${mux_addr}[my mux]00000000]
258 }
259 uwt3 {
260 set disp_mux 3
261 my send_data [usb::convert ${mux_addr}[my mux]00000000]
262 }
263 base {
264 set disp_mux 4
265 my send_data [usb::convert ${mux_addr}[my mux]00000000]
266 }
267 }
268 }
269
270# ------------------------------------------------------------------------
271
272 OscDisplay instproc trig_val_update args {
273 my instvar number trig_val trig_mux
274
275 set mux_addr [format %04x [expr {20 + ${number}}]]
276
277 switch -- $trig_val {
278 data {
279 set trig_mux 0
280 my send_data [usb::convert ${mux_addr}[my mux]00000000]
281 }
282 uwt1 {
283 set trig_mux 1
284 my send_data [usb::convert ${mux_addr}[my mux]00000000]
285 }
286 uwt2 {
287 set trig_mux 2
288 my send_data [usb::convert ${mux_addr}[my mux]00000000]
289 }
290 uwt3 {
291 set trig_mux 3
292 my send_data [usb::convert ${mux_addr}[my mux]00000000]
293 }
294 base {
295 set trig_mux 4
296 my send_data [usb::convert ${mux_addr}[my mux]00000000]
297 }
298 }
299 }
300
301# -------------------------------------------------------------------------
302
303 OscDisplay instproc save_data {data} {
304 set file [tk_getSaveFile]
305 if {[string equal $file {}]} {
306 return
307 }
308
309 set x [catch {set fid [open $file w+]}]
310 set y [catch {puts $fid $data}]
311 set z [catch {close $fid}]
312
313 if { $x || $y || $z || ![file exists $file] || ![file isfile $file] || ![file readable $file] } {
314 tk_messageBox -icon error \
315 -message "An error occurred while writing to \"$file\""
316 } else {
317 tk_messageBox -icon info \
318 -message "File \"$file\" written successfully"
319 }
320 }
321
322# -------------------------------------------------------------------------
323
324 OscDisplay instproc register {} {
325 my save_data [my set data]
326 }
327
328# -------------------------------------------------------------------------
329
330 OscDisplay instproc send_data {data} {
331 global usb_handle
332
333 if {[catch {$usb_handle writeRaw $data} result]} {
334 puts {Error during write}
335 puts $result
336 }
337 }
338
339# -------------------------------------------------------------------------
340
341 OscDisplay instproc restart {} {
342 my instvar restart_command
343 my send_data $restart_command
344 }
345
346# -------------------------------------------------------------------------
347
348 OscDisplay instproc acquire {} {
349 global usb_handle
350 my instvar xvec yvec data
351 my instvar acquire_command
352
353 my send_data $acquire_command
354
355 set usb_data {}
356 if {[catch {$usb_handle readHex 2 1024} usb_data]} {
357 puts {Error during read}
358 puts $usb_data
359 set usb_data {}
360 }
361
362 set data $usb_data
363
364 $yvec set $usb_data
365 }
366
367# -------------------------------------------------------------------------
368
369 OscDisplay instproc acquire_restart_loop {} {
370 my instvar after_handle
371
372 my acquire
373 my restart
374
375 set after_handle [after 1000 [myproc acquire_restart_loop]]
376 }
377
378# -------------------------------------------------------------------------
379
380 Class HstDisplay -parameter {
381 {number}
382 {master}
383 }
384
385# -------------------------------------------------------------------------
386
387 HstDisplay instproc init {} {
388 my instvar data xvec yvec
389
390 set xvec [vector #auto]
391 set yvec [vector #auto]
392 # fill one vector for the x axis with 4097 points
393 $xvec seq 0 4096
394
395 my reset
396
397 my setup
398
399 next
400 }
401
402# -------------------------------------------------------------------------
403
404 HstDisplay instproc destroy {} {
405 next
406 }
407
408# -------------------------------------------------------------------------
409
410 HstDisplay instproc reset {} {
411 my instvar data xvec yvec
412
413 set data {}
414
415 $yvec set {}
416 }
417
418# -------------------------------------------------------------------------
419
420 HstDisplay instproc start {} {
421 my instvar config base_mux peak_mux
422
423 set base_mux 0
424 set peak_mux 1
425
426 trace add variable [myvar auto] write [myproc auto_update]
427 trace add variable [myvar peak] write [myproc peak_update]
428 trace add variable [myvar thrs] write [myproc thrs_update]
429 trace add variable [myvar thrs_val] write [myproc thrs_val_update]
430 trace add variable [myvar base] write [myproc base_update]
431 trace add variable [myvar base_typ] write [myproc base_typ_update]
432 trace add variable [myvar base_val] write [myproc base_val_update]
433
434 ${config}.auto_check select
435 ${config}.peak_check select
436 ${config}.thrs_check select
437 ${config}.thrs_field set 10
438 ${config}.base_check select
439 ${config}.base_const select
440 ${config}.base_field set 35
441 }
442
443# -------------------------------------------------------------------------
444
445 HstDisplay instproc setup {} {
446 my instvar number master
447 my instvar data xvec yvec
448 my instvar config auto thrs thrs_val base base_typ base_val
449
450 my set restart_command [usb::convert 0001001${number}00000000]
451 my set acquire_command [usb::convert 0002001${number}00000000]
452
453 # create a graph widget and show a grid
454 set graph [graph ${master}.graph -height 250 -leftmargin 80]
455 $graph crosshairs configure -hide no -linewidth 2 -dashes { 1 1 }
456 $graph grid configure -hide no
457 $graph legend configure -hide yes
458 $graph axis configure x -min 0 -max 4096
459
460 set config [frame ${master}.config]
461
462 checkbutton ${config}.auto_check -text {auto update} -variable [myvar auto]
463
464 frame ${config}.spc1 -width 10 -height 10
465
466 checkbutton ${config}.peak_check -text {peak detect} -variable [myvar peak]
467
468 frame ${config}.spc2 -width 10 -height 10
469
470 checkbutton ${config}.thrs_check -text threshold -variable [myvar thrs]
471 spinbox ${config}.thrs_field -from 1 -to 4095 \
472 -increment 5 -width 10 -textvariable [myvar thrs_val] \
473 -validate all -vcmd {::mca::validate %P}
474
475 frame ${config}.spc3 -width 10 -height 10
476
477 checkbutton ${config}.base_check -text baseline -variable [myvar base]
478 radiobutton ${config}.base_auto -text automatic -variable [myvar base_typ] -value auto
479 radiobutton ${config}.base_const -text constant -variable [myvar base_typ] -value const
480 spinbox ${config}.base_field -from 1 -to 4095 \
481 -increment 5 -width 10 -textvariable [myvar base_val] \
482 -validate all -vcmd {::mca::validate %P}
483
484 frame ${config}.spc4 -width 10 -height 10
485
486 button ${config}.acquire -text Acquire \
487 -bg green -activebackground green -command [myproc acquire]
488 button ${config}.restart -text Restart \
489 -bg yellow -activebackground yellow -command [myproc restart]
490 button ${config}.register -text Register \
491 -bg lightblue -activebackground lightblue -command [myproc register]
492
493 grid ${config}.auto_check -sticky w
494 grid ${config}.spc1
495 grid ${config}.peak_check -sticky w
496 grid ${config}.spc2
497 grid ${config}.thrs_check -sticky w
498 grid ${config}.thrs_field -sticky ew -pady 1 -padx 5
499 grid ${config}.spc3
500 grid ${config}.base_check -sticky w
501 grid ${config}.base_auto -sticky w
502 grid ${config}.base_const -sticky w
503 grid ${config}.base_field -sticky ew -pady 1 -padx 5
504 grid ${config}.spc4
505 grid ${config}.acquire -sticky ew -pady 3 -padx 5
506 grid ${config}.restart -sticky ew -pady 3 -padx 5
507 grid ${config}.register -sticky ew -pady 3 -padx 5
508
509 grid ${graph} -row 0 -column 0 -sticky news
510 grid ${config} -row 0 -column 1
511
512 # enable zooming
513 Blt_ZoomStack $graph
514
515 #bind .graph <Motion> {%W crosshairs configure -position @%x,%y}
516
517 # create one element with data for the x and y axis, no dots
518 $graph element create Spectrum1 -symbol none -smooth step -xdata $xvec -ydata $yvec
519 }
520
521# -------------------------------------------------------------------------
522
523 HstDisplay instproc auto_update args {
524 my instvar auto after_handle
525 my instvar config
526 if {$auto} {
527 ${config}.acquire configure -state disabled
528 ${config}.register configure -state disabled
529
530 my acquire_loop
531 } else {
532 if {[my exists after_handle]} {
533 after cancel $after_handle
534 }
535 ${config}.acquire configure -state active
536 ${config}.register configure -state active
537 }
538 }
539
540# -------------------------------------------------------------------------
541
542 HstDisplay instproc mux {} {
543 my instvar base_mux peak_mux
544
545 format {00%x%x} $base_mux $peak_mux
546 }
547
548# -------------------------------------------------------------------------
549
550 HstDisplay instproc peak_update args {
551 my instvar number peak peak_mux
552
553 set mux_addr [format %04x [expr {23 + ${number}}]]
554
555 if {$peak} {
556 set peak_mux 1
557 my send_data [usb::convert ${mux_addr}[my mux]00000000]
558 } else {
559 set peak_mux 0
560 my send_data [usb::convert ${mux_addr}[my mux]00000000]
561 }
562 }
563
564# -------------------------------------------------------------------------
565
566 HstDisplay instproc thrs_update args {
567 my instvar config number thrs thrs_val
568
569 set val_addr [format %04x [expr {14 + ${number}}]]
570 set value [format %04x $thrs_val]
571
572 if {$thrs} {
573 ${config}.thrs_field configure -state normal
574 my thrs_val_update
575 } else {
576 ${config}.thrs_field configure -state disabled
577 my send_data [usb::convert ${val_addr}000000000000]
578 }
579 }
580
581# -------------------------------------------------------------------------
582
583 HstDisplay instproc thrs_val_update args {
584 my instvar config number thrs_val
585
586 set val_addr [format %04x [expr {14 + ${number}}]]
587 set value [format %04x $thrs_val]
588
589 my send_data [usb::convert ${val_addr}${value}00000000]
590 }
591
592# -------------------------------------------------------------------------
593
594 HstDisplay instproc base_update args {
595 my instvar config number base base_val base_mux
596
597 set mux_addr [format %04x [expr {23 + ${number}}]]
598 set val_addr [format %04x [expr {11 + ${number}}]]
599
600 if {$base} {
601 ${config}.base_auto configure -state normal
602 ${config}.base_const configure -state normal
603 my base_typ_update
604 } else {
605 ${config}.base_auto configure -state disabled
606 ${config}.base_const configure -state disabled
607 ${config}.base_field configure -state disabled
608 set base_mux 0
609 my send_data [usb::convert ${mux_addr}[my mux]00000000${val_addr}000000000000]
610 }
611 }
612
613# -------------------------------------------------------------------------
614
615 HstDisplay instproc base_typ_update args {
616 my instvar config number base_typ base_val base_mux
617
618 set mux_addr [format %04x [expr {23 + ${number}}]]
619 set val_addr [format %04x [expr {11 + ${number}}]]
620 set value [format %04x $base_val]
621
622 switch -- $base_typ {
623 auto {
624 ${config}.base_field configure -state disabled
625 set base_mux 1
626 my send_data [usb::convert ${mux_addr}[my mux]00000000]
627 }
628 const {
629 ${config}.base_field configure -state normal
630 set base_mux 0
631 my send_data [usb::convert ${mux_addr}[my mux]00000000${val_addr}${value}00000000]
632 }
633 }
634 }
635
636# -------------------------------------------------------------------------
637
638 HstDisplay instproc base_val_update args {
639 my instvar number base_val
640
641 set val_addr [format %04x [expr {11 + ${number}}]]
642 set value [format %04x $base_val]
643
644 my send_data [usb::convert ${val_addr}${value}00000000]
645 }
646
647# -------------------------------------------------------------------------
648
649 HstDisplay instproc save_data {data} {
650 set file [tk_getSaveFile]
651 if {[string equal $file {}]} {
652 return
653 }
654
655 set x [catch {set fid [open $file w+]}]
656 set y [catch {puts $fid $data}]
657 set z [catch {close $fid}]
658
659 if { $x || $y || $z || ![file exists $file] || ![file isfile $file] || ![file readable $file] } {
660 tk_messageBox -icon error \
661 -message "An error occurred while writing to \"$file\""
662 } else {
663 tk_messageBox -icon info \
664 -message "File \"$file\" written successfully"
665 }
666 }
667
668# -------------------------------------------------------------------------
669
670 HstDisplay instproc register {} {
671 my save_data [my set data]
672 }
673
674# -------------------------------------------------------------------------
675
676 HstDisplay instproc send_data {data} {
677 global usb_handle
678
679 if {[catch {$usb_handle writeRaw $data} result]} {
680 puts {Error during write}
681 puts $result
682 }
683 }
684
685# -------------------------------------------------------------------------
686
687 HstDisplay instproc restart {} {
688 my instvar restart_command
689 my send_data $restart_command
690 }
691
692# -------------------------------------------------------------------------
693
694 HstDisplay instproc acquire {} {
695 global usb_handle
696 my instvar xvec yvec data fltr_val
697 my instvar acquire_command
698
699 my send_data $acquire_command
700
701 set usb_data {}
702 if {[catch {$usb_handle readHex 4 4096} usb_data]} {
703 puts {Error during read}
704 puts $usb_data
705 set usb_data {}
706 }
707
708 set data $usb_data
709
710 $yvec set $usb_data
711 }
712
713# -------------------------------------------------------------------------
714
715 HstDisplay instproc acquire_loop {} {
716 my instvar after_handle
717
718 my acquire
719
720 set after_handle [after 1000 [myproc acquire_loop]]
721 }
722
723# -------------------------------------------------------------------------
724
725 namespace export HstDisplay
726 namespace export OscDisplay
727}
728
729set notebook [::blt::tabnotebook .notebook -side bottom]
730
731pack $notebook -expand 1 -fill both
732
733set window [frame ${notebook}.hst_2]
734$notebook insert end -text "Histogram" -window $window -fill both
735::mca::HstDisplay hst_2 -number 2 -master $window
736
737grid rowconfigure $window 0 -weight 1
738grid columnconfigure $window 0 -weight 1
739grid columnconfigure $window 1 -weight 0 -minsize 80
740
741set window [frame ${notebook}.osc_2]
742$notebook insert end -text "Pulse shape" -window $window -fill both
743::mca::OscDisplay osc_2 -number 2 -master $window
744
745grid rowconfigure $window 0 -weight 1
746grid columnconfigure $window 0 -weight 1
747grid columnconfigure $window 1 -weight 0 -minsize 80
748
749set usb_handle {}
750
751while {[catch {usb::connect 0x09FB 0x6001 1 1 0} usb_handle]} {
752 set answer [tk_messageBox -icon error -type retrycancel \
753 -message {Cannot access USB device} -detail $usb_handle]
754 if {[string equal $answer cancel]} break
755}
756
757hst_2 restart
758osc_2 restart
759
760hst_2 start
761osc_2 start
762
763# configure polarity
764# set polarity_command [usb::convert 000A011100000000]
765set polarity_command [usb::convert 000A001100000000]
766if {[catch {$usb_handle writeRaw $polarity_command} result]} {
767 puts {Error during write}
768 puts $result
769}
770
771hst_2 restart
772osc_2 restart
Note: See TracBrowser for help on using the repository browser.