source: sandbox/MultiChannelUSB/UserInterface.tcl@ 159

Last change on this file since 159 was 143, checked in by demin, 14 years ago

add dac and adc control

File size: 70.5 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 {ADC 1}
35 2 {ADC 2}
36 3 {ADC 3}
37 4 {ADC 4}
38 5 {ADC 5}
39 6 {ADC 6}
40 7 {ADC 7}
41 8 {ADC 8}
42 9 {ADC 9}
43 10 {ADC 10}
44 11 {ADC 11}
45 12 {ADC 12}
46 }
47
48# -------------------------------------------------------------------------
49
50 variable inpCodes
51 array set inpCodes {
52 0 {r}
53 1 {f}
54 2 {d}
55 3 {c}
56 }
57
58# -------------------------------------------------------------------------
59
60 proc validate {max size value} {
61 if {![regexp {^[0-9]*$} $value]} {
62 return 0
63 } elseif {[regexp {^0[0-9]+$} $value]} {
64 return 0
65 } elseif {$value > $max} {
66 return 0
67 } elseif {[string length $value] > $size} {
68 return 0
69 } else {
70 return 1
71 }
72 }
73
74# -------------------------------------------------------------------------
75
76 proc doublevalidate {max value} {
77 if {![regexp {^[0-9]{0,2}\.?[0-9]{0,3}$} $value]} {
78 return 0
79 } elseif {[regexp {^0[0-9]+$} $value]} {
80 return 0
81 } elseif {$value > $max} {
82 return 0
83 } else {
84 return 1
85 }
86 }
87
88# -------------------------------------------------------------------------
89
90 proc legendLabel {master row key title} {
91 label ${master}.${key}_label -anchor w -text ${title}
92 label ${master}.${key}_value -width 10 -anchor e -text {}
93
94 grid ${master}.${key}_label -row ${row} -column 1 -sticky w
95 grid ${master}.${key}_value -row ${row} -column 2 -sticky ew
96 }
97
98# -------------------------------------------------------------------------
99
100 proc legendButton {master row key title var bg {fg black}} {
101 checkbutton ${master}.${key}_check -variable $var
102 label ${master}.${key}_label -anchor w -text ${title} -bg ${bg} -fg $fg
103 label ${master}.${key}_value -width 10 -anchor e -text {} -bg ${bg} -fg $fg
104
105 grid ${master}.${key}_check -row ${row} -column 0 -sticky w
106 grid ${master}.${key}_label -row ${row} -column 1 -sticky w
107 grid ${master}.${key}_value -row ${row} -column 2 -sticky ew
108 }
109
110# -------------------------------------------------------------------------
111
112 Class UsbController
113
114# -------------------------------------------------------------------------
115
116 UsbController instproc usbConnect {} {
117 my instvar handle
118
119 puts usbConnect
120
121 if {[my exists handle]} {
122 $handle disconnect
123 unset handle
124 }
125if {1} {
126 while {[catch {usb::connect 0x09FB 0x6001 1 1 0} result]} {
127 set answer [tk_messageBox -icon error -type retrycancel \
128 -message {Cannot access USB device} -detail $result]
129# puts $result
130 if {[string equal $answer cancel]} exit
131 }
132
133 set handle $result
134
135}
136 }
137
138# -------------------------------------------------------------------------
139
140 UsbController instproc usbHandle {} {
141 my instvar handle
142
143 if {[my exists handle]} {
144 return $handle
145 } else {
146 my usbConnect
147 }
148 }
149
150# -------------------------------------------------------------------------
151
152 UsbController instproc usbCmd {command} {
153 set code [catch {[my usbHandle] writeRaw [usb::convert $command]} result]
154 switch -- $code {
155 1 {
156# puts $result
157 my usbConnect
158 }
159 }
160
161 }
162
163# -------------------------------------------------------------------------
164
165 UsbController instproc usbCmdReadRaw {command size data} {
166 my usbCmd $command
167
168 set code [catch {[my usbHandle] readRaw $size} result]
169 switch -- $code {
170 0 {
171 set $data $result
172 }
173 1 {
174# puts $result
175 my usbConnect
176 }
177 5 {
178# puts Busy
179 }
180 }
181 }
182
183# -------------------------------------------------------------------------
184
185 UsbController instproc usbCmdReadRaw {command size data} {
186 my usbCmd $command
187
188 set code [catch {[my usbHandle] readRaw $size} result]
189 switch -- $code {
190 0 {
191 set $data $result
192 }
193 1 {
194# puts $result
195 my usbConnect
196 }
197 5 {
198# puts Busy
199 }
200 }
201 }
202
203# -------------------------------------------------------------------------
204
205 UsbController instproc usbCmdReadHex {command width size data} {
206 my usbCmd $command
207
208 set code [catch {[my usbHandle] readHex $width $size} result]
209 switch -- $code {
210 0 {
211 set $data $result
212 }
213 1 {
214# puts $result
215 my usbConnect
216 }
217 5 {
218# puts Busy
219 }
220 }
221 }
222
223# -------------------------------------------------------------------------
224
225 Class SpiDisplay -parameter {
226 {master}
227 {controller}
228 }
229
230# -------------------------------------------------------------------------
231
232 SpiDisplay instproc init {} {
233
234 my setup
235
236 next
237 }
238
239# -------------------------------------------------------------------------
240
241 SpiDisplay instproc destroy {} {
242 next
243 }
244
245# -------------------------------------------------------------------------
246
247 SpiDisplay instproc start {} {
248 my instvar config
249
250 trace add variable [myvar dac1] write [myproc dac1_update]
251 trace add variable [myvar dac2] write [myproc dac2_update]
252
253 ${config(1)}.dac1 set 0
254 ${config(1)}.dac2 set 0
255 }
256
257# -------------------------------------------------------------------------
258
259 SpiDisplay instproc setup {} {
260 my instvar number master
261 my instvar config
262
263 set config(1) [labelframe ${master}.cfg1 -borderwidth 1 -relief sunken -text {DAC}]
264
265 frame ${config(1)}.limits
266 label ${config(1)}.limits.min -text {2.5V}
267 label ${config(1)}.limits.max -text {0.0V}
268
269 scale ${config(1)}.dac1 -orient vertical -from 4095 -to 0 -tickinterval 0 -variable [myvar dac1]
270 scale ${config(1)}.dac2 -orient vertical -from 4095 -to 0 -tickinterval 0 -variable [myvar dac2]
271
272 pack ${config(1)}.limits.min -anchor n -side top -pady 10
273 pack ${config(1)}.limits.max -anchor s -side bottom -pady 9
274
275 grid ${config(1)}.dac1 ${config(1)}.dac2 ${config(1)}.limits -sticky ns -pady 7
276
277
278 set config(2) [labelframe ${master}.cfg2 -borderwidth 1 -relief sunken -text {ADC}]
279
280 frame ${config(2)}.spc1 -width 130 -height 10
281 frame ${config(2)}.spc2 -width 130 -height 10
282 frame ${config(2)}.spc3 -width 130 -height 10
283
284 button ${config(2)}.reset -text {Reset} -command [myproc adc_reset]
285 button ${config(2)}.pattern -text {Test pattern} -command [myproc adc_pattern]
286 button ${config(2)}.ramp -text {Test ramp} -command [myproc adc_ramp]
287 button ${config(2)}.100mV -text {Test 100 mV} -command [myproc adc_100mV]
288 button ${config(2)}.150mV -text {Test 150 mV} -command [myproc adc_150mV]
289 button ${config(2)}.fltr0 -text {Filter 14MHz} -command [myproc adc_fltr0]
290 button ${config(2)}.fltr1 -text {Filter 10MHz} -command [myproc adc_fltr1]
291 button ${config(2)}.fltr2 -text {Filter 7.5MHz} -command [myproc adc_fltr2]
292
293 grid ${config(2)}.spc1
294 grid ${config(2)}.reset -sticky ew -pady 3 -padx 5
295 grid ${config(2)}.spc2
296 grid ${config(2)}.pattern -sticky ew -pady 3 -padx 5
297 grid ${config(2)}.ramp -sticky ew -pady 3 -padx 5
298 grid ${config(2)}.100mV -sticky ew -pady 3 -padx 5
299 grid ${config(2)}.150mV -sticky ew -pady 3 -padx 5
300 grid ${config(2)}.spc3
301 grid ${config(2)}.fltr0 -sticky ew -pady 3 -padx 5
302 grid ${config(2)}.fltr1 -sticky ew -pady 3 -padx 5
303 grid ${config(2)}.fltr2 -sticky ew -pady 3 -padx 5
304
305 grid ${config(1)} -row 1 -column 1 -sticky ns
306 grid ${config(2)} -row 1 -column 2 -sticky ns
307
308 grid columnconfigure ${master} 0 -weight 1
309 grid columnconfigure ${master} 1 -weight 1
310 grid columnconfigure ${master} 2 -weight 1
311 grid columnconfigure ${master} 3 -weight 1
312
313 grid rowconfigure ${master} 0 -weight 0
314 grid rowconfigure ${master} 1 -weight 1
315 grid rowconfigure ${master} 2 -weight 0
316
317 grid rowconfigure ${config(1)} 0 -weight 1
318 }
319
320# -------------------------------------------------------------------------
321
322 SpiDisplay instproc dac1_update args {
323 my instvar controller dac1
324
325 set value [format {3%03x} $dac1]
326
327 set prefix [format {%x} 12]
328
329 set command {}
330 append command 0001${prefix}00000020000000402[string range $value 0 1]
331 append command 0001${prefix}000000200000004[string range $value 2 3]00
332
333 $controller usbCmd $command
334 }
335
336# -------------------------------------------------------------------------
337
338 SpiDisplay instproc dac2_update args {
339 my instvar controller dac2
340
341 set value [format {b%03x} $dac1]
342
343 set prefix [format {%x} 12]
344
345 set command {}
346 append command 0001${prefix}00000020000000402[string range $value 0 1]
347 append command 0001${prefix}000000200000004[string range $value 2 3]00
348
349 $controller usbCmd $command
350 }
351
352# -------------------------------------------------------------------------
353
354 SpiDisplay instproc adc_reset args {
355 my instvar controller
356
357 set prefix [format {%x} 12]
358
359 set command {}
360
361 set value {000001}
362 append command 0001${prefix}00000020000000401[string range $value 0 1]
363 append command 0001${prefix}000000200000004[string range $value 2 5]
364
365 set value {040008}
366 append command 0001${prefix}00000020000000401[string range $value 0 1]
367 append command 0001${prefix}000000200000004[string range $value 2 5]
368
369 $controller usbCmd $command
370 }
371
372# -------------------------------------------------------------------------
373
374 SpiDisplay instproc adc_pattern args {
375 my instvar controller
376
377 set value {022000}
378
379 set prefix [format {%x} 12]
380
381 set command {}
382 append command 0001${prefix}00000020000000401[string range $value 0 1]
383 append command 0001${prefix}000000200000004[string range $value 2 5]
384
385 $controller usbCmd $command
386 }
387
388# -------------------------------------------------------------------------
389
390 SpiDisplay instproc adc_ramp args {
391 my instvar controller
392
393 set value {02E000}
394
395 set prefix [format {%x} 12]
396
397 set command {}
398 append command 0001${prefix}00000020000000401[string range $value 0 1]
399 append command 0001${prefix}000000200000004[string range $value 2 5]
400
401 $controller usbCmd $command
402 }
403
404# -------------------------------------------------------------------------
405
406 SpiDisplay instproc adc_100mV args {
407 my instvar controller
408
409 set value {070080}
410
411 set prefix [format {%x} 12]
412
413 set command {}
414 append command 0001${prefix}00000020000000401[string range $value 0 1]
415 append command 0001${prefix}000000200000004[string range $value 2 5]
416
417 $controller usbCmd $command
418 }
419
420# -------------------------------------------------------------------------
421
422 SpiDisplay instproc adc_150mV args {
423 my instvar controller
424
425 set value {070180}
426
427 set prefix [format {%x} 12]
428
429 set command {}
430 append command 0001${prefix}00000020000000401[string range $value 0 1]
431 append command 0001${prefix}000000200000004[string range $value 2 5]
432
433 $controller usbCmd $command
434 }
435
436# -------------------------------------------------------------------------
437
438 SpiDisplay instproc adc_fltr0 args {
439 my instvar controller
440
441 set value {070000}
442
443 set prefix [format {%x} 12]
444
445 set command {}
446 append command 0001${prefix}00000020000000401[string range $value 0 1]
447 append command 0001${prefix}000000200000004[string range $value 2 5]
448
449 $controller usbCmd $command
450 }
451
452# -------------------------------------------------------------------------
453
454 SpiDisplay instproc adc_fltr1 args {
455 my instvar controller
456
457 set value {070004}
458
459 set prefix [format {%x} 12]
460
461 set command {}
462 append command 0001${prefix}00000020000000401[string range $value 0 1]
463 append command 0001${prefix}000000200000004[string range $value 2 5]
464
465 $controller usbCmd $command
466 }
467
468# -------------------------------------------------------------------------
469
470 SpiDisplay instproc adc_fltr2 args {
471 my instvar controller
472
473 set value {070008}
474
475 set prefix [format {%x} 12]
476
477 set command {}
478 append command 0001${prefix}00000020000000401[string range $value 0 1]
479 append command 0001${prefix}000000200000004[string range $value 2 5]
480
481 $controller usbCmd $command
482 }
483
484# -------------------------------------------------------------------------
485
486 Class MuxDisplay -parameter {
487 {master}
488 {controller}
489 }
490
491# -------------------------------------------------------------------------
492
493 MuxDisplay instproc init {} {
494
495 my setup
496
497 next
498 }
499
500# -------------------------------------------------------------------------
501
502 MuxDisplay instproc destroy {} {
503 next
504 }
505
506# -------------------------------------------------------------------------
507
508 MuxDisplay instproc start {} {
509 variable adcCodes
510 my instvar config chan_val
511
512 set chan_val(1) 0
513 set chan_val(2) 0
514 set chan_val(3) 0
515 set chan_val(4) 0
516 set chan_val(5) 0
517 set chan_val(6) 0
518
519 trace add variable [myvar chan_val] write [myproc chan_val_update]
520 trace add variable [myvar polar] write [myproc polar_update]
521
522 $config(1).chan_0_1 select
523 $config(2).chan_0_2 select
524 $config(3).chan_0_3 select
525 $config(4).chan_0_4 select
526 $config(5).chan_0_5 select
527 $config(6).chan_0_1 select
528
529 foreach {ch dummy} [array get adcCodes] {
530 $config(inv).polar_${ch} deselect
531 }
532 }
533
534# -------------------------------------------------------------------------
535
536 MuxDisplay instproc setup {} {
537 variable oscCodes
538 variable adcCodes
539 variable inpCodes
540 my instvar master
541 my instvar config
542
543 set size [array size inpCodes]
544 set oscList [array get oscCodes]
545 set adcList [array get adcCodes]
546 set inpList [array get inpCodes]
547
548 set mux [frame ${master}.mux]
549 set key [frame ${master}.key]
550 set inv [frame ${master}.inv]
551
552 foreach {osc title} $oscList {
553 set config($osc) [labelframe ${mux}.$osc -borderwidth 1 -relief sunken -text $title]
554 set column 1
555 foreach {code input} $inpList {
556 label $config($osc).input_${input} -text " ${input}"
557 grid $config($osc).input_${input} -row 0 -column ${column} -sticky w
558 incr column
559 }
560 foreach {ch dummy} $adcList {
561 label $config($osc).chan_${ch} -text "${ch} "
562 grid $config($osc).chan_${ch} -row ${ch} -column 0 -sticky ew
563 foreach {code input} $inpList {
564 set column [expr {$code + 1}]
565 set value [expr {$size * ($ch - 1) + $code}]
566 radiobutton $config($osc).chan_${code}_${ch} -variable [myvar chan_val($osc)] -value ${value}
567 grid $config($osc).chan_${code}_${ch} -row ${ch} -column ${column} -sticky w
568 }
569 }
570 set column [expr {($osc - 1) % 6}]
571 set row [expr {($osc - 1) / 6}]
572 grid $config($osc) -row ${row} -column ${column} -sticky news -padx 10
573 }
574
575 set config(key) [labelframe ${key}.frame -borderwidth 1 -relief sunken -text {legend}]
576
577 label $config(key).r -text "r - raw signal"
578 grid $config(key).r -row 0 -column 0 -sticky news
579
580 label $config(key).f -text "f - filtered signal"
581 grid $config(key).f -row 0 -column 1 -sticky news
582
583 label $config(key).d -text "d - deconvoluted signal"
584 grid $config(key).d -row 0 -column 2 -sticky news
585
586 label $config(key).c -text "c - clipped signal"
587 grid $config(key).c -row 0 -column 3 -sticky news
588
589 grid $config(key) -row 0 -column 0 -sticky news -padx 10
590
591
592 set config(inv) [labelframe ${inv}.frame -borderwidth 1 -relief sunken -text {polarity inversion}]
593 label $config(inv).chan_label -text "channel "
594 grid $config(inv).chan_label -row 0 -column 0 -sticky e
595 label $config(inv).polar_label -text "polarity"
596 grid $config(inv).polar_label -row 1 -column 0 -sticky e
597 foreach {ch dummy} $adcList {
598 label $config(inv).chan_${ch} -text "${ch} "
599 grid $config(inv).chan_${ch} -row 0 -column ${ch} -sticky ew
600 checkbutton $config(inv).polar_${ch} -variable [myvar polar($ch)]
601 grid $config(inv).polar_${ch} -row 1 -column ${ch} -sticky w
602 }
603 grid $config(inv) -row 0 -column 0 -sticky news -padx 10
604
605 grid ${key} -row 0 -column 0 -sticky news
606 grid ${mux} -row 1 -column 0 -sticky news
607 grid ${inv} -row 2 -column 0 -sticky news
608
609 grid columnconfigure ${master} 0 -weight 1
610 grid rowconfigure ${master} 0 -weight 1
611 grid rowconfigure ${master} 1 -weight 1
612 grid rowconfigure ${master} 2 -weight 1
613
614 grid columnconfigure ${inv} 0 -weight 1
615
616 grid columnconfigure ${key} 0 -weight 1
617 grid columnconfigure $config(key) 0 -weight 1
618 grid columnconfigure $config(key) 1 -weight 1
619 grid columnconfigure $config(key) 2 -weight 1
620 grid columnconfigure $config(key) 3 -weight 1
621
622
623 grid columnconfigure ${mux} 0 -weight 1
624 grid columnconfigure ${mux} 1 -weight 1
625 grid columnconfigure ${mux} 2 -weight 1
626 grid columnconfigure ${mux} 3 -weight 1
627 grid columnconfigure ${mux} 4 -weight 1
628 grid columnconfigure ${mux} 5 -weight 1
629
630
631 }
632
633
634# ------------------------------------------------------------------------
635
636 MuxDisplay instproc chan_val_update args {
637 my instvar controller chan_val
638
639 set byte1 [format {%02x%02x} $chan_val(2) $chan_val(1)]
640 set byte2 [format {%02x%02x} $chan_val(4) $chan_val(3)]
641 set byte3 [format {%02x%02x} $chan_val(6) $chan_val(5)]
642
643 $controller usbCmd 000200020004${byte1}000200030004${byte2}000200040004${byte3}
644 }
645
646# -------------------------------------------------------------------------
647
648 MuxDisplay instproc polar_update args {
649 my instvar controller polar
650
651 set value {0b}
652 for {set i 12} {$i >= 1} {incr i -1} {
653 append value $polar($i)
654 }
655
656 set value [format {%04x} $value]
657
658 $controller usbCmd 000200010004${value}
659 }
660
661# -------------------------------------------------------------------------
662
663 Class HstDisplay -parameter {
664 {number}
665 {master}
666 {controller}
667 }
668
669# -------------------------------------------------------------------------
670
671 HstDisplay instproc init {} {
672
673 my set data {}
674
675 vector create [myvar xvec](4096)
676 vector create [myvar yvec](4096)
677
678 # fill one vector for the x axis with 4096 points
679 [myvar xvec] seq -0.5 4095.5
680
681 my setup
682
683 next
684 }
685
686# -------------------------------------------------------------------------
687
688 HstDisplay instproc destroy {} {
689 next
690 }
691
692# -------------------------------------------------------------------------
693
694 HstDisplay instproc start {} {
695 my instvar config
696
697 trace add variable [myvar data] write [myproc data_update]
698 trace add variable [myvar cntr_val] write [myproc cntr_val_update]
699 trace add variable [myvar rate_val] write [myproc rate_val_update]
700
701 trace add variable [myvar axis] write [myproc axis_update]
702 trace add variable [myvar thrs] write [myproc thrs_update]
703 trace add variable [myvar thrs_val] write [myproc thrs_update]
704 trace add variable [myvar base] write [myproc base_update]
705 trace add variable [myvar base_typ] write [myproc base_typ_update]
706 trace add variable [myvar base_val] write [myproc base_val_update]
707
708 ${config}.axis_check select
709
710 ${config}.thrs_check select
711 ${config}.thrs_field set 25
712
713 ${config}.base_auto select
714 ${config}.base_field set 20
715 ${config}.base_check select
716
717 set cntr_tmp 1200000000
718 my set cntr_val $cntr_tmp
719 my set cntr_bak $cntr_tmp
720 my set cntr_old $cntr_tmp
721 my set yvec_bak 0.0
722 my set yvec_old 0.0
723
724 my set rate_val(inst) 0.0
725 my set rate_val(mean) 0.0
726
727# my cntr_reset
728 }
729
730# -------------------------------------------------------------------------
731
732 HstDisplay instproc setup {} {
733 my instvar number master
734 my instvar xvec yvec graph
735 my instvar config thrs thrs_val base base_typ base_val
736 my instvar cntr_h cntr_m cntr_s
737
738 # create a graph widget and show a grid
739 set graph [graph ${master}.graph -height 250 -leftmargin 80]
740 $graph crosshairs configure -hide no -linewidth 1 -color darkblue -dashes {2 2}
741 $graph grid configure -hide no
742 $graph legend configure -hide yes
743 $graph axis configure x -min 0 -max 4096
744
745 set config [frame ${master}.config -width 170]
746
747 checkbutton ${config}.axis_check -text {log scale} -variable [myvar axis]
748
749 frame ${config}.spc1 -width 170 -height 30
750
751 frame ${config}.rate_frame -borderwidth 0 -width 170
752 legendLabel ${config}.rate_frame 0 inst {Inst. rate, 1/s}
753 legendLabel ${config}.rate_frame 1 mean {Avg. rate, 1/s}
754
755 frame ${config}.spc2 -width 170 -height 30
756
757 frame ${config}.chan_frame -borderwidth 0 -width 170
758 legendLabel ${config}.chan_frame 0 axisy {Bin entries}
759 legendLabel ${config}.chan_frame 1 axisx {Bin number}
760
761 frame ${config}.spc3 -width 170 -height 30
762
763 frame ${config}.cntr_frame -borderwidth 0 -width 170
764
765 label ${config}.cntr_frame.h -width 3 -anchor w -text {h}
766 entry ${config}.cntr_frame.h_field -width 3 -textvariable [myvar cntr_h] \
767 -validate all -vcmd {::mca::validate 999 3 %P}
768 label ${config}.cntr_frame.m -width 3 -anchor w -text {m}
769 entry ${config}.cntr_frame.m_field -width 3 -textvariable [myvar cntr_m] \
770 -validate all -vcmd {::mca::validate 59 2 %P}
771 label ${config}.cntr_frame.s -width 3 -anchor w -text {s}
772 entry ${config}.cntr_frame.s_field -width 6 -textvariable [myvar cntr_s] \
773 -validate all -vcmd {::mca::doublevalidate 59.999 %P}
774
775 grid ${config}.cntr_frame.h_field ${config}.cntr_frame.h \
776 ${config}.cntr_frame.m_field ${config}.cntr_frame.m ${config}.cntr_frame.s_field ${config}.cntr_frame.s
777
778 frame ${config}.spc4 -width 170 -height 10
779
780 button ${config}.start -text Start \
781 -bg yellow -activebackground yellow -command [myproc cntr_start]
782 button ${config}.reset -text Reset \
783 -bg red -activebackground red -command [myproc cntr_reset]
784
785 frame ${config}.spc5 -width 170 -height 30
786
787 checkbutton ${config}.thrs_check -text threshold -variable [myvar thrs]
788 spinbox ${config}.thrs_field -from 1 -to 4095 \
789 -increment 5 -width 10 -textvariable [myvar thrs_val] \
790 -validate all -vcmd {::mca::validate 4095 4 %P}
791
792 frame ${config}.spc6 -width 170 -height 30
793
794 checkbutton ${config}.base_check -text baseline -variable [myvar base]
795 radiobutton ${config}.base_auto -text automatic -variable [myvar base_typ] -value 1
796 radiobutton ${config}.base_const -text constant -variable [myvar base_typ] -value 0
797 spinbox ${config}.base_field -from 1 -to 4095 \
798 -increment 5 -width 10 -textvariable [myvar base_val] \
799 -validate all -vcmd {::mca::validate 4095 4 %P}
800
801 frame ${config}.spc7 -width 170 -height 30
802
803 button ${config}.register -text Register \
804 -bg lightblue -activebackground lightblue -command [myproc register]
805
806 grid ${config}.axis_check -sticky w
807 grid ${config}.spc1
808 grid ${config}.rate_frame -sticky ew -padx 5
809 grid ${config}.spc2
810 grid ${config}.chan_frame -sticky ew -padx 5
811 grid ${config}.spc3
812 grid ${config}.cntr_frame -sticky ew -padx 5
813 grid ${config}.spc4
814 grid ${config}.start -sticky ew -pady 3 -padx 5
815 grid ${config}.reset -sticky ew -pady 3 -padx 5
816 grid ${config}.spc5
817 grid ${config}.thrs_check -sticky w
818 grid ${config}.thrs_field -sticky ew -pady 1 -padx 5
819 grid ${config}.spc6
820 grid ${config}.base_check -sticky w
821 grid ${config}.base_auto -sticky w
822 grid ${config}.base_const -sticky w
823 grid ${config}.base_field -sticky ew -pady 1 -padx 5
824 grid ${config}.spc7
825 grid ${config}.register -sticky ew -pady 3 -padx 5
826
827 grid ${graph} -row 0 -column 0 -sticky news
828 grid ${config} -row 0 -column 1
829
830 grid rowconfigure ${master} 0 -weight 1
831 grid columnconfigure ${master} 0 -weight 1
832 grid columnconfigure ${master} 1 -weight 0 -minsize 80
833
834 grid columnconfigure ${config}.rate_frame 1 -weight 1
835 grid columnconfigure ${config}.chan_frame 1 -weight 1
836
837 # enable zooming
838 Blt_ZoomStack $graph
839
840 my crosshairs $graph
841
842 #bind .graph <Motion> {%W crosshairs configure -position @%x,%y}
843
844 # create one element with data for the x and y axis, no dots
845 $graph element create Spectrum1 -color blue -linewidth 2 -symbol none -smooth step -xdata [myvar xvec] -ydata [myvar yvec]
846 }
847
848# -------------------------------------------------------------------------
849
850 HstDisplay instproc coor_update {W x y} {
851 my instvar config graph
852
853 $W crosshairs configure -position @${x},${y}
854
855 set index [$W axis invtransform x $x]
856 set index [::tcl::mathfunc::round $index]
857 catch {
858 ${config}.chan_frame.axisy_value configure -text [[myvar yvec] index $index]
859 ${config}.chan_frame.axisx_value configure -text ${index}.0
860 }
861 }
862# -------------------------------------------------------------------------
863
864 HstDisplay instproc crosshairs {graph} {
865 set method [myproc coor_update]
866 bind $graph <Motion> [list [self] coor_update %W %x %y]
867 bind $graph <Leave> {
868 %W crosshairs off
869 }
870 bind $graph <Enter> {
871 %W crosshairs on
872 }
873 }
874
875# -------------------------------------------------------------------------
876
877 HstDisplay instproc axis_update args {
878 my instvar axis graph
879 if {$axis} {
880 $graph axis configure y -min 1 -max 1E10 -logscale yes
881 } else {
882 $graph axis configure y -min {} -max {} -logscale no
883 }
884 }
885
886# -------------------------------------------------------------------------
887
888 HstDisplay instproc thrs_update args {
889 my instvar controller config number thrs thrs_val
890
891 if {[string equal $thrs_val {}]} {
892 set thrs_val 0
893 }
894
895 set val_addr [format %02x [expr {6 + 2 * ${number}}]]
896
897 if {$thrs} {
898 ${config}.thrs_field configure -state normal
899 set value [format %03x $thrs_val]
900 } else {
901 ${config}.thrs_field configure -state disabled
902 set value 000
903 }
904
905 $controller usbCmd 000200${val_addr}00040${value}
906 }
907
908# -------------------------------------------------------------------------
909
910 HstDisplay instproc base_update args {
911 my instvar controller config number base base_typ
912
913 set val_addr [format %02x [expr {7 + 2 * ${number}}]]
914
915 if {$base} {
916 ${config}.base_auto configure -state normal
917 ${config}.base_const configure -state normal
918 my base_typ_update
919 } else {
920 ${config}.base_auto configure -state disabled
921 ${config}.base_const configure -state disabled
922 ${config}.base_field configure -state disabled
923 $controller usbCmd 000200${val_addr}0004${base_typ}000
924 }
925 }
926
927# -------------------------------------------------------------------------
928
929 HstDisplay instproc base_typ_update args {
930 my instvar config base_typ
931
932 switch -- $base_typ {
933 1 {
934 ${config}.base_field configure -state disabled
935 }
936 0 {
937 ${config}.base_field configure -state normal
938 }
939 }
940
941 my base_val_update
942 }
943
944# -------------------------------------------------------------------------
945
946 HstDisplay instproc base_val_update args {
947 my instvar controller number base_typ base_val
948
949 if {[string equal $base_val {}]} {
950 set base_val 0
951 }
952
953 set val_addr [format %02x [expr {7 + 2 * ${number}}]]
954 set value [format %03x $base_val]
955
956 $controller usbCmd 000200${val_addr}0004${base_typ}${value}
957 }
958
959# -------------------------------------------------------------------------
960
961 HstDisplay instproc rate_val_update {name key op} {
962 my instvar config rate_val
963
964 ${config}.rate_frame.${key}_value configure -text [format {%.2e} $rate_val(${key})]
965 }
966
967# -------------------------------------------------------------------------
968
969 HstDisplay instproc cntr_val_update args {
970 my instvar cntr_val cntr_h cntr_m cntr_s
971
972 set cntr_tmp [expr {${cntr_val}/20000}]
973 set cntr_h [expr {${cntr_tmp}/3600000}]
974 set cntr_m [expr {${cntr_tmp}%3600000/60000}]
975 set cntr_s [expr {${cntr_tmp}%3600000%60000/1000.0}]
976 }
977
978# -------------------------------------------------------------------------
979
980 HstDisplay instproc cntr_setup {} {
981 my instvar controller number cntr_val
982
983 set word0 [format %08x [expr {${cntr_val} & 0xFFFFFFFF}]]
984 set word1 [format %08x [expr {${cntr_val} >> 32}]]
985
986 set prefix [format %x [expr {5 + ${number}}]]
987
988 set command {}
989 append command 0001${prefix}000000200000004[string range $word0 4 7]
990 append command 0001${prefix}000000200010004[string range $word0 0 3]
991 append command 0001${prefix}000000200020004[string range $word1 4 7]
992 append command 0001${prefix}000000200030004[string range $word1 0 3]
993
994 # send counter value
995 $controller usbCmd $command
996
997 # load counter value
998# set val_addr [format %02x [expr {12 + ${number}}]]
999# $controller usbCmd 000200${val_addr}00040001000200${val_addr}00040000
1000 }
1001
1002# -------------------------------------------------------------------------
1003
1004 HstDisplay instproc cntr_reset {} {
1005 my instvar controller number after_handle
1006 my instvar cntr_val cntr_bak cntr_old yvec_bak yvec_old
1007
1008 my cntr_stop
1009
1010 set value [format %04x [expr {1 << (5 + ${number})}]]
1011 $controller usbCmd 000200000004${value}0002000000040000
1012
1013 set cntr_val $cntr_bak
1014 my cntr_setup
1015
1016 set cntr_old $cntr_bak
1017 set yvec_bak 0.0
1018 set yvec_old 0.0
1019
1020 my acquire
1021
1022 my cntr_ready
1023 }
1024
1025# -------------------------------------------------------------------------
1026
1027 HstDisplay instproc cntr_ready {} {
1028 my instvar config cntr_val cntr_bak
1029
1030 set cntr_val $cntr_bak
1031
1032 ${config}.start configure -text Start -command [myproc cntr_start]
1033 ${config}.reset configure -state active
1034
1035 ${config}.cntr_frame.h_field configure -state normal
1036 ${config}.cntr_frame.m_field configure -state normal
1037 ${config}.cntr_frame.s_field configure -state normal
1038 }
1039
1040# -------------------------------------------------------------------------
1041
1042 HstDisplay instproc cntr_start {} {
1043 my instvar config
1044 my instvar cntr_h cntr_m cntr_s
1045 my instvar cntr_val cntr_bak cntr_old yvec_bak yvec_old
1046
1047 set h $cntr_h
1048 set m $cntr_m
1049 set s $cntr_s
1050
1051 if {[string equal $h {}]} {
1052 set h 0
1053 }
1054 if {[string equal $m {}]} {
1055 set m 0
1056 }
1057 if {[string equal $s {}]} {
1058 set s 0
1059 }
1060
1061 set cntr_tmp [expr {${h}*3600000 + ${m}*60000 + ${s}*1000}]
1062 set cntr_tmp [expr {entier(20000 * ${cntr_tmp})}]
1063
1064 if {$cntr_tmp > 0} {
1065 ${config}.cntr_frame.h_field configure -state disabled
1066 ${config}.cntr_frame.m_field configure -state disabled
1067 ${config}.cntr_frame.s_field configure -state disabled
1068
1069 set cntr_val $cntr_tmp
1070 set cntr_bak $cntr_tmp
1071 set cntr_old $cntr_tmp
1072 set yvec_bak [usb::integrateBlt [myvar yvec] 0]
1073 set yvec_old $yvec_bak
1074
1075 my cntr_setup
1076
1077 my cntr_resume
1078 }
1079 }
1080
1081# -------------------------------------------------------------------------
1082
1083 HstDisplay instproc cntr_pause {} {
1084 my instvar config
1085
1086 my cntr_stop
1087
1088 ${config}.start configure -text Resume -command [myproc cntr_resume]
1089# ${config}.reset configure -state active
1090
1091 }
1092
1093# -------------------------------------------------------------------------
1094
1095 HstDisplay instproc cntr_resume {} {
1096 my instvar controller config number auto
1097
1098 set val_addr [format %02x [expr {13 + ${number}}]]
1099
1100 ${config}.start configure -text Pause -command [myproc cntr_pause]
1101# ${config}.reset configure -state disabled
1102
1103 $controller usbCmd 000200${val_addr}00040002
1104
1105 set auto 1
1106
1107 after 100 [myproc acquire_loop]
1108 }
1109
1110# -------------------------------------------------------------------------
1111
1112 HstDisplay instproc cntr_stop {} {
1113 my instvar controller config number auto
1114
1115 set val_addr [format %02x [expr {13 + ${number}}]]
1116
1117 $controller usbCmd 000200${val_addr}00040000
1118
1119 set auto 0
1120 }
1121
1122# -------------------------------------------------------------------------
1123
1124 HstDisplay instproc data_update args {
1125 my instvar data
1126 usb::convertBlt $data 4 [myvar yvec]
1127 }
1128
1129# -------------------------------------------------------------------------
1130
1131 HstDisplay instproc acquire_loop {} {
1132 my instvar cntr_val auto
1133
1134 my acquire
1135
1136 if {$cntr_val == 0} {
1137 my cntr_stop
1138 my cntr_ready
1139 } elseif {$auto} {
1140 after 1000 [myproc acquire_loop]
1141 }
1142 }
1143
1144# -------------------------------------------------------------------------
1145
1146 HstDisplay instproc acquire {} {
1147 my instvar controller config number
1148 my instvar cntr_val cntr_bak cntr_old yvec_bak yvec_old rate_val
1149
1150 set size 4096
1151
1152 set prefix [format {%x} [expr {$number + 2}]]
1153
1154 set value [format {%08x} [expr {$size * 2}]]
1155
1156 set command 0001${prefix}000000200000001[string range $value 0 3]0003[string range $value 4 7]00050000
1157
1158 $controller usbCmdReadRaw $command [expr {$size * 4}] [myvar data]
1159 set yvec_new [usb::integrateBlt [myvar yvec]]
1160
1161 set prefix [format {%x} [expr {$number + 5}]]
1162 set command 0001${prefix}000000200000003000400050000
1163
1164 $controller usbCmdReadHex $command 8 1 [myvar cntr_val]
1165 set cntr_new $cntr_val
1166
1167 if {$cntr_new < $cntr_old} {
1168 set rate_val(inst) [expr {($yvec_new - $yvec_old)*20000000/($cntr_old - $cntr_new)}]
1169 set rate_val(mean) [expr {($yvec_new - $yvec_bak)*20000000/($cntr_bak - $cntr_new)}]
1170 set yvec_old $yvec_new
1171 set cntr_old $cntr_new
1172 }
1173 }
1174
1175# -------------------------------------------------------------------------
1176
1177 HstDisplay instproc save_data {data} {
1178 my instvar number
1179
1180 set types {
1181 {{Data Files} {.dat} }
1182 {{All Files} * }
1183 }
1184
1185 set stamp [clock format [clock seconds] -format %Y%m%d_%H%M%S]
1186 set fname spectrum_[expr {$number + 1}]_${stamp}.dat
1187
1188 set fname [tk_getSaveFile -filetypes $types -initialfile $fname]
1189 if {[string equal $fname {}]} {
1190 return
1191 }
1192
1193 set x [catch {
1194 set fid [open $fname w+]
1195 puts $fid $data
1196 close $fid
1197 }]
1198
1199 if { $x || ![file exists $fname] || ![file isfile $fname] || ![file readable $fname] } {
1200 tk_messageBox -icon error \
1201 -message "An error occurred while writing to \"$fname\""
1202 } else {
1203 tk_messageBox -icon info \
1204 -message "File \"$fname\" written successfully"
1205 }
1206 }
1207
1208# -------------------------------------------------------------------------
1209
1210 HstDisplay instproc register {} {
1211 my save_data [join [[myvar yvec] range 0 4095] \n]
1212 }
1213
1214# -------------------------------------------------------------------------
1215
1216 Class CntDisplay -parameter {
1217 {master}
1218 {controller}
1219 }
1220
1221# -------------------------------------------------------------------------
1222
1223 CntDisplay instproc init {} {
1224
1225 my set data {}
1226 my set cntr 0
1227 my set recs 0
1228
1229 vector create [myvar xvec](16384)
1230 vector create [myvar yvec](16384)
1231
1232 # fill one vector for the x axis with 16384 points
1233 [myvar xvec] seq -0.5 16384.5
1234
1235 my setup
1236
1237 next
1238 }
1239
1240# -------------------------------------------------------------------------
1241
1242 CntDisplay instproc destroy {} {
1243 next
1244 }
1245
1246# -------------------------------------------------------------------------
1247
1248 CntDisplay instproc start {} {
1249 my instvar config
1250
1251 trace add variable [myvar data] write [myproc data_update]
1252
1253 trace add variable [myvar thrs_val] write [myproc thrs_val_update]
1254
1255 trace add variable [myvar cntr] write [myproc cntr_update]
1256 trace add variable [myvar recs] write [myproc recs_update]
1257
1258 trace add variable [myvar axis] write [myproc axis_update]
1259
1260 ${config}.axis_check select
1261
1262 my set thrs_val 100
1263
1264 my set cntr_val 100
1265 my set cntr_bak 100
1266 my set recs_val 100
1267 my set recs_bak 100
1268
1269# my cntr_reset
1270 }
1271
1272# -------------------------------------------------------------------------
1273
1274 CntDisplay instproc setup {} {
1275 my instvar master
1276 my instvar xvec yvec graph
1277 my instvar config
1278 my instvar cntr_ms
1279
1280 # create a graph widget and show a grid
1281 set graph [graph ${master}.graph -height 250 -leftmargin 80]
1282 $graph crosshairs configure -hide no -linewidth 1 -color darkblue -dashes {2 2}
1283 $graph grid configure -hide no
1284 $graph legend configure -hide yes
1285 $graph axis configure x -min 0 -max 16384
1286
1287 set config [frame ${master}.config -width 170]
1288
1289 checkbutton ${config}.axis_check -text {log scale} -variable [myvar axis]
1290
1291 frame ${config}.spc1 -width 170 -height 30
1292
1293 frame ${config}.chan_frame -borderwidth 0 -width 170
1294 legendLabel ${config}.chan_frame 0 mean {Mean value}
1295 legendLabel ${config}.chan_frame 1 entr {Total entries}
1296 legendLabel ${config}.chan_frame 2 empty {}
1297 legendLabel ${config}.chan_frame 3 axisy {Bin entries}
1298 legendLabel ${config}.chan_frame 4 axisx {Bin number}
1299
1300 frame ${config}.spc3 -width 170 -height 30
1301
1302 label ${config}.thrs -text {amplitude threshold}
1303 spinbox ${config}.thrs_field -from 1 -to 4095 \
1304 -increment 5 -width 10 -textvariable [myvar thrs_val] \
1305 -validate all -vcmd {::mca::validate 4095 4 %P}
1306
1307 frame ${config}.spc4 -width 170 -height 30
1308
1309 label ${config}.cntr -text {time of exposure (s)}
1310 spinbox ${config}.cntr_field -from 0 -to 9999 \
1311 -increment 10 -width 10 -textvariable [myvar cntr_val] \
1312 -validate all -vcmd {::mca::validate 9999 4 %P}
1313
1314 frame ${config}.spc5 -width 170 -height 10
1315
1316 label ${config}.recs -text {number of exposures}
1317 spinbox ${config}.recs_field -from 0 -to 99999 \
1318 -increment 10 -width 10 -textvariable [myvar recs_val] \
1319 -validate all -vcmd {::mca::validate 99999 5 %P}
1320
1321 frame ${config}.spc6 -width 170 -height 10
1322
1323 button ${config}.start -text {Start} \
1324 -bg yellow -activebackground yellow -command [myproc recs_start]
1325
1326 button ${config}.reset -text Reset \
1327 -bg red -activebackground red -command [myproc cntr_reset]
1328
1329 frame ${config}.spc7 -width 170 -height 30
1330
1331 button ${config}.register -text Register \
1332 -bg lightblue -activebackground lightblue -command [myproc register]
1333
1334 grid ${config}.axis_check -sticky w
1335 grid ${config}.spc1
1336 grid ${config}.chan_frame -sticky ew -padx 5
1337 grid ${config}.spc3
1338 grid ${config}.thrs -sticky w -pady 1 -padx 3
1339 grid ${config}.thrs_field -sticky ew -pady 1 -padx 5
1340 grid ${config}.spc4
1341 grid ${config}.cntr -sticky w -pady 1 -padx 3
1342 grid ${config}.cntr_field -sticky ew -pady 1 -padx 5
1343 grid ${config}.spc5
1344 grid ${config}.recs -sticky w -pady 1 -padx 3
1345 grid ${config}.recs_field -sticky ew -pady 1 -padx 5
1346 grid ${config}.spc6
1347 grid ${config}.start -sticky ew -pady 3 -padx 5
1348 grid ${config}.reset -sticky ew -pady 3 -padx 5
1349 grid ${config}.spc7
1350 grid ${config}.register -sticky ew -pady 3 -padx 5
1351
1352 grid ${graph} -row 0 -column 0 -sticky news
1353 grid ${config} -row 0 -column 1
1354
1355 grid rowconfigure ${master} 0 -weight 1
1356 grid columnconfigure ${master} 0 -weight 1
1357 grid columnconfigure ${master} 1 -weight 0 -minsize 80
1358
1359 grid columnconfigure ${config}.chan_frame 1 -weight 1
1360
1361 # enable zooming
1362 Blt_ZoomStack $graph
1363
1364 my crosshairs $graph
1365
1366 #bind .graph <Motion> {%W crosshairs configure -position @%x,%y}
1367
1368 # create one element with data for the x and y axis, no dots
1369 $graph element create Spectrum1 -color blue -linewidth 2 -symbol none -smooth step -xdata [myvar xvec] -ydata [myvar yvec]
1370 }
1371
1372# -------------------------------------------------------------------------
1373
1374 CntDisplay instproc coor_update {W x y} {
1375 my instvar config graph
1376
1377 $W crosshairs configure -position @${x},${y}
1378
1379 set index [$W axis invtransform x $x]
1380 set index [::tcl::mathfunc::round $index]
1381 catch {
1382 ${config}.chan_frame.axisy_value configure -text [[myvar yvec] index $index]
1383 ${config}.chan_frame.axisx_value configure -text ${index}.0
1384 }
1385 }
1386# -------------------------------------------------------------------------
1387
1388 CntDisplay instproc crosshairs {graph} {
1389 set method [myproc coor_update]
1390 bind $graph <Motion> [list [self] coor_update %W %x %y]
1391 bind $graph <Leave> {
1392 %W crosshairs off
1393 }
1394 bind $graph <Enter> {
1395 %W crosshairs on
1396 }
1397 }
1398
1399# -------------------------------------------------------------------------
1400
1401 CntDisplay instproc thrs_val_update args {
1402 my instvar controller config thrs_val
1403
1404 if {[string equal $thrs_val {}]} {
1405 set thrs_val 0
1406 }
1407
1408 set val_addr [format %02x 12]
1409
1410 ${config}.thrs_field configure -state normal
1411 set value [format %03x $thrs_val]
1412
1413 $controller usbCmd 000200${val_addr}00040${value}
1414 }
1415
1416# -------------------------------------------------------------------------
1417
1418 CntDisplay instproc cntr_update args {
1419 my instvar cntr cntr_val
1420 set cntr_val [expr {${cntr}/20000000}]
1421
1422 }
1423
1424# -------------------------------------------------------------------------
1425
1426 CntDisplay instproc recs_update args {
1427 my instvar recs recs_val
1428 set recs_val [expr {${recs}*1}]
1429 }
1430
1431# -------------------------------------------------------------------------
1432
1433 CntDisplay instproc cntr_setup {} {
1434 my instvar controller cntr_val
1435
1436 set cntr_tmp [expr {${cntr_val} * 20000000}]
1437 set word0 [format {%08x} [expr {${cntr_tmp} & 0xFFFFFFFF}]]
1438 set word1 [format {%08x} [expr {${cntr_tmp} >> 32}]]
1439
1440 set prefix [format {%x} 9]
1441
1442 set command {}
1443 append command 0001${prefix}000000200000004[string range $word0 4 7]
1444 append command 0001${prefix}000000200010004[string range $word0 0 3]
1445 append command 0001${prefix}000000200020004[string range $word1 4 7]
1446 append command 0001${prefix}000000200030004[string range $word1 0 3]
1447
1448 # send counter value
1449 $controller usbCmd $command
1450 }
1451
1452# -------------------------------------------------------------------------
1453
1454 CntDisplay instproc recs_setup {} {
1455 my instvar controller recs_val
1456
1457 set word0 [format {%08x} [expr {${recs_val} & 0xFFFFFFFF}]]
1458 set word1 [format {%08x} [expr {${recs_val} >> 32}]]
1459
1460 set prefix [format {%x} 10]
1461
1462 set command {}
1463 append command 0001${prefix}000000200000004[string range $word0 4 7]
1464 append command 0001${prefix}000000200010004[string range $word0 0 3]
1465 append command 0001${prefix}000000200020004[string range $word1 4 7]
1466 append command 0001${prefix}000000200030004[string range $word1 0 3]
1467
1468 # send counter value
1469 $controller usbCmd $command
1470 }
1471
1472# -------------------------------------------------------------------------
1473
1474 CntDisplay instproc cntr_reset {} {
1475 my instvar controller after_handle
1476 my instvar cntr_val cntr_bak recs_val recs_bak
1477
1478 my cntr_stop
1479
1480 set value [format {%04x} [expr {1 << 11}]]
1481 $controller usbCmd 000200000004${value}0002000000040000
1482
1483 my recs_stop
1484 }
1485
1486# -------------------------------------------------------------------------
1487
1488 CntDisplay instproc cntr_ready {} {
1489 my instvar config cntr_val cntr_bak recs_val recs_bak
1490
1491 set cntr_val $cntr_bak
1492 set recs_val $recs_bak
1493
1494 ${config}.start configure -text Start -command [myproc recs_start]
1495 ${config}.reset configure -state active
1496
1497 ${config}.start configure -state active
1498 ${config}.cntr_field configure -state normal
1499 ${config}.recs_field configure -state normal
1500 }
1501
1502# -------------------------------------------------------------------------
1503
1504 CntDisplay instproc recs_start {} {
1505 my instvar controller config auto
1506 my instvar cntr_val cntr_bak recs_val recs_bak
1507
1508 if {$cntr_val > 0 && $recs_val > 0} {
1509 ${config}.start configure -text {Stop} -command [myproc recs_stop]
1510 ${config}.cntr_field configure -state disabled
1511 ${config}.recs_field configure -state disabled
1512
1513 set cntr_bak $cntr_val
1514 set recs_bak $recs_val
1515
1516 my cntr_setup
1517 my recs_setup
1518
1519 set val_addr [format {%02x} 16]
1520
1521 $controller usbCmd 000200${val_addr}00040002
1522
1523 set auto 1
1524
1525 after 100 [myproc acquire_loop]
1526 }
1527 }
1528
1529# -------------------------------------------------------------------------
1530
1531 CntDisplay instproc recs_stop {} {
1532 my instvar cntr_val cntr_bak recs_val recs_bak
1533
1534 my cntr_stop
1535
1536 set cntr_val $cntr_bak
1537 my cntr_setup
1538
1539 set recs_val $recs_bak
1540 my recs_setup
1541
1542 my acquire
1543
1544 my cntr_ready
1545 }
1546
1547# -------------------------------------------------------------------------
1548
1549 CntDisplay instproc cntr_stop {} {
1550 my instvar controller config auto
1551
1552 set val_addr [format {%02x} 16]
1553
1554 $controller usbCmd 000200${val_addr}00040000
1555
1556 set auto 0
1557 }
1558
1559# -------------------------------------------------------------------------
1560
1561 CntDisplay instproc acquire_loop {} {
1562 my instvar recs_val auto
1563
1564 my acquire
1565
1566 if {$recs_val == 0} {
1567 my cntr_stop
1568 my cntr_ready
1569 } elseif {$auto} {
1570 after 1000 [myproc acquire_loop]
1571 }
1572 }
1573
1574# -------------------------------------------------------------------------
1575
1576 CntDisplay instproc data_update args {
1577 my instvar config data
1578 usb::convertBlt $data 2 [myvar yvec]
1579
1580 ${config}.chan_frame.mean_value configure \
1581 -text [format {%.2e} [usb::integrateBlt [myvar yvec] 1]]
1582 ${config}.chan_frame.entr_value configure \
1583 -text [usb::integrateBlt [myvar yvec] 0]
1584
1585 }
1586
1587# -------------------------------------------------------------------------
1588
1589 CntDisplay instproc axis_update args {
1590 my instvar axis graph
1591 if {$axis} {
1592 $graph axis configure y -min 1 -max 1E5 -logscale yes
1593 } else {
1594 $graph axis configure y -min {} -max {} -logscale no
1595 }
1596 }
1597
1598# -------------------------------------------------------------------------
1599
1600 CntDisplay instproc acquire {} {
1601 my instvar controller config
1602 my instvar cntr cntr_val recs recs_val
1603
1604 set size 16384
1605
1606 set prefix [format {%x} 8]
1607
1608 set value [format {%08x} $size]
1609
1610 set command 0001${prefix}000000200000001[string range $value 0 3]0003[string range $value 4 7]00050000
1611
1612 $controller usbCmdReadRaw $command [expr {$size * 2}] [myvar data]
1613
1614 set prefix [format {%x} 9]
1615 set command 0001${prefix}000000200000003000400050000
1616
1617 $controller usbCmdReadHex $command 8 1 [myvar cntr]
1618
1619 set prefix [format {%x} 10]
1620 set command 0001${prefix}000000200000003000400050000
1621
1622 $controller usbCmdReadHex $command 8 1 [myvar recs]
1623 }
1624
1625# -------------------------------------------------------------------------
1626
1627 CntDisplay instproc save_data {data} {
1628
1629 set types {
1630 {{Data Files} {.dat} }
1631 {{All Files} * }
1632 }
1633
1634 set stamp [clock format [clock seconds] -format %Y%m%d_%H%M%S]
1635 set fname counts_${stamp}.dat
1636
1637 set fname [tk_getSaveFile -filetypes $types -initialfile $fname]
1638 if {[string equal $fname {}]} {
1639 return
1640 }
1641
1642 set x [catch {
1643 set fid [open $fname w+]
1644 puts $fid $data
1645 close $fid
1646 }]
1647
1648 if { $x || ![file exists $fname] || ![file isfile $fname] || ![file readable $fname] } {
1649 tk_messageBox -icon error \
1650 -message "An error occurred while writing to \"$fname\""
1651 } else {
1652 tk_messageBox -icon info \
1653 -message "File \"$fname\" written successfully"
1654 }
1655 }
1656
1657# -------------------------------------------------------------------------
1658
1659 CntDisplay instproc register {} {
1660 my save_data [join [[myvar yvec] range 0 16383] \n]
1661 }
1662
1663# -------------------------------------------------------------------------
1664
1665 Class OscDisplay -parameter {
1666 {master}
1667 {controller}
1668 }
1669
1670# -------------------------------------------------------------------------
1671
1672 OscDisplay instproc init {} {
1673 my instvar sequence data xvec yvec
1674
1675 set data {}
1676
1677 set sequence 0
1678
1679# set xvec [vector create #auto(262144)]
1680 set xvec [vector create #auto(10000)]
1681
1682 for {set i 1} {$i <= 9} {incr i} {
1683# dict set yvec $i [vector create #auto(262144)]
1684 dict set yvec $i [vector create #auto(10000)]
1685 }
1686
1687 # fill one vector for the x axis
1688# $xvec seq 0 262143
1689 $xvec seq 0 10000
1690
1691 my setup
1692
1693 next
1694 }
1695
1696# -------------------------------------------------------------------------
1697
1698 OscDisplay instproc destroy {} {
1699 next
1700 }
1701
1702# -------------------------------------------------------------------------
1703
1704 OscDisplay instproc start {} {
1705 my instvar config
1706 my instvar recs_val directory
1707
1708 set directory $::env(HOMEPATH)
1709 set recs_val 100
1710
1711 trace add variable [myvar chan] write [myproc chan_update]
1712
1713 trace add variable [myvar data] write [myproc data_update]
1714
1715 trace add variable [myvar auto] write [myproc auto_update]
1716
1717 trace add variable [myvar thrs] write [myproc thrs_update 0]
1718 trace add variable [myvar thrs_val] write [myproc thrs_update 0]
1719
1720 trace add variable [myvar recs_val] write [myproc recs_val_update]
1721
1722 trace add variable [myvar last] write [myproc last_update]
1723
1724 ${config}.chan_frame.chan1_check select
1725 ${config}.chan_frame.chan2_check select
1726 ${config}.chan_frame.chan3_check select
1727 ${config}.chan_frame.chan4_check select
1728 ${config}.chan_frame.chan5_check select
1729 ${config}.chan_frame.chan6_check select
1730
1731 ${config}.thrs_check select
1732 ${config}.thrs_field set 100
1733 }
1734
1735# -------------------------------------------------------------------------
1736
1737 OscDisplay instproc setup {} {
1738 my instvar master
1739 my instvar xvec yvec graph
1740 my instvar config
1741
1742 # create a graph widget and show a grid
1743 set graph [graph ${master}.graph -height 250 -leftmargin 80]
1744 $graph crosshairs configure -hide no -linewidth 1 -color darkblue -dashes {2 2}
1745 $graph grid configure -hide no
1746 $graph legend configure -hide yes
1747 $graph axis configure x -min 0 -max 10000
1748 $graph axis configure y -min 0 -max 4100
1749
1750# scale ${master}.last -orient horizontal -from 1 -to 27 -tickinterval 0 -showvalue no -variable [myvar last]
1751
1752 set config [frame ${master}.config -width 170]
1753
1754 frame ${config}.chan_frame -width 170
1755 legendButton ${config}.chan_frame 0 chan1 {Channel 1} [myvar chan(1)] turquoise2
1756 legendButton ${config}.chan_frame 1 chan2 {Channel 2} [myvar chan(2)] SpringGreen2
1757 legendButton ${config}.chan_frame 2 chan3 {Channel 3} [myvar chan(3)] orchid2
1758 legendButton ${config}.chan_frame 3 chan4 {Channel 4} [myvar chan(4)] orange2
1759 legendButton ${config}.chan_frame 4 chan5 {Channel 5} [myvar chan(5)] blue1 white
1760 legendButton ${config}.chan_frame 5 chan6 {Channel 6} [myvar chan(6)] gray65 white
1761 legendLabel ${config}.chan_frame 6 axisx {Time axis}
1762
1763 frame ${config}.spc1 -width 170 -height 30
1764
1765 checkbutton ${config}.auto_check -text {auto update} -variable [myvar auto]
1766
1767 frame ${config}.spc2 -width 170 -height 30
1768
1769 checkbutton ${config}.thrs_check -text threshold -variable [myvar thrs]
1770 spinbox ${config}.thrs_field -from 1 -to 4095 \
1771 -increment 5 -width 10 -textvariable [myvar thrs_val] \
1772 -validate all -vcmd {::mca::validate 4095 4 %P}
1773
1774 frame ${config}.spc3 -width 170 -height 30
1775
1776 button ${config}.acquire -text Acquire \
1777 -bg green -activebackground green -command [myproc acquire_start]
1778 button ${config}.register -text Register \
1779 -bg lightblue -activebackground lightblue -command [myproc register]
1780
1781 frame ${config}.spc4 -width 170 -height 30
1782
1783 label ${config}.recs -text {number of records}
1784 spinbox ${config}.recs_field -from 0 -to 10000 \
1785 -increment 10 -width 10 -textvariable [myvar recs_val] \
1786 -validate all -vcmd {::mca::validate 10000 5 %P}
1787
1788 frame ${config}.spc5 -width 170 -height 10
1789
1790 button ${config}.sequence -text {Start Recording} -command [myproc sequence_start] \
1791 -bg yellow -activebackground yellow
1792
1793 frame ${config}.spc6 -width 170 -height 10
1794
1795 button ${config}.recover -text {Read file} \
1796 -bg lightblue -activebackground lightblue -command [myproc recover]
1797
1798 grid ${config}.chan_frame -sticky ew
1799 grid ${config}.spc1
1800 grid ${config}.auto_check -sticky w
1801 grid ${config}.spc2
1802 grid ${config}.thrs_check -sticky w
1803 grid ${config}.thrs_field -sticky ew -pady 1 -padx 5
1804 grid ${config}.spc3
1805 grid ${config}.acquire -sticky ew -pady 3 -padx 5
1806 grid ${config}.register -sticky ew -pady 3 -padx 5
1807 grid ${config}.spc4
1808 grid ${config}.recs -sticky w -pady 1 -padx 3
1809 grid ${config}.recs_field -sticky ew -pady 1 -padx 5
1810 grid ${config}.spc5
1811 grid ${config}.sequence -sticky ew -pady 3 -padx 5
1812 grid ${config}.spc6
1813 grid ${config}.recover -sticky ew -pady 3 -padx 5
1814
1815 grid ${graph} -row 0 -column 0 -sticky news
1816 grid ${config} -row 0 -column 1
1817
1818# grid ${master}.last -row 1 -column 0 -columnspan 2 -sticky ew
1819
1820 grid rowconfigure ${master} 0 -weight 1
1821 grid columnconfigure ${master} 0 -weight 1
1822 grid columnconfigure ${master} 1 -weight 0 -minsize 120
1823
1824 grid columnconfigure ${config}.chan_frame 2 -weight 1
1825
1826 # enable zooming
1827 Blt_ZoomStack $graph
1828
1829 my crosshairs $graph
1830
1831 # create one element with data for the x and y axis, no dots
1832 $graph pen create pen1 -color turquoise3 -linewidth 2 -symbol none
1833 $graph pen create pen2 -color SpringGreen3 -linewidth 2 -symbol none
1834 $graph pen create pen3 -color orchid3 -linewidth 2 -symbol none
1835 $graph pen create pen4 -color orange3 -linewidth 2 -symbol none
1836 $graph pen create pen5 -color blue2 -linewidth 2 -symbol none
1837 $graph pen create pen6 -color gray55 -linewidth 2 -symbol none
1838
1839 $graph element create Spectrum1 -pen pen1 -xdata $xvec -ydata [dict get $yvec 1]
1840 $graph element create Spectrum2 -pen pen2 -xdata $xvec -ydata [dict get $yvec 2]
1841 $graph element create Spectrum3 -pen pen3 -xdata $xvec -ydata [dict get $yvec 3]
1842 $graph element create Spectrum4 -pen pen4 -xdata $xvec -ydata [dict get $yvec 4]
1843 $graph element create Spectrum5 -pen pen5 -xdata $xvec -ydata [dict get $yvec 5]
1844 $graph element create Spectrum6 -pen pen6 -xdata $xvec -ydata [dict get $yvec 6]
1845 }
1846
1847# -------------------------------------------------------------------------
1848
1849 OscDisplay instproc coor_update {W x y} {
1850 my instvar xvec yvec graph
1851 my instvar config
1852
1853 $W crosshairs configure -position @${x},${y}
1854
1855 set index [$W axis invtransform x $x]
1856 set index [::tcl::mathfunc::round $index]
1857 catch {
1858 ${config}.chan_frame.chan1_value configure -text [[dict get $yvec 1] index $index]
1859 ${config}.chan_frame.chan2_value configure -text [[dict get $yvec 2] index $index]
1860 ${config}.chan_frame.chan3_value configure -text [[dict get $yvec 3] index $index]
1861 ${config}.chan_frame.chan4_value configure -text [[dict get $yvec 4] index $index]
1862 ${config}.chan_frame.chan5_value configure -text [[dict get $yvec 5] index $index]
1863 ${config}.chan_frame.chan6_value configure -text [[dict get $yvec 6] index $index]
1864 ${config}.chan_frame.axisx_value configure -text ${index}.0
1865 }
1866 }
1867# -------------------------------------------------------------------------
1868
1869 OscDisplay instproc crosshairs {graph} {
1870 set method [myproc coor_update]
1871 bind $graph <Motion> [list [self] coor_update %W %x %y]
1872 bind $graph <Leave> {
1873 %W crosshairs off
1874 }
1875 bind $graph <Enter> {
1876 %W crosshairs on
1877 }
1878 }
1879
1880# -------------------------------------------------------------------------
1881
1882 OscDisplay instproc chan_update {name key op} {
1883 my instvar config graph chan
1884
1885 if {$chan(${key})} {
1886 $graph pen configure pen${key} -linewidth 2
1887 } else {
1888 $graph pen configure pen${key} -linewidth 0
1889 }
1890 }
1891
1892# -------------------------------------------------------------------------
1893
1894 OscDisplay instproc recs_val_update args {
1895 my instvar recs_val
1896 if {[string equal $recs_val {}]} {
1897 set recs_val 0
1898 }
1899 }
1900
1901# -------------------------------------------------------------------------
1902
1903 OscDisplay instproc last_update args {
1904 my instvar graph last
1905
1906 set first [expr {$last - 1}]
1907
1908 $graph axis configure x -min ${first}0000 -max ${last}0000
1909 }
1910
1911# -------------------------------------------------------------------------
1912
1913 OscDisplay instproc thrs_update {reset args} {
1914 my instvar controller config thrs thrs_val
1915
1916 if {[string equal $thrs_val {}]} {
1917 set thrs_val 0
1918 }
1919
1920 if {$thrs} {
1921 ${config}.thrs_field configure -state normal
1922 set value [format %03x $thrs_val]
1923 } else {
1924 ${config}.thrs_field configure -state disabled
1925 set value 000
1926 }
1927
1928 set command {}
1929 if {$reset} {
1930 append command 0002000500041${value}
1931 }
1932 append command 0002000500040${value}
1933
1934 $controller usbCmd $command
1935 }
1936
1937# -------------------------------------------------------------------------
1938
1939 OscDisplay instproc data_update args {
1940 my instvar data yvec
1941 my instvar graph chan waiting sequence auto
1942
1943 usb::convertOsc $data $yvec
1944
1945 foreach {key value} [array get chan] {
1946 $graph pen configure pen${key} -dashes 0
1947 }
1948
1949 set waiting 0
1950
1951 if {$sequence} {
1952 my sequence_register
1953 } elseif {$auto} {
1954 after 1000 [myproc acquire_start]
1955 }
1956 }
1957
1958# -------------------------------------------------------------------------
1959
1960 OscDisplay instproc acquire_start {} {
1961 my instvar graph chan controller waiting
1962
1963 foreach {key value} [array get chan] {
1964 $graph pen configure pen${key} -dashes dot
1965 }
1966
1967 # restart
1968 my thrs_update 1
1969
1970 set waiting 1
1971
1972 after 200 [myproc acquire_loop]
1973 }
1974
1975# -------------------------------------------------------------------------
1976
1977 OscDisplay instproc acquire_loop {} {
1978 my instvar controller waiting
1979
1980# set size 262144
1981 set size 10000
1982
1983 set value [format {%08x} [expr {$size * 4}]]
1984
1985 set command 00011000000200000001[string range $value 0 3]0003[string range $value 4 7]00050000
1986
1987 $controller usbCmdReadRaw $command [expr {$size * 8}] [myvar data]
1988
1989 if {$waiting} {
1990 after 200 [myproc acquire_loop]
1991 }
1992 }
1993
1994# -------------------------------------------------------------------------
1995
1996 OscDisplay instproc auto_update args {
1997 my instvar config auto
1998
1999 if {$auto} {
2000 ${config}.recs_field configure -state disabled
2001 ${config}.sequence configure -state disabled
2002 ${config}.acquire configure -state disabled
2003 ${config}.register configure -state disabled
2004 ${config}.recover configure -state disabled
2005
2006 my acquire_start
2007 } else {
2008 ${config}.recs_field configure -state normal
2009 ${config}.sequence configure -state active
2010 ${config}.acquire configure -state active
2011 ${config}.register configure -state active
2012 ${config}.recover configure -state active
2013 }
2014 }
2015
2016# -------------------------------------------------------------------------
2017
2018 OscDisplay instproc save_data {fname} {
2019 my instvar data
2020
2021 set fid [open $fname w+]
2022 fconfigure $fid -translation binary -encoding binary
2023
2024# puts -nonewline $fid [binary format "H*iH*" "1f8b0800" [clock seconds] "0003"]
2025# puts -nonewline $fid [zlib deflate $data]
2026 puts -nonewline $fid $data
2027# puts -nonewline $fid [binary format i [zlib crc32 $data]]
2028# puts -nonewline $fid [binary format i [string length $data]]
2029
2030 close $fid
2031 }
2032
2033# -------------------------------------------------------------------------
2034
2035 OscDisplay instproc open_data {} {
2036 set types {
2037 {{Data Files} {.dat} }
2038 {{All Files} * }
2039 }
2040
2041 set fname [tk_getOpenFile -filetypes $types]
2042 if {[string equal $fname {}]} {
2043 return
2044 }
2045
2046 set x [catch {
2047 set fid [open $fname r+]
2048 fconfigure $fid -translation binary -encoding binary
2049# set size [file size $fname]
2050# seek $fid 10
2051# my set data [zlib inflate [read $fid [expr {$size - 18}]]]
2052 my set data [read $fid]
2053 close $fid
2054 }]
2055
2056 if { $x || ![file exists $fname] || ![file isfile $fname] || ![file readable $fname] } {
2057 tk_messageBox -icon error \
2058 -message "An error occurred while reading \"$fname\""
2059 } else {
2060 tk_messageBox -icon info \
2061 -message "File \"$fname\" read successfully"
2062 }
2063 }
2064
2065# -------------------------------------------------------------------------
2066
2067 OscDisplay instproc register {} {
2068 set types {
2069 {{Data Files} {.dat} }
2070 {{All Files} * }
2071 }
2072
2073 set stamp [clock format [clock seconds] -format %Y%m%d_%H%M%S]
2074 set fname oscillogram_${stamp}.dat
2075
2076 set fname [tk_getSaveFile -filetypes $types -initialfile $fname]
2077 if {[string equal $fname {}]} {
2078 return
2079 }
2080
2081 if {[catch {my save_data $fname} result]} {
2082 tk_messageBox -icon error \
2083 -message "An error occurred while writing to \"$fname\""
2084 } else {
2085 tk_messageBox -icon info \
2086 -message "File \"$fname\" written successfully"
2087 }
2088 }
2089
2090# -------------------------------------------------------------------------
2091
2092 OscDisplay instproc recover {} {
2093 my open_data
2094 }
2095
2096# -------------------------------------------------------------------------
2097
2098 OscDisplay instproc sequence_start {} {
2099 my instvar config recs_val recs_bak directory counter sequence
2100
2101 set counter 1
2102 if {$counter > $recs_val} {
2103 return
2104 }
2105
2106 set directory [tk_chooseDirectory -initialdir $directory -title {Choose a directory}]
2107
2108 if {[string equal $directory {}]} {
2109 return
2110 }
2111
2112 ${config}.recs_field configure -state disabled
2113 ${config}.sequence configure -text {Stop Recording} -command [myproc sequence_stop]
2114 ${config}.acquire configure -state disabled
2115 ${config}.register configure -state disabled
2116 ${config}.recover configure -state disabled
2117
2118 set recs_bak $recs_val
2119
2120 set sequence 1
2121
2122 my acquire_start
2123 }
2124
2125# -------------------------------------------------------------------------
2126
2127 OscDisplay instproc sequence_register {} {
2128 my instvar config recs_val recs_bak directory counter
2129
2130 set fname [file join $directory oscillogram_$counter.dat]
2131
2132 my incr counter
2133
2134 if {[catch {my save_data $fname} result]} {
2135 tk_messageBox -icon error \
2136 -message "An error occurred while writing to \"$fname\""
2137 } elseif {$counter <= $recs_bak} {
2138 set recs_val [expr {$recs_bak - $counter}]
2139 my acquire_start
2140 return
2141 }
2142
2143 my sequence_stop
2144 }
2145
2146# -------------------------------------------------------------------------
2147
2148 OscDisplay instproc sequence_stop {} {
2149 my instvar config recs_val recs_bak sequence
2150
2151 set sequence 0
2152
2153 set recs_val $recs_bak
2154
2155 ${config}.recs_field configure -state normal
2156 ${config}.sequence configure -text {Start Recording} -command [myproc sequence_start]
2157 ${config}.acquire configure -state active
2158 ${config}.register configure -state active
2159 ${config}.recover configure -state active
2160 }
2161
2162# -------------------------------------------------------------------------
2163
2164 namespace export MuxDisplay
2165 namespace export HstDisplay
2166 namespace export CntDisplay
2167 namespace export OscDisplay
2168}
2169
2170set notebook [::blt::tabnotebook .notebook -borderwidth 1 -selectforeground black -side bottom]
2171
2172grid ${notebook} -row 0 -column 0 -sticky news -pady 5
2173
2174grid rowconfigure . 0 -weight 1
2175grid columnconfigure . 0 -weight 1
2176
2177::mca::UsbController usb
2178usb usbCmd 00000000
2179
2180set window [frame ${notebook}.spi]
2181$notebook insert end -text "DAC & ADC control" -window $window -fill both
2182::mca::SpiDisplay spi -master $window -controller usb
2183
2184set window [frame ${notebook}.mux]
2185$notebook insert end -text "Interconnect" -window $window -fill both
2186::mca::MuxDisplay mux -master $window -controller usb
2187
2188set window [frame ${notebook}.ept]
2189$notebook insert end -text "Oscilloscope" -window $window -fill both
2190::mca::OscDisplay osc -master $window -controller usb
2191
2192update
2193
2194spi start
2195
2196mux start
2197
2198osc start
2199
2200spi adc_reset
Note: See TracBrowser for help on using the repository browser.