Skip to content

Instantly share code, notes, and snippets.

@et
Created November 18, 2010 21:43
Show Gist options
  • Save et/705703 to your computer and use it in GitHub Desktop.
Save et/705703 to your computer and use it in GitHub Desktop.
Slightly broken clock.
#==========================================================================
# Clock interaction
#==========================================================================
::itcl::class Clock {
package require Thread
constructor {supervisor type {tick_rate 1}} {
set itsSupervisor $supervisor
set itsType $type
set itsTickRate $tick_rate ;# How fast should we sent out tick messages. Make smaller to send messages out faster.
set itsInterval 1 ;# How many seconds per tick. Must be positive.
set itsThread [thread::create {thread::wait} ]
}
destructor {
thread::release $itsThread
}
method Start {} {
if {! $isStarted } { set isStarted 1 }
set itsStartTime [clock seconds]
set isRunning 1
Tick
}
method Stop {} {
set isRunning 0
}
method SetInterval {interval} { set itsInterval $interval }
method IsRunning {} { return $isRunning }
method IsStarted {} { return $isStarted }
##
# @remarks - this method should be private
#
private method Tick {} {
if { $isRunning } {
thread::send $itsThread {
return [expr [clock seconds]]
} itsTimeNow
$itsSupervisor UpdateClock [GetCurrentTime] [GetClockMsg]
after [expr { int($itsTickRate * 1000) }] [itcl::code Tick]
} else {
set itsElapsedTime [GetTotalElapsedTime]
}
puts "Its elapsed time: $itsElapsedTime"
puts "Its start time: $itsStartTime"
puts "Its start time: $itsStartTime"
}
method GetCurrentTime {} {
return [FormatTime $itsElapsedTime]
}
private method GetTotalElapsedTime {} {
return [expr $itsTimeNow - $itsStartTime + $itsElapsedTime]
}
private method FormatTime {seconds} {
return [clock format $seconds -format {%T} -gmt true]
}
private method GetClockMsg {} {
set time [GetCurrentTime]
return "Type ${itsType} Time ${time} Ticking true Interval ${itsInterval} Rate ${itsTickRate} @ !*"
}
private variable isRunning 0
private variable isStarted 0
private variable itsType "Tick"
private variable itsTickRate
private variable itsInterval
private variable itsSupervisor
private variable itsThread
private variable itsTimeNow 0
private variable itsStartTime 0
private variable itsElapsedTime 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment