Index: trunk/MultiChannelUSB/UserInterface.tcl
===================================================================
--- trunk/MultiChannelUSB/UserInterface.tcl	(revision 84)
+++ trunk/MultiChannelUSB/UserInterface.tcl	(revision 85)
@@ -5,5 +5,7 @@
 package require usb
 
-wm minsize . 800 600
+package require zlib
+
+wm minsize . 1000 700
 
 namespace eval ::mca {
@@ -14,8 +16,8 @@
     namespace import ::blt::tabnotebook
 
-    proc validate {value} {
-        if {![regexp {^[1-9][0-9]*$} $value]} {
+    proc validate {max value} {
+        if {![regexp {^[0-9]*$} $value]} {
             return 0
-        } elseif {$value > 4095} {
+        } elseif {$value > $max} {
             return 0
         } elseif {[string length $value] > 4} {
@@ -28,5 +30,54 @@
 # -------------------------------------------------------------------------
 
-    Class CfgDisplay -parameter {
+    Class Display
+
+# -------------------------------------------------------------------------
+
+    Display instproc usbCmd {command} {
+        global usb_handle
+
+        if {[catch {$usb_handle writeRaw [usb::convert $command]} result]} {
+            puts {Error during write}
+            puts $result
+        }
+    }
+
+# -------------------------------------------------------------------------
+
+    Display instproc usbCmdRead {command width size} {
+        global usb_handle
+
+        my usbCmd $command
+
+        set usb_data {}
+        if {[catch {$usb_handle readHex $width $size} result]} {
+            puts {Error during read}
+            puts $result
+            set result {}
+        }
+
+        my set data $result
+    }
+
+# -------------------------------------------------------------------------
+
+    Display instproc usbCmdReadEpt {command size} {
+        global usb_handle
+
+        my usbCmd $command
+
+        set usb_data {}
+        if {[catch {$usb_handle readEpt $size} result]} {
+            puts {Error during read}
+            puts $result
+            set result {}
+        }
+
+        my set data $result
+    }
+
+# -------------------------------------------------------------------------
+
+    Class CfgDisplay -superclass Display -parameter {
         {master}
     }
@@ -116,5 +167,5 @@
         set command 0005012000050030000500[string range $value 0 1]000502[string index $value 2]0
 
-        my send_data [usb::convert $command]
+        my usbCmd $command
     }
 
@@ -127,5 +178,5 @@
         set command 0005012400050030000500[string range $value 0 1]000502[string index $value 2]0
 
-        my send_data [usb::convert $command]
+        my usbCmd $command
     }
 
@@ -137,21 +188,10 @@
         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 {
+        my usbCmd 000A${value}
+    }
+
+# -------------------------------------------------------------------------
+
+    Class OscDisplay -superclass Display -parameter {
         {number}
         {master}
@@ -199,4 +239,5 @@
         set disp_mux 2
 
+        trace add variable [myvar data] write [myproc data_update]
         trace add variable [myvar auto] write [myproc auto_update]
         trace add variable [myvar thrs] write [myproc thrs_update]
@@ -207,5 +248,5 @@
         ${config}.auto_check select
         ${config}.thrs_check select
-        ${config}.thrs_field set 60
+        ${config}.thrs_field set 1278
         ${config}.disp_uwt2 select
         ${config}.trig_uwt2 select
@@ -218,7 +259,4 @@
         my instvar data xvec yvec
         my instvar config auto thrs thrs_val disp_val trig_val
-
-        my set restart_command [usb::convert 0001000${number}]
-        my set acquire_command [usb::convert 0002000${number}]
 
         # create a graph widget and show a grid
@@ -239,5 +277,5 @@
         spinbox ${config}.thrs_field -from 1 -to 4095 \
             -increment 5 -width 10 -textvariable [myvar thrs_val] \
-            -validate all -vcmd {::mca::validate %P}
+            -validate all -vcmd {::mca::validate 4095 %P}
 
         frame ${config}.spc2 -width 10 -height 10
@@ -313,4 +351,11 @@
 # -------------------------------------------------------------------------
 
+    OscDisplay instproc data_update args {
+        my instvar data yvec
+        $yvec set $data
+    }
+
+# -------------------------------------------------------------------------
+
     OscDisplay instproc auto_update args {
         my instvar config auto after_handle
@@ -344,5 +389,5 @@
         } else {
             ${config}.thrs_field configure -state disabled
-            my send_data [usb::convert ${val_addr}0000]
+            my usbCmd ${val_addr}0000
         }
     }
@@ -353,8 +398,12 @@
         my instvar config number thrs_val
 
+        if {[string equal $thrs_val {}]} {
+            set thrs_val 0
+        }
+
         set val_addr [format %04x [expr {17 + ${number}}]]
         set value [format %04x $thrs_val]
 
-        my send_data [usb::convert ${val_addr}${value}]
+        my usbCmd ${val_addr}${value}
     }
 
@@ -377,21 +426,21 @@
             data {
                 set disp_mux 0
-                my send_data [usb::convert ${mux_addr}[my mux]]
+                my usbCmd ${mux_addr}[my mux]
             }
             uwt1 {
                 set disp_mux 1
-                my send_data [usb::convert ${mux_addr}[my mux]]
+                my usbCmd ${mux_addr}[my mux]
             }
             uwt2 {
                 set disp_mux 2
-                my send_data [usb::convert ${mux_addr}[my mux]]
+                my usbCmd ${mux_addr}[my mux]
             }
             uwt3 {
                 set disp_mux 3
-                my send_data [usb::convert ${mux_addr}[my mux]]
+                my usbCmd ${mux_addr}[my mux]
             }
             base {
                 set disp_mux 4
-                my send_data [usb::convert ${mux_addr}[my mux]]
+                my usbCmd ${mux_addr}[my mux]
             }
         }
@@ -408,21 +457,21 @@
             data {
                 set trig_mux 0
-                my send_data [usb::convert ${mux_addr}[my mux]]
+                my usbCmd ${mux_addr}[my mux]
             }
             uwt1 {
                 set trig_mux 1
-                my send_data [usb::convert ${mux_addr}[my mux]]
+                my usbCmd ${mux_addr}[my mux]
             }
             uwt2 {
                 set trig_mux 2
-                my send_data [usb::convert ${mux_addr}[my mux]]
+                my usbCmd ${mux_addr}[my mux]
             }
             uwt3 {
                 set trig_mux 3
-                my send_data [usb::convert ${mux_addr}[my mux]]
+                my usbCmd ${mux_addr}[my mux]
             }
             base {
                 set trig_mux 4
-                my send_data [usb::convert ${mux_addr}[my mux]]
+                my usbCmd ${mux_addr}[my mux]
             }
         }
@@ -452,4 +501,18 @@
 # -------------------------------------------------------------------------
 
+    OscDisplay instproc acquire {} {
+        my instvar number
+        my usbCmdRead 0002000${number} 2 1024
+   }
+
+# -------------------------------------------------------------------------
+
+    OscDisplay instproc restart {} {
+        my instvar number
+        my usbCmd 0001000${number}
+    }
+
+# -------------------------------------------------------------------------
+
     OscDisplay instproc register {} {
         my save_data [my set data]
@@ -458,45 +521,6 @@
 # -------------------------------------------------------------------------
 
-    OscDisplay instproc send_data {data} {
-        global usb_handle
-
-        if {[catch {$usb_handle writeRaw $data} result]} {
-            puts {Error during write}
-            puts $result
-        }
-    }
-
-# -------------------------------------------------------------------------
-
-    OscDisplay instproc restart {} {
-        my instvar restart_command
-        my send_data $restart_command
-    }
-
-# -------------------------------------------------------------------------
-
-    OscDisplay instproc acquire {} {
-        global usb_handle
-        my instvar xvec yvec data
-        my instvar acquire_command
-
-        my send_data $acquire_command
-
-        set usb_data {}
-        if {[catch {$usb_handle readHex 2 1024} usb_data]} {
-            puts {Error during read}
-            puts $usb_data
-            set usb_data {}
-        }
-
-        set data $usb_data
-
-        $yvec set $usb_data
-    }
-
-# -------------------------------------------------------------------------
-
     OscDisplay instproc acquire_restart_loop {} {
-        my instvar after_handle
+        my instvar number after_handle
 
         my acquire
@@ -508,5 +532,5 @@
 # -------------------------------------------------------------------------
 
-    Class HstDisplay -parameter {
+    Class HstDisplay -superclass Display -parameter {
         {number}
         {master}
@@ -554,4 +578,6 @@
         set peak_mux 1
 
+        trace add variable [myvar axis] write [myproc axis_update]
+        trace add variable [myvar data] write [myproc data_update]
         trace add variable [myvar auto] write [myproc auto_update]
         trace add variable [myvar peak] write [myproc peak_update]
@@ -562,11 +588,14 @@
         trace add variable [myvar base_val] write [myproc base_val_update]
 
+        ${config}.axis_check deselect
         ${config}.auto_check select
         ${config}.peak_check select
+
         ${config}.thrs_check select
-        ${config}.thrs_field set 10
-        ${config}.base_check select
+        ${config}.thrs_field set 1278
+
         ${config}.base_const select
         ${config}.base_field set 35
+        ${config}.base_check deselect
     }
 
@@ -575,9 +604,6 @@
     HstDisplay instproc setup {} {
         my instvar number master
-        my instvar data xvec yvec
+        my instvar data xvec yvec graph
         my instvar config auto thrs thrs_val base base_typ base_val
-
-        my set restart_command [usb::convert 0001001${number}]
-        my set acquire_command [usb::convert 0002001${number}]
 
         # create a graph widget and show a grid
@@ -590,18 +616,22 @@
         set config [frame ${master}.config]
 
+        checkbutton ${config}.axis_check -text {log scale} -variable [myvar axis]
+
+        frame ${config}.spc1 -width 10 -height 10
+
         checkbutton ${config}.auto_check -text {auto update} -variable [myvar auto]
 
-        frame ${config}.spc1 -width 10 -height 10
+        frame ${config}.spc2 -width 10 -height 10
 
         checkbutton ${config}.peak_check -text {peak detect} -variable [myvar peak]
 
-        frame ${config}.spc2 -width 10 -height 10
+        frame ${config}.spc3 -width 10 -height 10
 
         checkbutton ${config}.thrs_check -text threshold -variable [myvar thrs]
         spinbox ${config}.thrs_field -from 1 -to 4095 \
             -increment 5 -width 10 -textvariable [myvar thrs_val] \
-            -validate all -vcmd {::mca::validate %P}
-
-        frame ${config}.spc3 -width 10 -height 10
+            -validate all -vcmd {::mca::validate 4095 %P}
+
+        frame ${config}.spc4 -width 10 -height 10
 
         checkbutton ${config}.base_check -text baseline -variable [myvar base]
@@ -610,7 +640,7 @@
         spinbox ${config}.base_field -from 1 -to 4095 \
             -increment 5 -width 10 -textvariable [myvar base_val] \
-            -validate all -vcmd {::mca::validate %P}
-
-        frame ${config}.spc4 -width 10 -height 10
+            -validate all -vcmd {::mca::validate 4095 %P}
+
+        frame ${config}.spc5 -width 10 -height 10
 
         button ${config}.acquire -text Acquire \
@@ -621,16 +651,18 @@
             -bg lightblue -activebackground lightblue -command [myproc register]
 
+        grid ${config}.axis_check -sticky w
+        grid ${config}.spc1
         grid ${config}.auto_check -sticky w
-        grid ${config}.spc1
+        grid ${config}.spc2
         grid ${config}.peak_check -sticky w
-        grid ${config}.spc2
+        grid ${config}.spc3
         grid ${config}.thrs_check -sticky w
         grid ${config}.thrs_field -sticky ew -pady 1 -padx 5
-        grid ${config}.spc3
+        grid ${config}.spc4
         grid ${config}.base_check -sticky w
         grid ${config}.base_auto -sticky w
         grid ${config}.base_const -sticky w
         grid ${config}.base_field -sticky ew -pady 1 -padx 5
-        grid ${config}.spc4
+        grid ${config}.spc5
         grid ${config}.acquire -sticky ew -pady 3 -padx 5
         grid ${config}.restart -sticky ew -pady 3 -padx 5
@@ -651,4 +683,22 @@
         # create one element with data for the x and y axis, no dots
         $graph element create Spectrum1 -symbol none -smooth step -xdata $xvec -ydata $yvec
+    }
+
+# -------------------------------------------------------------------------
+
+    HstDisplay instproc axis_update args {
+        my instvar axis graph
+        if {$axis} {
+            $graph axis configure y -min 1 -max 1E10 -logscale yes
+        } else {
+            $graph axis configure y -min {} -max {} -logscale no
+        }
+    }
+
+# -------------------------------------------------------------------------
+
+    HstDisplay instproc data_update args {
+        my instvar data yvec
+        $yvec set $data
     }
 
@@ -689,8 +739,8 @@
         if {$peak} {
             set peak_mux 1
-            my send_data [usb::convert ${mux_addr}[my mux]]
+            my usbCmd ${mux_addr}[my mux]
         } else {
             set peak_mux 0
-            my send_data [usb::convert ${mux_addr}[my mux]]
+            my usbCmd ${mux_addr}[my mux]
         }
     }
@@ -702,5 +752,4 @@
 
         set val_addr [format %04x [expr {14 + ${number}}]]
-        set value [format %04x $thrs_val]
 
         if {$thrs} {
@@ -709,5 +758,5 @@
         } else {
             ${config}.thrs_field configure -state disabled
-            my send_data [usb::convert ${val_addr}0000]
+            my usbCmd ${val_addr}0000
         }
     }
@@ -718,8 +767,12 @@
         my instvar config number thrs_val
 
+        if {[string equal $thrs_val {}]} {
+            set thrs_val 0
+        }
+
         set val_addr [format %04x [expr {14 + ${number}}]]
         set value [format %04x $thrs_val]
 
-        my send_data [usb::convert ${val_addr}${value}]
+        my usbCmd ${val_addr}${value}
     }
 
@@ -741,5 +794,5 @@
             ${config}.base_field configure -state disabled
             set base_mux 0
-            my send_data [usb::convert ${mux_addr}[my mux]${val_addr}0000]
+            my usbCmd ${mux_addr}[my mux]${val_addr}0000
         }
     }
