Index: /trunk/MultiChannelUSB/UserInterface.tcl
===================================================================
--- /trunk/MultiChannelUSB/UserInterface.tcl	(revision 76)
+++ /trunk/MultiChannelUSB/UserInterface.tcl	(revision 77)
@@ -28,4 +28,132 @@
 # -------------------------------------------------------------------------
 
+    Class CfgDisplay -parameter {
+        {master}
+    }
+
+# -------------------------------------------------------------------------
+
+    CfgDisplay instproc init {} {
+
+        my reset
+
+        my setup
+
+        next
+    }
+
+# -------------------------------------------------------------------------
+
+    CfgDisplay instproc destroy {} {
+        next
+    }
+
+# -------------------------------------------------------------------------
+
+    CfgDisplay instproc reset {} {
+    }
+
+# -------------------------------------------------------------------------
+
+    CfgDisplay instproc start {} {
+        my instvar config
+
+        trace add variable [myvar dac1] write [myproc dac1_update]
+        trace add variable [myvar dac2] write [myproc dac2_update]
+        trace add variable [myvar polar] write [myproc polar_update]
+
+        ${config(1)}.scale set 0
+        ${config(2)}.scale set 0
+
+        ${config(3)}.polar1 select
+        ${config(3)}.polar2 select
+    }
+
+# -------------------------------------------------------------------------
+
+    CfgDisplay instproc setup {} {
+        my instvar number master
+        my instvar config
+
+        set config(1) [labelframe ${master}.cfg1 -text {DAC 20}]
+        set config(2) [labelframe ${master}.cfg2 -text {DAC 24}]
+        set config(3) [labelframe ${master}.cfg3 -text {polarity inversion}]
+
+        frame ${config(1)}.limits
+        label ${config(1)}.limits.min -text {0V}
+        label ${config(1)}.limits.max -text {-3.3V}
+        scale ${config(1)}.scale -orient horizontal -from 0 -to 4095 -tickinterval 500 -variable [myvar dac1]
+
+        frame ${config(2)}.limits
+        label ${config(2)}.limits.min -text {0V}
+        label ${config(2)}.limits.max -text {-3.3V}
+        scale ${config(2)}.scale -orient horizontal -from 0 -to 4095 -tickinterval 500 -variable [myvar dac2]
+
+        checkbutton ${config(3)}.polar1 -text {channel 1} -variable [myvar polar(1)]
+        checkbutton ${config(3)}.polar2 -text {channel 2} -variable [myvar polar(2)]
+        checkbutton ${config(3)}.polar3 -text {channel 3} -variable [myvar polar(3)]
+
+        pack ${config(1)} ${config(2)} -side top -expand yes -fill x -padx 10
+        pack ${config(3)} -side top -expand yes -fill x -padx 10
+
+        pack ${config(1)}.limits.min -anchor w -side left -padx 13
+        pack ${config(1)}.limits.max -anchor e -side right -padx 5
+        pack ${config(1)}.limits ${config(1)}.scale -side top -expand yes -fill x
+
+        pack ${config(2)}.limits.min -anchor w -side left -padx 13
+        pack ${config(2)}.limits.max -anchor e -side right -padx 5
+        pack ${config(2)}.limits ${config(2)}.scale -side top -expand yes -fill x
+
+        pack ${config(3)}.polar1 -side left -expand yes -fill x -pady 10
+        pack ${config(3)}.polar2 -side left -expand yes -fill x -pady 10
+        pack ${config(3)}.polar3 -side left -expand yes -fill x -pady 10
+
+    }
+
+# -------------------------------------------------------------------------
+
+    CfgDisplay instproc dac1_update args {
+        my instvar dac1
+
+        set value [format {%03x} $dac1]
+        set command 0005012000050030000500[string range $value 0 1]000502[string index $value 2]0
+
+        my send_data [usb::convert $command]
+    }
+
+# -------------------------------------------------------------------------
+
+    CfgDisplay instproc dac2_update args {
+        my instvar dac2
+
+        set value [format {%03x} $dac2]
+        set command 0005012400050030000500[string range $value 0 1]000502[string index $value 2]0
+
+        my send_data [usb::convert $command]
+    }
+
+# -------------------------------------------------------------------------
+
+    CfgDisplay instproc polar_update args {
+        my instvar polar
+
+        set value [format {0%x%x%x} $polar(3) $polar(2) $polar(1)]
+
+        my send_data [usb::convert 000A${value}]
+    }
+
+# -------------------------------------------------------------------------
+
+    CfgDisplay instproc send_data {data} {
+        global usb_handle
+
+        if {[catch {$usb_handle writeRaw $data} result]} {
+            puts {Error during write}
+            puts $result
+        }
+    }
+
+# -------------------------------------------------------------------------
+
     Class OscDisplay -parameter {
         {number}
@@ -94,6 +222,6 @@
         my instvar config auto thrs thrs_val disp_val trig_val
 
-        my set restart_command [usb::convert 0001000${number}00000000]
-        my set acquire_command [usb::convert 0002000${number}00000000]
+        my set restart_command [usb::convert 0001000${number}]
+        my set acquire_command [usb::convert 0002000${number}]
 
         # create a graph widget and show a grid
@@ -103,4 +231,5 @@
         $graph legend configure -hide yes
         $graph axis configure x -min 0 -max 1024
+        $graph axis configure y -min 0 -max 4100
 
         set config [frame ${master}.config]
@@ -172,4 +301,8 @@
         grid ${config} -row 0 -column 1
 
+        grid rowconfigure ${master} 0 -weight 1
+        grid columnconfigure ${master} 0 -weight 1
+        grid columnconfigure ${master} 1 -weight 0 -minsize 80
+
         # enable zooming
         Blt_ZoomStack $graph
@@ -214,5 +347,5 @@
         } else {
             ${config}.thrs_field configure -state disabled
-            my send_data [usb::convert ${val_addr}000000000000]
+            my send_data [usb::convert ${val_addr}0000]
         }
     }
@@ -226,5 +359,5 @@
         set value [format %04x $thrs_val]
 
-        my send_data [usb::convert ${val_addr}${value}00000000]
+        my send_data [usb::convert ${val_addr}${value}]
     }
 
@@ -247,21 +380,21 @@
             data {
                 set disp_mux 0
-                my send_data [usb::convert ${mux_addr}[my mux]00000000]
+                my send_data [usb::convert ${mux_addr}[my mux]]
             }
             uwt1 {
                 set disp_mux 1
-                my send_data [usb::convert ${mux_addr}[my mux]00000000]
+                my send_data [usb::convert ${mux_addr}[my mux]]
             }
             uwt2 {
                 set disp_mux 2
-                my send_data [usb::convert ${mux_addr}[my mux]00000000]
+                my send_data [usb::convert ${mux_addr}[my mux]]
             }
             uwt3 {
                 set disp_mux 3
-                my send_data [usb::convert ${mux_addr}[my mux]00000000]
+                my send_data [usb::convert ${mux_addr}[my mux]]
             }
             base {
                 set disp_mux 4
-                my send_data [usb::convert ${mux_addr}[my mux]00000000]
+                my send_data [usb::convert ${mux_addr}[my mux]]
             }
         }
@@ -278,21 +411,21 @@
             data {
                 set trig_mux 0
-                my send_data [usb::convert ${mux_addr}[my mux]00000000]
+                my send_data [usb::convert ${mux_addr}[my mux]]
             }
             uwt1 {
                 set trig_mux 1
-                my send_data [usb::convert ${mux_addr}[my mux]00000000]
+                my send_data [usb::convert ${mux_addr}[my mux]]
             }
             uwt2 {
                 set trig_mux 2
-                my send_data [usb::convert ${mux_addr}[my mux]00000000]
+                my send_data [usb::convert ${mux_addr}[my mux]]
             }
             uwt3 {
                 set trig_mux 3
-                my send_data [usb::convert ${mux_addr}[my mux]00000000]
+                my send_data [usb::convert ${mux_addr}[my mux]]
             }
             base {
                 set trig_mux 4
-                my send_data [usb::convert ${mux_addr}[my mux]00000000]
+                my send_data [usb::convert ${mux_addr}[my mux]]
             }
         }
@@ -448,6 +581,6 @@
         my instvar config auto thrs thrs_val base base_typ base_val
 
-        my set restart_command [usb::convert 0001001${number}00000000]
-        my set acquire_command [usb::convert 0002001${number}00000000]
+        my set restart_command [usb::convert 0001001${number}]
+        my set acquire_command [usb::convert 0002001${number}]
 
         # create a graph widget and show a grid
@@ -510,4 +643,8 @@
         grid ${config} -row 0 -column 1
 
+        grid rowconfigure ${master} 0 -weight 1
+        grid columnconfigure ${master} 0 -weight 1
+        grid columnconfigure ${master} 1 -weight 0 -minsize 80
+
         # enable zooming
         Blt_ZoomStack $graph
