1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#!/usr/bin/env tclsh
## -*- tcl -*-
# scope.tcl --
# Timing test of oscilloscope mode.
# Two traces are plotted.
# sin(x)
# square wave
#
# Each interval a random shift is put on the data points.
#
# Interested to see how 'live' plotchart can do this.
#
package require Tk
package require Plotchart
set update 50; # Ms between updates.
set square [list]
set sine [list]
##
# Create square wave.
# This creates a square wave 1000 points long.
# The period of the square wave is 100 pts.
# (10 cycles).
#
# @return 2 element list. The first element is
# the x coordinate the
# second element the y coordinate.
#
proc squareWave {} {
set flip 50; # points before polarity flip.
set magnitude 100; # Magnitude of the wave.
set multiplier 1
for {set i 0} {$i < 1000} {incr i} {
lappend x $i
lappend y [expr {$magnitude * $multiplier}]
# flip from positive to negative polarity etc.
if {($i % $flip) == 0} {
set multiplier [expr {$multiplier * -1}]
}
}
return [list $x $y]
}
##
# Create a sine wave.
# Wave is 1000 pts long
# Period is 100pts (2pi/100 is the argument increment to sin).
# Wave is offset by 300 units.
#
# @return 2 element list First element are the x coordinates.
# second elements the y coordinates.
#
proc sineWave {} {
set pi 3.141592654; # close enough.
set magnitude 100
for {set i 0} {$i < 1000} {incr i} {
lappend x $i
lappend y [expr {($magnitude*sin($i/($pi*2.0)) + 300)}]
}
return [list $x $y]
}
##
# Plot the data
#
# @param period - refresh period.
# @param plot - plot chart plot id.
# @param wf1 - first waveform to plot.
#
# Each time 400pts with a random starting offset of
# up to 600pts will be ploted.
#
set time 0
proc updatePlot {period plot args} {
$plot deletedata
set offset [expr {int(600*rand())}]
set last [expr {$offset + 350}]
foreach wf $args {
set name [lindex $wf 0]; # series name.
set pts [lindex $wf 1]
set x [lindex $pts 0]
set y [lindex $pts 1]
$plot plotlist $name [lrange $x 0 400] [lrange $y $offset $last]
}
after $period [list updatePlot $period $plot {*}$args]
}
set square [squareWave]; # Create the square wave.
set sine [sineWave]
canvas .c -width 8i -height 4i
pack .c
::Plotchart::plotstyle configure scope xyplot leftaxis color green
::Plotchart::plotstyle configure scope xyplot leftaxis textcolor green
::Plotchart::plotstyle configure scope xyplot bottomaxis color green
::Plotchart::plotstyle configure scope xyplot bottomaxis textcolor green
::Plotchart::plotstyle configure scope xyplot background outercolor black
::Plotchart::plotstyle configure scope xyplot background innercolor black
::Plotchart::plotstyle configure scope xyplot title background black
::Plotchart::plotstyle load scope
.c configure -background black
set plot [::Plotchart::createXYPlot .c [list 0 400 100] [list -2048 2048 1000]]
$plot dataconfig sine -color green
$plot dataconfig square -color green
$plot xticklines green
$plot yticklines green
updatePlot $update $plot [list square $square] [list sine $sine]
|