@@ -758,10 +811,10 @@
                 ${config}.base_field configure -state disabled
                 set base_mux 1
-                my send_data [usb::convert ${mux_addr}[my mux]]
+                my usbCmd ${mux_addr}[my mux]
             }
             const {
                 ${config}.base_field configure -state normal
                 set base_mux 0
-                my send_data [usb::convert ${mux_addr}[my mux]${val_addr}${value}]
+                my usbCmd ${mux_addr}[my mux]${val_addr}${value}
             }
         }
@@ -773,8 +826,26 @@
         my instvar number base_val
 
+        if {[string equal $base_val {}]} {
+            set base_val 0
+        }
+
         set val_addr [format %04x [expr {11 + ${number}}]]
         set value [format %04x $base_val]
 
-        my send_data [usb::convert ${val_addr}${value}]
+        my usbCmd ${val_addr}${value}
+    }
+
+# -------------------------------------------------------------------------
+
+    HstDisplay instproc acquire {} {
+        my instvar number
+        my usbCmdRead 0002001${number} 4 4096
+   }
+
+# -------------------------------------------------------------------------
+
+    HstDisplay instproc restart {} {
+        my instvar number
+        my usbCmd 0001001${number}
     }
 
@@ -808,53 +879,450 @@
 # -------------------------------------------------------------------------
 
