class TkRTTimer - tk-0.2.0 Documentation

# class TkRTTimer

### Constants

DEFAULT_OFFSET_LIST_SIZE

### Public Class Methods

new(*args, &b) click to toggle source
Calls superclass method TkTimer.new

    # File lib/tk/timer.rb, line 540
    def initialize(*args, &b)
      super(*args, &b)

      @offset_list = Array.new(DEFAULT_OFFSET_LIST_SIZE){ [0, 0] }
      @offset_s = 0
      @offset_u = 0
      @est_time = nil
    end

### Public Instance Methods

cancel() click to toggle source
Calls superclass method TkTimer#cancel

    # File lib/tk/timer.rb, line 556
    def cancel
      super()
      @est_time = nil
      @cb_start_time = Time.now
      self
    end

Also aliased as: stop
cb_call() click to toggle source

    # File lib/tk/timer.rb, line 645
    def cb_call
      if @est_time
        @offset_list.shift

        @cb_start_time = Time.now

        if @current_sleep == 0
          @offset_list.push([
                              @offset_s - @cb_start_time.to_i,
                              @offset_u - @cb_start_time.usec
                            ])
        else
          @offset_list.push([
                              @offset_s + (@est_time.to_i - @cb_start_time.to_i),
                              @offset_u + (@est_time.usec - @cb_start_time.usec)
                            ])
        end
      end

      @cb_cmd.call
    end

continue(wait=nil) click to toggle source
Calls superclass method TkTimer#continue

    # File lib/tk/timer.rb, line 564
    def continue(wait=nil)
      fail RuntimeError, "is already running" if @running
      @cb_start_time = Time.now
      super(wait)
    end

set_interval(interval) click to toggle source
Calls superclass method TkTimer#set_interval

    # File lib/tk/timer.rb, line 570
    def set_interval(interval)
      super(interval)
      @est_time = nil
    end

set_next_callback(args) click to toggle source

    # File lib/tk/timer.rb, line 591
    def set_next_callback(args)
      if @running == false || @proc_max == 0 || @do_loop == 0
        Tk_CBTBL.delete(@id) ;# for GC
        @running = false
        # @wait_var.value = 0
        __at_end__
        return
      end
      if @current_pos >= @proc_max
        if @do_loop < 0 || (@do_loop -= 1) > 0
          @current_pos = 0
        else
          Tk_CBTBL.delete(@id) ;# for GC
          @running = false
          # @wait_var.value = 0
          __at_end__
          return
        end
      end

      @current_args = args

      cmd, *cmd_args = @loop_proc[@current_pos]
      @current_pos += 1
      @current_proc = cmd

      @offset_s, @offset_u = _offset_ave

      if TkComm._callback_entry?(@sleep_time)
        sleep = @sleep_time.call(self)
      else
        sleep = @sleep_time
      end

      if @est_time
        @est_time = Time.at(@est_time.to_i, @est_time.usec + sleep*1000)
      else
        @est_time = Time.at(@cb_start_time.to_i,
                            @cb_start_time.usec + sleep*1000)
      end

      now = Time.now
      real_sleep = ((@est_time.to_i - now.to_i + @offset_s)*1000.0 +
                    (@est_time.usec - now.usec + @offset_u)/1000.0).round
      if real_sleep <= 0
        real_sleep = 0
        @offset_s = now.to_i
        @offset_u = now.usec
      end
      @current_sleep = real_sleep

      set_callback(real_sleep, cmd_args)
    end

start(*args, &b) click to toggle source
Calls superclass method TkTimer.start

    # File lib/tk/timer.rb, line 549
    def start(*args, &b)
      return nil if @running
      @est_time = nil
      @cb_start_time = Time.now
      super(*args, &b)
    end

stop()
Alias for: cancel

### Private Instance Methods

_offset_ave() click to toggle source

    # File lib/tk/timer.rb, line 575
    def _offset_ave
      size = 0
      d_sec = 0; d_usec = 0
      @offset_list.each_with_index{|offset, idx|
        # weight = 1
        weight = idx + 1
        size += weight
        d_sec += offset[0] * weight
        d_usec += offset[1] * weight
      }
      offset_s, mod = d_sec.divmod(size)
      offset_u = ((mod * 1000000 + d_usec) / size.to_f).round
      [offset_s, offset_u]
    end