@@ -555,8 +692,8 @@
         if {$peak} {
             set peak_mux 1
-            my send_data [usb::convert ${mux_addr}[my mux]00000000]
+            my send_data [usb::convert ${mux_addr}[my mux]]
         } else {
             set peak_mux 0
-            my send_data [usb::convert ${mux_addr}[my mux]00000000]
+            my send_data [usb::convert ${mux_addr}[my mux]]
         }
     }
@@ -575,5 +712,5 @@
         } else {
             ${config}.thrs_field configure -state disabled
-            my send_data [usb::convert ${val_addr}000000000000]
+            my send_data [usb::convert ${val_addr}0000]
         }
     }
@@ -587,5 +724,5 @@
         set value [format %04x $thrs_val]
 
-        my send_data [usb::convert ${val_addr}${value}00000000]
+        my send_data [usb::convert ${val_addr}${value}]
     }
 
@@ -607,5 +744,5 @@
             ${config}.base_field configure -state disabled
             set base_mux 0
-            my send_data [usb::convert ${mux_addr}[my mux]00000000${val_addr}000000000000]
+            my send_data [usb::convert ${mux_addr}[my mux]${val_addr}0000]
         }
     }
@@ -624,10 +761,10 @@
                 ${config}.base_field configure -state disabled
                 set base_mux 1
-                my send_data [usb::convert ${mux_addr}[my mux]00000000]
+                my send_data [usb::convert ${mux_addr}[my mux]]
             }
             const {
                 ${config}.base_field configure -state normal
                 set base_mux 0
-                my send_data [usb::convert ${mux_addr}[my mux]00000000${val_addr}${value}00000000]
+                my send_data [usb::convert ${mux_addr}[my mux]${val_addr}${value}]
             }
         }
@@ -642,5 +779,5 @@
         set value [format %04x $base_val]
 
-        my send_data [usb::convert ${val_addr}${value}00000000]
+        my send_data [usb::convert ${val_addr}${value}]
     }
 
@@ -725,25 +862,26 @@
     namespace export HstDisplay
     namespace export OscDisplay
+    namespace export CfgDisplay
 }
 
-set notebook [::blt::tabnotebook .notebook -side bottom]
+set notebook [::blt::tabnotebook .notebook -selectforeground black -side bottom]
 
 pack $notebook -expand 1 -fill both
 
-set window [frame ${notebook}.hst_2]
-$notebook insert end -text "Histogram" -window $window -fill both
-::mca::HstDisplay hst_2 -number 2 -master $window
-
-grid rowconfigure $window 0 -weight 1
-grid columnconfigure $window 0 -weight 1
-grid columnconfigure $window 1 -weight 0 -minsize 80
-
-set window [frame ${notebook}.osc_2]
-$notebook insert end -text "Pulse shape" -window $window -fill both
-::mca::OscDisplay osc_2 -number 2 -master $window
-
-grid rowconfigure $window 0 -weight 1
-grid columnconfigure $window 0 -weight 1
-grid columnconfigure $window 1 -weight 0 -minsize 80
+foreach i {0 1 2} {
+    set channel [expr $i + 1]
+
+    set window [frame ${notebook}.hst_$i]
+    $notebook insert end -text "Histogram $channel" -window $window -fill both
+    ::mca::HstDisplay hst_$i -number $i -master $window
+
+    set window [frame ${notebook}.osc_$i]
+    $notebook insert end -text "Pulse shape $channel" -window $window -fill both
+    ::mca::OscDisplay osc_$i -number $i -master $window
+}
+
+set window [frame ${notebook}.cfg]
+$notebook insert end -text "Configuration" -window $window -fill both
+::mca::CfgDisplay cfg -master $window
 
 set usb_handle {}
@@ -755,18 +893,18 @@
 }
 
-hst_2 restart
-osc_2 restart
-
-hst_2 start
-osc_2 start
-
-# configure polarity
-# set polarity_command [usb::convert 000A011100000000]
-set polarity_command [usb::convert 000A001100000000]
-if {[catch {$usb_handle writeRaw $polarity_command} result]} {
+# cfg reset
+set reset_command [usb::convert 00000000]
+if {[catch {$usb_handle writeRaw $reset_command} result]} {
     puts {Error during write}
     puts $result
 }
 
-hst_2 restart
-osc_2 restart
+cfg start
+
+foreach i {0 1 2} {
+    hst_$i start
+    osc_$i start
+
+    hst_$i restart
+    osc_$i restart
+}