-    HstDisplay instproc send_data {data} {
-        global usb_handle
-
-        if {[catch {$usb_handle writeRaw $data} result]} {
-            puts {Error during write}
-            puts $result
-        }
-    }
-
-# -------------------------------------------------------------------------
-
-    HstDisplay instproc restart {} {
-        my instvar restart_command
-        my send_data $restart_command
-    }
-
-# -------------------------------------------------------------------------
-
-    HstDisplay instproc acquire {} {
-        global usb_handle
-        my instvar xvec yvec data fltr_val
-        my instvar acquire_command
-
-        my send_data $acquire_command
-
-        set usb_data {}
-        if {[catch {$usb_handle readHex 4 4096} usb_data]} {
-            puts {Error during read}
-            puts $usb_data
-            set usb_data {}
-        }
-
-        set data $usb_data
-
-        $yvec set $usb_data
-    }
-
-# -------------------------------------------------------------------------
-
     HstDisplay instproc acquire_loop {} {
+        my instvar number after_handle
+
+        my acquire
+
+        set after_handle [after 1000 [myproc acquire_loop]]
+    }
+
+# -------------------------------------------------------------------------
+
+    Class EptDisplay -superclass Display -parameter {
+        {number}
+        {master}
+    }
+
+# -------------------------------------------------------------------------
+
+    EptDisplay instproc init {} {
+        my instvar data xvec yvec
+
+        set xvec [vector #auto]
+
+        for {set i 0} {$i < 11} {incr i} {
+          set yvec($i) [vector #auto]
+        }
+
+        # fill one vector for the x axis
+        $xvec seq 0 10000
+
+        my reset
+
+        my setup
+
+        next
+    }
+
+# -------------------------------------------------------------------------
+
+    EptDisplay instproc destroy {} {
+        next
+    }
+
+# -------------------------------------------------------------------------
+
+    EptDisplay instproc reset {} {
+        my instvar data xvec yvec
+        my instvar number_val directory
+
+        set data {}
+
+        set directory $::env(HOMEPATH)
+        set number_val 10
+
+        for {set i 0} {$i < 11} {incr i} {
+          $yvec($i) set {}
+        }
+    }
+
+# -------------------------------------------------------------------------
+
+    EptDisplay instproc start {} {
+        my instvar config
+
+        trace add variable [myvar recs_val] write [myproc recs_val_update]
+        trace add variable [myvar data] write [myproc data_update]
+        trace add variable [myvar last] write [myproc data_update]
+
+    }
+
+# -------------------------------------------------------------------------
+
+    EptDisplay instproc setup {} {
+        my instvar master
+        my instvar data xvec yvec graph
+        my instvar config number_val
+
+        # create a graph widget and show a grid
+        set display [frame ${master}.display]
+
+        set graph(0) [graph ${display}.graph0 -height 200 -leftmargin 80]
+        $graph(0) crosshairs configure -hide no -linewidth 2 -dashes { 1 1 }
+        $graph(0) grid configure -hide no
+        $graph(0) legend configure -hide yes
+        $graph(0) axis configure x -min 0 -max 10000
+        $graph(0) axis configure y -min 0 -max 4100
+
+        set graph(1) [graph ${display}.graph1 -height 200 -leftmargin 80]
+        $graph(1) crosshairs configure -hide no -linewidth 2 -dashes { 1 1 }
+        $graph(1) grid configure -hide no
+        $graph(1) legend configure -hide yes
+        $graph(1) axis configure x -min 0 -max 10000 -hide yes
+        $graph(1) axis configure y -min 0 -max 4100
+
+        set graph(2) [graph ${display}.graph2 -height 200 -leftmargin 80]
+        $graph(2) crosshairs configure -hide no -linewidth 2 -dashes { 1 1 }
+        $graph(2) grid configure -hide no
+        $graph(2) legend configure -hide yes
+        $graph(2) axis configure x -min 0 -max 10000 -hide yes
+        $graph(2) axis configure y -min 0 -max 4100
+
+        set graph(3) [graph ${display}.graph3 -height 100 -leftmargin 80]
+        $graph(3) crosshairs configure -hide no -linewidth 2 -dashes { 1 1 }
+        $graph(3) grid configure -hide no
+        $graph(3) legend configure -hide yes
+        $graph(3) axis configure x -min 0 -max 10000 -hide yes
+        $graph(3) axis configure y -min 0 -max 25
+
+        scale ${master}.last -orient horizontal -from 1 -to 35 -tickinterval 0 -showvalue no -variable [myvar last]
+
+        set config [frame ${master}.config -width 120]
+
+        label ${config}.recs -text {number of records}
+        spinbox ${config}.recs_field -from 5 -to 100 \
+            -increment 5 -width 10 -textvariable [myvar recs_val] \
+            -validate all -vcmd {::mca::validate 100 %P}
+
+        frame ${config}.spc1 -width 10 -height 10
+
+        button ${config}.sequence -text {Start Recording}  \
+            -bg red -activebackground red -command [myproc sequence]
+
+        frame ${config}.spc2 -width 10 -height 10
+
+        label ${config}.stat -text {}
+
+        frame ${config}.spc3 -width 10 -height 20
+
+        button ${config}.acquire -text Acquire \
+            -bg green -activebackground green -command [myproc acquire]
+        button ${config}.register -text Register \
+            -bg lightblue -activebackground lightblue -command [myproc register]
+
+
+        grid ${config}.recs -sticky w -pady 1 -padx 3
+        grid ${config}.recs_field -sticky ew -pady 1 -padx 5
+        grid ${config}.spc1
+        grid ${config}.sequence -sticky ew -pady 3 -padx 5
+        grid ${config}.spc2
+        grid ${config}.stat -sticky w -pady 1 -padx 3
+        grid ${config}.spc3
+        grid ${config}.acquire -sticky ew -pady 3 -padx 5
+        grid ${config}.register -sticky ew -pady 3 -padx 5
+
+        grid ${graph(3)} -row 0 -column 0 -sticky news
+        grid ${graph(2)} -row 1 -column 0 -sticky news
+        grid ${graph(1)} -row 2 -column 0 -sticky news
+        grid ${graph(0)} -row 3 -column 0 -sticky news
+
+        grid ${display} -row 0 -column 0 -sticky news
+        grid ${config} -row 0 -column 1
+
+        grid ${master}.last -row 1 -column 0 -columnspan 2 -sticky ew
+
+        grid rowconfigure ${master} 0 -weight 1
+        grid columnconfigure ${master} 0 -weight 1
+        grid columnconfigure ${master} 1 -weight 0 -minsize 120
+
+        grid columnconfigure ${display} 0 -weight 1
+        grid rowconfigure ${display} 0 -weight 1
+        grid rowconfigure ${display} 1 -weight 1
+        grid rowconfigure ${display} 2 -weight 1
+        grid rowconfigure ${display} 3 -weight 1
+
+        # enable zooming
+        Blt_ZoomStack $graph(0)
+        Blt_ZoomStack $graph(1)
+        Blt_ZoomStack $graph(2)
+        Blt_ZoomStack $graph(3)
+
+        #bind .graph <Motion> {%W crosshairs configure -position @%x,%y}
+
+        # create one element with data for the x and y axis, no dots
+        $graph(0) element create Spectrum0 -symbol none -xdata $xvec -ydata $yvec(0)
+        $graph(1) element create Spectrum1 -symbol none -xdata $xvec -ydata $yvec(1)
+        $graph(2) element create Spectrum2 -symbol none -xdata $xvec -ydata $yvec(2)
+        for {set i 3} {$i < 11} {incr i} {
+          $graph(3) element create Spectrum$i -symbol none -xdata $xvec -ydata $yvec($i)
+        }
+    }
+
+# -------------------------------------------------------------------------
+
+    EptDisplay instproc recs_val_update args {
+        my instvar recs_val
+        if {[string equal $recs_val {}]} {
+            set number_val 0
+        }
+    }
+
+# -------------------------------------------------------------------------
+
+    EptDisplay instproc data_update args {
+        my instvar data xvec yvec graph last
+
+        set first [expr {$last - 1}]
+        
+        $xvec seq ${first}0000 ${last}0000
+        for {set i 0} {$i < 4} {incr i} {
+            $graph($i) axis configure x -min ${first}0000 -max ${last}0000
+        }
+
+        for {set i 0} {$i < 11} {incr i} {
+            $yvec($i) set [lrange [lindex $data $i] ${first}0000 ${last}0000]
+        }
+    }
+
+
+# -------------------------------------------------------------------------
+
+    EptDisplay instproc save_data {data} {
+        set file [tk_getSaveFile]
+        if {[string equal $file {}]} {
+            return
+        }
+
+        set x [catch {set fid [open $file w+]}]
+        set y [catch {puts $fid $data}]
+        set z [catch {close $fid}]
+
+        if { $x || $y || $z || ![file exists $file] || ![file isfile $file] || ![file readable $file] } {
+            tk_messageBox -icon error \
+                -message "An error occurred while writing to \"$file\""
+        } else {
+            tk_messageBox -icon info \
+                -message "File \"$file\" written successfully"
+        }
+    }
+
+# -------------------------------------------------------------------------
+
+    EptDisplay instproc acquire {} {
+        my usbCmdReadEpt 00060000 2097152
+   }
+
+# -------------------------------------------------------------------------
+
+    EptDisplay instproc register {} {
+        my save_data [my set data]
+    }
+
+# -------------------------------------------------------------------------
+
+    EptDisplay instproc sequence {} {
+        my instvar config recs_val directory counter
+
+        set directory [tk_chooseDirectory -initialdir $directory -title {Choose a directory}]
+
+        if {[string equal $directory {}]} {
+           return
+        }
+
+        ${config}.recs_field configure -state disabled
+        ${config}.sequence configure -state disabled
+        ${config}.acquire configure -state disabled
+        ${config}.register configure -state disabled
+        
+        set counter 1
+        
+        if {$counter <= $recs_val} {
+            ${config}.stat configure -text "record $counter of $recs_val"
+            set after_handle [after 100 [myproc acquire_register_loop]]
+        }
+    }
+
+# -------------------------------------------------------------------------
+
+    EptDisplay instproc acquire_register_loop {} {
         my instvar after_handle
+        my instvar config data recs_val directory counter
 
         my acquire
 
-        set after_handle [after 1000 [myproc acquire_loop]]
-    }
-
-# -------------------------------------------------------------------------
-
+        set file [file join $directory ept_$counter.dat.gz]
+
+        set x [catch {set fid [open $file w+]}]
+        fconfigure $fid -translation binary -encoding binary
+
+        set y [catch {
+            puts -nonewline $fid [binary format "H*iH*" "1f8b0800" [clock seconds] "0003"]
+            puts -nonewline $fid [zlib deflate $data]
+            puts -nonewline $fid [binary format i [zlib crc32 $data]]
+            puts -nonewline $fid [binary format i [string length $data]]
+        }]
+
+        set z [catch {close $fid}]
+
+        my incr counter
+
+        if { $x || $y || $z || ![file exists $file] || ![file isfile $file] || ![file readable $file] } {
+            tk_messageBox -icon error -message "An error occurred while writing to \"$file\""
+        } elseif {$counter <= $recs_val} {
+            ${config}.stat configure -text "record $counter of $recs_val"
+            set after_handle [after 100 [myproc acquire_register_loop]]
+            return
+        }
+        
+        ${config}.stat configure -text {}
+        ${config}.recs_field configure -state normal
+        ${config}.sequence configure -state active
+        ${config}.acquire configure -state active
+        ${config}.register configure -state active
+
+    }
+
+# -------------------------------------------------------------------------
+
+    Class MemDisplay -superclass Display -parameter {
+        {number}
+        {master}
+    }
+
+# -------------------------------------------------------------------------
+
+    MemDisplay instproc init {} {
+        my instvar data xvec yvec
+
+        set xvec [vector #auto]
+        set yvec [vector #auto]
+
+        # fill one vector for the x axis
+        $xvec seq 0 10000
+
+        my reset
+
+        my setup
+
+        next
+    }
+
+# -------------------------------------------------------------------------
+
+    MemDisplay instproc destroy {} {
+        next
+    }
+
+# -------------------------------------------------------------------------
+
+    MemDisplay instproc reset {} {
+        my instvar data xvec yvec
+
+        set data {}
+
+        $yvec set {}
+    }
+
+# -------------------------------------------------------------------------
+
+    MemDisplay instproc start {} {
+        my instvar config
+
+        trace add variable [myvar data] write [myproc data_update]
+        trace add variable [myvar last] write [myproc data_update]
+    }
+
+# -------------------------------------------------------------------------
+
+    MemDisplay instproc setup {} {
+        my instvar master
+        my instvar data xvec yvec
+        my instvar config auto thrs thrs_val disp_val trig_val
+
+        # create a graph widget and show a grid
+        set graph [graph ${master}.graph -height 250 -leftmargin 80]
+        $graph crosshairs configure -hide no -linewidth 2 -dashes { 1 1 }
+        $graph grid configure -hide no
+        $graph legend configure -hide yes
+        $graph axis configure x -min 0 -max 10000
+#        $graph axis configure y -min 0 -max 4100
+
+        scale ${master}.last -orient horizontal -from 1 -to 105 -tickinterval 0 -variable [myvar last]
+
+        set config [frame ${master}.config]
+
+        button ${config}.acquire -text Acquire \
+            -bg green -activebackground green -command [myproc acquire]
+        button ${config}.register -text Register \
+            -bg lightblue -activebackground lightblue -command [myproc register]
+
+        grid ${config}.acquire -sticky ew -pady 3 -padx 5
+        grid ${config}.register -sticky ew -pady 3 -padx 5
+
+        grid ${graph} -row 0 -column 0 -sticky news
+        grid ${config} -row 0 -column 1
+
+        grid ${master}.last -row 1 -column 0 -columnspan 2 -sticky ew
+
+        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
+
+        #bind .graph <Motion> {%W crosshairs configure -position @%x,%y}
+
+        # create one element with data for the x and y axis, no dots
+        $graph element create Spectrum -symbol none -xdata $xvec -ydata $yvec
+    }
+
+# -------------------------------------------------------------------------
+
+    MemDisplay instproc data_update args {
+        my instvar data yvec last
+
+        set first [expr {$last - 1}]
+        $yvec set [lrange $data ${first}0000 ${last}0000]
+    }
+
+# -------------------------------------------------------------------------
+
+    MemDisplay instproc save_data {data} {
+        set file [tk_getSaveFile]
+        if {[string equal $file {}]} {
+            return
+        }
+
+        set x [catch {set fid [open $file w+]}]
+        set y [catch {puts $fid $data}]
+        set z [catch {close $fid}]
+
+        if { $x || $y || $z || ![file exists $file] || ![file isfile $file] || ![file readable $file] } {
+            tk_messageBox -icon error \
+                -message "An error occurred while writing to \"$file\""
+        } else {
+            tk_messageBox -icon info \
+                -message "File \"$file\" written successfully"
+        }
+    }
+
+# -------------------------------------------------------------------------
+
+    MemDisplay instproc acquire {} {
+        my usbCmdRead 00040000 1 524288
+#        my usbCmdRead 00060000 1 1048576
+   }
+
+# -------------------------------------------------------------------------
+
+    MemDisplay instproc register {} {
+        my save_data [my set data]
+    }
+
+# -------------------------------------------------------------------------
+
+    namespace export MemDisplay
+    namespace export EptDisplay
     namespace export HstDisplay
     namespace export OscDisplay
@@ -888,4 +1356,12 @@
 ::mca::CfgDisplay cfg -master $config
 
+set window [frame ${notebook}.ept]
+$notebook insert end -text "EPT" -window $window -fill both
+::mca::EptDisplay ept -master $window
+
+#set window [frame ${notebook}.mem]
+#$notebook insert end -text "Memory test" -window $window -fill both
+#::mca::MemDisplay mem -master $window
+
 set usb_handle {}
 
@@ -893,13 +1369,9 @@
     set answer [tk_messageBox -icon error -type retrycancel \
         -message {Cannot access USB device} -detail $usb_handle]
-    if {[string equal $answer cancel]} exit
+    if {[string equal $answer cancel]} break
 }
 
 # cfg reset
-set reset_command [usb::convert 00000000]
-if {[catch {$usb_handle writeRaw $reset_command} result]} {
-    puts {Error during write}
-    puts $result
-}
+cfg reset
 
 cfg start
@@ -912,2 +1384,5 @@
     osc_$i restart
 }
+
+ept start
+#mem start
