Skip to content

Instantly share code, notes, and snippets.

@NorimasaNabeta
Last active August 29, 2015 14:07
Show Gist options
  • Save NorimasaNabeta/0b476bb2920d4f00fbb1 to your computer and use it in GitHub Desktop.
Save NorimasaNabeta/0b476bb2920d4f00fbb1 to your computer and use it in GitHub Desktop.
StopWatch for PSP0
#!/bin/sh -*- mode: TCL; coding: utf-8 -*-
#
# Time-stamp: <Sun Oct 05 18:20:20 NorimasaNabeta>
#
# the next line restarts using wish \
exec wish "$0" ${1+"$@"}
#
# Multiple stopwatch
#
set stopped 1
set cfont {{Courier New} 12}
set timeList {
{ 1 "DESGN" "-- PAUSE --" }
{ 2 "CODING" "-- PAUSE --" }
{ 3 "DEBUG" "-- PAUSE --" }
}
array set startMoment { 1 0 2 0 3 0 }
array set wrapMS { 1 0.0 2 0.0 3 0.0 }
foreach { spc } $timeList {
foreach { id titleFace titleBack } $spc { break }
frame .f$id
label .f$id.wrap -text 0.00 -font $cfont -foreground red -relief raised -width 10 -padx 2m -pady 1m
label .f$id.counter -text 0.00 -font $cfont -foreground red -relief raised -width 10 -padx 2m -pady 1m
button .f$id.start -text $titleFace -font $cfont -foreground red -width 10 -command [list toggle $id ]
pack .f$id.wrap .f$id.counter .f$id.start -side left -fill both -expand yes
pack .f$id -side bottom -fill both
}
# toggle
#
#
proc toggle { idx } {
if {$::stopped} {
set ::stopped 0
set ::startMoment($idx) [clock clicks -milliseconds]
tick
foreach { spc } $::timeList {
foreach { id titleFace titleBack } $spc { break }
if { $id == $idx } {
.f$id.start configure -text $titleBack
} else {
.f$id.start configure -state disabled
}
}
} else {
set ::stopped 1
set ::wrapMS($idx) [expr {[clock clicks -milliseconds] - $::startMoment($idx) + $::wrapMS($idx) }]
set ::startMoment($idx) 0
# .f$idx.counter config -text 0.00
foreach { spc } $::timeList {
foreach { id titleFace titleBack } $spc { break }
if { $id == $idx } {
.f$id.start configure -text $titleFace
} else {
.f$id.start configure -state normal
}
}
}
}
# tick
#
#
proc tick {} {
if {$::stopped} {return}
after 50 tick
foreach { spc } $::timeList {
foreach { id titleFace titleBack } $spc { break }
if { $::startMoment($id) != 0 } {
set elapsedMS [expr {[clock clicks -milliseconds] - $::startMoment($id)}]
set wrapMS [expr $elapsedMS + $::wrapMS($id)]
.f$id.counter config -text [format "%.2f" [expr {double($elapsedMS)/1000}]]
.f$id.wrap config -text [format "%.2f" [expr {double($wrapMS)/1000}]]
}
}
}
bind . <Control-c> {destroy .}
bind . <Control-q> {destroy .}
focus .
# Local Variables:
# mode: tcl
# End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment